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