1 | // (C) Copyright Jonathan Turkanis 2004 |
---|
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 | #include <fstream> |
---|
8 | #include <boost/config.hpp> |
---|
9 | #include <boost/detail/workaround.hpp> |
---|
10 | #include <boost/iostreams/device/mapped_file.hpp> |
---|
11 | #include <boost/iostreams/stream.hpp> |
---|
12 | #include <boost/test/test_tools.hpp> |
---|
13 | #include <boost/test/unit_test.hpp> |
---|
14 | #include "detail/temp_file.hpp" |
---|
15 | #include "detail/verification.hpp" |
---|
16 | |
---|
17 | using namespace std; |
---|
18 | using namespace boost; |
---|
19 | using namespace boost::iostreams; |
---|
20 | using namespace boost::iostreams::test; |
---|
21 | using boost::unit_test::test_suite; |
---|
22 | |
---|
23 | // Code generation bugs cause tests to fail with global optimization. |
---|
24 | #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) |
---|
25 | # pragma optimize("g", off) |
---|
26 | #endif |
---|
27 | |
---|
28 | void mapped_file_test() |
---|
29 | { |
---|
30 | BOOST_MESSAGE("about to begin"); |
---|
31 | |
---|
32 | //--------------Reading from a mapped_file_source-------------------------// |
---|
33 | |
---|
34 | { |
---|
35 | // Note: the ifstream second is placed in a nested scope because |
---|
36 | // closing and reopening a single ifstream failed for CW 9.4 on Windows. |
---|
37 | |
---|
38 | // Test reading from a stream based on a mapped_file_source, |
---|
39 | // in chars. |
---|
40 | test_file test1, test2; |
---|
41 | stream<mapped_file_source> first(test1.name()); |
---|
42 | { |
---|
43 | ifstream second( test2.name().c_str(), |
---|
44 | BOOST_IOS::in | BOOST_IOS::binary ); |
---|
45 | BOOST_CHECK_MESSAGE( |
---|
46 | compare_streams_in_chars(first, second), |
---|
47 | "failed reading from stream<mapped_file_source> in chars" |
---|
48 | ); |
---|
49 | |
---|
50 | BOOST_MESSAGE( |
---|
51 | "done reading from stream<mapped_file_source> in chars" |
---|
52 | ); |
---|
53 | } |
---|
54 | first.close(); |
---|
55 | |
---|
56 | // Test reading from a stream based on a mapped_file_source, |
---|
57 | // in chunks. (Also tests reopening the stream.) |
---|
58 | first.open(mapped_file_source(test1.name())); |
---|
59 | { |
---|
60 | ifstream second( test2.name().c_str(), |
---|
61 | BOOST_IOS::in | BOOST_IOS::binary ); |
---|
62 | BOOST_CHECK_MESSAGE( |
---|
63 | compare_streams_in_chunks(first, second), |
---|
64 | "failed reading from stream<mapped_file_source> in chunks" |
---|
65 | ); |
---|
66 | |
---|
67 | BOOST_MESSAGE( |
---|
68 | "done reading from stream<mapped_file_source> in chunks" |
---|
69 | ); |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | //--------------Writing to a mapped_file_sink-----------------------------// |
---|
74 | |
---|
75 | { |
---|
76 | // Test writing to a stream based on a mapped_file_sink, in |
---|
77 | // chars. |
---|
78 | uppercase_file first, second; // Will overwrite these. |
---|
79 | test_file test; |
---|
80 | |
---|
81 | stream<mapped_file_sink> out; |
---|
82 | out.open(mapped_file_sink(first.name())); |
---|
83 | write_data_in_chars(out); |
---|
84 | out.close(); |
---|
85 | BOOST_CHECK_MESSAGE( |
---|
86 | compare_files(first.name(), test.name()), |
---|
87 | "failed writing to stream<mapped_file_sink> in chars" |
---|
88 | ); |
---|
89 | |
---|
90 | BOOST_MESSAGE( |
---|
91 | "done writing to stream<mapped_file_source> in chars" |
---|
92 | ); |
---|
93 | |
---|
94 | // Test writing to a stream based on a mapped_file_sink, in |
---|
95 | // chunks. (Also tests reopening the stream.) |
---|
96 | out.open(mapped_file_sink(second.name())); |
---|
97 | write_data_in_chunks(out); |
---|
98 | out.close(); |
---|
99 | BOOST_CHECK_MESSAGE( |
---|
100 | compare_files(second.name(), test.name()), |
---|
101 | "failed writing to stream<mapped_file_sink> in chunks" |
---|
102 | ); |
---|
103 | |
---|
104 | BOOST_MESSAGE( |
---|
105 | "done writing to stream<mapped_file_source> in chunks" |
---|
106 | ); |
---|
107 | } |
---|
108 | |
---|
109 | //--------------Writing to a newly created file-----------------------------// |
---|
110 | |
---|
111 | { |
---|
112 | // Test writing to a newly created mapped file. |
---|
113 | temp_file first, second; |
---|
114 | test_file test; |
---|
115 | |
---|
116 | mapped_file_params p(first.name()); |
---|
117 | p.new_file_size = data_reps * data_length(); |
---|
118 | stream<mapped_file_sink> out; |
---|
119 | out.open(mapped_file_sink(p)); |
---|
120 | write_data_in_chars(out); |
---|
121 | out.close(); |
---|
122 | BOOST_CHECK_MESSAGE( |
---|
123 | compare_files(first.name(), test.name()), |
---|
124 | "failed writing to newly created mapped file in chars" |
---|
125 | ); |
---|
126 | |
---|
127 | |
---|
128 | // Test writing to a newly created mapped file. |
---|
129 | // (Also tests reopening the stream.) |
---|
130 | p.path = second.name(); |
---|
131 | out.open(mapped_file_sink(p)); |
---|
132 | write_data_in_chunks(out); |
---|
133 | out.close(); |
---|
134 | BOOST_CHECK_MESSAGE( |
---|
135 | compare_files(second.name(), test.name()), |
---|
136 | "failed writing to newly created mapped file in chunks" |
---|
137 | ); |
---|
138 | } |
---|
139 | |
---|
140 | //--------------Random access with a mapped_file--------------------------// |
---|
141 | |
---|
142 | { |
---|
143 | // Test reading, writing and seeking within a stream based on a |
---|
144 | // mapped_file, in chars. |
---|
145 | test_file test; |
---|
146 | stream<mapped_file> io; |
---|
147 | io.open(mapped_file(test.name())); |
---|
148 | BOOST_CHECK_MESSAGE( |
---|
149 | test_seekable_in_chars(io), |
---|
150 | "failed seeking within stream<mapped_file> in chars" |
---|
151 | ); |
---|
152 | |
---|
153 | BOOST_MESSAGE( |
---|
154 | "done seeking within stream<mapped_file> in chars" |
---|
155 | ); |
---|
156 | |
---|
157 | io.close(); |
---|
158 | |
---|
159 | // Test reading, writing and seeking within a stream based on a |
---|
160 | // mapped_file, in chunks. (Also tests reopening the |
---|
161 | // stream.) |
---|
162 | io.open(mapped_file(test.name())); |
---|
163 | BOOST_CHECK_MESSAGE( |
---|
164 | test_seekable_in_chunks(io), |
---|
165 | "failed seeking within stream<mapped_file> in chunks" |
---|
166 | ); |
---|
167 | |
---|
168 | BOOST_MESSAGE( |
---|
169 | "done seeking within stream<mapped_file> in chunks" |
---|
170 | ); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) |
---|
175 | # pragma optimize("", on) |
---|
176 | #endif |
---|
177 | |
---|
178 | test_suite* init_unit_test_suite(int, char* []) |
---|
179 | { |
---|
180 | test_suite* test = BOOST_TEST_SUITE("mapped_file test"); |
---|
181 | test->add(BOOST_TEST_CASE(&mapped_file_test)); |
---|
182 | return test; |
---|
183 | } |
---|