[libc-commits] [libc] [libc] implement rewind (PR #191302)

Alexey Samsonov via libc-commits libc-commits at lists.llvm.org
Thu Apr 9 15:42:41 PDT 2026


================
@@ -0,0 +1,77 @@
+//===-- 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/ferror.h"
+#include "src/stdio/fopen.h"
+#include "src/stdio/fread.h"
+#include "src/stdio/fwrite.h"
+#include "src/stdio/rewind.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcRewindTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::EQ;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::NE;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::returns;
+
+TEST_F(LlvmLibcRewindTest, WriteRewindRead) {
+  constexpr char FILENAME[] = APPEND_LIBC_TEST("testdata/rewind.test");
+  auto FILEPATH = libc_make_test_file_path(FILENAME);
+  ::FILE *file = LIBC_NAMESPACE::fopen(FILEPATH, "w");
+  ASSERT_FALSE(file == nullptr);
+
+  constexpr char FIRST_DATA[] = "123456789";
+  ASSERT_THAT(LIBC_NAMESPACE::fwrite(FIRST_DATA, 1, sizeof(FIRST_DATA), file),
----------------
vonosmas wrote:

I think that for calls like this you can just write

`ASSERT_THAT(LIBC_NAMESPACE::fwrite(...), Succeeds(sizeof(FIRST_DATA)));`

as it will also validate that errno is 0. Same for the calls below

https://github.com/llvm/llvm-project/pull/191302


More information about the libc-commits mailing list