• 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 const char *wrstring = "This is a test sample!";
21 const char *path = "/data/test.txt";
22 
23 /**
24  * @tc.name      : fseeko_0100
25  * @tc.desc      : Verify that the file pointer is moved to the beginning of the file
26  * @tc.level     : Level 0
27  */
fseeko_0100(void)28 void fseeko_0100(void)
29 {
30     FILE *fp = fopen(path, "w+");
31     if (!fp) {
32         t_error("%s fopen failed\n", __func__);
33     }
34 
35     size_t ret = fwrite(wrstring, sizeof(char), strlen(wrstring), fp);
36     if (ret < 0) {
37         t_error("%s fwrite failed\n", __func__);
38     }
39 
40     int result = fseeko(fp, 0L, SEEK_SET);
41     char ch = fgetc(fp);
42     if (result != 0) {
43         t_error("%s fseeko failed, result is %d\n", __func__, result);
44     }
45     if (ch != 'T') {
46         t_error("%s fseeko failed, ch is %c\n", __func__, ch);
47     }
48 
49     fclose(fp);
50     remove(path);
51 }
52 
53 /**
54  * @tc.name      : fseeko_0200
55  * @tc.desc      : Verify that the file pointer is moved to any position in the file
56  * @tc.level     : Level 0
57  */
fseeko_0200(void)58 void fseeko_0200(void)
59 {
60     FILE *fp = fopen(path, "w+");
61     if (!fp) {
62         t_error("%s fopen failed\n", __func__);
63     }
64 
65     size_t ret = fwrite(wrstring, sizeof(char), strlen(wrstring), fp);
66     if (ret < 0) {
67         t_error("%s fwrite failed\n", __func__);
68     }
69 
70     int result = fseeko(fp, 8L, SEEK_SET);
71     char ch = fgetc(fp);
72     if (result != 0) {
73         t_error("%s fseeko failed, result is %d\n", __func__, result);
74     }
75     if (ch != 'a') {
76         t_error("%s fseeko failed, ch is %c\n", __func__, ch);
77     }
78 
79     fclose(fp);
80     remove(path);
81 }
82 
83 /**
84  * @tc.name      : fseeko_0300
85  * @tc.desc      : Verify that the file pointer is moved to the current position of the file
86  * @tc.level     : Level 0
87  */
fseeko_0300(void)88 void fseeko_0300(void)
89 {
90     FILE *fp = fopen(path, "w+");
91     if (!fp) {
92         t_error("%s fopen failed\n", __func__);
93     }
94 
95     size_t ret = fwrite(wrstring, sizeof(char), strlen(wrstring), fp);
96     if (ret < 0) {
97         t_error("%s fwrite failed\n", __func__);
98     }
99 
100     int code = fseeko(fp, 10L, SEEK_SET);
101     if (code != 0) {
102         t_error("%s fseeko failed, code is %d\n", __func__, code);
103     }
104 
105     int data = fseeko(fp, 0L, SEEK_CUR);
106     if (data != 0) {
107         t_error("%s fseeko failed, data is %d\n", __func__, data);
108     }
109 
110     char ch = fgetc(fp);
111     if (ch != 't') {
112         t_error("%s fseeko failed, ch is %c\n", __func__, ch);
113     }
114 
115     fclose(fp);
116     remove(path);
117 }
118 
119 /**
120  * @tc.name      : fseeko_0400
121  * @tc.desc      : Verify that the file pointer is moved to the end of the file
122  * @tc.level     : Level 0
123  */
fseeko_0400(void)124 void fseeko_0400(void)
125 {
126     FILE *fp = fopen(path, "w+");
127     if (!fp) {
128         t_error("%s fopen failed\n", __func__);
129     }
130 
131     size_t ret = fwrite(wrstring, sizeof(char), strlen(wrstring), fp);
132     if (ret < 0) {
133         t_error("%s fwrite failed\n", __func__);
134     }
135 
136     int result = fseeko(fp, -1L, SEEK_END);
137     char ch = fgetc(fp);
138     if (result != 0) {
139         t_error("%s fseeko failed, result is %d\n", __func__, result);
140     }
141     if (ch != '!') {
142         t_error("%s fseeko failed, ch is %c\n", __func__, ch);
143     }
144 
145     fclose(fp);
146     remove(path);
147 }
148 
149 /**
150  * @tc.name      : fseeko_0500
151  * @tc.desc      : Verify that the moved file pointer position exceeds the starting position pointer movement failed
152  * @tc.level     : Level 2
153  */
fseeko_0500(void)154 void fseeko_0500(void)
155 {
156     FILE *fp = fopen(path, "w+");
157     if (!fp) {
158         t_error("%s fopen failed\n", __func__);
159     }
160 
161     size_t ret = fwrite(wrstring, sizeof(char), strlen(wrstring), fp);
162     if (ret < 0) {
163         t_error("%s fwrite failed\n", __func__);
164     }
165 
166     int result = fseeko(fp, -10L, SEEK_SET);
167     if (result != -1) {
168         t_error("%s fseeko should be failed, result is %d\n", __func__, result);
169     }
170 
171     fclose(fp);
172     remove(path);
173 }
174 
175 /**
176  * @tc.name      : fseeko_0600
177  * @tc.desc      : Verify that the moved file pointer position exceeds the end position pointer movement failed
178  * @tc.level     : Level 1
179  */
fseeko_0600(void)180 void fseeko_0600(void)
181 {
182     FILE *fp = fopen(path, "w+");
183     if (!fp) {
184         t_error("%s fopen failed\n", __func__);
185     }
186 
187     size_t ret = fwrite(wrstring, sizeof(char), strlen(wrstring), fp);
188     if (ret < 0) {
189         t_error("%s fwrite failed\n", __func__);
190     }
191 
192     int result = fseeko(fp, 10L, SEEK_END);
193     if (result != 0) {
194         t_error("%s fseeko failed, result is %d\n", __func__, result);
195     }
196 
197     fclose(fp);
198     remove(path);
199 }
200 
main(int argc,char * argv[])201 int main(int argc, char *argv[])
202 {
203     fseeko_0100();
204     fseeko_0200();
205     fseeko_0300();
206     fseeko_0400();
207     fseeko_0500();
208     fseeko_0600();
209 
210     return t_status;
211 }