• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // (C) Copyright 2014 Jorge Lodos
2 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
3 // (C) Copyright 2004-2007 Jonathan Turkanis
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
6 
7 // See http://www.boost.org/libs/iostreams for documentation.
8 
9 #include <sstream>
10 #include <boost/iostreams/device/file.hpp>
11 #include <boost/iostreams/filtering_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 
verification_function_seekable_test()23 void verification_function_seekable_test()
24 {
25     {
26         temp_file f;
27         fstream io(f.name().c_str(), BOOST_IOS::in | BOOST_IOS::out | BOOST_IOS::binary | BOOST_IOS::trunc);
28         BOOST_CHECK_MESSAGE(
29             test_seekable_in_chars(io),
30             "failed using test_seekable_in_chars"
31         );
32         io.close();
33     }
34 
35     {
36         temp_file f;
37         fstream io(f.name().c_str(), BOOST_IOS::in | BOOST_IOS::out | BOOST_IOS::binary | BOOST_IOS::trunc);
38         BOOST_CHECK_MESSAGE(
39             test_seekable_in_chunks(io),
40             "failed using test_seekable_in_chunks"
41         );
42         io.close();
43     }
44 
45     {
46         temp_file f;
47         fstream io(f.name().c_str(), BOOST_IOS::in | BOOST_IOS::out | BOOST_IOS::binary | BOOST_IOS::trunc);
48         for (int i = 0; i < data_reps; ++i)
49             io.write(narrow_data(), chunk_size);
50         io.seekg(0, BOOST_IOS::beg);
51         BOOST_CHECK_MESSAGE(
52             test_input_seekable(io),
53             "failed using test_input_seekable"
54         );
55         io.close();
56     }
57 
58     {
59         temp_file f;
60         fstream io(f.name().c_str(), BOOST_IOS::in | BOOST_IOS::out | BOOST_IOS::binary | BOOST_IOS::trunc);
61         BOOST_CHECK_MESSAGE(
62             test_output_seekable(io),
63             "failed using test_output_seekable"
64         );
65         io.close();
66     }
67 }
68 
verification_function_dual_seekable_test()69 void verification_function_dual_seekable_test()
70 {
71    {
72        stringstream ss(BOOST_IOS::in | BOOST_IOS::out);
73        BOOST_CHECK_MESSAGE(
74             test_seekable_in_chars(ss),
75             "failed using test_seekable_in_chars"
76        );
77    }
78 
79    {
80        stringstream ss(BOOST_IOS::in | BOOST_IOS::out);
81        BOOST_CHECK_MESSAGE(
82             test_seekable_in_chunks(ss),
83             "failed using test_seekable_in_chunks"
84        );
85    }
86 
87    {
88        string s;
89        for (int i = 0; i < data_reps; ++i)
90             s.append(narrow_data(), chunk_size);
91        stringstream ss(s, BOOST_IOS::in | BOOST_IOS::out);
92        BOOST_CHECK_MESSAGE(
93             test_input_seekable(ss),
94             "failed using test_input_seekable"
95        );
96    }
97 
98    {
99        stringstream ss(BOOST_IOS::in | BOOST_IOS::out);
100        BOOST_CHECK_MESSAGE(
101             test_output_seekable(ss),
102             "failed using test_output_seekable"
103        );
104    }
105 
106    {
107        stringstream ss(BOOST_IOS::in | BOOST_IOS::out);
108        BOOST_CHECK_MESSAGE(
109             test_dual_seekable(ss),
110             "failed using test_dual_seekable"
111        );
112    }
113 }
114 
dual_seekable_test()115 void dual_seekable_test()
116 {
117    {
118        stringstream ss(BOOST_IOS::in | BOOST_IOS::out);
119        filtering_stream<dual_seekable> io(ss);
120        io.exceptions(BOOST_IOS::failbit | BOOST_IOS::badbit);
121        BOOST_CHECK_MESSAGE(
122             test_seekable_in_chars(io),
123             "failed seeking within a string, in chars"
124        );
125    }
126 
127    {
128        stringstream ss(BOOST_IOS::in | BOOST_IOS::out);
129        filtering_stream<dual_seekable> io(ss);
130        io.exceptions(BOOST_IOS::failbit | BOOST_IOS::badbit);
131        BOOST_CHECK_MESSAGE(
132             test_seekable_in_chunks(io),
133             "failed seeking within a string, in chunks"
134        );
135    }
136 
137    {
138        string s;
139        for (int i = 0; i < data_reps; ++i)
140             s.append(narrow_data(), chunk_size);
141        stringstream ss(s, BOOST_IOS::in | BOOST_IOS::out);
142        filtering_stream<dual_seekable> io(ss);
143        io.exceptions(BOOST_IOS::failbit | BOOST_IOS::badbit);
144        BOOST_CHECK_MESSAGE(
145             test_input_seekable(io),
146             "failed seeking within a string source"
147        );
148    }
149 
150    {
151        stringstream ss(BOOST_IOS::in | BOOST_IOS::out);
152        filtering_stream<dual_seekable> io(ss);
153        io.exceptions(BOOST_IOS::failbit | BOOST_IOS::badbit);
154        BOOST_CHECK_MESSAGE(
155             test_output_seekable(io),
156             "failed seeking within a string sink"
157        );
158    }
159 
160    {
161        stringstream ss(BOOST_IOS::in | BOOST_IOS::out);
162        filtering_stream<dual_seekable> io(ss);
163        io.exceptions(BOOST_IOS::failbit | BOOST_IOS::badbit);
164        BOOST_CHECK_MESSAGE(
165             test_dual_seekable(io),
166             "failed dual seeking within a string"
167        );
168    }
169 }
170 
init_unit_test_suite(int,char * [])171 test_suite* init_unit_test_suite(int, char* [])
172 {
173    test_suite* test = BOOST_TEST_SUITE("dual seekable test");
174    test->add(BOOST_TEST_CASE(&verification_function_seekable_test));
175    test->add(BOOST_TEST_CASE(&verification_function_dual_seekable_test));
176    test->add(BOOST_TEST_CASE(&dual_seekable_test));
177    return test;
178 }
179