• 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      : setlinebuf_0100
26  * @tc.desc      : Set file stream to linear buffer
27  * @tc.level     : Level 0
28  */
setlinebuf_0100(void)29 void setlinebuf_0100(void)
30 {
31     FILE *f = fopen(path, "w+");
32     char buffer[BUFSIZ];
33     errno = 0;
34     setlinebuf(f);
35     if (errno != 0) {
36         t_error("%s failed: errno = %d\n", __func__, errno);
37     }
38 
39     fputs(str, f);
40     fclose(f);
41     f = fopen(path, "r");
42 
43     memset(buffer, 0, sizeof(buffer));
44     fread(buffer, sizeof(buffer), 1, f);
45 
46     if (strcmp(buffer, str)) {
47         t_error("%s failed: setlinebuf. buf = %s\n", __func__, buffer);
48     }
49     fclose(f);
50     remove(path);
51 }
52 
53 /**
54  * @tc.name      : setlinebuf_0200
55  * @tc.desc      : Output to console
56  * @tc.level     : Level 2
57  */
setlinebuf_0200(void)58 void setlinebuf_0200(void)
59 {
60     errno = 0;
61     setlinebuf(stdout);
62     if (errno != 0) {
63         t_error("%s failed: errno = %d\n", __func__, errno);
64     }
65 
66     printf("test");
67     sleep(1);
68 }
69 
main(int argc,char * argv[])70 int main(int argc, char *argv[])
71 {
72     setlinebuf_0100();
73     setlinebuf_0200();
74     return t_status;
75 }