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