1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2 // (C) Copyright 2004-2007 Jonathan Turkanis
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
5
6 // See http://www.boost.org/libs/iostreams for documentation.
7
8 #include <algorithm> // equal.
9 #include <fstream>
10 #include <boost/iostreams/device/array.hpp>
11 #include <boost/iostreams/detail/adapter/direct_adapter.hpp>
12 #include <boost/iostreams/stream.hpp>
13 #include <boost/test/test_tools.hpp>
14 #include <boost/test/unit_test.hpp>
15 #include "detail/sequence.hpp"
16 #include "detail/temp_file.hpp"
17 #include "detail/verification.hpp"
18
19 using namespace std;
20 using namespace boost::iostreams;
21 using namespace boost::iostreams::test;
22 using boost::unit_test::test_suite;
23
direct_adapter_test()24 void direct_adapter_test()
25 {
26
27 typedef boost::iostreams::detail::direct_adapter<array_source>
28 indirect_array_source;
29 typedef boost::iostreams::detail::direct_adapter<array_sink>
30 indirect_array_sink;
31 typedef boost::iostreams::detail::direct_adapter<boost::iostreams::array>
32 indirect_array;
33 typedef stream<indirect_array_source>
34 indirect_array_istream;
35 typedef stream<indirect_array_sink>
36 indirect_array_ostream;
37 typedef stream<indirect_array>
38 indirect_array_stream;
39
40 test_file test;
41 test_sequence<> seq;
42
43 //--------------indirect_array_istream------------------------------------//
44
45 {
46 indirect_array_istream first(&seq[0], &seq[0] + seq.size());
47 ifstream second(test.name().c_str());
48 BOOST_CHECK_MESSAGE(
49 compare_streams_in_chars(first, second),
50 "failed reading from indirect_array_istream in chars"
51 );
52 }
53
54 {
55 indirect_array_istream first(&seq[0], &seq[0] + seq.size());
56 ifstream second(test.name().c_str());
57 BOOST_CHECK_MESSAGE(
58 compare_streams_in_chunks(first, second),
59 "failed reading from indirect_array_istream in chunks"
60 );
61 }
62
63 //--------------indirect_array_ostream------------------------------------//
64
65 {
66 vector<char> dest(data_reps * data_length(), '?');
67 indirect_array_ostream out(&dest[0], &dest[0] + dest.size());
68 write_data_in_chars(out);
69 BOOST_CHECK_MESSAGE(
70 std::equal(seq.begin(), seq.end(), dest.begin()),
71 "failed writing to indirect_array_ostream in chunks"
72 );
73 }
74
75 {
76 vector<char> dest(data_reps * data_length(), '?');
77 indirect_array_ostream out(&dest[0], &dest[0] + dest.size());
78 write_data_in_chunks(out);
79 BOOST_CHECK_MESSAGE(
80 std::equal(seq.begin(), seq.end(), dest.begin()),
81 "failed writing to indirect_array_ostream in chunks"
82 );
83 }
84
85 //--------------indirect_array_stream-------------------------------------//
86
87 {
88 vector<char> test(data_reps * data_length(), '?');
89 indirect_array_stream io(&test[0], &test[0] + test.size());
90 BOOST_CHECK_MESSAGE(
91 test_seekable_in_chars(io),
92 "failed seeking within indirect_array_stream, in chars"
93 );
94 }
95
96 {
97 vector<char> test(data_reps * data_length(), '?');
98 indirect_array_stream io(&test[0], &test[0] + test.size());
99 BOOST_CHECK_MESSAGE(
100 test_seekable_in_chunks(io),
101 "failed seeking within indirect_array_stream, in chunks"
102 );
103 }
104 }
105
init_unit_test_suite(int,char * [])106 test_suite* init_unit_test_suite(int, char* [])
107 {
108 test_suite* test = BOOST_TEST_SUITE("direct_adapter test");
109 test->add(BOOST_TEST_CASE(&direct_adapter_test));
110 return test;
111 }
112