#include "../../unity/unity.h" #include #include #include #include #include /* Unity hooks */ void setUp(void) { /* Setup code here, or leave empty */ } void tearDown(void) { /* Cleanup code here, or leave empty */ } /* The target function 'max_out' is defined earlier in the same translation unit and available here due to source inclusion order. We do not redeclare it. */ static void test_max_out_simple_decimal(void) { char fmt[] = "%d"; int expected = snprintf(NULL, 0, fmt, INT_MAX); idx_t got = max_out(fmt); TEST_ASSERT_EQUAL_INT(expected, (int)got); TEST_ASSERT_EQUAL_STRING("%d", fmt); /* no mutation expected */ } static void test_max_out_mutates_u_to_d_and_length_correct(void) { char fmt_mutable[] = "%u"; /* Expected uses %d, since max_out should mutate %u to %d */ const char expected_fmt[] = "%d"; int expected = snprintf(NULL, 0, expected_fmt, INT_MAX); idx_t got = max_out(fmt_mutable); TEST_ASSERT_EQUAL_INT(expected, (int)got); TEST_ASSERT_EQUAL_STRING("%d", fmt_mutable); /* verify mutation */ } static void test_max_out_with_width_and_zero_flag(void) { char fmt[] = "%020d"; /* zero-padded width 20 */ int expected = snprintf(NULL, 0, fmt, INT_MAX); idx_t got = max_out(fmt); TEST_ASSERT_EQUAL_INT(expected, (int)got); TEST_ASSERT_EQUAL_STRING("%020d", fmt); /* format should remain unchanged */ } static void test_max_out_with_precision_and_text(void) { char fmt[] = "abc%.12dXYZ"; int expected = snprintf(NULL, 0, fmt, INT_MAX); idx_t got = max_out(fmt); TEST_ASSERT_EQUAL_INT(expected, (int)got); TEST_ASSERT_EQUAL_STRING("abc%.12dXYZ", fmt); } static void test_max_out_with_literal_percent_and_padding(void) { char fmt[] = "%%--%08d--%%"; int expected = snprintf(NULL, 0, fmt, INT_MAX); idx_t got = max_out(fmt); TEST_ASSERT_EQUAL_INT(expected, (int)got); TEST_ASSERT_EQUAL_STRING("%%--%08d--%%", fmt); } static void test_max_out_i_specifier_behaves_like_d(void) { char fmt[] = "%i"; int expected = snprintf(NULL, 0, fmt, INT_MAX); idx_t got = max_out(fmt); TEST_ASSERT_EQUAL_INT(expected, (int)got); TEST_ASSERT_EQUAL_STRING("%i", fmt); } static void test_max_out_with_left_align_and_text_suffix(void) { char fmt[] = "%-10d_end"; int expected = snprintf(NULL, 0, fmt, INT_MAX); idx_t got = max_out(fmt); TEST_ASSERT_EQUAL_INT(expected, (int)got); TEST_ASSERT_EQUAL_STRING("%-10d_end", fmt); } int main(void) { UNITY_BEGIN(); RUN_TEST(test_max_out_simple_decimal); RUN_TEST(test_max_out_mutates_u_to_d_and_length_correct); RUN_TEST(test_max_out_with_width_and_zero_flag); RUN_TEST(test_max_out_with_precision_and_text); RUN_TEST(test_max_out_with_literal_percent_and_padding); RUN_TEST(test_max_out_i_specifier_behaves_like_d); RUN_TEST(test_max_out_with_left_align_and_text_suffix); return UNITY_END(); }