| #include "../../unity/unity.h" |
| #include <string.h> |
| #include <stdlib.h> |
|
|
| |
| void setUp(void) { |
| |
| } |
| void tearDown(void) { |
| |
| } |
|
|
| |
| 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); |
| 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) |
| { |
| |
| char big[256]; |
| size_t ndigits = 200; |
| memset(big, '9', ndigits); |
| big[ndigits] = '\0'; |
|
|
| VALUE *v = make_integer_from_cstr(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); |
|
|
| 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); |
| 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); |
| 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(); |
| } |