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 <stdbool.h>
17 #include "functionalext.h"
18
19 /**
20 * @tc.name : fgets_0100
21 * @tc.desc : The parameter n is greater than the number of characters,and all content can be read.
22 * @tc.level : Level 0
23 */
fgets_0100(void)24 void fgets_0100(void)
25 {
26 bool successflag = false;
27 char str[100];
28 const char *ptr = "fgetstest.txt";
29 char *wrstring = "this is a test\n";
30 FILE *fptr = fopen(ptr, "wr+");
31 fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
32 fflush(fptr);
33 fseek(fptr, 0L, SEEK_SET);
34 char *content = fgets(str, 100, fptr);
35 if (strcmp(content, "this is a test\n") == 0) {
36 successflag = true;
37 }
38 EXPECT_TRUE("fgets_0100", successflag);
39 fclose(fptr);
40 remove(ptr);
41 fptr = NULL;
42 ptr = NULL;
43 wrstring = NULL;
44 }
45
46 /**
47 * @tc.name : fgets_0200
48 * @tc.desc : The parameter n is greater than 2,and part of the content can be read.
49 * @tc.level : Level 0
50 */
fgets_0200(void)51 void fgets_0200(void)
52 {
53 char str[5];
54 bool successflag = false;
55 const char *ptr = "fgetstest.txt";
56 char *wrstring = "this is a test\n";
57 FILE *fptr = fopen(ptr, "wr+");
58 fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
59 fflush(fptr);
60 fseek(fptr, 0L, SEEK_SET);
61 char *content = fgets(str, 5, fptr);
62 if (strcmp(content, "this") == 0) {
63 successflag = true;
64 }
65 EXPECT_TRUE("fgets_0200", successflag);
66 fclose(fptr);
67 remove(ptr);
68 fptr = NULL;
69 ptr = NULL;
70 }
71
72 /**
73 * @tc.name : fgets_0300
74 * @tc.desc : The parameter n is equal to 2,and part of the content can be read.
75 * @tc.level : Level 1
76 */
fgets_0300(void)77 void fgets_0300(void)
78 {
79 char str[2];
80 bool successflag = false;
81 const char *ptr = "fgetstest.txt";
82 char *wrstring = "this is a test\n";
83 FILE *fptr = fopen(ptr, "wr+");
84 fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
85 fflush(fptr);
86 fseek(fptr, 0L, SEEK_SET);
87 char *content = fgets(str, 2, fptr);
88 if (strcmp(content, "t") == 0) {
89 successflag = true;
90 }
91 EXPECT_TRUE("fgets_0300", successflag);
92 fclose(fptr);
93 remove(ptr);
94 fptr = NULL;
95 ptr = NULL;
96 }
97
98 /**
99 * @tc.name : fgets_0400
100 * @tc.desc : The parameter n is equal to 1,and the content cannot be read.
101 * @tc.level : Level 2
102 */
fgets_0400(void)103 void fgets_0400(void)
104 {
105 char str[2];
106 bool successflag = false;
107 const char *ptr = "fgetstest.txt";
108 char *wrstring = "this is a test\n";
109 FILE *fptr = fopen(ptr, "wr+");
110 fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
111 fflush(fptr);
112 fseek(fptr, 0L, SEEK_SET);
113 char *content = fgets(str, 1, fptr);
114 if (*content == 0) {
115 successflag = true;
116 }
117 EXPECT_TRUE("fgets_0400", successflag);
118 fclose(fptr);
119 remove(ptr);
120 fptr = NULL;
121 ptr = NULL;
122 }
123
124 /**
125 * @tc.name : fgets_0500
126 * @tc.desc : The parameter n is equal to 0,and the content cannot be read.
127 * @tc.level : Level 2
128 */
fgets_0500(void)129 void fgets_0500(void)
130 {
131 char str[2];
132 bool successflag = false;
133 const char *ptr = "fgetstest.txt";
134 char *wrstring = "this is a test\n";
135 FILE *fptr = fopen(ptr, "wr+");
136 fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
137 fflush(fptr);
138 fseek(fptr, 0L, SEEK_SET);
139 char *content = fgets(str, 0, fptr);
140 if (content == NULL) {
141 successflag = true;
142 }
143 EXPECT_TRUE("fgets_0500", successflag);
144 fclose(fptr);
145 remove(ptr);
146 fptr = NULL;
147 ptr = NULL;
148 }
149
150 /**
151 * @tc.name : fgets_0600
152 * @tc.desc : Points to the end of the file,and the content cannot be read.
153 * @tc.level : Level 2
154 */
fgets_0600(void)155 void fgets_0600(void)
156 {
157 char str[100];
158 bool successflag = false;
159 const char *ptr = "fgetstest.txt";
160 char *wrstring = "this is a test\n";
161 FILE *fptr = fopen(ptr, "wr+");
162 fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
163 fflush(fptr);
164 fseek(fptr, 0L, SEEK_END);
165 char *content = fgets(str, 100, fptr);
166 if (content == NULL) {
167 successflag = true;
168 }
169 EXPECT_TRUE("fgets_0600", successflag);
170 fclose(fptr);
171 remove(ptr);
172 fptr = NULL;
173 ptr = NULL;
174 }
175
main(int argc,char * argv[])176 int main(int argc, char *argv[])
177 {
178 fgets_0100();
179 fgets_0200();
180 fgets_0300();
181 fgets_0400();
182 fgets_0500();
183 fgets_0600();
184
185 return t_status;
186 }