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

source code of libc/test/src/stdio/putc_test.cpp