xref: /llvm-project/libc/test/src/stdio/setbuf_test.cpp (revision c63112a9118277a20ae440f3f69189c0937e8f4d)
14eea8849SSiva Chandra Reddy //===-- Unittests for setbuf ----------------------------------------------===//
24eea8849SSiva Chandra Reddy //
34eea8849SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44eea8849SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
54eea8849SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64eea8849SSiva Chandra Reddy //
74eea8849SSiva Chandra Reddy //===----------------------------------------------------------------------===//
84eea8849SSiva Chandra Reddy 
9*c63112a9Slntue #include "hdr/stdio_macros.h"
104eea8849SSiva Chandra Reddy #include "src/stdio/fclose.h"
114eea8849SSiva Chandra Reddy #include "src/stdio/fopen.h"
124eea8849SSiva Chandra Reddy #include "src/stdio/fread.h"
134eea8849SSiva Chandra Reddy #include "src/stdio/fwrite.h"
144eea8849SSiva Chandra Reddy #include "src/stdio/setbuf.h"
154eea8849SSiva Chandra Reddy #include "src/stdio/ungetc.h"
16af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
174eea8849SSiva Chandra Reddy 
184eea8849SSiva Chandra Reddy TEST(LlvmLibcSetbufTest, DefaultBufsize) {
194eea8849SSiva Chandra Reddy   // The idea in this test is to change the buffer after opening a file and
204eea8849SSiva Chandra Reddy   // ensure that read and write work as expected.
214eea8849SSiva Chandra Reddy   constexpr char FILENAME[] = "testdata/setbuf_test_default_bufsize.test";
22b6bc9d72SGuillaume Chatelet   ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
234eea8849SSiva Chandra Reddy   ASSERT_FALSE(file == nullptr);
244eea8849SSiva Chandra Reddy   char buffer[BUFSIZ];
25b6bc9d72SGuillaume Chatelet   LIBC_NAMESPACE::setbuf(file, buffer);
264eea8849SSiva Chandra Reddy   constexpr char CONTENT[] = "abcdef";
274eea8849SSiva Chandra Reddy   constexpr size_t CONTENT_SIZE = sizeof(CONTENT);
28b6bc9d72SGuillaume Chatelet   ASSERT_EQ(CONTENT_SIZE,
29b6bc9d72SGuillaume Chatelet             LIBC_NAMESPACE::fwrite(CONTENT, 1, CONTENT_SIZE, file));
30b6bc9d72SGuillaume Chatelet   ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
314eea8849SSiva Chandra Reddy 
32b6bc9d72SGuillaume Chatelet   file = LIBC_NAMESPACE::fopen(FILENAME, "r");
33b6bc9d72SGuillaume Chatelet   LIBC_NAMESPACE::setbuf(file, buffer);
344eea8849SSiva Chandra Reddy   ASSERT_FALSE(file == nullptr);
354eea8849SSiva Chandra Reddy   char data[CONTENT_SIZE];
36b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::fread(&data, 1, CONTENT_SIZE, file), CONTENT_SIZE);
374eea8849SSiva Chandra Reddy   ASSERT_STREQ(CONTENT, data);
38b6bc9d72SGuillaume Chatelet   ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
394eea8849SSiva Chandra Reddy }
404eea8849SSiva Chandra Reddy 
414eea8849SSiva Chandra Reddy TEST(LlvmLibcSetbufTest, NullBuffer) {
424eea8849SSiva Chandra Reddy   // The idea in this test is that we set a null buffer and ensure that
434eea8849SSiva Chandra Reddy   // everything works correctly.
444eea8849SSiva Chandra Reddy   constexpr char FILENAME[] = "testdata/setbuf_test_null_buffer.test";
45b6bc9d72SGuillaume Chatelet   ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
464eea8849SSiva Chandra Reddy   ASSERT_FALSE(file == nullptr);
47b6bc9d72SGuillaume Chatelet   LIBC_NAMESPACE::setbuf(file, nullptr);
484eea8849SSiva Chandra Reddy   constexpr char CONTENT[] = "abcdef";
494eea8849SSiva Chandra Reddy   constexpr size_t CONTENT_SIZE = sizeof(CONTENT);
50b6bc9d72SGuillaume Chatelet   ASSERT_EQ(CONTENT_SIZE,
51b6bc9d72SGuillaume Chatelet             LIBC_NAMESPACE::fwrite(CONTENT, 1, CONTENT_SIZE, file));
52b6bc9d72SGuillaume Chatelet   ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
534eea8849SSiva Chandra Reddy 
54b6bc9d72SGuillaume Chatelet   file = LIBC_NAMESPACE::fopen(FILENAME, "r");
55b6bc9d72SGuillaume Chatelet   LIBC_NAMESPACE::setbuf(file, nullptr);
564eea8849SSiva Chandra Reddy   ASSERT_FALSE(file == nullptr);
574eea8849SSiva Chandra Reddy   char data[CONTENT_SIZE];
58b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::fread(&data, 1, CONTENT_SIZE, file), CONTENT_SIZE);
594eea8849SSiva Chandra Reddy   ASSERT_STREQ(CONTENT, data);
604eea8849SSiva Chandra Reddy 
614eea8849SSiva Chandra Reddy   // Ensure that ungetc also works.
624eea8849SSiva Chandra Reddy   char unget_char = 'z';
63b6bc9d72SGuillaume Chatelet   ASSERT_EQ(int(unget_char), LIBC_NAMESPACE::ungetc(unget_char, file));
644eea8849SSiva Chandra Reddy   char c;
65b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::fread(&c, 1, 1, file), size_t(1));
664eea8849SSiva Chandra Reddy   ASSERT_EQ(c, unget_char);
674eea8849SSiva Chandra Reddy 
68b6bc9d72SGuillaume Chatelet   ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
694eea8849SSiva Chandra Reddy }
70