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 "test.h"
19 
20 /**
21  * @tc.name      : index_0100
22  * @tc.desc      : Test the index method to get the position of the first occurrence of the target character
23  * @tc.level     : Level 0
24  */
index_0100(void)25 void index_0100(void)
26 {
27     char *str = "hello world";
28     char *result = index(str, 'e');
29     if (strcmp(str + 1, result) != 0) {
30         t_error("%s index get result is %s are not  want %s\n", __func__, result, str + 1);
31     }
32 }
33 
34 /**
35  * @tc.name      : index_0200
36  * @tc.desc      : The result returned by the index function when the target character appears multiple times in the
37  * test string
38  * @tc.level     : Level 1
39  */
index_0200(void)40 void index_0200(void)
41 {
42     int add_size = 2;
43     char *str = "hello world";
44     char *result = index(str, 'l');
45     if (strcmp(str + add_size, result) != 0) {
46         t_error("%s index get result is %s are not  want %s\n", __func__, result, str + add_size);
47     }
48 }
49 
50 /**
51  * @tc.name      : index_0300
52  * @tc.desc      : The result returned by the index function when the target character does not appear in the test
53  * string
54  * @tc.level     : Level 1
55  */
index_0300(void)56 void index_0300(void)
57 {
58     char *str = "hello world";
59     char *result = index(str, 'a');
60     if (result) {
61         t_error("%s index get result is %s are not  want ''\n", __func__, result);
62     }
63 }
64 
main(int argc, char *argv[])65 int main(int argc, char *argv[])
66 {
67     index_0100();
68     index_0200();
69     index_0300();
70     return t_status;
71 }