File size: 2,940 Bytes
78d2150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "../../unity/unity.h"
#include <string.h>
#include <stdlib.h>

/* Unity required hooks */
void setUp(void) {
  /* No setup required */
}
void tearDown(void) {
  /* No teardown required */
}

/* Helper to create an integer VALUE from a decimal C string */
static VALUE* make_integer_from_cstr(const char *s)
{
  VALUE *v = xmalloc(sizeof *v);
  v->type = integer;
  int rc = mpz_init_set_str(v->u.i, s, 10);
  if (rc != 0)
  {
    TEST_FAIL_MESSAGE("mpz_init_set_str failed (invalid decimal input)");
  }
  return v;
}

static void test_tostring_integer_zero(void)
{
  VALUE *v = int_value(0UL); /* uses mpz_init_set_ui */
  tostring(v);

  TEST_ASSERT_EQUAL_INT((int)string, (int)v->type);
  TEST_ASSERT_NOT_NULL(v->u.s);
  TEST_ASSERT_EQUAL_STRING("0", v->u.s);

  freev(v);
}

static void test_tostring_integer_positive_small(void)
{
  VALUE *v = int_value(123456UL);
  tostring(v);

  TEST_ASSERT_EQUAL_INT((int)string, (int)v->type);
  TEST_ASSERT_NOT_NULL(v->u.s);
  TEST_ASSERT_EQUAL_STRING("123456", v->u.s);

  freev(v);
}

static void test_tostring_integer_negative(void)
{
  VALUE *v = make_integer_from_cstr("-9876543210");
  tostring(v);

  TEST_ASSERT_EQUAL_INT((int)string, (int)v->type);
  TEST_ASSERT_NOT_NULL(v->u.s);
  TEST_ASSERT_EQUAL_STRING("-9876543210", v->u.s);

  freev(v);
}

static void test_tostring_integer_large_number(void)
{
  /* Build a 200-digit number "999...9" (200 times) */
  char big[256];
  size_t ndigits = 200;
  memset(big, '9', ndigits);
  big[ndigits] = '\0';

  VALUE *v = make_integer_from_cstr(big);

  /* Pre-compute expected using the same canonical form (already in 'big') */
  tostring(v);

  TEST_ASSERT_EQUAL_INT((int)string, (int)v->type);
  TEST_ASSERT_NOT_NULL(v->u.s);
  TEST_ASSERT_EQUAL_STRING(big, v->u.s);

  freev(v);
}

static void test_tostring_on_string_is_noop(void)
{
  VALUE *v = str_value("hello world");
  char *orig_ptr = v->u.s;

  tostring(v); /* Should be a no-op */

  TEST_ASSERT_EQUAL_INT((int)string, (int)v->type);
  TEST_ASSERT_EQUAL_PTR(orig_ptr, v->u.s);
  TEST_ASSERT_EQUAL_STRING("hello world", v->u.s);

  freev(v);
}

static void test_tostring_double_call_idempotence(void)
{
  VALUE *v = int_value(42UL);
  tostring(v); /* First call converts */
  TEST_ASSERT_EQUAL_INT((int)string, (int)v->type);
  TEST_ASSERT_NOT_NULL(v->u.s);
  TEST_ASSERT_EQUAL_STRING("42", v->u.s);

  char *first_ptr = v->u.s;
  tostring(v); /* Second call should be a no-op */
  TEST_ASSERT_EQUAL_INT((int)string, (int)v->type);
  TEST_ASSERT_EQUAL_PTR(first_ptr, v->u.s);
  TEST_ASSERT_EQUAL_STRING("42", v->u.s);

  freev(v);
}

int main(void)
{
  UNITY_BEGIN();

  RUN_TEST(test_tostring_integer_zero);
  RUN_TEST(test_tostring_integer_positive_small);
  RUN_TEST(test_tostring_integer_negative);
  RUN_TEST(test_tostring_integer_large_number);
  RUN_TEST(test_tostring_on_string_is_noop);
  RUN_TEST(test_tostring_double_call_idempotence);

  return UNITY_END();
}