aboutsummaryrefslogtreecommitdiff
path: root/libc/test/src/stdio/fopen_test.cpp
blob: 42e7c57cffe04b67541b01de6c7249b20b47eec1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//===-- Unittests for fopen / fclose --------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/File/file.h"
#include "src/stdio/fclose.h"
#include "src/stdio/fopen.h"
#include "src/stdio/fwrite.h"
#include "src/stdio/fread.h"

#include "test/UnitTest/Test.h"

TEST(LlvmLibcFOpenTest, PrintToFile) {
  int result;

  FILE *file = LIBC_NAMESPACE::fopen("./testdata/test_data.txt", "w");
  ASSERT_FALSE(file == nullptr);

  static constexpr char STRING[] = "A simple string written to a file\n";
  result = LIBC_NAMESPACE::fwrite(STRING, 1, sizeof(STRING) - 1, file);
  EXPECT_GE(result, 0);

  ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));

  FILE *new_file = LIBC_NAMESPACE::fopen("./testdata/test_data.txt", "r");
  ASSERT_FALSE(new_file == nullptr);

  static char data[64] = {0};
  ASSERT_EQ(LIBC_NAMESPACE::fread(data, 1, sizeof(STRING) - 1, new_file),
            sizeof(STRING) - 1);
  data[sizeof(STRING) - 1] = '\0';
  ASSERT_STREQ(data, STRING);

  ASSERT_EQ(0, LIBC_NAMESPACE::fclose(new_file));
}