• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      : strndup_0100
22  * @tc.desc      : Test the strndup function to copy the string according to the input parameter
23  * @tc.level     : Level 0
24  */
strndup_0100(void)25 void strndup_0100(void)
26 {
27     char *str = "helloworld";
28     char *result = strndup(str, 2);
29     if (strcmp(result, "he")) {
30         t_error("%s strndup get result is '%s' are not 'he'\n", __func__, result);
31     }
32 }
33 
34 /**
35  * @tc.name      : strndup_0200
36  * @tc.desc      : Test the return value of strndup when the number of copies of the input exceeds the passed string
37  * @tc.level     : Level 1
38  */
strndup_0200(void)39 void strndup_0200(void)
40 {
41     char *str = "helloworld";
42     char *result = strndup(str, 12);
43     if (strcmp(result, "helloworld")) {
44         t_error("%s strndup get result is '%s' are not 'he'\n", __func__, result);
45     }
46 }
47 
48 /**
49  * @tc.name      : strndup_0300
50  * @tc.desc      : The return value of strndup when the number of test copies is 0
51  * @tc.level     : Level 1
52  */
strndup_0300(void)53 void strndup_0300(void)
54 {
55     char *str = "helloworld";
56     char *result = strndup(str, 0);
57     if (strcmp(result, "") != 0) {
58         t_error("%s strndup get result is '%s' are not ''\n", __func__, result);
59     }
60 }
61 
main(int argc,char * argv[])62 int main(int argc, char *argv[])
63 {
64     strndup_0100();
65     strndup_0200();
66     strndup_0300();
67     return t_status;
68 }