]>
Commit | Line | Data |
---|---|---|
3a9019d9 FG |
1 | // Boost Sort library string_sort_test.cpp file ----------------------------//\r |
2 | \r | |
3 | // Copyright Steven Ross 2009. Use, modification and\r | |
4 | // distribution is subject to the Boost Software License, Version\r | |
5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at\r | |
6 | // http://www.boost.org/LICENSE_1_0.txt)\r | |
7 | \r | |
8 | // See http://www.boost.org/libs/sort for library home page.\r | |
9 | \r | |
10 | #include <boost/sort/spreadsort/detail/string_sort.hpp>\r | |
11 | #include <boost/sort/spreadsort/string_sort.hpp>\r | |
12 | #include <boost/sort/spreadsort/spreadsort.hpp>\r | |
13 | // Include unit test framework\r | |
14 | #include <boost/test/included/test_exec_monitor.hpp>\r | |
15 | #include <boost/test/test_tools.hpp>\r | |
16 | #include <vector>\r | |
17 | #include <string>\r | |
18 | \r | |
19 | \r | |
20 | using namespace std;\r | |
21 | using namespace boost::sort::spreadsort;\r | |
22 | using boost::sort::spreadsort::detail::offset_less_than;\r | |
23 | using boost::sort::spreadsort::detail::offset_greater_than;\r | |
24 | using boost::sort::spreadsort::detail::update_offset;\r | |
25 | \r | |
26 | struct bracket {\r | |
27 | unsigned char operator()(const string &x, size_t offset) const {\r | |
28 | return x[offset];\r | |
29 | }\r | |
30 | };\r | |
31 | \r | |
32 | struct wbracket {\r | |
33 | wchar_t operator()(const wstring &x, size_t offset) const {\r | |
34 | return x[offset];\r | |
35 | }\r | |
36 | };\r | |
37 | \r | |
38 | struct get_size {\r | |
39 | size_t operator()(const string &x) const{ return x.size(); }\r | |
40 | };\r | |
41 | \r | |
42 | struct wget_size {\r | |
43 | size_t operator()(const wstring &x) const{ return x.size(); }\r | |
44 | };\r | |
45 | \r | |
46 | static const unsigned input_count = 100000;\r | |
47 | \r | |
48 | // Test that update_offset finds the first character with a difference.\r | |
49 | void update_offset_test() {\r | |
50 | vector<string> input;\r | |
51 | input.push_back("test1");\r | |
52 | input.push_back("test2");\r | |
53 | size_t char_offset = 1;\r | |
54 | update_offset<vector<string>::iterator, unsigned char>(input.begin(),\r | |
55 | input.end(),\r | |
56 | char_offset);\r | |
57 | BOOST_CHECK(char_offset == 4);\r | |
58 | \r | |
59 | // Functor version\r | |
60 | char_offset = 1;\r | |
61 | update_offset(input.begin(), input.end(), char_offset, bracket(), get_size());\r | |
62 | BOOST_CHECK(char_offset == 4);\r | |
63 | }\r | |
64 | \r | |
65 | // Test that offset comparison operators only look after the offset.\r | |
66 | void offset_comparison_test() {\r | |
67 | string input1 = "ab";\r | |
68 | string input2 = "ba";\r | |
69 | string input3 = "aba";\r | |
70 | offset_less_than<string, unsigned char> less_than(0);\r | |
71 | offset_greater_than<string, unsigned char> greater_than(0);\r | |
72 | BOOST_CHECK(less_than(input1, input2));\r | |
73 | BOOST_CHECK(less_than(input1, input3));\r | |
74 | BOOST_CHECK(!less_than(input2, input1));\r | |
75 | BOOST_CHECK(!less_than(input1, input1));\r | |
76 | BOOST_CHECK(!greater_than(input1, input2));\r | |
77 | BOOST_CHECK(!greater_than(input1, input3));\r | |
78 | BOOST_CHECK(greater_than(input2, input1));\r | |
79 | BOOST_CHECK(!greater_than(input1, input1));\r | |
80 | \r | |
81 | // Offset comparisons only check after the specified offset.\r | |
82 | offset_less_than<string, unsigned char> offset_less(1);\r | |
83 | offset_greater_than<string, unsigned char> offset_greater(1);\r | |
84 | BOOST_CHECK(!offset_less(input1, input2));\r | |
85 | BOOST_CHECK(offset_less(input1, input3));\r | |
86 | BOOST_CHECK(offset_less(input2, input1));\r | |
87 | BOOST_CHECK(!offset_less(input1, input1));\r | |
88 | BOOST_CHECK(offset_greater(input1, input2));\r | |
89 | BOOST_CHECK(!offset_greater(input1, input3));\r | |
90 | BOOST_CHECK(!offset_greater(input2, input1));\r | |
91 | BOOST_CHECK(!offset_greater(input1, input1));\r | |
92 | }\r | |
93 | \r | |
94 | void string_test()\r | |
95 | {\r | |
96 | // Prepare inputs\r | |
97 | vector<string> base_vec;\r | |
98 | const unsigned max_length = 32;\r | |
99 | srand(1);\r | |
100 | //Generating semirandom numbers\r | |
101 | for (unsigned u = 0; u < input_count; ++u) {\r | |
102 | unsigned length = rand() % max_length;\r | |
103 | string result;\r | |
104 | for (unsigned v = 0; v < length; ++v) {\r | |
105 | result.push_back(rand() % 256);\r | |
106 | }\r | |
107 | base_vec.push_back(result);\r | |
108 | }\r | |
109 | vector<string> sorted_vec = base_vec;\r | |
110 | vector<string> test_vec = base_vec;\r | |
111 | std::sort(sorted_vec.begin(), sorted_vec.end());\r | |
112 | //Testing basic call\r | |
113 | string_sort(test_vec.begin(), test_vec.end());\r | |
114 | BOOST_CHECK(test_vec == sorted_vec);\r | |
115 | //Testing boost::sort::spreadsort wrapper\r | |
116 | test_vec = base_vec;\r | |
117 | boost::sort::spreadsort::spreadsort(test_vec.begin(), test_vec.end());\r | |
118 | BOOST_CHECK(test_vec == sorted_vec);\r | |
119 | //Character functors\r | |
120 | test_vec = base_vec;\r | |
121 | string_sort(test_vec.begin(), test_vec.end(), bracket(), get_size());\r | |
122 | BOOST_CHECK(test_vec == sorted_vec);\r | |
123 | //All functors\r | |
124 | test_vec = base_vec;\r | |
125 | string_sort(test_vec.begin(), test_vec.end(), bracket(), get_size(),\r | |
126 | less<string>());\r | |
127 | BOOST_CHECK(test_vec == sorted_vec);\r | |
128 | //reverse order\r | |
129 | std::sort(sorted_vec.begin(), sorted_vec.end(), greater<string>());\r | |
130 | reverse_string_sort(test_vec.begin(), test_vec.end(), greater<string>());\r | |
131 | BOOST_CHECK(test_vec == sorted_vec);\r | |
132 | //reverse order with functors\r | |
133 | test_vec = base_vec;\r | |
134 | reverse_string_sort(test_vec.begin(), test_vec.end(), bracket(), get_size(),\r | |
135 | greater<string>());\r | |
136 | BOOST_CHECK(test_vec == sorted_vec); \r | |
137 | }\r | |
138 | \r | |
139 | // Verify that 0, 1, and input_count empty strings all sort correctly.\r | |
140 | void corner_test() {\r | |
141 | vector<string> test_vec;\r | |
142 | boost::sort::spreadsort::spreadsort(test_vec.begin(), test_vec.end());\r | |
143 | test_vec.resize(1);\r | |
144 | boost::sort::spreadsort::spreadsort(test_vec.begin(), test_vec.end());\r | |
145 | BOOST_CHECK(test_vec[0].empty());\r | |
146 | test_vec.resize(input_count);\r | |
147 | boost::sort::spreadsort::spreadsort(test_vec.begin(), test_vec.end());\r | |
148 | BOOST_CHECK(test_vec.size() == input_count);\r | |
149 | for (unsigned i = 0; i < test_vec.size(); ++i) {\r | |
150 | BOOST_CHECK(test_vec[i].empty());\r | |
151 | }\r | |
152 | }\r | |
153 | \r | |
154 | // test main \r | |
155 | int test_main( int, char*[] )\r | |
156 | {\r | |
157 | update_offset_test();\r | |
158 | offset_comparison_test();\r | |
159 | string_test();\r | |
160 | corner_test();\r | |
161 | return 0;\r | |
162 | }\r |