• 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 <errno.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include "test.h"
20 
21 const char *path = "/data/tests/libc-test/src/file.txt";
22 const char *str = "Hello";
23 
24 /**
25  * @tc.name      : setbuffer_0100
26  * @tc.desc      : The buffer type is _IOFBF
27  * @tc.level     : Level 0
28  */
setbuffer_0100(void)29 void setbuffer_0100(void)
30 {
31     FILE *fp = fopen(path, "w+");
32     char buf[BUFSIZ] = {0};
33     errno = 0;
34     setbuffer(fp, buf, BUFSIZ);
35     if (errno != 0) {
36         t_error("%s failed: errno = %d\n", __func__, errno);
37     }
38 
39     fputs(str, fp);
40     fflush(fp);
41     fclose(fp);
42 
43     fp = fopen(path, "r");
44 
45     memset(buf, 0, sizeof(buf));
46     fread(buf, sizeof(buf), 1, fp);
47 
48     if (strcmp(buf, str)) {
49         t_error("%s failed: setbuf. buf = %s\n", __func__, buf);
50     }
51 
52     fclose(fp);
53     remove(path);
54 }
55 
56 /**
57  * @tc.name      : setbuffer_0200
58  * @tc.desc      : Stream buffering operations with NULL
59  * @tc.level     : Level 1
60  */
setbuffer_0200(void)61 void setbuffer_0200(void)
62 {
63     FILE *f = fopen(path, "w+");
64 
65     errno = 0;
66     setbuffer(f, NULL, BUFSIZ);
67     if (errno != 0) {
68         t_error("%s failed: errno = %d\n", __func__, errno);
69     }
70 
71     fclose(f);
72     remove(path);
73 }
74 
main(int argc,char * argv[])75 int main(int argc, char *argv[])
76 {
77     setbuffer_0100();
78     setbuffer_0200();
79     return t_status;
80 }