1 //===-- Unittests for putc ---------------------------------------------===//
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/ferror.h"
11 #include "src/stdio/fopen.h"
12 #include "src/stdio/fread.h"
14 #include "src/stdio/putc.h"
16 #include "test/UnitTest/Test.h"
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);
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);
28 ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file
));
30 file
= LIBC_NAMESPACE::fopen(FILENAME
, "r");
31 ASSERT_FALSE(file
== nullptr);
34 ASSERT_EQ(LIBC_NAMESPACE::fread(data
, 1, sizeof(simple
) - 1, file
),
36 data
[sizeof(simple
) - 1] = '\0';
38 ASSERT_STREQ(data
, simple
);
40 ASSERT_EQ(LIBC_NAMESPACE::ferror(file
), 0);
41 EXPECT_LT(LIBC_NAMESPACE::putc('L', file
), 0);
43 ASSERT_EQ(LIBC_NAMESPACE::fclose(file
), 0);