]> git.saurik.com Git - apple/libc.git/blob - tests/envbuf.c
Libc-1272.200.26.tar.gz
[apple/libc.git] / tests / envbuf.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4
5 #include <darwintest.h>
6 #include "darwintest_utils.h"
7
8 #define IS_FLAG_SET(fp, flag) ((fp)->_flags & (flag))
9 #define BUFFER_SIZE(fp) ((fp)->_bf._size)
10
11 static FILE* test_setup(void)
12 {
13 char* temp_dir;
14 temp_dir = dt_tmpdir();
15 T_LOG("Temp Dir: %s\n", temp_dir);
16
17 char temp_path[128];
18 snprintf(temp_path, sizeof(temp_path), "%s/%s", temp_dir, "test_file");
19 T_LOG("Using Temporary File: %s\n", temp_path);
20 FILE* fp = fopen(temp_path, "w");
21 T_LOG("File opened: %d\n", fileno(fp));
22 T_ASSERT_EQ(fileno(fp), 3, "file descriptor matches");
23 // Need to prime the buffer
24 fprintf(fp, "Test\n");
25
26 return fp;
27 }
28
29 T_DECL(envbuf_PR_38637477_unbuf_all, "Forcing unbuffered through environment variable", T_META_ENVVAR("STDBUF=U"))
30 {
31 FILE* fp = test_setup();
32
33 T_EXPECT_TRUE(IS_FLAG_SET(fp, __SNBF), "unbuffered");
34 T_EXPECT_FALSE(IS_FLAG_SET(fp, __SLBF), "not line buffered");
35 T_EXPECT_EQ(BUFFER_SIZE(fp), 1, "buffer size 1");
36 }
37
38
39 T_DECL(envbuf_PR_38637477_line_all, "Forcing line buffering through environment variable", T_META_ENVVAR("STDBUF=L16"))
40 {
41 FILE* fp = test_setup();
42
43 T_EXPECT_FALSE(IS_FLAG_SET(fp, __SNBF), "not unbuffered");
44 T_EXPECT_TRUE(IS_FLAG_SET(fp, __SLBF), "line buffered");
45 T_EXPECT_EQ(BUFFER_SIZE(fp), 16, "buffer size 16");
46 }
47
48 T_DECL(envbuf_PR_38637477_full_all, "Forcing full buffering through environment variable", T_META_ENVVAR("STDBUF=F16"))
49 {
50 FILE* fp = test_setup();
51
52 T_EXPECT_FALSE(IS_FLAG_SET(fp, __SNBF), "not unbuffered");
53 T_EXPECT_FALSE(IS_FLAG_SET(fp, __SLBF), "not line buffered");
54 T_EXPECT_EQ(BUFFER_SIZE(fp), 16, "buffer size 16");
55 }
56
57 T_DECL(envbuf_PR_38637477_unbuf_stdout, "Forcing unbuffered through environment variable for stdout", T_META_ENVVAR("STDBUF1=U"))
58 {
59
60 fprintf(stdout, "Test\n");
61
62 T_EXPECT_TRUE(IS_FLAG_SET(stdout, __SNBF), "unbuffered");
63 T_EXPECT_FALSE(IS_FLAG_SET(stdout, __SLBF), "not linebuffered");
64 T_EXPECT_EQ(BUFFER_SIZE(stdout), 1, "buffer size 1");
65 }
66
67 T_DECL(envbuf_PR_38637477_line_stdout, "Forcing line buffering through environment variable for stdout", T_META_ENVVAR("STDBUF1=L32"))
68 {
69
70 fprintf(stdout, "Test\n");
71
72 T_EXPECT_FALSE(IS_FLAG_SET(stdout, __SNBF), "not unbuffered");
73 T_EXPECT_TRUE(IS_FLAG_SET(stdout, __SLBF), "line buffered");
74 T_EXPECT_EQ(BUFFER_SIZE(stdout), 32, "buffer size 32");
75 }
76
77 T_DECL(envbuf_PR_38637477_full_stdout, "Forcing full buffering through environment variable for stdout", T_META_ENVVAR("STDBUF1=F16"))
78 {
79
80 fprintf(stdout, "Test\n");
81
82 T_EXPECT_FALSE(IS_FLAG_SET(stdout, __SNBF), "not unbuffered");
83 T_EXPECT_FALSE(IS_FLAG_SET(stdout, __SLBF), "not line buffered");
84 T_EXPECT_EQ(BUFFER_SIZE(stdout), 16, "buffer size 16");
85 }