coreutils / tests /od /tests_for_simple_strtoi.c
AryaWu's picture
Upload folder using huggingface_hub
78d2150 verified
#include "../../unity/unity.h"
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
/* Unity setUp/tearDown */
void setUp(void) {
/* Setup code here, or leave empty */
}
void tearDown(void) {
/* Cleanup code here, or leave empty */
}
/* Target under test is static in the same translation unit as this file
and is available due to direct inclusion into the program source:
static bool simple_strtoi (char const *s, char const **p, int *val);
Do not redeclare it here to avoid conflicts. */
/* Helper: build a decimal string for INT_MAX into buf, and optionally append a suffix */
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; /* any non-null sentinel */
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); /* should point to NUL terminator */
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); /* points to 'a' */
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; /* sentinel */
int val = 42;
bool ok = simple_strtoi(s, &p, &val);
TEST_ASSERT_TRUE(ok);
TEST_ASSERT_EQUAL_PTR(s, p); /* '+' is not a digit */
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 a value larger than INT_MAX by appending a digit */
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); /* unchanged */
TEST_ASSERT_EQUAL_INT(sentinel_val, val); /* unchanged */
}
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); /* space is non-digit */
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();
}