5 #include <darwintest.h>
6 #include "darwintest_utils.h"
8 #define IS_FLAG_SET(fp, flag) ((fp)->_flags & (flag))
9 #define BUFFER_SIZE(fp) ((fp)->_bf._size)
11 static FILE* test_setup(void)
14 temp_dir
= dt_tmpdir();
15 T_LOG("Temp Dir: %s\n", temp_dir
);
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");
29 T_DECL(envbuf_PR_38637477_unbuf_all
, "Forcing unbuffered through environment variable", T_META_ENVVAR("STDBUF=U"))
31 FILE* fp
= test_setup();
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");
39 T_DECL(envbuf_PR_38637477_line_all
, "Forcing line buffering through environment variable", T_META_ENVVAR("STDBUF=L16"))
41 FILE* fp
= test_setup();
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");
48 T_DECL(envbuf_PR_38637477_full_all
, "Forcing full buffering through environment variable", T_META_ENVVAR("STDBUF=F16"))
50 FILE* fp
= test_setup();
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");
57 T_DECL(envbuf_PR_38637477_unbuf_stdout
, "Forcing unbuffered through environment variable for stdout", T_META_ENVVAR("STDBUF1=U"))
60 fprintf(stdout
, "Test\n");
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");
67 T_DECL(envbuf_PR_38637477_line_stdout
, "Forcing line buffering through environment variable for stdout", T_META_ENVVAR("STDBUF1=L32"))
70 fprintf(stdout
, "Test\n");
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");
77 T_DECL(envbuf_PR_38637477_full_stdout
, "Forcing full buffering through environment variable for stdout", T_META_ENVVAR("STDBUF1=F16"))
80 fprintf(stdout
, "Test\n");
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");