15 changes: 15 additions & 0 deletions libc/test/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ add_libc_unittest(
libc.src.stdio.fwrite
)

add_libc_unittest(
unlocked_fileop_test
SUITE
libc_stdio_unittests
SRCS
unlocked_fileop_test.cpp
DEPENDS
libc.src.stdio.fclose
libc.src.stdio.flockfile
libc.src.stdio.fopen
libc.src.stdio.fread_unlocked
libc.src.stdio.funlockfile
libc.src.stdio.fwrite_unlocked
)

add_subdirectory(printf_core)

add_subdirectory(testdata)
44 changes: 44 additions & 0 deletions libc/test/src/stdio/unlocked_fileop_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===-- Unittests for file operations like fopen, flcose etc --------------===//
//
// 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/stdio/fclose.h"
#include "src/stdio/flockfile.h"
#include "src/stdio/fopen.h"
#include "src/stdio/fread_unlocked.h"
#include "src/stdio/funlockfile.h"
#include "src/stdio/fwrite_unlocked.h"
#include "utils/UnitTest/Test.h"

#include <stdio.h>

TEST(LlvmLibcFILETest, UnlockedReadAndWrite) {
constexpr char FILENAME[] = "testdata/unlocked_read_and_write.test";
::FILE *file = __llvm_libc::fopen(FILENAME, "w");
ASSERT_FALSE(file == nullptr);
constexpr char CONTENT[] = "1234567890987654321";
__llvm_libc::flockfile(file);
ASSERT_EQ(sizeof(CONTENT) - 1, __llvm_libc::fwrite_unlocked(
CONTENT, 1, sizeof(CONTENT) - 1, file));
__llvm_libc::funlockfile(file);
ASSERT_EQ(0, __llvm_libc::fclose(file));

file = __llvm_libc::fopen(FILENAME, "r");
ASSERT_FALSE(file == nullptr);

constexpr size_t READ_SIZE = 5;
char data[READ_SIZE * 2 + 1];
data[READ_SIZE * 2] = '\0';
__llvm_libc::flockfile(file);
ASSERT_EQ(__llvm_libc::fread_unlocked(data, 1, READ_SIZE, file), READ_SIZE);
ASSERT_EQ(__llvm_libc::fread_unlocked(data + READ_SIZE, 1, READ_SIZE, file),
READ_SIZE);
__llvm_libc::funlockfile(file);
ASSERT_STREQ(data, "1234567890");

ASSERT_EQ(__llvm_libc::fclose(file), 0);
}