1 | //===-- Unittests for setbuf ----------------------------------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "hdr/stdio_macros.h" |
10 | #include "src/stdio/fclose.h" |
11 | #include "src/stdio/fopen.h" |
12 | #include "src/stdio/fread.h" |
13 | #include "src/stdio/fwrite.h" |
14 | #include "src/stdio/setbuf.h" |
15 | #include "src/stdio/ungetc.h" |
16 | #include "test/UnitTest/Test.h" |
17 | |
18 | TEST(LlvmLibcSetbufTest, DefaultBufsize) { |
19 | // The idea in this test is to change the buffer after opening a file and |
20 | // ensure that read and write work as expected. |
21 | constexpr char FILENAME[] = "testdata/setbuf_test_default_bufsize.test" ; |
22 | ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w" ); |
23 | ASSERT_FALSE(file == nullptr); |
24 | char buffer[BUFSIZ]; |
25 | LIBC_NAMESPACE::setbuf(file, buffer); |
26 | constexpr char CONTENT[] = "abcdef" ; |
27 | constexpr size_t CONTENT_SIZE = sizeof(CONTENT); |
28 | ASSERT_EQ(CONTENT_SIZE, |
29 | LIBC_NAMESPACE::fwrite(CONTENT, 1, CONTENT_SIZE, file)); |
30 | ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file)); |
31 | |
32 | file = LIBC_NAMESPACE::fopen(FILENAME, "r" ); |
33 | LIBC_NAMESPACE::setbuf(file, buffer); |
34 | ASSERT_FALSE(file == nullptr); |
35 | char data[CONTENT_SIZE]; |
36 | ASSERT_EQ(LIBC_NAMESPACE::fread(&data, 1, CONTENT_SIZE, file), CONTENT_SIZE); |
37 | ASSERT_STREQ(CONTENT, data); |
38 | ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file)); |
39 | } |
40 | |
41 | TEST(LlvmLibcSetbufTest, NullBuffer) { |
42 | // The idea in this test is that we set a null buffer and ensure that |
43 | // everything works correctly. |
44 | constexpr char FILENAME[] = "testdata/setbuf_test_null_buffer.test" ; |
45 | ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w" ); |
46 | ASSERT_FALSE(file == nullptr); |
47 | LIBC_NAMESPACE::setbuf(file, nullptr); |
48 | constexpr char CONTENT[] = "abcdef" ; |
49 | constexpr size_t CONTENT_SIZE = sizeof(CONTENT); |
50 | ASSERT_EQ(CONTENT_SIZE, |
51 | LIBC_NAMESPACE::fwrite(CONTENT, 1, CONTENT_SIZE, file)); |
52 | ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file)); |
53 | |
54 | file = LIBC_NAMESPACE::fopen(FILENAME, "r" ); |
55 | LIBC_NAMESPACE::setbuf(file, nullptr); |
56 | ASSERT_FALSE(file == nullptr); |
57 | char data[CONTENT_SIZE]; |
58 | ASSERT_EQ(LIBC_NAMESPACE::fread(&data, 1, CONTENT_SIZE, file), CONTENT_SIZE); |
59 | ASSERT_STREQ(CONTENT, data); |
60 | |
61 | // Ensure that ungetc also works. |
62 | char unget_char = 'z'; |
63 | ASSERT_EQ(int(unget_char), LIBC_NAMESPACE::ungetc(unget_char, file)); |
64 | char c; |
65 | ASSERT_EQ(LIBC_NAMESPACE::fread(&c, 1, 1, file), size_t(1)); |
66 | ASSERT_EQ(c, unget_char); |
67 | |
68 | ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file)); |
69 | } |
70 | |