• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "ohos_types.h"
33 #include "posix_test.h"
34 #include "los_config.h"
35 #include "kernel_test.h"
36 #include "ctype.h"
37 #include "stdlib.h"
38 #include "string.h"
39 #include "log.h"
40 
41 /* *
42  * @tc.desc      : register a test suite, this suite is used to test basic flow and interface dependency
43  * @param        : subsystem name is utils
44  * @param        : module name is utilsFile
45  * @param        : test suit name is CmsisTaskFuncTestSuite
46  */
47 LITE_TEST_SUIT(Posix, Posixtimer, PosixStdlibAtoiTest);
48 
49 /* *
50  * @tc.setup     : setup for all testcases
51  * @return       : setup result, TRUE is success, FALSE is fail
52  */
PosixStdlibAtoiTestSetUp(void)53 static BOOL PosixStdlibAtoiTestSetUp(void)
54 {
55     return TRUE;
56 }
57 
58 /* *
59  * @tc.teardown  : teardown for all testcases
60  * @return       : teardown result, TRUE is success, FALSE is fail
61  */
PosixStdlibAtoiTestTearDown(void)62 static BOOL PosixStdlibAtoiTestTearDown(void)
63 {
64     LOG("+-------------------------------------------+\n");
65     return TRUE;
66 }
67 
68 /* *
69  * @tc.number    : TEST_STDLIB_ATOI_001
70  * @tc.name      : convert string to integer
71  * @tc.desc      : [C- SOFTWARE -0200]
72  */
73 LITE_TEST_CASE(PosixStdlibAtoiTest, testStdlibAtoi001, Function | MediumTest | Level1)
74 {
75     int value = atoi("2147483647");
76     if (value == 2147483647) {
77         LOG("[DEMO] posix stdlib test case 1:atoi(%d) ok.\n", value);
78     } else {
79         LOG("[DEMO] posix stdlib test case 1:atoi(%d) fail.\n", value);
80     }
81     TEST_ASSERT_EQUAL_INT(2147483647, value);
82     return 0;
83 }
84 
85 /* *
86  * @tc.number    : TEST_STDLIB_ATOI_002
87  * @tc.name      : convert string to integer
88  * @tc.desc      : [C- SOFTWARE -0200]
89  */
90 LITE_TEST_CASE(PosixStdlibAtoiTest, testStdlibAtoi002, Function | MediumTest | Level1)
91 {
92     int value = atoi("-2147483648");
93     if (value == -2147483648) {
94         LOG("[DEMO] posix stdlib test case 2:atoi(%d) ok.\n", value);
95     } else {
96         LOG("[DEMO] posix stdlib test case 2:atoi(%d) fail.\n", value);
97     }
98     TEST_ASSERT_EQUAL_INT(-2147483648, value);
99     return 0;
100 }
101 
102 /* *
103  * @tc.number    : TEST_STDLIB_ATOI_003
104  * @tc.name      : convert string to integer
105  * @tc.desc      : [C- SOFTWARE -0200]
106  */
107 LITE_TEST_CASE(PosixStdlibAtoiTest, testStdlibAtoi003, Function | MediumTest | Level1)
108 {
109     int value = atoi("100");
110     if (value == 100) {
111         LOG("[DEMO] posix stdlib test case 3:atoi(%d) ok.\n", value);
112     } else {
113         LOG("[DEMO] posix stdlib test case 3:atoi(%d) fail.\n", value);
114     }
115     TEST_ASSERT_EQUAL_INT(100, value);
116     return 0;
117 }
118 
119 #if (LOSCFG_LIBC_MUSL == 1)
120 /* *
121  * @tc.number    : TEST_STDLIB_ATOI_004
122  * @tc.name      : convert string to integer
123  * @tc.desc      : [C- SOFTWARE -0200]
124  */
125 LITE_TEST_CASE(PosixStdlibAtoiTest, testStdlibAtoi004, Function | MediumTest | Level1)
126 {
127     int value = atoi("2147483648");
128     if (value == -2147483648) {
129         LOG("[DEMO] posix stdlib test case 4(except):atoi(%d) != 2147483648 ok.\n", value);
130     } else {
131         LOG("[DEMO] posix stdlib test case 4(except):atoi(%d) fail.\n", value);
132     }
133     TEST_ASSERT_EQUAL_INT(-2147483648, value);
134     return 0;
135 }
136 
137 /* *
138  * @tc.number    : TEST_STDLIB_ATOI_005
139  * @tc.name      : convert string to integer
140  * @tc.desc      : [C- SOFTWARE -0200]
141  */
142 LITE_TEST_CASE(PosixStdlibAtoiTest, testStdlibAtoi005, Function | MediumTest | Level1)
143 {
144     int value = atoi("-2147483649");
145     if (value == 2147483647) {
146         LOG("[DEMO] posix stdlib test case 5(except):atoi(%d) != -2147483649 ok.\n", value);
147     } else {
148         LOG("[DEMO] posix stdlib test case 5(except):atoi(%d) fail.\n", value);
149     }
150     TEST_ASSERT_EQUAL_INT(2147483647, value);
151     return 0;
152 }
153 #endif
154 
155 /* *
156  * @tc.number    : TEST_STDLIB_ATOI_006
157  * @tc.name      : convert string to integer
158  * @tc.desc      : [C- SOFTWARE -0200]
159  */
160 LITE_TEST_CASE(PosixStdlibAtoiTest, testStdlibAtoi006, Function | MediumTest | Level1)
161 {
162     int value = atoi("+100");
163     if (value == 100) {
164         LOG("[DEMO] posix stdlib test case 6:atoi(%d) == +100 ok.\n", value);
165     } else {
166         LOG("[DEMO] posix stdlib test case 6:atoi(%d) fail.\n", value);
167     }
168     TEST_ASSERT_EQUAL_INT(100, value);
169     return 0;
170 }
171 
172 /* *
173  * @tc.number    : TEST_STDLIB_ATOI_007
174  * @tc.name      : convert string to integer
175  * @tc.desc      : [C- SOFTWARE -0200]
176  */
177 LITE_TEST_CASE(PosixStdlibAtoiTest, testStdlibAtoi007, Function | MediumTest | Level1)
178 {
179     int value = atoi("-100");
180     if (value == -100) {
181         LOG("[DEMO] posix stdlib test case 7:atoi(%d) == -100 ok.\n", value);
182     } else {
183         LOG("[DEMO] posix stdlib test case 7:atoi(%d) fail.\n", value);
184     }
185     TEST_ASSERT_EQUAL_INT(-100, value);
186     return 0;
187 }
188 
189 /* *
190  * @tc.number    : TEST_STDLIB_ATOI_008
191  * @tc.name      : convert string to integer
192  * @tc.desc      : [C- SOFTWARE -0200]
193  */
194 LITE_TEST_CASE(PosixStdlibAtoiTest, testStdlibAtoi008, Function | MediumTest | Level1)
195 {
196     int value = atoi("+-100");
197     if (value == 0) {
198         LOG("[DEMO] posix stdlib test case 8(except):atoi(%d) ==  +-100 ok.\n", value);
199     } else {
200         LOG("[DEMO] posix stdlib test case 8(except):atoi(%d) fail.\n", value);
201     }
202     TEST_ASSERT_EQUAL_INT(0, value);
203     return 0;
204 }
205 
206 /* *
207  * @tc.number    : TEST_STDLIB_ATOI_009
208  * @tc.name      : convert string to integer
209  * @tc.desc      : [C- SOFTWARE -0200]
210  */
211 LITE_TEST_CASE(PosixStdlibAtoiTest, testStdlibAtoi009, Function | MediumTest | Level1)
212 {
213     int value = atoi("12+-100");
214     if (value == 12) {
215         LOG("[DEMO] posix stdlib test case 9(except):atoi(%d) ok == 12+-100.\n", value);
216     } else {
217         LOG("[DEMO] posix stdlib test case 9(except):atoi(%d) fail.\n", value);
218     }
219     TEST_ASSERT_EQUAL_INT(12, value);
220     return 0;
221 }
222 
223 #if (LOSCFG_LIBC_MUSL == 1)
224 /* *
225  * @tc.number    : TEST_STDLIB_ATOI_010
226  * @tc.name      : convert string to integer
227  * @tc.desc      : [C- SOFTWARE -0200]
228  */
229 LITE_TEST_CASE(PosixStdlibAtoiTest, testStdlibAtoi010, Function | MediumTest | Level1)
230 {
231     int value = atoi("21474836470");
232     if (value == -10) {
233         LOG("[DEMO] posix stdlib test case 10(except):atoi(%d) ok == 21474836470.\n", value);
234     } else {
235         LOG("[DEMO] posix stdlib test case 10(except):atoi(%d) fail.\n", value);
236     }
237     TEST_ASSERT_EQUAL_INT(-10, value);
238     return 0;
239 }
240 #endif
241 
242 RUN_TEST_SUITE(PosixStdlibAtoiTest);
243 
PosixStdlibAtoiFuncTest()244 void PosixStdlibAtoiFuncTest()
245 {
246     LOG("begin PosixStdlibAtoiFuncTest....");
247     RUN_ONE_TESTCASE(testStdlibAtoi001);
248     RUN_ONE_TESTCASE(testStdlibAtoi002);
249     RUN_ONE_TESTCASE(testStdlibAtoi003);
250 #if (LOSCFG_LIBC_MUSL == 1)
251     RUN_ONE_TESTCASE(testStdlibAtoi004);
252     RUN_ONE_TESTCASE(testStdlibAtoi005);
253 #endif
254     RUN_ONE_TESTCASE(testStdlibAtoi006);
255     RUN_ONE_TESTCASE(testStdlibAtoi007);
256     RUN_ONE_TESTCASE(testStdlibAtoi008);
257     RUN_ONE_TESTCASE(testStdlibAtoi009);
258 #if (LOSCFG_LIBC_MUSL == 1)
259     RUN_ONE_TESTCASE(testStdlibAtoi010);
260 #endif
261     return;
262 }