brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.7 KiB · 9fc3cb4 Raw
82 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <fstream>10 11// Test that we can seek using offsets larger than 32 bit, and that we can12// retrieve file offsets larger than 32 bit.13 14// On 32 bit Android platforms, off_t is 32 bit by default. By defining15// _FILE_OFFSET_BITS=64, one gets a 64 bit off_t, but the corresponding16// 64 bit ftello/fseeko functions are only available since Android API 24 (7.0).17// (On 64 bit Android platforms, off_t has always been 64 bit.)18//19// XFAIL: target={{i686|arm.*}}-{{.+}}-android{{.*}}20 21// Writing the >4 GB test file fails on 32 bit AIX.22//23// XFAIL: target=powerpc-{{.+}}-aix{{.*}}24 25// By default, off_t is typically a 32-bit integer on ARMv7 Linux systems,26// meaning it can represent file sizes up to 2GB (2^31 bytes) only.27//28// UNSUPPORTED: target=armv7-unknown-linux-gnueabihf29 30#include <fstream>31#include <iostream>32#include <cassert>33#include <vector>34 35#include "assert_macros.h"36#include "platform_support.h"37#include "test_macros.h"38 39void test_tellg(std::streamoff total_size) {40  std::vector<char> data(8192);41  for (std::size_t i = 0; i < data.size(); ++i)42    data[i] = static_cast<char>(i % (1 << 8 * sizeof(char)));43  std::string p = get_temp_file_name();44  {45    std::ofstream ofs;46    ofs.open(p, std::ios::out | std::ios::binary);47    assert(ofs.is_open());48    for (std::streamoff size = 0; size < total_size;) {49      std::streamoff n = std::min(static_cast<std::streamoff>(data.size()), total_size - size);50      ofs.write(data.data(), n);51      size += n;52    }53    assert(!ofs.fail());54    ofs.close();55  }56  {57    std::ifstream ifs;58    ifs.open(p, std::ios::binary);59    assert(ifs.is_open());60    std::streamoff in_off = ifs.tellg();61    TEST_REQUIRE(in_off == 0, "in_off not zero at start");62    ifs.seekg(total_size - 20, std::ios::beg);63    in_off = ifs.tellg();64    TEST_REQUIRE(in_off == total_size - 20, "in_off incorrect after >32 bit seek");65    ifs.seekg(10, std::ios::cur);66    in_off = ifs.tellg();67    TEST_REQUIRE(in_off == total_size - 10, "in_off incorrect after incremental seek");68    ifs.seekg(0, std::ios::end);69    in_off = ifs.tellg();70    TEST_REQUIRE(in_off == total_size, "in_off incorrect after seek to end");71  }72  std::remove(p.c_str());73}74 75int main(int, char**) {76  // This test assumes and requires that std::streamoff is larger than77  // 32 bit - this is not required in the standard itself.78  static_assert(sizeof(std::streamoff) > 4, "");79  test_tellg(0x100000042ULL);80  return 0;81}82