#include "../../unity/unity.h" #include #include #include #include #include #include #include /* parse_integer is defined in dd.c and this file is included after it. */ static void assert_parse_ok(const char *s, intmax_t expected) { strtol_error err = (strtol_error)0xDEADBEEF; /* sentinel */ intmax_t r = parse_integer(s, &err); TEST_ASSERT_EQUAL_INT_MESSAGE(LONGINT_OK, err, "Expected LONGINT_OK"); TEST_ASSERT_TRUE_MESSAGE(r == expected, "Returned value mismatch"); } static void assert_parse_err_bits(const char *s, int err_mask) { strtol_error err = LONGINT_OK; (void)parse_integer(s, &err); TEST_ASSERT_TRUE_MESSAGE((err & err_mask) != 0, "Expected error bits not set"); } void setUp(void) { /* no-op */ } void tearDown(void) { /* no-op */ } void test_parse_integer_basic_and_B_suffix(void) { assert_parse_ok("0", 0); assert_parse_ok("123", 123); /* Trailing 'B' is accepted as a flag (not a multiplier) */ assert_parse_ok("10B", 10); /* 'BB' is invalid: only a single trailing B is allowed */ strtol_error err = LONGINT_OK; intmax_t r = parse_integer("1BB", &err); TEST_ASSERT_TRUE(r == 1); TEST_ASSERT_TRUE((err & LONGINT_INVALID_SUFFIX_CHAR) != 0); } void test_parse_integer_multiplicative_x(void) { assert_parse_ok("2x3", 6); /* Missing RHS after 'x' should be invalid */ assert_parse_err_bits("2x", LONGINT_INVALID); } void test_parse_integer_chain_multiplication(void) { assert_parse_ok("2x3x4", 24); } void test_parse_integer_suffix_variants(void) { /* Common suffixes parsed by xstrtoumax according to dd semantics */ assert_parse_ok("1c", 1); /* c = 1 */ assert_parse_ok("1w", 2); /* w = 2 */ assert_parse_ok("1b", 512); /* b = 512 */ assert_parse_ok("1K", 1024); /* K = 1024 */ assert_parse_ok("1kB", 1000); /* kB = 1000 */ } void test_parse_integer_overflow_simple(void) { /* Build a string representing INTMAX_MAX + 1 to trigger overflow */ char buf[128]; uintmax_t big = (uintmax_t)INTMAX_MAX + 1; snprintf(buf, sizeof buf, "%