xref: /llvm-project/libc/test/src/stdio/putc_test.cpp (revision c63112a9118277a20ae440f3f69189c0937e8f4d)
140b49439SMichael Jones //===-- Unittests for putc ---------------------------------------------===//
240b49439SMichael Jones //
340b49439SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
440b49439SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
540b49439SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
640b49439SMichael Jones //
740b49439SMichael Jones //===----------------------------------------------------------------------===//
840b49439SMichael Jones 
940b49439SMichael Jones #include "src/stdio/fclose.h"
1040b49439SMichael Jones #include "src/stdio/ferror.h"
1140b49439SMichael Jones #include "src/stdio/fopen.h"
1240b49439SMichael Jones #include "src/stdio/fread.h"
1340b49439SMichael Jones 
1440b49439SMichael Jones #include "src/stdio/putc.h"
1540b49439SMichael Jones 
16af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
1740b49439SMichael Jones 
1840b49439SMichael Jones TEST(LlvmLibcPutcTest, WriteToFile) {
1940b49439SMichael Jones   constexpr char FILENAME[] = "testdata/putc_output.test";
20*b6bc9d72SGuillaume Chatelet   ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
2140b49439SMichael Jones   ASSERT_FALSE(file == nullptr);
2240b49439SMichael Jones 
2340b49439SMichael Jones   constexpr char simple[] = "simple letters";
2440b49439SMichael Jones   for (size_t i = 0; i < sizeof(simple); ++i) {
25*b6bc9d72SGuillaume Chatelet     ASSERT_EQ(LIBC_NAMESPACE::putc(simple[i], file), 0);
2640b49439SMichael Jones   }
2740b49439SMichael Jones 
28*b6bc9d72SGuillaume Chatelet   ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
2940b49439SMichael Jones 
30*b6bc9d72SGuillaume Chatelet   file = LIBC_NAMESPACE::fopen(FILENAME, "r");
3140b49439SMichael Jones   ASSERT_FALSE(file == nullptr);
3240b49439SMichael Jones   char data[50];
3340b49439SMichael Jones 
34*b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::fread(data, 1, sizeof(simple) - 1, file),
3540b49439SMichael Jones             sizeof(simple) - 1);
3640b49439SMichael Jones   data[sizeof(simple) - 1] = '\0';
3740b49439SMichael Jones 
3840b49439SMichael Jones   ASSERT_STREQ(data, simple);
3940b49439SMichael Jones 
40*b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
41*b6bc9d72SGuillaume Chatelet   EXPECT_LT(LIBC_NAMESPACE::putc('L', file), 0);
4240b49439SMichael Jones 
43*b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
4440b49439SMichael Jones }
45