stream_offset_64bit_test.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.(See accompanying
  3. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  4. *
  5. * See http://www.boost.org/libs/iostreams for documentation.
  6. * File: libs/iostreams/test/stream_offset_64bit_test.cpp
  7. * Date: Sun Dec 23 21:11:23 MST 2007
  8. * Copyright: 2007-2008 CodeRage, LLC
  9. * Author: Jonathan Turkanis
  10. * Contact: turkanis at coderage dot com
  11. *
  12. * Tests the functions defined in the header "boost/iostreams/positioning.hpp"
  13. * with large (64-bit) file offsets.
  14. */
  15. #include <cstdio> // fpos_t
  16. #include <iostream>
  17. #include <sstream>
  18. #include <boost/config.hpp> // BOOST_MSVC
  19. #include <boost/iostreams/detail/ios.hpp>
  20. #include <boost/iostreams/positioning.hpp>
  21. #include <boost/test/test_tools.hpp>
  22. #include <boost/test/unit_test.hpp>
  23. using namespace std;
  24. using namespace boost;
  25. using namespace boost::iostreams;
  26. using boost::unit_test::test_suite;
  27. #ifdef BOOST_MSVC
  28. # pragma warning(disable:4127)
  29. #endif
  30. void stream_offset_64bit_test()
  31. {
  32. stream_offset large_file = (stream_offset) 100 *
  33. (stream_offset) 1024 *
  34. (stream_offset) 1024 *
  35. (stream_offset) 1024;
  36. stream_offset first = -large_file - (-large_file) % 10000000;
  37. stream_offset last = large_file - large_file % 10000000;
  38. for (stream_offset off = first; off < last; off += 10000000)
  39. {
  40. if (off != position_to_offset(offset_to_position(off))) {
  41. cout << "****************************************\n"
  42. << "* sizeof(fpos_t) = " << sizeof(fpos_t) << "\n"
  43. << "* sizeof(streamoff) = " << sizeof(streamoff) << "\n"
  44. << "* sizeof(stream_offset) = "
  45. << sizeof(stream_offset) << "\n"
  46. << "****************************************\n";
  47. stringstream s;
  48. s << "off != position_to_offset(offset_to_position(off)) "
  49. "failed for (off >> 32) == 0x"
  50. << hex
  51. << static_cast<unsigned int>(off >> 32)
  52. << " and (off & 0xFFFFFFFF) == 0x"
  53. << static_cast<unsigned int>(off & 0xFFFFFFFF)
  54. << std::endl;
  55. BOOST_REQUIRE_MESSAGE(0, s.str().c_str());
  56. }
  57. }
  58. }
  59. void stream_offset_64bit_test2()
  60. {
  61. boost::int64_t val = boost::int64_t(1) << 31;
  62. std::streampos pos = boost::iostreams::offset_to_position(val);
  63. pos -= 2;
  64. BOOST_CHECK_EQUAL(val - 2, boost::iostreams::position_to_offset(pos));
  65. val = -val;
  66. pos = boost::iostreams::offset_to_position(val);
  67. pos += 2;
  68. BOOST_CHECK_EQUAL(val + 2, boost::iostreams::position_to_offset(pos));
  69. }
  70. test_suite* init_unit_test_suite(int, char* [])
  71. {
  72. test_suite* test = BOOST_TEST_SUITE("stream_offset 64-bit test");
  73. test->add(BOOST_TEST_CASE(&stream_offset_64bit_test));
  74. test->add(BOOST_TEST_CASE(&stream_offset_64bit_test2));
  75. return test;
  76. }