File: offset_range.pass.cpp

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (81 lines) | stat: -rw-r--r-- 2,803 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//===----------------------------------------------------------------------===//
//
// 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>

// Test that we can seek using offsets larger than 32 bit, and that we can
// retrieve file offsets larger than 32 bit.

// On 32 bit Android platforms, off_t is 32 bit by default. By defining
// _FILE_OFFSET_BITS=64, one gets a 64 bit off_t, but the corresponding
// 64 bit ftello/fseeko functions are only available since Android API 24 (7.0).
// (On 64 bit Android platforms, off_t has always been 64 bit.)
//
// XFAIL: target={{i686|arm.*}}-{{.+}}-android{{.*}}

// Writing the >4 GB test file fails on 32 bit AIX.
//
// XFAIL: target=powerpc-{{.+}}-aix{{.*}}

// By default, off_t is typically a 32-bit integer on ARMv7 Linux systems,
// meaning it can represent file sizes up to 2GB (2^31 bytes) only.
//
// UNSUPPORTED: target=armv7-unknown-linux-gnueabihf

#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, "in_off not zero at start");
    ifs.seekg(total_size - 20, std::ios::beg);
    in_off = ifs.tellg();
    TEST_REQUIRE(in_off == total_size - 20, "in_off incorrect after >32 bit seek");
    ifs.seekg(10, std::ios::cur);
    in_off = ifs.tellg();
    TEST_REQUIRE(in_off == total_size - 10, "in_off incorrect after incremental seek");
    ifs.seekg(0, std::ios::end);
    in_off = ifs.tellg();
    TEST_REQUIRE(in_off == total_size, "in_off incorrect after seek to end");
  }
  std::remove(p.c_str());
}

int main(int, char**) {
  // This test assumes and requires that std::streamoff is larger than
  // 32 bit - this is not required in the standard itself.
  static_assert(sizeof(std::streamoff) > 4, "");
  test_tellg(0x100000042ULL);
  return 0;
}