#include "../../unity/unity.h" #include #include /* 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(); }