| 1 | //===-- Unittests for putc ---------------------------------------------===// |
| 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 "src/stdio/fclose.h" |
| 10 | #include "src/stdio/ferror.h" |
| 11 | #include "src/stdio/fopen.h" |
| 12 | #include "src/stdio/fread.h" |
| 13 | |
| 14 | #include "src/stdio/putc.h" |
| 15 | |
| 16 | #include "test/UnitTest/Test.h" |
| 17 | |
| 18 | TEST(LlvmLibcPutcTest, WriteToFile) { |
| 19 | constexpr char FILENAME[] = "testdata/putc_output.test" ; |
| 20 | ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w" ); |
| 21 | ASSERT_FALSE(file == nullptr); |
| 22 | |
| 23 | constexpr char simple[] = "simple letters" ; |
| 24 | for (size_t i = 0; i < sizeof(simple); ++i) { |
| 25 | ASSERT_EQ(LIBC_NAMESPACE::putc(simple[i], file), 0); |
| 26 | } |
| 27 | |
| 28 | ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file)); |
| 29 | |
| 30 | file = LIBC_NAMESPACE::fopen(FILENAME, "r" ); |
| 31 | ASSERT_FALSE(file == nullptr); |
| 32 | char data[50]; |
| 33 | |
| 34 | ASSERT_EQ(LIBC_NAMESPACE::fread(data, 1, sizeof(simple) - 1, file), |
| 35 | sizeof(simple) - 1); |
| 36 | data[sizeof(simple) - 1] = '\0'; |
| 37 | |
| 38 | ASSERT_STREQ(data, simple); |
| 39 | |
| 40 | ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0); |
| 41 | EXPECT_LT(LIBC_NAMESPACE::putc('L', file), 0); |
| 42 | |
| 43 | ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0); |
| 44 | } |
| 45 | |