• 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 "log.h"
35 #include "los_config.h"
36 #include "kernel_test.h"
37 #include <ctype.h>
38 #include <stdlib.h>
39 #include <string.h>
40 /* *
41  * @tc.desc      : register a test suite, this suite is used to test basic flow and interface dependency
42  * @param        : subsystem name is utils
43  * @param        : module name is utilsFile
44  * @param        : test suit name is CmsisTaskFuncTestSuite
45  */
46 LITE_TEST_SUIT(Posix, Posixtimer, PosixStringStrStrTest);
47 
48 /* *
49  * @tc.setup     : setup for all testcases
50  * @return       : setup result, TRUE is success, FALSE is fail
51  */
PosixStringStrStrTestSetUp(void)52 static BOOL PosixStringStrStrTestSetUp(void)
53 {
54     return TRUE;
55 }
56 
57 /* *
58  * @tc.teardown  : teardown for all testcases
59  * @return       : teardown result, TRUE is success, FALSE is fail
60  */
PosixStringStrStrTestTearDown(void)61 static BOOL PosixStringStrStrTestTearDown(void)
62 {
63     LOG("+-------------------------------------------+\n");
64     return TRUE;
65 }
66 
67 /* *
68  * @tc.number    : TEST_STRING_STRSTR_001
69  * @tc.name      : find the first occurrence of sub-string in a string
70  * @tc.desc      : [C- SOFTWARE -0200]
71  */
72 LITE_TEST_CASE(PosixStringStrStrTest, testStringStrStr001, Function | MediumTest | Level1)
73 {
74     const char destS[] = "string this is string";
75     const char srcS[] = "string";
76 
77     char *ret = strstr(destS, srcS);
78     if (strcmp(ret, destS) == 0) {
79         LOG("[DEMO] posix string test case 1:strstr(%s) %s ok.\n", srcS, destS);
80     } else {
81         LOG("[DEMO] posix string test case 1:strstr(%s) %s fail.\n", srcS, destS);
82     }
83     ICUNIT_ASSERT_STRING_EQUAL(ret, destS, 0);
84     return 0;
85 }
86 
87 /* *
88  * @tc.number    : TEST_STRING_STRSTR_002
89  * @tc.name      : find the first occurrence of sub-string in a string
90  * @tc.desc      : [C- SOFTWARE -0200]
91  */
92 LITE_TEST_CASE(PosixStringStrStrTest, testStringStrStr002, Function | MediumTest | Level1)
93 {
94     const char destS[] = "string this is string";
95     const char srcS[] = "this is";
96 
97     char *ret = strstr(destS, srcS);
98     if (strcmp(ret, "this is string") == 0) {
99         LOG("[DEMO] posix string test case 2:strstr(%s) %s ok.\n", srcS, destS);
100     } else {
101         LOG("[DEMO] posix string test case 2:strstr(%s) %s fail.\n", srcS, destS);
102     }
103     ICUNIT_ASSERT_STRING_EQUAL(ret, "this is string", 0);
104     return 0;
105 }
106 
107 /* *
108  * @tc.number    : TEST_STRING_STRSTR_003
109  * @tc.name      : find the first occurrence of sub-string in a string
110  * @tc.desc      : [C- SOFTWARE -0200]
111  */
112 LITE_TEST_CASE(PosixStringStrStrTest, testStringStrStr003, Function | MediumTest | Level1)
113 {
114     const char dest[] = "hello world !";
115     const char srcT[] = "\0hello";
116 
117     char *ret = strstr(dest, srcT);
118     if (strcmp(ret, dest) == 0) {
119         LOG("[DEMO] posix string test case 3:strstr(%s) %s ok.\n", srcT, dest);
120     } else {
121         LOG("[DEMO] posix string test case 3:strstr(%s) %s fail.\n", srcT, dest);
122     }
123     ICUNIT_ASSERT_STRING_EQUAL(ret, dest, 0);
124     return 0;
125 }
126 
127 /* *
128  * @tc.number    : TEST_STRING_STRSTR_004
129  * @tc.name      : find the first occurrence of sub-string in a string
130  * @tc.desc      : [C- SOFTWARE -0200]
131  */
132 LITE_TEST_CASE(PosixStringStrStrTest, testStringStrStr004, Function | MediumTest | Level1)
133 {
134     const char dest[] = "hello world !";
135     const char src[] = "heAlo";
136 
137     char *ret = strstr(dest, src);
138     if (ret == NULL) {
139         LOG("[DEMO] posix string test case 4(except):strstr(%s) %s ok.\n", src, dest);
140     } else {
141         LOG("[DEMO] posix string test case 4(except):strstr(%s) %s fail.\n", src, dest);
142     }
143     ICUNIT_ASSERT_EQUAL(ret, NULL, 0);
144     return 0;
145 }
146 
147 /* *
148  * @tc.number    : TEST_STRING_STRSTR_005
149  * @tc.name      : find the first occurrence of sub-string in a string
150  * @tc.desc      : [C- SOFTWARE -0200]
151  */
152 LITE_TEST_CASE(PosixStringStrStrTest, testStringStrStr005, Function | MediumTest | Level1)
153 {
154     const char dest[] = "hello world !";
155     const char src[] = "hellm";
156 
157     char *ret = strstr(dest, src);
158     if (ret == NULL) {
159         LOG("[DEMO] posix string test case 5(except):strstr(%s) %s ok.\n", src, dest);
160     } else {
161         LOG("[DEMO] posix string test case 5(except):strstr(%s) %s fail.\n", src, dest);
162     }
163     ICUNIT_ASSERT_EQUAL(ret, NULL, 0);
164     return 0;
165 }
166 
167 /* *
168  * @tc.number    : TEST_STRING_STRSTR_006
169  * @tc.name      : find the first occurrence of sub-string in a string
170  * @tc.desc      : [C- SOFTWARE -0200]
171  */
172 LITE_TEST_CASE(PosixStringStrStrTest, testStringStrStr006, Function | MediumTest | Level1)
173 {
174     const char destS[] = "string this is string";
175     const char srcOne[] = "t"; // one byte
176 
177     char *ret = strstr(destS, srcOne);
178     if (strcmp(ret, "tring this is string") == 0) {
179         LOG("[DEMO] posix string test case 6:strstr(%s) %s ok.\n", srcOne, destS);
180     } else {
181         LOG("[DEMO] posix string test case 6:strstr(%s) %s fail.\n", srcOne, destS);
182     }
183     ICUNIT_ASSERT_STRING_EQUAL(ret, "tring this is string", 0);
184     return 0;
185 }
186 
187 /* *
188  * @tc.number    : TEST_STRING_STRSTR_007
189  * @tc.name      : find the first occurrence of sub-string in a string
190  * @tc.desc      : [C- SOFTWARE -0200]
191  */
192 LITE_TEST_CASE(PosixStringStrStrTest, testStringStrStr007, Function | MediumTest | Level1)
193 {
194     const char destS[] = "string this is string";
195     const char srcOne[] = "m"; // one byte
196 
197     char *ret = strstr(destS, srcOne);
198     if (ret == NULL) {
199         LOG("[DEMO] posix string test case 7(except):strstr(%s) %s ok.\n", srcOne, destS);
200     } else {
201         LOG("[DEMO] posix string test case 7(except):strstr(%s) %s fail.\n", srcOne, destS);
202     }
203     ICUNIT_ASSERT_EQUAL(ret, NULL, 0);
204     return 0;
205 }
206 
207 /* *
208  * @tc.number    : TEST_STRING_STRSTR_008
209  * @tc.name      : find the first occurrence of sub-string in a string
210  * @tc.desc      : [C- SOFTWARE -0200]
211  */
212 LITE_TEST_CASE(PosixStringStrStrTest, testStringStrStr008, Function | MediumTest | Level1)
213 {
214     const char destS[] = "string this is string";
215     const char srcTwo[] = "th"; // two byte
216 
217     char *ret = strstr(destS, srcTwo);
218     if (strcmp(ret, "this is string") == 0) {
219         LOG("[DEMO] posix string test case 8:strstr(%s) %s ok.\n", srcTwo, destS);
220     } else {
221         LOG("[DEMO] posix string test case 8:strstr(%s) %s fail.\n", srcTwo, destS);
222     }
223     ICUNIT_ASSERT_STRING_EQUAL(ret, "this is string", 0);
224     return 0;
225 }
226 
227 /* *
228  * @tc.number    : TEST_STRING_STRSTR_009
229  * @tc.name      : find the first occurrence of sub-string in a string
230  * @tc.desc      : [C- SOFTWARE -0200]
231  */
232 LITE_TEST_CASE(PosixStringStrStrTest, testStringStrStr009, Function | MediumTest | Level1)
233 {
234     const char destS[] = "string this is string";
235     const char srcTwo2[] = "tm"; // two byte
236 
237     char *ret = strstr(destS, srcTwo2);
238     if (ret == NULL) {
239         LOG("[DEMO] posix string test case 9(except):strstr(%s) %s ok.\n", srcTwo2, destS);
240     } else {
241         LOG("[DEMO] posix string test case 9(except):strstr(%s) %s fail.\n", srcTwo2, destS);
242     }
243     ICUNIT_ASSERT_EQUAL(ret, NULL, 0);
244     return 0;
245 }
246 
247 /* *
248  * @tc.number    : TEST_STRING_STRSTR_010
249  * @tc.name      : find the first occurrence of sub-string in a string
250  * @tc.desc      : [C- SOFTWARE -0200]
251  */
252 LITE_TEST_CASE(PosixStringStrStrTest, testStringStrStr010, Function | MediumTest | Level1)
253 {
254     const char destS[] = "string this is string";
255     const char srcThree[] = "thi"; // three byte
256 
257     char *ret = strstr(destS, srcThree);
258     if (strcmp(ret, "this is string") == 0) {
259         LOG("[DEMO] posix string test case 10:strstr(%s) %s ok.\n", srcThree, destS);
260     } else {
261         LOG("[DEMO] posix string test case 10:strstr(%s) %s fail.\n", srcThree, destS);
262     }
263     ICUNIT_ASSERT_STRING_EQUAL(ret, "this is string", 0);
264     return 0;
265 }
266 
267 /* *
268  * @tc.number    : TEST_STRING_STRSTR_011
269  * @tc.name      : find the first occurrence of sub-string in a string
270  * @tc.desc      : [C- SOFTWARE -0200]
271  */
272 LITE_TEST_CASE(PosixStringStrStrTest, testStringStrStr011, Function | MediumTest | Level1)
273 {
274     const char destS[] = "string this is string";
275     const char srcThree[] = "thm"; // three byte
276 
277     char *ret = strstr(destS, srcThree);
278     if (ret == NULL) {
279         LOG("[DEMO] posix string test case 11(except):strstr(%s) %s ok.\n", srcThree, destS);
280     } else {
281         LOG("[DEMO] posix string test case 11(except):strstr(%s) %s fail.\n", srcThree, destS);
282     }
283     ICUNIT_ASSERT_EQUAL(ret, NULL, 0);
284     return 0;
285 }
286 
287 /* *
288  * @tc.number    : TEST_STRING_STRSTR_012
289  * @tc.name      : find the first occurrence of sub-string in a string
290  * @tc.desc      : [C- SOFTWARE -0200]
291  */
292 LITE_TEST_CASE(PosixStringStrStrTest, testStringStrStr012, Function | MediumTest | Level1)
293 {
294     const char destS[] = "string this is string";
295     const char srcFour[] = "this"; // four byte
296 
297     char *ret = strstr(destS, srcFour);
298     if (strcmp(ret, "this is string") == 0) {
299         LOG("[DEMO] posix string test case 12:strstr(%s) %s ok.\n", srcFour, destS);
300     } else {
301         LOG("[DEMO] posix string test case 12:strstr(%s) %s fail.\n", srcFour, destS);
302     }
303     ICUNIT_ASSERT_STRING_EQUAL(ret, "this is string", 0);
304     return 0;
305 }
306 
307 /* *
308  * @tc.number    : TEST_STRING_STRSTR_013
309  * @tc.name      : find the first occurrence of sub-string in a string
310  * @tc.desc      : [C- SOFTWARE -0200]
311  */
312 LITE_TEST_CASE(PosixStringStrStrTest, testStringStrStr013, Function | MediumTest | Level1)
313 {
314     const char destS[] = "string this is string";
315     const char srcFour[] = "thim"; // four byte
316 
317     char *ret = strstr(destS, srcFour);
318     if (ret == NULL) {
319         LOG("[DEMO] posix string test case 13(except):strstr(%s) %s ok.\n", srcFour, destS);
320     } else {
321         LOG("[DEMO] posix string test case 13(except):strstr(%s) %s fail.\n", srcFour, destS);
322     }
323     ICUNIT_ASSERT_EQUAL(ret, NULL, 0);
324     return 0;
325 }
326 
327 RUN_TEST_SUITE(PosixStringStrStrTest);
328 
PosixStringStrstrTest(void)329 void PosixStringStrstrTest(void)
330 {
331     LOG("begin PosixStringStrstrTest....");
332     RUN_ONE_TESTCASE(testStringStrStr001);
333     RUN_ONE_TESTCASE(testStringStrStr002);
334     RUN_ONE_TESTCASE(testStringStrStr003);
335     RUN_ONE_TESTCASE(testStringStrStr004);
336     RUN_ONE_TESTCASE(testStringStrStr005);
337     RUN_ONE_TESTCASE(testStringStrStr006);
338     RUN_ONE_TESTCASE(testStringStrStr007);
339     RUN_ONE_TESTCASE(testStringStrStr008);
340     RUN_ONE_TESTCASE(testStringStrStr009);
341     RUN_ONE_TESTCASE(testStringStrStr010);
342     RUN_ONE_TESTCASE(testStringStrStr011);
343     RUN_ONE_TESTCASE(testStringStrStr012);
344     RUN_ONE_TESTCASE(testStringStrStr013);
345 
346     return;
347 }
348