1 /*
2 Copyright (c) Marshall Clow 2012-2012.
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 For more information, see http://www.boost.org
8 */
9
10 #include <iostream>
11 #include <algorithm>
12 #include <string>
13
14 #include <boost/utility/string_ref.hpp>
15
16 #include <boost/core/lightweight_test.hpp>
17
18 typedef boost::string_ref string_ref;
19
20 // Should be equal
interop(const std::string & str,string_ref ref)21 void interop ( const std::string &str, string_ref ref ) {
22 // BOOST_TEST ( str == ref );
23 BOOST_TEST ( str.size () == ref.size ());
24 BOOST_TEST ( std::equal ( str.begin (), str.end (), ref.begin ()));
25 BOOST_TEST ( std::equal ( str.rbegin (), str.rend (), ref.rbegin ()));
26 }
27
null_tests(const char * p)28 void null_tests ( const char *p ) {
29 // All zero-length string-refs should be equal
30 string_ref sr1; // NULL, 0
31 string_ref sr2 ( NULL, 0 );
32 string_ref sr3 ( p, 0 );
33 string_ref sr4 ( p );
34 sr4.clear ();
35
36 BOOST_TEST ( sr1 == sr2 );
37 BOOST_TEST ( sr1 == sr3 );
38 BOOST_TEST ( sr2 == sr3 );
39 BOOST_TEST ( sr1 == sr4 );
40 }
41
42 // make sure that substrings work just like strings
test_substr(const std::string & str)43 void test_substr ( const std::string &str ) {
44 const size_t sz = str.size ();
45 string_ref ref ( str );
46
47 // Substrings at the end
48 for ( size_t i = 0; i <= sz; ++ i )
49 interop ( str.substr ( i ), ref.substr ( i ));
50
51 // Substrings at the beginning
52 for ( size_t i = 0; i <= sz; ++ i )
53 interop ( str.substr ( 0, i ), ref.substr ( 0, i ));
54
55 // All possible substrings
56 for ( size_t i = 0; i < sz; ++i )
57 for ( size_t j = i; j < sz; ++j )
58 interop ( str.substr ( i, j ), ref.substr ( i, j ));
59 }
60
61 // make sure that removing prefixes and suffixes work just like strings
test_remove(const std::string & str)62 void test_remove ( const std::string &str ) {
63 const size_t sz = str.size ();
64 std::string work;
65 string_ref ref;
66
67 for ( size_t i = 1; i <= sz; ++i ) {
68 work = str;
69 ref = str;
70 while ( ref.size () >= i ) {
71 interop ( work, ref );
72 work.erase ( 0, i );
73 ref.remove_prefix (i);
74 }
75 }
76
77 for ( size_t i = 1; i < sz; ++ i ) {
78 work = str;
79 ref = str;
80 while ( ref.size () >= i ) {
81 interop ( work, ref );
82 work.erase ( work.size () - i, i );
83 ref.remove_suffix (i);
84 }
85 }
86 }
87
88 const char *test_strings [] = {
89 "",
90 "1",
91 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
92 "0123456789",
93 NULL
94 };
95
main()96 int main()
97 {
98 const char **p = &test_strings[0];
99
100 while ( *p != NULL ) {
101 interop ( *p, *p );
102 test_substr ( *p );
103 test_remove ( *p );
104 null_tests ( *p );
105
106 p++;
107 }
108
109 return boost::report_errors();
110 }
111