1 //===-- Unittests for file operations like fopen, flcose etc --------------===// 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/clearerr.h" 10 #include "src/stdio/fclose.h" 11 #include "src/stdio/feof.h" 12 #include "src/stdio/ferror.h" 13 #include "src/stdio/fflush.h" 14 #include "src/stdio/fopen.h" 15 #include "src/stdio/fread.h" 16 #include "src/stdio/fseek.h" 17 #include "src/stdio/fwrite.h" 18 #include "utils/UnitTest/Test.h" 19 20 #include <errno.h> 21 #include <stdio.h> 22 23 TEST(LlvmLibcFILETest, SimpleFileOperations) { 24 constexpr char FILENAME[] = "testdata/simple_operations.test"; 25 ::FILE *file = __llvm_libc::fopen(FILENAME, "w"); 26 ASSERT_FALSE(file == nullptr); 27 constexpr char CONTENT[] = "1234567890987654321"; 28 ASSERT_EQ(sizeof(CONTENT) - 1, 29 __llvm_libc::fwrite(CONTENT, 1, sizeof(CONTENT) - 1, file)); 30 31 // This is not a readable file. 32 char read_data[sizeof(CONTENT)]; 33 ASSERT_EQ(__llvm_libc::fread(read_data, 1, sizeof(CONTENT), file), size_t(0)); 34 ASSERT_NE(__llvm_libc::ferror(file), 0); 35 EXPECT_NE(errno, 0); 36 errno = 0; 37 38 __llvm_libc::clearerr(file); 39 ASSERT_EQ(__llvm_libc::ferror(file), 0); 40 41 ASSERT_EQ(0, __llvm_libc::fclose(file)); 42 43 file = __llvm_libc::fopen(FILENAME, "r"); 44 ASSERT_FALSE(file == nullptr); 45 46 constexpr size_t READ_SIZE = 5; 47 char data[READ_SIZE]; 48 data[READ_SIZE - 1] = '\0'; 49 ASSERT_EQ(__llvm_libc::fread(data, 1, READ_SIZE - 1, file), READ_SIZE - 1); 50 ASSERT_STREQ(data, "1234"); 51 ASSERT_EQ(__llvm_libc::fseek(file, 5, SEEK_CUR), 0); 52 ASSERT_EQ(__llvm_libc::fread(data, 1, READ_SIZE - 1, file), READ_SIZE - 1); 53 ASSERT_STREQ(data, "0987"); 54 ASSERT_EQ(__llvm_libc::fseek(file, -5, SEEK_CUR), 0); 55 ASSERT_EQ(__llvm_libc::fread(data, 1, READ_SIZE - 1, file), READ_SIZE - 1); 56 ASSERT_STREQ(data, "9098"); 57 58 // Reading another time should trigger eof. 59 ASSERT_NE(sizeof(CONTENT), 60 __llvm_libc::fread(read_data, 1, sizeof(CONTENT), file)); 61 ASSERT_NE(__llvm_libc::feof(file), 0); 62 63 // Should be an error to write. 64 ASSERT_EQ(size_t(0), __llvm_libc::fwrite(CONTENT, 1, sizeof(CONTENT), file)); 65 ASSERT_NE(__llvm_libc::ferror(file), 0); 66 ASSERT_NE(errno, 0); 67 errno = 0; 68 69 __llvm_libc::clearerr(file); 70 ASSERT_EQ(__llvm_libc::ferror(file), 0); 71 72 ASSERT_EQ(__llvm_libc::fclose(file), 0); 73 } 74 75 TEST(LlvmLibcFILETest, FFlush) { 76 constexpr char FILENAME[] = "testdata/fflush.test"; 77 ::FILE *file = __llvm_libc::fopen(FILENAME, "w+"); 78 ASSERT_FALSE(file == nullptr); 79 constexpr char CONTENT[] = "1234567890987654321"; 80 ASSERT_EQ(sizeof(CONTENT), 81 __llvm_libc::fwrite(CONTENT, 1, sizeof(CONTENT), file)); 82 83 // Flushing at this point should write the data to disk. So, we should be 84 // able to read it back. 85 ASSERT_EQ(0, __llvm_libc::fflush(file)); 86 87 char data[sizeof(CONTENT)]; 88 ASSERT_EQ(__llvm_libc::fseek(file, 0, SEEK_SET), 0); 89 ASSERT_EQ(__llvm_libc::fread(data, 1, sizeof(CONTENT), file), 90 sizeof(CONTENT)); 91 ASSERT_STREQ(data, CONTENT); 92 93 ASSERT_EQ(__llvm_libc::fclose(file), 0); 94 } 95