[libcxx-commits] [libcxx] [libcxx] [test] Add a test for the range of file offsets (PR #122798)

Martin Storsjö via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jan 14 00:33:04 PST 2025


================
@@ -0,0 +1,67 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <fstream>
+
+// XFAIL: target={{.*}}-windows{{.*}}
+
+#include <fstream>
+#include <iostream>
+#include <cassert>
+#include <vector>
+
+#include "assert_macros.h"
+#include "platform_support.h"
+#include "test_macros.h"
+
+void test_tellg(std::streamoff total_size) {
+  std::vector<char> data(8192);
+  for (std::size_t i = 0; i < data.size(); ++i)
+    data[i] = static_cast<char>(i % (1 << 8 * sizeof(char)));
+  std::string p = get_temp_file_name();
+  {
+    std::ofstream ofs;
+    ofs.open(p, std::ios::out | std::ios::binary);
+    assert(ofs.is_open());
+    for (std::streamoff size = 0; size < total_size;) {
+      std::size_t n = std::min(static_cast<std::streamoff>(data.size()), total_size - size);
+      ofs.write(data.data(), n);
+      size += n;
+    }
+    assert(!ofs.fail());
+    ofs.close();
+  }
+  {
+    std::ifstream ifs;
+    ifs.open(p, std::ios::binary);
+    assert(ifs.is_open());
+    std::streamoff in_off = ifs.tellg();
+    TEST_REQUIRE(in_off == 0, [&] { test_eprintf("in_off = %ld\n", in_off); });
+    ifs.seekg(total_size - 20, std::ios::beg);
+    in_off = ifs.tellg();
+    TEST_REQUIRE(in_off == total_size - 20, [&] {
+      test_eprintf("ref = %zu, in_off = %ld\n", total_size - 20, in_off);
+    });
+    ifs.seekg(10, std::ios::cur);
+    in_off = ifs.tellg();
+    TEST_REQUIRE(in_off == total_size - 10, [&] {
+      test_eprintf("ref = %zu, in_off = %ld\n", total_size - 10, in_off);
+    });
+    ifs.seekg(0, std::ios::end);
+    in_off = ifs.tellg();
+    TEST_REQUIRE(in_off == total_size, [&] { test_eprintf("ref = %zu, in_off = %ld\n", total_size, in_off); });
+  }
+  std::remove(p.c_str());
+}
+
+int main(int, char**) {
+  // TODO: What if std::streamoff is only 32 bit, which may be the case on
+  // some platforms?
----------------
mstorsjo wrote:

The `static_assert()` is making the tests fail in C++03 mode:
```
# .---command stderr------------
  # | /__w/llvm-project/llvm-project/libcxx/test/std/input.output/file.streams/fstreams/ifstream.members/offset_range.pass.cpp:68:3: error: 'static_assert' with no message is a C++17 extension [-Werror,-Wc++17-extensions]
  # |    68 |   static_assert(sizeof(std::streamoff) > 4);
  # |       |   ^
  # | /__w/llvm-project/llvm-project/build/generic-cxx03/libcxx/test-suite-install/include/c++/v1/__config:307:58: note: expanded from macro 'static_assert'
  # |   307 | #    define static_assert(...) _Static_assert(__VA_ARGS__)
  # |       |                                                          ^
  # | 1 error generated.
  # `-----------------------------
```

Any suggestion on which way to go about it?

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


More information about the libcxx-commits mailing list