1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <string.h>
18 #include <wchar.h>
19 #include "test.h"
20 
21 #define N(s, sub)                                                                  \
22     {                                                                              \
23         wchar_t *p = s;                                                            \
24         wchar_t *q = wcswcs(p, sub);                                               \
25         if (q)                                                                     \
26             t_error("wcswcs(%s,%s) returned str+%d, wanted 0\n", #s, #sub, q - p); \
27     }
28 
29 #define T(s, sub, n)                                                                       \
30     {                                                                                      \
31         wchar_t *p = s;                                                                    \
32         wchar_t *q = wcswcs(p, sub);                                                       \
33         if (q == 0)                                                                        \
34             t_error("wcswcs(%s,%s) returned 0, wanted str+%d\n", #s, #sub, n);             \
35         else if (q - p != n)                                                               \
36             t_error("wcswcs(%s,%s) returned str+%d, wanted str+%d\n", #s, #sub, q - p, n); \
37     }
38 
39 /**
40  * @tc.name      : wcswcs_0100
41  * @tc.desc      : Test the wcswcs method to find the first occurrence of the second wide string from the first wide
42  *                 string
43  * @tc.level     : Level 0
44  */
wcswcs_0100(void)45 void wcswcs_0100(void)
46 {
47     T(L"", L"", 0)
48     T(L"abcd", L"", 0)
49     T(L"abcd", L"a", 0)
50     T(L"abcd", L"b", 1)
51     T(L"abcd", L"c", 2)
52     T(L"abcd", L"d", 3)
53     T(L"abcd", L"ab", 0)
54     T(L"abcd", L"bc", 1)
55     T(L"abcd", L"cd", 2)
56     T(L"ababa", L"baba", 1)
57     T(L"ababab", L"babab", 1)
58     T(L"abababa", L"bababa", 1)
59     T(L"abababab", L"bababab", 1)
60     T(L"ababababa", L"babababa", 1)
61     T(L"abbababab", L"bababa", 2)
62     T(L"abbababab", L"ababab", 3)
63     T(L"abacabcabcab", L"abcabcab", 4)
64     T(L"nanabanabanana", L"aba", 3)
65     T(L"nanabanabanana", L"ban", 4)
66     T(L"nanabanabanana", L"anab", 1)
67     T(L"nanabanabanana", L"banana", 8)
68     T(L"_ _\xff_ _", L"_\xff_", 2)
69 }
70 
71 /**
72  * @tc.name      : wcswcs_0200
73  * @tc.desc      : Test the result of the wcswcs method when the second substring cannot be found from the first
74  * substring
75  * @tc.level     : Level 1
76  */
wcswcs_0200(void)77 void wcswcs_0200(void)
78 {
79     N(L"", L"a")
80     N(L"a", L"aa")
81     N(L"a", L"b")
82     N(L"aa", L"ab")
83     N(L"aa", L"aaa")
84     N(L"abba", L"aba")
85     N(L"abc abc", L"abcd")
86     N(L"0-1-2-3-4-5-6-7-8-9", L"-3-4-56-7-8-")
87     N(L"0-1-2-3-4-5-6-7-8-9", L"-3-4-5+6-7-8-")
88     N(L"_ _ _\xff_ _ _", L"_\x7f_")
89     N(L"_ _ _\x7f_ _ _", L"_\xff_")
90 }
91 
main(int argc, char *argv[])92 int main(int argc, char *argv[])
93 {
94     wcswcs_0100();
95     wcswcs_0200();
96     return t_status;
97 }