• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_view.hpp>
15 #include <boost/container_hash/hash.hpp>
16 
17 #include <boost/core/lightweight_test.hpp>
18 
19 typedef boost::string_view string_view;
20 
21 //  Should be equal
interop(const std::string & str,string_view ref)22 void interop ( const std::string &str, string_view ref ) {
23 //  BOOST_TEST ( str == ref );
24     BOOST_TEST ( str.size () == ref.size ());
25     BOOST_TEST ( std::equal ( str.begin (),  str.end (),  ref.begin ()));
26     BOOST_TEST ( std::equal ( str.rbegin (), str.rend (), ref.rbegin ()));
27     }
28 
null_tests(const char * p)29 void null_tests ( const char *p ) {
30 //  All zero-length string-refs should be equal
31     string_view sr1; // NULL, 0
32     string_view sr2 ( NULL, 0 );
33     string_view sr3 ( p, 0 );
34     string_view sr4 ( p );
35     sr4.clear ();
36 
37     BOOST_TEST ( sr1 == sr2 );
38     BOOST_TEST ( sr1 == sr3 );
39     BOOST_TEST ( sr2 == sr3 );
40     BOOST_TEST ( sr1 == sr4 );
41     }
42 
43 //  make sure that substrings work just like strings
test_substr(const std::string & str)44 void test_substr ( const std::string &str ) {
45     const size_t sz = str.size ();
46     string_view ref ( str );
47 
48 //  Substrings at the end
49     for ( size_t i = 0; i <= sz; ++ i )
50         interop ( str.substr ( i ), ref.substr ( i ));
51 
52 //  Substrings at the beginning
53     for ( size_t i = 0; i <= sz; ++ i )
54         interop ( str.substr ( 0, i ), ref.substr ( 0, i ));
55 
56 //  All possible substrings
57     for ( size_t i = 0; i < sz; ++i )
58         for ( size_t j = i; j < sz; ++j )
59             interop ( str.substr ( i, j ), ref.substr ( i, j ));
60     }
61 
62 //  make sure that removing prefixes and suffixes work just like strings
test_remove(const std::string & str)63 void test_remove ( const std::string &str ) {
64     const size_t sz = str.size ();
65     std::string work;
66     string_view ref;
67 
68     for ( size_t i = 1; i <= sz; ++i ) {
69       work = str;
70       ref  = str;
71       while ( ref.size () >= i ) {
72           interop ( work, ref );
73           work.erase ( 0, i );
74           ref.remove_prefix (i);
75           }
76       }
77 
78     for ( size_t i = 1; i < sz; ++ i ) {
79       work = str;
80       ref  = str;
81       while ( ref.size () >= i ) {
82           interop ( work, ref );
83           work.erase ( work.size () - i, i );
84           ref.remove_suffix (i);
85           }
86       }
87     }
88 
test_hash(const std::string & str)89 void test_hash(const std::string& str) {
90     string_view ref = str;
91     BOOST_TEST(boost::hash_value(ref) == boost::hash_value(str));
92     boost::hash<std::string> hstr;
93     boost::hash<string_view> hsv;
94     BOOST_TEST(hsv(ref) == hstr(str));
95     }
96 
97 const char *test_strings [] = {
98     "",
99     "1",
100     "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
101     "0123456789",
102     NULL
103     };
104 
main()105 int main()
106 {
107     const char **p = &test_strings[0];
108 
109     while ( *p != NULL ) {
110         interop ( *p, *p );
111         test_substr ( *p );
112         test_remove ( *p );
113         null_tests ( *p );
114         test_hash( *p );
115 
116         p++;
117         }
118 
119     return boost::report_errors();
120 }
121