Skip to content

Commit 5eb7e00

Browse files
committed
[libcxx] [test] Add a test for the range of file offsets
This adds a test for an issue reported downstream at mstorsjo/llvm-mingw#462; this is known to fail on Windows right now, where the fseek/ftell calls end up truncated to 32 bits.
1 parent 67efbd0 commit 5eb7e00

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===----------------------------------------------------------------------===//
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+
// <fstream>
10+
11+
// XFAIL: target={{.*}}-windows{{.*}}
12+
13+
#include <fstream>
14+
#include <iostream>
15+
#include <cassert>
16+
#include <vector>
17+
18+
#include "assert_macros.h"
19+
#include "platform_support.h"
20+
#include "test_macros.h"
21+
22+
void test_tellg(std::streamoff total_size) {
23+
std::vector<char> data(8192);
24+
for (std::size_t i = 0; i < data.size(); ++i)
25+
data[i] = static_cast<char>(i % (1 << 8 * sizeof(char)));
26+
std::string p = get_temp_file_name();
27+
{
28+
std::ofstream ofs;
29+
ofs.open(p, std::ios::out | std::ios::binary);
30+
assert(ofs.is_open());
31+
for (std::streamoff size = 0; size < total_size;) {
32+
std::size_t n = std::min(static_cast<std::streamoff>(data.size()), total_size - size);
33+
ofs.write(data.data(), n);
34+
size += n;
35+
}
36+
assert(!ofs.fail());
37+
ofs.close();
38+
}
39+
{
40+
std::ifstream ifs;
41+
ifs.open(p, std::ios::binary);
42+
assert(ifs.is_open());
43+
std::streamoff in_off = ifs.tellg();
44+
TEST_REQUIRE(in_off == 0, [&] { test_eprintf("in_off = %ld\n", in_off); });
45+
ifs.seekg(total_size - 20, std::ios::beg);
46+
in_off = ifs.tellg();
47+
TEST_REQUIRE(in_off == total_size - 20, [&] {
48+
test_eprintf("ref = %zu, in_off = %ld\n", total_size - 20, in_off);
49+
});
50+
ifs.seekg(10, std::ios::cur);
51+
in_off = ifs.tellg();
52+
TEST_REQUIRE(in_off == total_size - 10, [&] {
53+
test_eprintf("ref = %zu, in_off = %ld\n", total_size - 10, in_off);
54+
});
55+
ifs.seekg(0, std::ios::end);
56+
in_off = ifs.tellg();
57+
TEST_REQUIRE(in_off == total_size, [&] { test_eprintf("ref = %zu, in_off = %ld\n", total_size, in_off); });
58+
}
59+
std::remove(p.c_str());
60+
}
61+
62+
int main(int, char**) {
63+
// TODO: What if std::streamoff is only 32 bit, which may be the case on
64+
// some platforms?
65+
test_tellg(0x100000042ULL);
66+
return 0;
67+
}

0 commit comments

Comments
 (0)