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, PosixStringStrchrTest);
48
49 /* *
50 * @tc.setup : setup for all testcases
51 * @return : setup result, TRUE is success, FALSE is fail
52 */
PosixStringStrchrTestSetUp(void)53 static BOOL PosixStringStrchrTestSetUp(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 */
PosixStringStrchrTestTearDown(void)62 static BOOL PosixStringStrchrTestTearDown(void)
63 {
64 LOG("+-------------------------------------------+\n");
65 return TRUE;
66 }
67
68 /* *
69 * @tc.number : TEST_STRING_STRCHR_001
70 * @tc.name : find the first occurrence of a character in a string
71 * @tc.desc : [C- SOFTWARE -0200]
72 */
73 LITE_TEST_CASE(PosixStringStrchrTest, testStringStrchr001, Function | MediumTest | Level1)
74 {
75 char src[] = "hello !! world";
76 char *ret = strchr(src, '!');
77 if (strcmp(ret, "!! world") == 0) {
78 LOG("[DEMO] posix string test case 1:strchr(!) %s ok.\n", src);
79 } else {
80 LOG("[DEMO] posix string test case 1:strchr(!) %s fail.\n", src);
81 }
82 ICUNIT_ASSERT_STRING_EQUAL(ret, "!! world", 0);
83 return 0;
84 }
85
86 /* *
87 * @tc.number : TEST_STRING_STRCHR_002
88 * @tc.name : find the first occurrence of a character in a string
89 * @tc.desc : [C- SOFTWARE -0200]
90 */
91 LITE_TEST_CASE(PosixStringStrchrTest, testStringStrchr002, Function | MediumTest | Level1)
92 {
93 char src[] = "hello !! world";
94 char *ret = strchr(src, 'l');
95 if (strcmp(ret, "llo !! world") == 0) {
96 LOG("[DEMO] posix string test case 2:strchr(l) %s ok.\n", src);
97 } else {
98 LOG("[DEMO] posix string test case 2:strchr(l) %s fail.\n", src);
99 }
100 ICUNIT_ASSERT_STRING_EQUAL(ret, "llo !! world", 0);
101 return 0;
102 }
103
104 /* *
105 * @tc.number : TEST_STRING_STRCHR_003
106 * @tc.name : find the first occurrence of a character in a string
107 * @tc.desc : [C- SOFTWARE -0200]
108 */
109 LITE_TEST_CASE(PosixStringStrchrTest, testStringStrchr003, Function | MediumTest | Level1)
110 {
111 char src[] = "hello !! world";
112 char *ret = strchr(src, '\0');
113 if (ret != NULL) {
114 LOG("[DEMO] posix string test case 3:strchr(\'\\0\') %s ok.\n", src);
115 } else {
116 LOG("[DEMO] posix string test case 3:strchr(\'\\0\') %s fail.\n", src);
117 }
118 ICUNIT_ASSERT_NOT_EQUAL(ret, NULL, 0);
119 return 0;
120 }
121
122 /* *
123 * @tc.number : TEST_STRING_STRCHR_004
124 * @tc.name : find the first occurrence of a character in a string
125 * @tc.desc : [C- SOFTWARE -0200]
126 */
127 LITE_TEST_CASE(PosixStringStrchrTest, testStringStrchr004, Function | MediumTest | Level1)
128 {
129 char src[] = "hello !! world";
130 char *ret = strchr(src, '?');
131 if (ret == NULL) {
132 LOG("[DEMO] posix string test case 4(except):strchr(?) %s ok.\n", src);
133 } else {
134 LOG("[DEMO] posix string test case 4(except):strchr(?) %s fail.\n", src);
135 }
136 ICUNIT_ASSERT_EQUAL(ret, NULL, 0);
137 return 0;
138 }
139
140 /* *
141 * @tc.number : TEST_STRING_STRCHR_005
142 * @tc.name : find the first occurrence of a character in a string
143 * @tc.desc : [C- SOFTWARE -0200]
144 */
145 LITE_TEST_CASE(PosixStringStrchrTest, testStringStrchr005, Function | MediumTest | Level1)
146 {
147 char src[] = "hello !! world";
148 char *ret = strchr(src, 'm');
149 if (ret == NULL) {
150 LOG("[DEMO] posix string test case 5(except):strchr(m) %s ok.\n", src);
151 } else {
152 LOG("[DEMO] posix string test case 5(except):strchr(m) %s fail.\n", src);
153 }
154 ICUNIT_ASSERT_EQUAL(ret, NULL, 0);
155 return 0;
156 }
157
158 /* *
159 * @tc.number : TEST_STRING_STRCHR_005
160 * @tc.name : find the first occurrence of a character in a string
161 * @tc.desc : [C- SOFTWARE -0200]
162 */
163 LITE_TEST_CASE(PosixStringStrchrTest, testStringStrchr006, Function | MediumTest | Level1)
164 {
165 char src[] = "hello !! world";
166 char *ret = strchr(src, 0);
167 if (ret != NULL) {
168 LOG("[DEMO] posix string test case 6(except):strchr(0) %s ok.\n", src);
169 } else {
170 LOG("[DEMO] posix string test case 6(except):strchr(0) %s fail.\n", src);
171 }
172 ICUNIT_ASSERT_NOT_EQUAL(ret, NULL, 0);
173 return 0;
174 }
175
176 RUN_TEST_SUITE(PosixStringStrchrTest);
177
PosixStringStrchrTest()178 void PosixStringStrchrTest()
179 {
180 LOG("begin PosixStringStrchrTest....");
181 RUN_ONE_TESTCASE(testStringStrchr001);
182 RUN_ONE_TESTCASE(testStringStrchr002);
183 RUN_ONE_TESTCASE(testStringStrchr003);
184 RUN_ONE_TESTCASE(testStringStrchr004);
185 RUN_ONE_TESTCASE(testStringStrchr005);
186 RUN_ONE_TESTCASE(testStringStrchr006);
187
188 return;
189 }