• 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 <stdlib.h>
17 #include "functionalext.h"
18 
19 typedef void (*TEST_FUN)();
20 const int32_t NUM_ZERO = 0;
21 const int32_t NUM_TEN = 10;
22 const int32_t NUM_TWENTY = 20;
23 
24 /**
25  * @tc.name      : fread_0100
26  * @tc.desc      : Verify that the number of data read is less than the number of data in the file
27  * @tc.level     : Level 0
28  */
fread_0100(void)29 void fread_0100(void)
30 {
31     char abc[100] = {0};
32     const char *wrstring = "starttowritehelloworld";
33     const char *ptr = "/data/Freadtest.txt";
34     FILE *fptr = fopen(ptr, "w+");
35     EXPECT_PTRNE("fread_0100", fptr, NULL);
36 
37     fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
38     fseek(fptr, 0, SEEK_SET);
39 
40     size_t rsize = fread(abc, 1, 10, fptr);
41     EXPECT_EQ("fread_0100", rsize, 10);
42     EXPECT_STREQ("fread_0100", abc, "starttowri");
43 
44     fclose(fptr);
45     remove(ptr);
46 }
47 
48 /**
49  * @tc.name      : fread_0200
50  * @tc.desc      : Verify that the number of data read is greater than the number of data in the file
51  * @tc.level     : Level 1
52  */
fread_0200(void)53 void fread_0200(void)
54 {
55     char abc[100] = {0};
56     const char *wrstring = "startwritehelloworld";
57     const char *ptr = "/data/Freadtest.txt";
58 
59     FILE *fptr = fopen(ptr, "w+");
60     EXPECT_PTRNE("fread_0200", fptr, NULL);
61 
62     fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
63     fseek(fptr, 0, SEEK_SET);
64 
65     size_t rsize = fread(abc, 1, 25, fptr);
66     EXPECT_EQ("fread_0200", rsize, 20);
67     EXPECT_STREQ("fread_0200", abc, wrstring);
68 
69     fclose(fptr);
70     remove(ptr);
71 }
72 
73 /**
74  * @tc.name      : fread_0300
75  * @tc.desc      : Verify that the number of data read is greater than the number of data in the file
76  *                 (the file pointer is at the end)
77  * @tc.level     : Level 2
78  */
fread_0300(void)79 void fread_0300(void)
80 {
81     char abc[100] = {0};
82     const char *wrstring = "startwritehelloworld";
83     const char *ptr = "/data/Freadtest.txt";
84     FILE *fptr = fopen(ptr, "w+");
85     EXPECT_PTRNE("fread_0200", fptr, NULL);
86 
87     fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
88     fseek(fptr, 0, SEEK_END);
89     size_t rsize = fread(abc, 1, 10, fptr);
90     EXPECT_EQ("fread_0300", rsize, 0);
91 
92     fclose(fptr);
93     remove(ptr);
94 }
95 
96 /**
97  * @tc.name      : fread_0400
98  * @tc.desc      : Verify that the size parameter is invalid and cannot read the specified file content
99  * @tc.level     : Level 2
100  */
fread_0400(void)101 void fread_0400(void)
102 {
103     char abc[100] = {0};
104     const char *wrstring = "startwritehelloworld";
105     const char *ptr = "/data/Freadtest.txt";
106     FILE *fptr = fopen(ptr, "w+");
107     EXPECT_PTRNE("fread_0200", fptr, NULL);
108 
109     fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
110     fseek(fptr, 0, SEEK_END);
111 
112     size_t rsize = fread(abc, 0, 10, fptr);
113     EXPECT_EQ("fread_0400", rsize, 0);
114 
115     fclose(fptr);
116     remove(ptr);
117 }
118 
119 TEST_FUN G_Fun_Array[] = {
120     fread_0100,
121     fread_0200,
122     fread_0300,
123     fread_0400,
124 };
125 
main(int argc,char * argv[])126 int main(int argc, char *argv[])
127 {
128     int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
129     for (int pos = 0; pos < num; ++pos) {
130         G_Fun_Array[pos]();
131     }
132 
133     return t_status;
134 }