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 <string.h>
17 #include "functionalext.h"
18
19 /**
20 * @tc.name : getdelim_0100
21 * @tc.desc : Verify that the file content can be read with the specified delimiter (parameters are valid)
22 * @tc.level : Level 0
23 */
getdelim_0100(void)24 void getdelim_0100(void)
25 {
26 char *wrstring = "hello,world";
27 char *line = NULL;
28 size_t len = 0;
29
30 FILE *fp = fopen("getdelim.txt", "w+");
31 EXPECT_PTRNE("getdelim_0100", fp, NULL);
32
33 fwrite(wrstring, sizeof(char), strlen(wrstring), fp);
34 fseek(fp, 0, SEEK_SET);
35
36 ssize_t result = getdelim(&line, &len, ',', fp);
37 EXPECT_TRUE("getdelim_0100", result > 0);
38 EXPECT_TRUE("getdelim_0100", strcmp(line, "hello,") == 0);
39 EXPECT_TRUE("getdelim_0100", len > strlen("hello,"));
40
41 remove("getdelim.txt");
42 }
43
44 /**
45 * @tc.name : getdelim_0200
46 * @tc.desc : Verify that the delimiter cannot be specified to read the file content (the s parameter is invalid)
47 * @tc.level : Level 2
48 */
getdelim_0200(void)49 void getdelim_0200(void)
50 {
51 char *line = NULL;
52 char *wrstring = "helloworld";
53
54 FILE *fp = fopen("getdelim.txt", "w+");
55 EXPECT_PTRNE("getdelim_0200", fp, NULL);
56
57 ssize_t result = getdelim(&line, NULL, 'l', fp);
58 EXPECT_EQ("getdelim_0200", result, -1);
59 remove("getdelim.txt");
60 }
61
62 /**
63 * @tc.name : getdelim_0300
64 * @tc.desc : Verify that the delimiter cannot be specified to read the file content (the n parameter is invalid)
65 * @tc.level : Level 2
66 */
getdelim_0300(void)67 void getdelim_0300(void)
68 {
69 size_t a = 0;
70 char *wrstring = "helloworld";
71
72 FILE *fp = fopen("getdelim.txt", "w+");
73 EXPECT_PTRNE("getdelim_0300", fp, NULL);
74
75 ssize_t result = getdelim(NULL, &a, 'l', fp);
76 EXPECT_EQ("getdelim_0300", result, -1);
77 remove("getdelim.txt");
78 }
79
main(int argc,char * argv[])80 int main(int argc, char *argv[])
81 {
82 getdelim_0100();
83 getdelim_0200();
84 getdelim_0300();
85
86 return t_status;
87 }