• 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 <unistd.h>
18 #include "functionalext.h"
19 
20 #define TEST_BUFFER_SIZE 64
21 
22 /**
23  * @tc.name      : pread_0100
24  * @tc.desc      : Read data from a file into a buffer
25  * @tc.level     : Level 1
26  */
pread_0100(void)27 void pread_0100(void)
28 {
29     const char *txt = "This is pread_0100 test.";
30     char buffer[TEST_BUFFER_SIZE];
31     memset(buffer, 0x0, sizeof(buffer));
32     FILE *fp = tmpfile();
33     if (!fp) {
34         EXPECT_PTRNE("pread_0100", fp, NULL);
35         return;
36     }
37     int fd = fileno(fp);
38     pwrite(fd, txt, strlen(txt), 0);
39     lseek(fd, 0, SEEK_SET);
40     size_t cnt = pread(fd, buffer, TEST_BUFFER_SIZE, 0);
41     EXPECT_EQ("pread_0100", cnt, strlen(txt));
42     EXPECT_STREQ("pread_0100", txt, buffer);
43     int ret = fclose(fp);
44     EXPECT_EQ("pread_0100", ret, 0);
45 }
46 
47 /**
48  * @tc.name      : pread_0200
49  * @tc.desc      : The current position is at the end, read data from a file into a buffer
50  * @tc.level     : Level 1
51  */
pread_0200(void)52 void pread_0200(void)
53 {
54     const char *txt = "This is pread_0200 test.";
55     char buffer[TEST_BUFFER_SIZE];
56     memset(buffer, 0x0, sizeof(buffer));
57     FILE *fp = tmpfile();
58     if (!fp) {
59         EXPECT_PTRNE("pread_0200", fp, NULL);
60         return;
61     }
62     int fd = fileno(fp);
63     pwrite(fd, txt, strlen(txt), 0);
64     size_t cnt = pread(fd, buffer, TEST_BUFFER_SIZE, 0);
65     EXPECT_EQ("pread_0200", cnt, strlen(txt));
66     EXPECT_STREQ("pread_0200", txt, buffer);
67     int ret = fclose(fp);
68     EXPECT_EQ("pread_0200", ret, 0);
69 }
70 
71 /**
72  * @tc.name      : pread_0300
73  * @tc.desc      : The current position is at the end, read the following data
74  * @tc.level     : Level 2
75  */
pread_0300(void)76 void pread_0300(void)
77 {
78     const char *txt = "This is pread_0300 test.";
79     char buffer[TEST_BUFFER_SIZE];
80     memset(buffer, 0x0, sizeof(buffer));
81     FILE *fp = tmpfile();
82     if (!fp) {
83         EXPECT_PTRNE("pread_0300", fp, NULL);
84         return;
85     }
86     int fd = fileno(fp);
87     pwrite(fd, txt, strlen(txt), 0);
88     size_t cnt = pread(fd, buffer, TEST_BUFFER_SIZE, strlen(txt));
89     EXPECT_EQ("pread_0300", cnt, 0);
90     int ret = fclose(fp);
91     EXPECT_EQ("pread_0300", ret, 0);
92 }
93 
main(void)94 int main(void)
95 {
96     pread_0100();
97     pread_0200();
98     pread_0300();
99     return t_status;
100 }