xref: /aosp_15_r20/external/llvm-libc/test/src/stdio/fopen_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1*71db0c75SAndroid Build Coastguard Worker //===-- Unittests for fopen / fclose --------------------------------------===//
2*71db0c75SAndroid Build Coastguard Worker //
3*71db0c75SAndroid Build Coastguard Worker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*71db0c75SAndroid Build Coastguard Worker // See https://llvm.org/LICENSE.txt for license information.
5*71db0c75SAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*71db0c75SAndroid Build Coastguard Worker //
7*71db0c75SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
8*71db0c75SAndroid Build Coastguard Worker 
9*71db0c75SAndroid Build Coastguard Worker #include "src/__support/File/file.h"
10*71db0c75SAndroid Build Coastguard Worker #include "src/stdio/fclose.h"
11*71db0c75SAndroid Build Coastguard Worker #include "src/stdio/fopen.h"
12*71db0c75SAndroid Build Coastguard Worker #include "src/stdio/fwrite.h"
13*71db0c75SAndroid Build Coastguard Worker #include "src/stdio/fread.h"
14*71db0c75SAndroid Build Coastguard Worker 
15*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/Test.h"
16*71db0c75SAndroid Build Coastguard Worker 
TEST(LlvmLibcFOpenTest,PrintToFile)17*71db0c75SAndroid Build Coastguard Worker TEST(LlvmLibcFOpenTest, PrintToFile) {
18*71db0c75SAndroid Build Coastguard Worker   int result;
19*71db0c75SAndroid Build Coastguard Worker 
20*71db0c75SAndroid Build Coastguard Worker   FILE *file = LIBC_NAMESPACE::fopen("./testdata/test_data.txt", "w");
21*71db0c75SAndroid Build Coastguard Worker   ASSERT_FALSE(file == nullptr);
22*71db0c75SAndroid Build Coastguard Worker 
23*71db0c75SAndroid Build Coastguard Worker   static constexpr char STRING[] = "A simple string written to a file\n";
24*71db0c75SAndroid Build Coastguard Worker   result = LIBC_NAMESPACE::fwrite(STRING, 1, sizeof(STRING) - 1, file);
25*71db0c75SAndroid Build Coastguard Worker   EXPECT_GE(result, 0);
26*71db0c75SAndroid Build Coastguard Worker 
27*71db0c75SAndroid Build Coastguard Worker   ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
28*71db0c75SAndroid Build Coastguard Worker 
29*71db0c75SAndroid Build Coastguard Worker   FILE *new_file = LIBC_NAMESPACE::fopen("./testdata/test_data.txt", "r");
30*71db0c75SAndroid Build Coastguard Worker   ASSERT_FALSE(new_file == nullptr);
31*71db0c75SAndroid Build Coastguard Worker 
32*71db0c75SAndroid Build Coastguard Worker   static char data[64] = {0};
33*71db0c75SAndroid Build Coastguard Worker   ASSERT_EQ(LIBC_NAMESPACE::fread(data, 1, sizeof(STRING) - 1, new_file),
34*71db0c75SAndroid Build Coastguard Worker             sizeof(STRING) - 1);
35*71db0c75SAndroid Build Coastguard Worker   data[sizeof(STRING) - 1] = '\0';
36*71db0c75SAndroid Build Coastguard Worker   ASSERT_STREQ(data, STRING);
37*71db0c75SAndroid Build Coastguard Worker 
38*71db0c75SAndroid Build Coastguard Worker   ASSERT_EQ(0, LIBC_NAMESPACE::fclose(new_file));
39*71db0c75SAndroid Build Coastguard Worker }
40