File size: 4,564 Bytes
78d2150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#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();
}