1// Boost string_algo library predicate_test.cpp file ------------------//
2
3// Copyright Pavol Droba 2002-2003. Use, modification and
4// distribution is subject to the Boost Software License, Version
5// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6// http://www.boost.org/LICENSE_1_0.txt)
7
8// See http://www.boost.org for updates, documentation, and revision history.
9
10#include <boost/algorithm/string/predicate.hpp>
11#include <boost/algorithm/string/classification.hpp>
12
13// Include unit test framework
14#define BOOST_TEST_MAIN
15#include <boost/test/unit_test.hpp>
16
17#include <string>
18#include <vector>
19#include <iostream>
20#include <functional>
21#include <boost/test/test_tools.hpp>
22
23using namespace std;
24using namespace boost;
25
26void predicate_test()
27{
28 string str1("123xxx321");
29 string str1_prefix("123");
30 string str2("abc");
31 string str3("");
32 string str4("abc");
33 vector<int> vec1( str1.begin(), str1.end() );
34
35 // Basic tests
36 BOOST_CHECK( starts_with( str1, string("123") ) );
37 BOOST_CHECK( !starts_with( str1, string("1234") ) );
38
39 BOOST_CHECK( istarts_with( "aBCxxx", "abc" ) );
40 BOOST_CHECK( !istarts_with( "aBCxxx", "abcd" ) );
41
42 BOOST_CHECK( ends_with( str1, string("321") ) );
43 BOOST_CHECK( !ends_with( str1, string("123") ) );
44
45 BOOST_CHECK( iends_with( "aBCxXx", "XXX" ) );
46 BOOST_CHECK( !iends_with( "aBCxxX", "xXXX" ) );
47
48 BOOST_CHECK( contains( str1, string("xxx") ) );
49 BOOST_CHECK( !contains( str1, string("yyy") ) );
50
51 BOOST_CHECK( icontains( "123XxX321", "xxx" ) );
52 BOOST_CHECK( !icontains( "123xXx321", "yyy" ) );
53
54 BOOST_CHECK( equals( str2, string("abc") ) );
55 BOOST_CHECK( !equals( str1, string("yyy") ) );
56
57 BOOST_CHECK( iequals( "AbC", "abc" ) );
58 BOOST_CHECK( !iequals( "aBc", "yyy" ) );
59
60 BOOST_CHECK( lexicographical_compare("abc", "abd") );
61 BOOST_CHECK( !lexicographical_compare("abc", "abc") );
62 BOOST_CHECK( lexicographical_compare("abc", "abd", is_less()) );
63
64 BOOST_CHECK( !ilexicographical_compare("aBD", "AbC") );
65 BOOST_CHECK( ilexicographical_compare("aBc", "AbD") );
66 BOOST_CHECK( lexicographical_compare("abC", "aBd", is_iless()) );
67
68 // multi-type comparison test
69 BOOST_CHECK( starts_with( vec1, string("123") ) );
70 BOOST_CHECK( ends_with( vec1, string("321") ) );
71 BOOST_CHECK( contains( vec1, string("xxx") ) );
72 BOOST_CHECK( equals( vec1, str1 ) );
73
74 // overflow test
75 BOOST_CHECK( !starts_with( str2, string("abcd") ) );
76 BOOST_CHECK( !ends_with( str2, string("abcd") ) );
77 BOOST_CHECK( !contains( str2, string("abcd") ) );
78 BOOST_CHECK( !equals( str2, string("abcd") ) );
79
80 // equal test
81 BOOST_CHECK( starts_with( str2, string("abc") ) );
82 BOOST_CHECK( ends_with( str2, string("abc") ) );
83 BOOST_CHECK( contains( str2, string("abc") ) );
84 BOOST_CHECK( equals( str2, string("abc") ) );
85
86 //! Empty string test
87 BOOST_CHECK( starts_with( str2, string("") ) );
88 BOOST_CHECK( ends_with( str2, string("") ) );
89 BOOST_CHECK( contains( str2, string("") ) );
90 BOOST_CHECK( equals( str3, string("") ) );
91
92 //! Container compatibility test
93 BOOST_CHECK( starts_with( "123xxx321", "123" ) );
94 BOOST_CHECK( ends_with( "123xxx321", "321" ) );
95 BOOST_CHECK( contains( "123xxx321", "xxx" ) );
96 BOOST_CHECK( equals( "123xxx321", "123xxx321" ) );
97
98}
99
100template<typename Pred, typename Input>
101void test_pred(const Pred& pred, const Input& input, bool bYes)
102{
103 // test assignment operator
104 Pred pred1=pred;
105 pred1=pred;
106 pred1=pred1;
107 if(bYes)
108 {
109 BOOST_CHECK( all( input, pred ) );
110 BOOST_CHECK( all( input, pred1 ) );
111 }
112 else
113 {
114 BOOST_CHECK( !all( input, pred ) );
115 BOOST_CHECK( !all( input, pred1 ) );
116 }
117}
118
119#define TEST_CLASS( Pred, YesInput, NoInput )\
120{\
121 test_pred(Pred, YesInput, true); \
122 test_pred(Pred, NoInput, false); \
123}
124
125void classification_test()
126{
127 TEST_CLASS( is_space(), "\n\r\t ", "..." );
128 TEST_CLASS( is_alnum(), "ab129ABc", "_ab129ABc" );
129 TEST_CLASS( is_alpha(), "abc", "abc1" );
130 TEST_CLASS( is_cntrl(), "\n\t\r", "..." );
131 TEST_CLASS( is_digit(), "1234567890", "abc" );
132 TEST_CLASS( is_graph(), "123abc.,", " \t" );
133 TEST_CLASS( is_lower(), "abc", "Aasdf" );
134 TEST_CLASS( is_print(), "abs", "\003\004asdf" );
135 TEST_CLASS( is_punct(), ".,;\"", "abc" );
136 TEST_CLASS( is_upper(), "ABC", "aBc" );
137 TEST_CLASS( is_xdigit(), "ABC123", "XFD" );
138 TEST_CLASS( is_any_of( string("abc") ), "aaabbcc", "aaxb" );
139 TEST_CLASS( is_any_of( "abc" ), "aaabbcc", "aaxb" );
140 TEST_CLASS( is_from_range( 'a', 'c' ), "aaabbcc", "aaxb" );
141
142 TEST_CLASS( !is_classified(std::ctype_base::space), "...", "..\n\r\t " );
143 TEST_CLASS( ( !is_any_of("abc") && is_from_range('a','e') ) || is_space(), "d e", "abcde" );
144
145 // is_any_of test
146// TEST_CLASS( !is_any_of(""), "", "aaa" )
147 TEST_CLASS( is_any_of("a"), "a", "ab" )
148 TEST_CLASS( is_any_of("ba"), "ab", "abc" )
149 TEST_CLASS( is_any_of("cba"), "abc", "abcd" )
150 TEST_CLASS( is_any_of("hgfedcba"), "abcdefgh", "abcdefghi" )
151 TEST_CLASS( is_any_of("qponmlkjihgfedcba"), "abcdefghijklmnopq", "zzz" )
152}
153
154#undef TEST_CLASS
155
156BOOST_AUTO_TEST_CASE( test_main )
157{
158 predicate_test();
159 classification_test();
160}
161

source code of boost/libs/algorithm/string/test/predicate_test.cpp