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