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 <stdlib.h>
17 #include <stdint.h>
18 #include "functionalext.h"
19
20 typedef void (*TEST_FUN)();
21
22 const int32_t NUN_ELEVEN = 11;
23
24 /**
25 * @tc.name : fgetpos_0100
26 * @tc.desc : File pointer at the end of a file, the position to which the current file pointer points
27 * @tc.level : Level 0
28 */
fgetpos_0100()29 void fgetpos_0100()
30 {
31 fpos_t pos;
32 char buff[] = "hello world";
33 const char *path = "/data/test.txt";
34
35 FILE *fp = fopen(path, "w+");
36 EXPECT_PTRNE("fgetpos_0100", fp, NULL);
37
38 fputs(buff, fp);
39 fseek(fp, 0, SEEK_END);
40
41 int result = fgetpos(fp, &pos);
42 EXPECT_EQ("fgetpos_0100", result, 0);
43
44 fclose(fp);
45 remove(path);
46 }
47
48 /**
49 * @tc.name : fgetpos_0200
50 * @tc.desc : File pointer at the beginning of a file, the location to which the current file pointer points
51 * @tc.level : Level 0
52 */
fgetpos_0200()53 void fgetpos_0200()
54 {
55 fpos_t pos;
56 char buff[] = "hello world";
57 const char *path = "/data/test.txt";
58
59 FILE *fptr = fopen(path, "w+");
60 EXPECT_PTRNE("fgetpos_0100", fptr, NULL);
61
62 fputs(buff, fptr);
63 fseek(fptr, 0, SEEK_SET);
64 int result = fgetpos(fptr, &pos);
65 EXPECT_EQ("fgetpos_0200", result, 0);
66
67 fclose(fptr);
68 remove(path);
69 }
70
71 /**
72 * @tc.name : fgetpos_0300
73 * @tc.desc : Invalid argument. Cannot get the location of the current file pointer
74 * @tc.level : Level 2
75 */
fgetpos_0300()76 void fgetpos_0300()
77 {
78 fpos_t pos;
79 const char *path = "/data/test.txt";
80 FILE *fptr = fopen(path, "w+");
81 fclose(fptr);
82
83 int result = fgetpos(fptr, &pos);
84 EXPECT_EQ("fgetpos_0300", result, -1);
85 remove(path);
86 }
87
88 TEST_FUN G_Fun_Array[] = {
89 fgetpos_0100,
90 fgetpos_0200,
91 fgetpos_0300,
92 };
93
main(int argc,char * argv[])94 int main(int argc, char *argv[])
95 {
96 int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
97 for (int pos = 0; pos < num; ++pos) {
98 G_Fun_Array[pos]();
99 }
100
101 return t_status;
102 }