42 lines · cpp
1//===-- Unittests for fopen / fclose --------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/__support/File/file.h"10#include "src/stdio/fclose.h"11#include "src/stdio/fopen.h"12#include "src/stdio/fwrite.h"13#include "src/stdio/fread.h"14 15#include "test/UnitTest/Test.h"16 17TEST(LlvmLibcFOpenTest, PrintToFile) {18 int result;19 20 FILE *file =21 LIBC_NAMESPACE::fopen(APPEND_LIBC_TEST("testdata/test.txt"), "w");22 ASSERT_FALSE(file == nullptr);23 24 static constexpr char STRING[] = "A simple string written to a file\n";25 result = LIBC_NAMESPACE::fwrite(STRING, 1, sizeof(STRING) - 1, file);26 EXPECT_GE(result, 0);27 28 ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));29 30 FILE *new_file =31 LIBC_NAMESPACE::fopen(APPEND_LIBC_TEST("testdata/test.txt"), "r");32 ASSERT_FALSE(new_file == nullptr);33 34 static char data[64] = {0};35 ASSERT_EQ(LIBC_NAMESPACE::fread(data, 1, sizeof(STRING) - 1, new_file),36 sizeof(STRING) - 1);37 data[sizeof(STRING) - 1] = '\0';38 ASSERT_STREQ(data, STRING);39 40 ASSERT_EQ(0, LIBC_NAMESPACE::fclose(new_file));41}42