• 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 <fcntl.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include "test.h"
20 
21 const char *wrstring = "This is a test sample!";
22 const char *path = "test.txt";
23 
24 /**
25  * @tc.name      : ftell_0100
26  * @tc.desc      : Verify that the file pointer is at the beginning (call the ftell function to see the return value)
27  * @tc.level     : Level 0
28  */
ftell_0100(void)29 void ftell_0100(void)
30 {
31     FILE *fptr = fopen(path, "w+");
32     if (!fptr) {
33         t_error("%s fopen failed\n", __func__);
34     }
35 
36     size_t ret = fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
37     if (ret < 0) {
38         t_error("%s fwrite failed\n", __func__);
39     }
40 
41     int fret = fseek(fptr, 0L, SEEK_SET);
42     if (fret != 0) {
43         t_error("%s fseek failed\n", __func__);
44     }
45 
46     long result = ftell(fptr);
47     if (result != 0) {
48         t_error("%s ftell failed\n", __func__);
49     }
50 
51     fclose(fptr);
52     remove(path);
53 }
54 
55 /**
56  * @tc.name      : ftell_0200
57  * @tc.desc      : Verify that the file pointer is in the middle (call the ftell function to see the return value)
58  * @tc.level     : Level 0
59  */
ftell_0200(void)60 void ftell_0200(void)
61 {
62     FILE *fptr = fopen(path, "w+");
63     if (!fptr) {
64         t_error("%s fopen failed\n", __func__);
65     }
66 
67     size_t ret = fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
68     if (ret < 0) {
69         t_error("%s fwrite failed\n", __func__);
70     }
71 
72     int fret = fseek(fptr, 8L, SEEK_SET);
73     if (fret != 0) {
74         t_error("%s fseek failed\n", __func__);
75     }
76 
77     long result = ftell(fptr);
78     if (result != 8) {
79         t_error("%s ftell failed\n", __func__);
80     }
81 
82     fclose(fptr);
83     remove(path);
84 }
85 
86 /**
87  * @tc.name      : ftell_0300
88  * @tc.desc      : Verify that the file pointer is at the end (call the ftell function to see the return value)
89  * @tc.level     : Level 0
90  */
ftell_0300(void)91 void ftell_0300(void)
92 {
93     FILE *fptr = fopen(path, "w+");
94     if (!fptr) {
95         t_error("%s fopen failed\n", __func__);
96     }
97 
98     size_t ret = fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
99     if (ret < 0) {
100         t_error("%s fwrite failed\n", __func__);
101     }
102 
103     int fret = fseek(fptr, 0L, SEEK_END);
104     if (fret != 0) {
105         t_error("%s fseek failed\n", __func__);
106     }
107     long result = ftell(fptr);
108     if (result != 22) {
109         t_error("%s ftell failed\n", __func__);
110     }
111 
112     fclose(fptr);
113     remove(path);
114 }
115 
main(int argc,char * argv[])116 int main(int argc, char *argv[])
117 {
118     ftell_0100();
119     ftell_0200();
120     ftell_0300();
121 
122     return t_status;
123 }