1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <stdbool.h>
17570af302Sopenharmony_ci#include "functionalext.h"
18570af302Sopenharmony_ci
19570af302Sopenharmony_ci/**
20570af302Sopenharmony_ci * @tc.name      : fgets_0100
21570af302Sopenharmony_ci * @tc.desc      : The parameter n is greater than the number of characters,and all content can be read.
22570af302Sopenharmony_ci * @tc.level     : Level 0
23570af302Sopenharmony_ci */
24570af302Sopenharmony_civoid fgets_0100(void)
25570af302Sopenharmony_ci{
26570af302Sopenharmony_ci    bool successflag = false;
27570af302Sopenharmony_ci    char str[100];
28570af302Sopenharmony_ci    const char *ptr = "fgetstest.txt";
29570af302Sopenharmony_ci    char *wrstring = "this is a test\n";
30570af302Sopenharmony_ci    FILE *fptr = fopen(ptr, "wr+");
31570af302Sopenharmony_ci    fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
32570af302Sopenharmony_ci    fflush(fptr);
33570af302Sopenharmony_ci    fseek(fptr, 0L, SEEK_SET);
34570af302Sopenharmony_ci    char *content = fgets(str, 100, fptr);
35570af302Sopenharmony_ci    if (strcmp(content, "this is a test\n") == 0) {
36570af302Sopenharmony_ci        successflag = true;
37570af302Sopenharmony_ci    }
38570af302Sopenharmony_ci    EXPECT_TRUE("fgets_0100", successflag);
39570af302Sopenharmony_ci    fclose(fptr);
40570af302Sopenharmony_ci    remove(ptr);
41570af302Sopenharmony_ci    fptr = NULL;
42570af302Sopenharmony_ci    ptr = NULL;
43570af302Sopenharmony_ci    wrstring = NULL;
44570af302Sopenharmony_ci}
45570af302Sopenharmony_ci
46570af302Sopenharmony_ci/**
47570af302Sopenharmony_ci * @tc.name      : fgets_0200
48570af302Sopenharmony_ci * @tc.desc      : The parameter n is greater than 2,and part of the content can be read.
49570af302Sopenharmony_ci * @tc.level     : Level 0
50570af302Sopenharmony_ci */
51570af302Sopenharmony_civoid fgets_0200(void)
52570af302Sopenharmony_ci{
53570af302Sopenharmony_ci    char str[5];
54570af302Sopenharmony_ci    bool successflag = false;
55570af302Sopenharmony_ci    const char *ptr = "fgetstest.txt";
56570af302Sopenharmony_ci    char *wrstring = "this is a test\n";
57570af302Sopenharmony_ci    FILE *fptr = fopen(ptr, "wr+");
58570af302Sopenharmony_ci    fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
59570af302Sopenharmony_ci    fflush(fptr);
60570af302Sopenharmony_ci    fseek(fptr, 0L, SEEK_SET);
61570af302Sopenharmony_ci    char *content = fgets(str, 5, fptr);
62570af302Sopenharmony_ci    if (strcmp(content, "this") == 0) {
63570af302Sopenharmony_ci        successflag = true;
64570af302Sopenharmony_ci    }
65570af302Sopenharmony_ci    EXPECT_TRUE("fgets_0200", successflag);
66570af302Sopenharmony_ci    fclose(fptr);
67570af302Sopenharmony_ci    remove(ptr);
68570af302Sopenharmony_ci    fptr = NULL;
69570af302Sopenharmony_ci    ptr = NULL;
70570af302Sopenharmony_ci}
71570af302Sopenharmony_ci
72570af302Sopenharmony_ci/**
73570af302Sopenharmony_ci * @tc.name      : fgets_0300
74570af302Sopenharmony_ci * @tc.desc      : The parameter n is equal to 2,and part of the content can be read.
75570af302Sopenharmony_ci * @tc.level     : Level 1
76570af302Sopenharmony_ci */
77570af302Sopenharmony_civoid fgets_0300(void)
78570af302Sopenharmony_ci{
79570af302Sopenharmony_ci    char str[2];
80570af302Sopenharmony_ci    bool successflag = false;
81570af302Sopenharmony_ci    const char *ptr = "fgetstest.txt";
82570af302Sopenharmony_ci    char *wrstring = "this is a test\n";
83570af302Sopenharmony_ci    FILE *fptr = fopen(ptr, "wr+");
84570af302Sopenharmony_ci    fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
85570af302Sopenharmony_ci    fflush(fptr);
86570af302Sopenharmony_ci    fseek(fptr, 0L, SEEK_SET);
87570af302Sopenharmony_ci    char *content = fgets(str, 2, fptr);
88570af302Sopenharmony_ci    if (strcmp(content, "t") == 0) {
89570af302Sopenharmony_ci        successflag = true;
90570af302Sopenharmony_ci    }
91570af302Sopenharmony_ci    EXPECT_TRUE("fgets_0300", successflag);
92570af302Sopenharmony_ci    fclose(fptr);
93570af302Sopenharmony_ci    remove(ptr);
94570af302Sopenharmony_ci    fptr = NULL;
95570af302Sopenharmony_ci    ptr = NULL;
96570af302Sopenharmony_ci}
97570af302Sopenharmony_ci
98570af302Sopenharmony_ci/**
99570af302Sopenharmony_ci * @tc.name      : fgets_0400
100570af302Sopenharmony_ci * @tc.desc      : The parameter n is equal to 1,and the content cannot be read.
101570af302Sopenharmony_ci * @tc.level     : Level 2
102570af302Sopenharmony_ci */
103570af302Sopenharmony_civoid fgets_0400(void)
104570af302Sopenharmony_ci{
105570af302Sopenharmony_ci    char str[2];
106570af302Sopenharmony_ci    bool successflag = false;
107570af302Sopenharmony_ci    const char *ptr = "fgetstest.txt";
108570af302Sopenharmony_ci    char *wrstring = "this is a test\n";
109570af302Sopenharmony_ci    FILE *fptr = fopen(ptr, "wr+");
110570af302Sopenharmony_ci    fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
111570af302Sopenharmony_ci    fflush(fptr);
112570af302Sopenharmony_ci    fseek(fptr, 0L, SEEK_SET);
113570af302Sopenharmony_ci    char *content = fgets(str, 1, fptr);
114570af302Sopenharmony_ci    if (*content == 0) {
115570af302Sopenharmony_ci        successflag = true;
116570af302Sopenharmony_ci    }
117570af302Sopenharmony_ci    EXPECT_TRUE("fgets_0400", successflag);
118570af302Sopenharmony_ci    fclose(fptr);
119570af302Sopenharmony_ci    remove(ptr);
120570af302Sopenharmony_ci    fptr = NULL;
121570af302Sopenharmony_ci    ptr = NULL;
122570af302Sopenharmony_ci}
123570af302Sopenharmony_ci
124570af302Sopenharmony_ci/**
125570af302Sopenharmony_ci * @tc.name      : fgets_0500
126570af302Sopenharmony_ci * @tc.desc      : The parameter n is equal to 0,and the content cannot be read.
127570af302Sopenharmony_ci * @tc.level     : Level 2
128570af302Sopenharmony_ci */
129570af302Sopenharmony_civoid fgets_0500(void)
130570af302Sopenharmony_ci{
131570af302Sopenharmony_ci    char str[2];
132570af302Sopenharmony_ci    bool successflag = false;
133570af302Sopenharmony_ci    const char *ptr = "fgetstest.txt";
134570af302Sopenharmony_ci    char *wrstring = "this is a test\n";
135570af302Sopenharmony_ci    FILE *fptr = fopen(ptr, "wr+");
136570af302Sopenharmony_ci    fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
137570af302Sopenharmony_ci    fflush(fptr);
138570af302Sopenharmony_ci    fseek(fptr, 0L, SEEK_SET);
139570af302Sopenharmony_ci    char *content = fgets(str, 0, fptr);
140570af302Sopenharmony_ci    if (content == NULL) {
141570af302Sopenharmony_ci        successflag = true;
142570af302Sopenharmony_ci    }
143570af302Sopenharmony_ci    EXPECT_TRUE("fgets_0500", successflag);
144570af302Sopenharmony_ci    fclose(fptr);
145570af302Sopenharmony_ci    remove(ptr);
146570af302Sopenharmony_ci    fptr = NULL;
147570af302Sopenharmony_ci    ptr = NULL;
148570af302Sopenharmony_ci}
149570af302Sopenharmony_ci
150570af302Sopenharmony_ci/**
151570af302Sopenharmony_ci * @tc.name      : fgets_0600
152570af302Sopenharmony_ci * @tc.desc      : Points to the end of the file,and the content cannot be read.
153570af302Sopenharmony_ci * @tc.level     : Level 2
154570af302Sopenharmony_ci */
155570af302Sopenharmony_civoid fgets_0600(void)
156570af302Sopenharmony_ci{
157570af302Sopenharmony_ci    char str[100];
158570af302Sopenharmony_ci    bool successflag = false;
159570af302Sopenharmony_ci    const char *ptr = "fgetstest.txt";
160570af302Sopenharmony_ci    char *wrstring = "this is a test\n";
161570af302Sopenharmony_ci    FILE *fptr = fopen(ptr, "wr+");
162570af302Sopenharmony_ci    fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
163570af302Sopenharmony_ci    fflush(fptr);
164570af302Sopenharmony_ci    fseek(fptr, 0L, SEEK_END);
165570af302Sopenharmony_ci    char *content = fgets(str, 100, fptr);
166570af302Sopenharmony_ci    if (content == NULL) {
167570af302Sopenharmony_ci        successflag = true;
168570af302Sopenharmony_ci    }
169570af302Sopenharmony_ci    EXPECT_TRUE("fgets_0600", successflag);
170570af302Sopenharmony_ci    fclose(fptr);
171570af302Sopenharmony_ci    remove(ptr);
172570af302Sopenharmony_ci    fptr = NULL;
173570af302Sopenharmony_ci    ptr = NULL;
174570af302Sopenharmony_ci}
175570af302Sopenharmony_ci
176570af302Sopenharmony_ciint main(int argc, char *argv[])
177570af302Sopenharmony_ci{
178570af302Sopenharmony_ci    fgets_0100();
179570af302Sopenharmony_ci    fgets_0200();
180570af302Sopenharmony_ci    fgets_0300();
181570af302Sopenharmony_ci    fgets_0400();
182570af302Sopenharmony_ci    fgets_0500();
183570af302Sopenharmony_ci    fgets_0600();
184570af302Sopenharmony_ci
185570af302Sopenharmony_ci    return t_status;
186570af302Sopenharmony_ci}