File: filter_test.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (98 lines) | stat: -rw-r--r-- 3,580 bytes parent folder | download | duplicates (21)
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 <string>
#include <boost/iostreams/filter/test.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include "detail/filters.hpp"

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

const std::string lower = 
    "in addition to providing an abstract framework the "
    "library provides a number of concrete filters, sources "
    "and sinks which serve as example applications of the "
    "library but are also useful in their own right. these "
    "include components for accessing memory-mapped files, "
    "for file access via operating system file descriptors, "
    "for code conversion, for text filtering with regular "
    "expressions, for line-ending conversion and for "
    "compression and decompression in the zlib, gzip and "
    "bzip2 formats.";

const std::string upper = 
    "IN ADDITION TO PROVIDING AN ABSTRACT FRAMEWORK THE "
    "LIBRARY PROVIDES A NUMBER OF CONCRETE FILTERS, SOURCES "
    "AND SINKS WHICH SERVE AS EXAMPLE APPLICATIONS OF THE "
    "LIBRARY BUT ARE ALSO USEFUL IN THEIR OWN RIGHT. THESE "
    "INCLUDE COMPONENTS FOR ACCESSING MEMORY-MAPPED FILES, "
    "FOR FILE ACCESS VIA OPERATING SYSTEM FILE DESCRIPTORS, "
    "FOR CODE CONVERSION, FOR TEXT FILTERING WITH REGULAR "
    "EXPRESSIONS, FOR LINE-ENDING CONVERSION AND FOR "
    "COMPRESSION AND DECOMPRESSION IN THE ZLIB, GZIP AND "
    "BZIP2 FORMATS.";

struct toupper_dual_use_filter : public dual_use_filter {
    template<typename Source>
    int get(Source& s) 
    { 
        int c = boost::iostreams::get(s);
        return c != EOF && c != WOULD_BLOCK ?
            std::toupper((unsigned char) c) :
            c;
    }
    template<typename Sink>
    bool put(Sink& s, char c)
    { 
        return boost::iostreams::put(
                   s, (char) std::toupper((unsigned char) c)
               ); 
    }
};

struct tolower_dual_use_filter : public dual_use_filter {
    template<typename Source>
    int get(Source& s) 
    { 
        int c = boost::iostreams::get(s);
        return c != EOF && c != WOULD_BLOCK ?
            std::tolower((unsigned char) c) :
            c;
    }
    template<typename Sink>
    bool put(Sink& s, char c)
    { 
        return boost::iostreams::put(
                   s, (char) std::tolower((unsigned char) c)
               ); 
    }
};

void filter_test()
{
    BOOST_CHECK(test_input_filter(toupper_filter(), lower, upper));
    BOOST_CHECK(test_input_filter(toupper_multichar_filter(), lower, upper));
    BOOST_CHECK(test_input_filter(toupper_dual_use_filter(), lower, upper));
    BOOST_CHECK(test_output_filter(tolower_filter(), upper, lower));
    BOOST_CHECK(test_output_filter(tolower_multichar_filter(), upper, lower));
    BOOST_CHECK(test_output_filter(tolower_dual_use_filter(), upper, lower));
    BOOST_CHECK(test_filter_pair(tolower_filter(), toupper_filter(), upper));
    BOOST_CHECK(
        test_filter_pair( tolower_multichar_filter(), 
                          toupper_multichar_filter(), upper )
    );
}

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