1 #include <inttypes.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5
6 struct atoi_data
7 {
8 char string[15]; // int max 2147483647
9 int ret_num;
10 };
11
12 struct atoi_data test_data[] =
13 {
14 /* positive integer */
15 {"0", 0},
16 {"1", 1},
17 {"1.123", 1},
18 {"123", 123},
19 {"98993489", 98993489},
20 {"98993489.12", 98993489},
21 {"2147483647", 2147483647},
22
23 /* negtive integer */
24 {"-1", -1},
25 {"-1.123", -1},
26 {"-123", -123},
27 {"-98993489", -98993489},
28 {"-98993489.12", -98993489},
29 {"-2147483647", -2147483647},
30
31 /* letters and numbers */
32 {"12a45", 12},
33 {"-12a45", -12},
34 {"12/45", 12},
35 {"-12/45", -12},
36
37 /* cannot be resolved */
38 {"", 0},
39 {" ", 0},
40 {"abc12", 0},
41 {" abc12", 0},
42 /* {NULL, -1} compiler warning */
43 };
44
45 #include <utest.h>
atoi_entry(void)46 int atoi_entry(void)
47 {
48 int i = 0;
49 int res = 0;
50 for (i = 0; i < sizeof(test_data) / sizeof(test_data[0]); i++)
51 {
52 res = atoi(test_data[i].string);
53 uassert_int_equal(res, test_data[i].ret_num);
54 }
55 return 0;
56 }
57
test_atoi(void)58 static void test_atoi(void)
59 {
60 atoi_entry();
61 }
testcase(void)62 static void testcase(void)
63 {
64 UTEST_UNIT_RUN(test_atoi);
65 }
66 UTEST_TC_EXPORT(testcase, "posix.stdlib_h.atoi_tc.c", RT_NULL, RT_NULL, 10);
67
68