|
|
#include "../../unity/unity.h" |
|
|
#include <limits.h> |
|
|
#include <stdbool.h> |
|
|
#include <stddef.h> |
|
|
#include <stdio.h> |
|
|
#include <string.h> |
|
|
|
|
|
|
|
|
void setUp(void) { |
|
|
|
|
|
} |
|
|
void tearDown(void) { |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void build_intmax_str(char *buf, size_t bufsz, const char *suffix) { |
|
|
if (!suffix) suffix = ""; |
|
|
(void)snprintf(buf, bufsz, "%d%s", INT_MAX, suffix); |
|
|
} |
|
|
|
|
|
void test_simple_strtoi_non_digit_initial(void) { |
|
|
const char *s = "abc"; |
|
|
const char *sentinel_p = "unchanged"; |
|
|
const char *p = sentinel_p; |
|
|
int val = -77; |
|
|
|
|
|
bool ok = simple_strtoi(s, &p, &val); |
|
|
TEST_ASSERT_TRUE(ok); |
|
|
TEST_ASSERT_EQUAL_PTR(s, p); |
|
|
TEST_ASSERT_EQUAL_INT(0, val); |
|
|
} |
|
|
|
|
|
void test_simple_strtoi_empty_string(void) { |
|
|
const char *s = ""; |
|
|
const char *p = (const char *)0x1; |
|
|
int val = 12345; |
|
|
|
|
|
bool ok = simple_strtoi(s, &p, &val); |
|
|
TEST_ASSERT_TRUE(ok); |
|
|
TEST_ASSERT_EQUAL_PTR(s, p); |
|
|
TEST_ASSERT_EQUAL_INT(0, val); |
|
|
} |
|
|
|
|
|
void test_simple_strtoi_zero_single(void) { |
|
|
const char *s = "0"; |
|
|
const char *p = NULL; |
|
|
int val = -1; |
|
|
|
|
|
bool ok = simple_strtoi(s, &p, &val); |
|
|
TEST_ASSERT_TRUE(ok); |
|
|
TEST_ASSERT_EQUAL_PTR(s + 1, p); |
|
|
TEST_ASSERT_EQUAL_INT(0, val); |
|
|
} |
|
|
|
|
|
void test_simple_strtoi_zero_multiple_leading_zeros(void) { |
|
|
const char *s = "0000"; |
|
|
const char *p = NULL; |
|
|
int val = -1; |
|
|
|
|
|
bool ok = simple_strtoi(s, &p, &val); |
|
|
TEST_ASSERT_TRUE(ok); |
|
|
TEST_ASSERT_EQUAL_PTR(s + 4, p); |
|
|
TEST_ASSERT_EQUAL_INT(0, val); |
|
|
} |
|
|
|
|
|
void test_simple_strtoi_stops_at_first_nondigit(void) { |
|
|
const char *s = "123abc456"; |
|
|
const char *p = NULL; |
|
|
int val = -1; |
|
|
|
|
|
bool ok = simple_strtoi(s, &p, &val); |
|
|
TEST_ASSERT_TRUE(ok); |
|
|
TEST_ASSERT_EQUAL_PTR(s + 3, p); |
|
|
TEST_ASSERT_EQUAL_INT(123, val); |
|
|
} |
|
|
|
|
|
void test_simple_strtoi_plus_sign_treated_as_nondigit(void) { |
|
|
const char *s = "+123"; |
|
|
const char *p = (const char *)0xABC; |
|
|
int val = 42; |
|
|
|
|
|
bool ok = simple_strtoi(s, &p, &val); |
|
|
TEST_ASSERT_TRUE(ok); |
|
|
TEST_ASSERT_EQUAL_PTR(s, p); |
|
|
TEST_ASSERT_EQUAL_INT(0, val); |
|
|
} |
|
|
|
|
|
void test_simple_strtoi_INT_MAX_success(void) { |
|
|
char buf[64]; |
|
|
build_intmax_str(buf, sizeof buf, ""); |
|
|
|
|
|
const char *s = buf; |
|
|
const char *p = NULL; |
|
|
int val = -1; |
|
|
|
|
|
bool ok = simple_strtoi(s, &p, &val); |
|
|
TEST_ASSERT_TRUE(ok); |
|
|
TEST_ASSERT_EQUAL_PTR(s + strlen(s), p); |
|
|
TEST_ASSERT_EQUAL_INT(INT_MAX, val); |
|
|
} |
|
|
|
|
|
void test_simple_strtoi_overflow_returns_false_and_does_not_modify_outputs(void) { |
|
|
char buf[80]; |
|
|
|
|
|
build_intmax_str(buf, sizeof buf, "0"); |
|
|
|
|
|
const char *s = buf; |
|
|
const char *sentinel_p = "unchanged"; |
|
|
const char *p = sentinel_p; |
|
|
int sentinel_val = -9999; |
|
|
int val = sentinel_val; |
|
|
|
|
|
bool ok = simple_strtoi(s, &p, &val); |
|
|
TEST_ASSERT_FALSE(ok); |
|
|
TEST_ASSERT_EQUAL_PTR(sentinel_p, p); |
|
|
TEST_ASSERT_EQUAL_INT(sentinel_val, val); |
|
|
} |
|
|
|
|
|
void test_simple_strtoi_digits_only_no_overflow(void) { |
|
|
const char *s = "987654321"; |
|
|
const char *p = NULL; |
|
|
int val = -1; |
|
|
|
|
|
bool ok = simple_strtoi(s, &p, &val); |
|
|
TEST_ASSERT_TRUE(ok); |
|
|
TEST_ASSERT_EQUAL_PTR(s + strlen(s), p); |
|
|
TEST_ASSERT_EQUAL_INT(987654321, val); |
|
|
} |
|
|
|
|
|
void test_simple_strtoi_whitespace_first_is_nondigit(void) { |
|
|
const char *s = " 42"; |
|
|
const char *p = NULL; |
|
|
int val = -1; |
|
|
|
|
|
bool ok = simple_strtoi(s, &p, &val); |
|
|
TEST_ASSERT_TRUE(ok); |
|
|
TEST_ASSERT_EQUAL_PTR(s, p); |
|
|
TEST_ASSERT_EQUAL_INT(0, val); |
|
|
} |
|
|
|
|
|
int main(void) { |
|
|
UNITY_BEGIN(); |
|
|
RUN_TEST(test_simple_strtoi_non_digit_initial); |
|
|
RUN_TEST(test_simple_strtoi_empty_string); |
|
|
RUN_TEST(test_simple_strtoi_zero_single); |
|
|
RUN_TEST(test_simple_strtoi_zero_multiple_leading_zeros); |
|
|
RUN_TEST(test_simple_strtoi_stops_at_first_nondigit); |
|
|
RUN_TEST(test_simple_strtoi_plus_sign_treated_as_nondigit); |
|
|
RUN_TEST(test_simple_strtoi_INT_MAX_success); |
|
|
RUN_TEST(test_simple_strtoi_overflow_returns_false_and_does_not_modify_outputs); |
|
|
RUN_TEST(test_simple_strtoi_digits_only_no_overflow); |
|
|
RUN_TEST(test_simple_strtoi_whitespace_first_is_nondigit); |
|
|
return UNITY_END(); |
|
|
} |