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 <cctype> |
---|
8 | #include <boost/iostreams/copy.hpp> |
---|
9 | #include <boost/iostreams/device/file.hpp> |
---|
10 | #include <boost/iostreams/device/null.hpp> |
---|
11 | #include <boost/iostreams/filter/counter.hpp> |
---|
12 | #include <boost/iostreams/filtering_stream.hpp> |
---|
13 | #include <boost/test/test_tools.hpp> |
---|
14 | #include <boost/test/unit_test.hpp> |
---|
15 | #include "detail/constants.hpp" |
---|
16 | #include "detail/filters.hpp" |
---|
17 | #include "detail/temp_file.hpp" |
---|
18 | #include "detail/verification.hpp" |
---|
19 | |
---|
20 | using namespace std; |
---|
21 | using namespace boost; |
---|
22 | using namespace boost::iostreams; |
---|
23 | using namespace boost::iostreams::test; |
---|
24 | using boost::unit_test::test_suite; |
---|
25 | |
---|
26 | void read_counter() |
---|
27 | { |
---|
28 | test_file src; |
---|
29 | filtering_istream in; |
---|
30 | in.push(counter()); |
---|
31 | in.push(padding_filter('a'), 0); |
---|
32 | in.push(counter()); |
---|
33 | in.push(file_source(src.name(), in_mode)); |
---|
34 | |
---|
35 | counter* first_counter = BOOST_IOSTREAMS_COMPONENT(in, 0, counter); |
---|
36 | counter* second_counter = BOOST_IOSTREAMS_COMPONENT(in, 2, counter); |
---|
37 | int first_count = 0; |
---|
38 | int second_count = 0; |
---|
39 | int line_count = 0; |
---|
40 | int reps = data_reps < 50 ? data_reps : 25; // Keep test short. |
---|
41 | for (int w = 0; w < reps; ++w) { |
---|
42 | int len = data_length(); |
---|
43 | for (int z = 0; z < len; ++z) { |
---|
44 | in.get(); |
---|
45 | ++first_count; |
---|
46 | ++second_count; |
---|
47 | BOOST_CHECK(first_counter->characters() == first_count); |
---|
48 | BOOST_CHECK(second_counter->characters() == second_count); |
---|
49 | in.get(); |
---|
50 | ++first_count; |
---|
51 | BOOST_CHECK(first_counter->characters() == first_count); |
---|
52 | BOOST_CHECK(second_counter->characters() == second_count); |
---|
53 | } |
---|
54 | ++line_count; |
---|
55 | BOOST_CHECK(first_counter->lines() == line_count); |
---|
56 | BOOST_CHECK(first_counter->lines() == line_count); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | void write_counter() |
---|
61 | { |
---|
62 | filtering_ostream out; |
---|
63 | out.push(counter()); |
---|
64 | out.push(padding_filter('a'), 0); |
---|
65 | out.push(counter()); |
---|
66 | out.push(null_sink()); |
---|
67 | |
---|
68 | counter* first_counter = BOOST_IOSTREAMS_COMPONENT(out, 0, counter); |
---|
69 | counter* second_counter = BOOST_IOSTREAMS_COMPONENT(out, 2, counter); |
---|
70 | int first_count = 0; |
---|
71 | int second_count = 0; |
---|
72 | int line_count = 0; |
---|
73 | int reps = data_reps < 50 ? data_reps : 25; // Keep test short. |
---|
74 | const char* data = narrow_data(); |
---|
75 | for (int w = 0; w < reps; ++w) { |
---|
76 | int len = data_length(); |
---|
77 | for (int z = 0; z < len; ++z) { |
---|
78 | out.put(data[z]); |
---|
79 | out.flush(); |
---|
80 | ++first_count; |
---|
81 | second_count += 2; |
---|
82 | BOOST_CHECK(first_counter->characters() == first_count); |
---|
83 | BOOST_CHECK(second_counter->characters() == second_count); |
---|
84 | } |
---|
85 | ++line_count; |
---|
86 | BOOST_CHECK(first_counter->lines() == line_count); |
---|
87 | BOOST_CHECK(first_counter->lines() == line_count); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | test_suite* init_unit_test_suite(int, char* []) |
---|
92 | { |
---|
93 | test_suite* test = BOOST_TEST_SUITE("counter test"); |
---|
94 | test->add(BOOST_TEST_CASE(&read_counter)); |
---|
95 | test->add(BOOST_TEST_CASE(&write_counter)); |
---|
96 | return test; |
---|
97 | } |
---|