summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/iostreams/test/counter_test.cpp
blob: 091dfe9f68b27b120342afa09685ea0a8f2fd579 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
// (C) Copyright 2004-2007 Jonathan Turkanis
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)

// See http://www.boost.org/libs/iostreams for documentation.

#include <cctype>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/device/null.hpp>
#include <boost/iostreams/filter/counter.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include "detail/constants.hpp"
#include "detail/filters.hpp"
#include "detail/temp_file.hpp"
#include "detail/verification.hpp"

using namespace std;
using namespace boost;
using namespace boost::iostreams;
using namespace boost::iostreams::test;
using boost::unit_test::test_suite;    

void read_counter()
{
    test_file          src;
    filtering_istream  in;
    in.push(counter());
    in.push(padding_filter('a'), 0);
    in.push(counter());
    in.push(file_source(src.name(), in_mode));

    counter*  first_counter = BOOST_IOSTREAMS_COMPONENT(in, 0, counter);
    counter*  second_counter = BOOST_IOSTREAMS_COMPONENT(in, 2, counter);
    int       first_count = 0;
    int       second_count = 0;
    int       line_count = 0;
    int       reps = data_reps < 50 ? data_reps : 25; // Keep test short.
    for (int w = 0; w < reps; ++w) {
        int len = data_length();
        for (int z = 0; z < len; ++z) {
            in.get();
            ++first_count;
            ++second_count;
            BOOST_CHECK(first_counter->characters() == first_count);
            BOOST_CHECK(second_counter->characters() == second_count);
            in.get();
            ++first_count;
            BOOST_CHECK(first_counter->characters() == first_count);
            BOOST_CHECK(second_counter->characters() == second_count);
        }
        ++line_count;
        BOOST_CHECK(first_counter->lines() == line_count);
        BOOST_CHECK(first_counter->lines() == line_count);
    }
}

void write_counter() 
{
    filtering_ostream  out;
    out.push(counter());
    out.push(padding_filter('a'), 0);
    out.push(counter());
    out.push(null_sink());

    counter*  first_counter = BOOST_IOSTREAMS_COMPONENT(out, 0, counter);
    counter*  second_counter = BOOST_IOSTREAMS_COMPONENT(out, 2, counter);
    int       first_count = 0;
    int       second_count = 0;
    int       line_count = 0;
    int       reps = data_reps < 50 ? data_reps : 25; // Keep test short.
    const char* data = narrow_data();
    for (int w = 0; w < reps; ++w) {
        int len = data_length();
        for (int z = 0; z < len; ++z) {
            out.put(data[z]);
            out.flush();
            ++first_count;
            second_count += 2;
            BOOST_CHECK(first_counter->characters() == first_count);
            BOOST_CHECK(second_counter->characters() == second_count);
        }
        ++line_count;
        BOOST_CHECK(first_counter->lines() == line_count);
        BOOST_CHECK(first_counter->lines() == line_count);
    }
}

test_suite* init_unit_test_suite(int, char* []) 
{
    test_suite* test = BOOST_TEST_SUITE("counter test");
    test->add(BOOST_TEST_CASE(&read_counter));
    test->add(BOOST_TEST_CASE(&write_counter));
    return test;
}