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