Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / test / test / algorithms_test.cpp
1 //  (C) Copyright Gennadiy Rozental 2003-2008.
2 //  Distributed under the Boost Software License, Version 1.0.
3 //  (See accompanying file LICENSE_1_0.txt or copy at 
4 //  http://www.boost.org/LICENSE_1_0.txt)
5
6 //  See http://www.boost.org/libs/test for the library home page.
7 //
8 //  File        : $RCSfile$
9 //
10 //  Version     : $Revision$
11 //
12 //  Description : unit test for class properties facility
13 // ***************************************************************************
14
15 // Boost.Test
16 #include <boost/test/unit_test.hpp>
17 #include <boost/test/utils/class_properties.hpp>
18 #include <boost/test/utils/basic_cstring/basic_cstring.hpp>
19 #include <boost/test/utils/algorithm.hpp>
20 namespace utf = boost::unit_test;
21 using utf::const_string;
22
23 // STL
24 #include <cctype>
25 #include <functional>
26
27 # ifdef BOOST_NO_STDC_NAMESPACE
28 namespace std { using ::toupper; }
29 # endif
30
31 //____________________________________________________________________________//
32
33 bool predicate( char c1, char c2 ) { return (std::toupper)( c1 ) == (std::toupper)( c2 ); }
34
35 //____________________________________________________________________________//
36
37 void test_mismatch()
38 {
39     const_string cs1( "test_string" );
40     const_string cs2( "test_stream" );
41
42     BOOST_CHECK_EQUAL( utf::mismatch( cs1.begin(), cs1.end(), cs2.begin(), cs2.end() ).first - cs1.begin(), 8 );
43
44     cs2 = "trest";
45     BOOST_CHECK_EQUAL( utf::mismatch( cs1.begin(), cs1.end(), cs2.begin(), cs2.end() ).first - cs1.begin(), 1 );
46
47     cs2 = "test_string_klmn";
48     BOOST_CHECK_EQUAL( utf::mismatch( cs1.begin(), cs1.end(), cs2.begin(), cs2.end() ).first - cs1.begin(), 11 );
49
50     cs2 = "TeSt_liNk";
51     BOOST_CHECK_EQUAL( 
52         utf::mismatch( cs1.begin(), cs1.end(), cs2.begin(), cs2.end(), std::ptr_fun( predicate ) ).first - cs1.begin(),
53         5 );
54 }
55
56 //____________________________________________________________________________//
57
58 void test_find_first_not_of()
59 {
60     const_string cs( "test_string" );
61     const_string another( "tes" );
62
63     BOOST_CHECK_EQUAL( utf::find_first_not_of( cs.begin(), cs.end(), another.begin(), another.end() ) - cs.begin(), 4 );
64
65     another = "T_sE";
66     BOOST_CHECK_EQUAL( 
67         utf::find_first_not_of( cs.begin(), cs.end(), another.begin(), another.end(), std::ptr_fun( predicate ) ) - cs.begin(), 
68         7 );
69
70     another = "tes_ring";
71     BOOST_CHECK( utf::find_last_not_of( cs.begin(), cs.end(), another.begin(), another.end() ) == cs.end() );
72 }
73
74 //____________________________________________________________________________//
75
76 void test_find_last_of()
77 {
78     const_string cs( "test_string" );
79     const_string another( "tes" );
80
81     BOOST_CHECK_EQUAL( utf::find_last_of( cs.begin(), cs.end(), another.begin(), another.end() ) - cs.begin(), 6 );
82
83     another = "_Se";
84     BOOST_CHECK_EQUAL( 
85         utf::find_last_of( cs.begin(), cs.end(), another.begin(), another.end(), std::ptr_fun( predicate ) ) - cs.begin(),
86         5 );
87
88     another = "qw";
89     BOOST_CHECK( utf::find_last_of( cs.begin(), cs.end(), another.begin(), another.end() ) == cs.end() );
90
91     cs = "qerty";
92     BOOST_CHECK_EQUAL( utf::find_last_of( cs.begin(), cs.end(), another.begin(), another.end() ) - cs.begin(), 0 );
93 }
94
95 //____________________________________________________________________________//
96
97 void test_find_last_not_of()
98 {
99     const_string cs( "test_string" );
100     const_string another( "string" );
101
102     BOOST_CHECK_EQUAL( utf::find_last_not_of( cs.begin(), cs.end(), another.begin(), another.end() ) - cs.begin(), 4 );
103
104     another = "_SeG";
105     BOOST_CHECK_EQUAL( 
106         utf::find_last_not_of( cs.begin(), cs.end(), another.begin(), another.end(), std::ptr_fun( predicate ) ) - cs.begin(),
107         9 );
108
109     another = "e_string";
110     BOOST_CHECK( utf::find_last_not_of( cs.begin(), cs.end(), another.begin(), another.end() ) == cs.end() );
111 }
112
113 //____________________________________________________________________________//
114
115 utf::test_suite*
116 init_unit_test_suite( int /* argc */, char* /* argv */ [] )
117 {
118     utf::test_suite* test= BOOST_TEST_SUITE("Algorithms test");
119
120     test->add( BOOST_TEST_CASE( test_mismatch ) );
121     test->add( BOOST_TEST_CASE( test_find_first_not_of ) );
122     test->add( BOOST_TEST_CASE( test_find_last_of ) );
123     test->add( BOOST_TEST_CASE( test_find_last_not_of ) );
124
125     return test;
126 }
127
128 //____________________________________________________________________________//
129
130 // EOF