1 //===-- Unittests for setvbuf ---------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "src/stdio/fclose.h"
10 #include "src/stdio/fopen.h"
11 #include "src/stdio/fread.h"
12 #include "src/stdio/fwrite.h"
13 #include "src/stdio/setvbuf.h"
14 #include "test/UnitTest/Test.h"
16 #include "hdr/stdio_macros.h"
17 #include "src/errno/libc_errno.h"
19 TEST(LlvmLibcSetvbufTest
, SetNBFBuffer
) {
20 // The idea in this test is that we open a file for writing and reading, and
21 // then set a NBF buffer to the write handle. Since it is NBF, the data
22 // written using the write handle should be immediately readable by the read
24 constexpr char FILENAME
[] = "testdata/setvbuf_nbf.test";
26 ::FILE *fw
= LIBC_NAMESPACE::fopen(FILENAME
, "w");
27 ASSERT_FALSE(fw
== nullptr);
29 ASSERT_EQ(LIBC_NAMESPACE::setvbuf(fw
, buffer
, _IONBF
, BUFSIZ
), 0);
31 ::FILE *fr
= LIBC_NAMESPACE::fopen(FILENAME
, "r");
32 ASSERT_FALSE(fr
== nullptr);
34 constexpr char CONTENT
[] = "abcdef";
35 constexpr size_t CONTENT_SIZE
= sizeof(CONTENT
);
36 for (size_t i
= 0; i
< CONTENT_SIZE
; ++i
) {
37 ASSERT_EQ(size_t(1), LIBC_NAMESPACE::fwrite(CONTENT
+ i
, 1, 1, fw
));
39 ASSERT_EQ(size_t(1), LIBC_NAMESPACE::fread(&c
, 1, 1, fr
));
40 ASSERT_EQ(c
, CONTENT
[i
]);
43 ASSERT_EQ(0, LIBC_NAMESPACE::fclose(fw
));
44 ASSERT_EQ(0, LIBC_NAMESPACE::fclose(fr
));
46 // Make sure NBF buffer has no effect for reading.
47 fr
= LIBC_NAMESPACE::fopen(FILENAME
, "r");
48 char data
[CONTENT_SIZE
];
49 ASSERT_EQ(LIBC_NAMESPACE::setvbuf(fr
, buffer
, _IONBF
, BUFSIZ
), 0);
50 ASSERT_EQ(CONTENT_SIZE
, LIBC_NAMESPACE::fread(data
, 1, CONTENT_SIZE
, fr
));
51 ASSERT_STREQ(CONTENT
, data
);
52 ASSERT_EQ(0, LIBC_NAMESPACE::fclose(fr
));
55 TEST(LlvmLibcSetvbufTest
, SetLBFBuffer
) {
56 // The idea in this test is that we open a file for writing and reading, and
57 // then set a LBF buffer to the write handle. Since it is LBF, the data
58 // written using the write handle should be available right after a '\n' is
60 constexpr char FILENAME
[] = "testdata/setvbuf_lbf.test";
62 ::FILE *fw
= LIBC_NAMESPACE::fopen(FILENAME
, "w");
63 ASSERT_FALSE(fw
== nullptr);
65 ASSERT_EQ(LIBC_NAMESPACE::setvbuf(fw
, buffer
, _IOLBF
, BUFSIZ
), 0);
67 ::FILE *fr
= LIBC_NAMESPACE::fopen(FILENAME
, "r");
68 ASSERT_FALSE(fr
== nullptr);
70 constexpr char CONTENT
[] = "abcdef\n";
71 constexpr size_t CONTENT_SIZE
= sizeof(CONTENT
);
72 ASSERT_EQ(CONTENT_SIZE
, LIBC_NAMESPACE::fwrite(CONTENT
, 1, CONTENT_SIZE
, fw
));
74 // Note that CONTENT_SIZE worth of data written also includes the
75 // null-terminator '\0'. But, since it is after the new line character,
76 // it should not be availabe for reading.
77 char data
[CONTENT_SIZE
];
78 ASSERT_EQ(CONTENT_SIZE
- 1, LIBC_NAMESPACE::fread(data
, 1, CONTENT_SIZE
, fr
));
80 ASSERT_EQ(size_t(0), LIBC_NAMESPACE::fread(&c
, 1, 1, fr
));
82 data
[CONTENT_SIZE
- 1] = '\0';
83 ASSERT_STREQ(CONTENT
, data
);
85 ASSERT_EQ(0, LIBC_NAMESPACE::fclose(fw
));
86 ASSERT_EQ(0, LIBC_NAMESPACE::fclose(fr
));
88 // Make sure LBF buffer has no effect for reading.
89 fr
= LIBC_NAMESPACE::fopen(FILENAME
, "r");
90 ASSERT_EQ(LIBC_NAMESPACE::setvbuf(fr
, buffer
, _IOLBF
, BUFSIZ
), 0);
91 ASSERT_EQ(CONTENT_SIZE
, LIBC_NAMESPACE::fread(data
, 1, CONTENT_SIZE
, fr
));
92 ASSERT_STREQ(CONTENT
, data
);
93 ASSERT_EQ(0, LIBC_NAMESPACE::fclose(fr
));
96 TEST(LlvmLibcSetbufTest
, InvalidBufferMode
) {
97 constexpr char FILENAME
[] = "testdata/setvbuf_invalid_bufmode.test";
98 ::FILE *f
= LIBC_NAMESPACE::fopen(FILENAME
, "w");
99 ASSERT_FALSE(f
== nullptr);
101 ASSERT_NE(LIBC_NAMESPACE::setvbuf(f
, buf
, _IOFBF
+ _IOLBF
+ _IONBF
, BUFSIZ
),
103 ASSERT_ERRNO_EQ(EINVAL
);
105 LIBC_NAMESPACE::libc_errno
= 0;
106 ASSERT_EQ(0, LIBC_NAMESPACE::fclose(f
));