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 "functionalext.h"
18
19 const int SUCCESS = 0;
20 const int SIZE = 20;
21 const int HALF_SIZE = 10;
22 const int END_SIZE = 0;
23
24 /**
25 * @tc.name : fsetpos_0100
26 * @tc.desc : Each parameter is valid, and the file pointer is set to the beginning of the file.
27 * @tc.level : Level 0
28 */
fsetpos_0100()29 void fsetpos_0100()
30 {
31 char str[100] = {0};
32 const char *ptr = "test.txt";
33 const char *wstring = "This is a test case!";
34 long long a = 0;
35 fpos_t pos;
36 pos = (fpos_t)a;
37 FILE *fptr = fopen(ptr, "w+");
38 EXPECT_TRUE("fsetpos_0100", fptr != NULL);
39 fwrite(wstring, sizeof(char), strlen(wstring), fptr);
40 int result = fsetpos(fptr, &pos);
41 EXPECT_EQ("fsetpos_0100", result, SUCCESS);
42 int rsize = fread(str, sizeof(char), 100, fptr);
43 EXPECT_EQ("fsetpos_0100", rsize, SIZE);
44 fclose(fptr);
45 remove(ptr);
46 fptr = NULL;
47 ptr = NULL;
48 }
49
50 /**
51 * @tc.name : fsetpos_0200
52 * @tc.desc : Each parameter is valid, and the file pointer is set to the middle of the file.
53 * @tc.level : Level 0
54 */
fsetpos_0200()55 void fsetpos_0200()
56 {
57 char str[100] = {0};
58 const char *ptr = "test.txt";
59 const char *wstring = "This is a test case!";
60 fpos_t pos;
61 long long a = 10;
62 pos = (fpos_t)a;
63 FILE *fptr = fopen(ptr, "w+");
64 fwrite(wstring, sizeof(char), strlen(wstring), fptr);
65 int data = fsetpos(fptr, &pos);
66 EXPECT_EQ("fsetpos_0200", data, SUCCESS);
67 int rsize = fread(str, 1, 100, fptr);
68 EXPECT_EQ("fsetpos_0200", rsize, HALF_SIZE);
69 fclose(fptr);
70 remove(ptr);
71 fptr = NULL;
72 ptr = NULL;
73 wstring = NULL;
74 }
75
76 /**
77 * @tc.name : fsetpos_0300
78 * @tc.desc : Each parameter is valid, and the file pointer is set to the end of the file.
79 * @tc.level : Level 0
80 */
fsetpos_0300()81 void fsetpos_0300()
82 {
83 char str[100] = {0};
84 const char *ptr = "test.txt";
85 const char *wstring = "This is a test case!";
86 fpos_t pos;
87 long long a = 20;
88 pos = (fpos_t)a;
89 FILE *fptr = fopen(ptr, "w+");
90 fwrite(wstring, sizeof(char), strlen(wstring), fptr);
91 int data = fsetpos(fptr, &pos);
92 EXPECT_EQ("fsetpos_0300", data, SUCCESS);
93 int rsize = fread(str, 1, 100, fptr);
94 EXPECT_EQ("fsetpos_0300", rsize, END_SIZE);
95 fclose(fptr);
96 remove(ptr);
97 fptr = NULL;
98 ptr = NULL;
99 wstring = NULL;
100 }
101
102 /**
103 * @tc.name : fsetpos_0400
104 * @tc.desc : The pos parameter is valid, and the file pointer cannot be set.
105 * @tc.level : Level 2
106 */
fsetpos_0400()107 void fsetpos_0400()
108 {
109 char str[100] = {0};
110 const char *ptr = "test.txt";
111 const char *wrstring = "This is a test case!";
112 fpos_t pos;
113 long long a = -1;
114 pos = (fpos_t)a;
115 FILE *fptr = fopen(ptr, "w+");
116 fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
117 int data = fsetpos(fptr, &pos);
118 EXPECT_NE("fsetpos_0400", data, SUCCESS);
119 fclose(fptr);
120 remove(ptr);
121 fptr = NULL;
122 ptr = NULL;
123 wrstring = NULL;
124 }
125
main(int argc,char * argv[])126 int main(int argc, char *argv[])
127 {
128 fsetpos_0100();
129 fsetpos_0200();
130 fsetpos_0300();
131 fsetpos_0400();
132 return t_status;
133 }