]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // (C) Copyright Gennadiy Rozental 2015. |
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 | |
9 | /// @brief tests collection comparison implementation | |
10 | // *************************************************************************** | |
11 | ||
12 | // Boost.Test | |
13 | #define BOOST_TEST_MODULE Test collection`s comparisons | |
14 | #include <boost/test/unit_test.hpp> | |
15 | namespace tt = boost::test_tools; | |
16 | namespace ut = boost::unit_test; | |
17 | ||
18 | BOOST_TEST_SPECIALIZED_COLLECTION_COMPARE(std::vector<int>) | |
19 | ||
20 | #define VALIDATE_OP( op ) \ | |
21 | { \ | |
22 | BOOST_TEST_INFO( "validating operator " #op ); \ | |
23 | bool expected = (c1 op c2); \ | |
24 | auto const& res = (tt::assertion::seed()->* c1 op c2).evaluate(); \ | |
25 | BOOST_TEST( expected == !!res ); \ | |
26 | } \ | |
27 | /**/ | |
28 | ||
29 | template<typename Col> | |
30 | void | |
31 | validate_comparisons(Col const& c1, Col const& c2 ) | |
32 | { | |
33 | VALIDATE_OP( == ) | |
34 | VALIDATE_OP( != ) | |
35 | VALIDATE_OP( < ) | |
36 | VALIDATE_OP( > ) | |
37 | VALIDATE_OP( <= ) | |
38 | VALIDATE_OP( >= ) | |
39 | } | |
40 | ||
41 | BOOST_AUTO_TEST_CASE( test_against_overloaded_comp_op ) | |
42 | { | |
43 | std::vector<int> a{1, 2, 3}; | |
44 | std::vector<int> b{1, 3, 2}; | |
45 | std::vector<int> c{1, 2, 3}; | |
46 | std::vector<int> d{1, 2, 3, 4}; | |
47 | ||
48 | BOOST_TEST_CONTEXT( "validating comparisons of a and b" ) | |
49 | validate_comparisons(a, b); | |
50 | BOOST_TEST_CONTEXT( "validating comparisons of a and c" ) | |
51 | validate_comparisons(a, c); | |
52 | BOOST_TEST_CONTEXT( "validating comparisons of a and d" ) | |
53 | validate_comparisons(a, d); | |
54 | } | |
55 | ||
56 | //____________________________________________________________________________// | |
57 | ||
58 | BOOST_AUTO_TEST_CASE( test_per_element_eq, * ut::expected_failures(2) ) | |
59 | { | |
60 | std::vector<int> a{1, 2, 3}; | |
61 | std::vector<int> b{1, 3, 2}; | |
62 | ||
63 | BOOST_TEST( a == b, tt::per_element() ); | |
64 | } | |
65 | ||
66 | //____________________________________________________________________________// | |
67 | ||
68 | BOOST_AUTO_TEST_CASE( test_per_element_ne, * ut::expected_failures(1) ) | |
69 | { | |
70 | std::vector<int> a{1, 2, 3}; | |
71 | std::vector<int> b{1, 3, 2}; | |
72 | ||
73 | BOOST_TEST( a != b, tt::per_element() ); | |
74 | } | |
75 | ||
76 | //____________________________________________________________________________// | |
77 | ||
78 | BOOST_AUTO_TEST_CASE( test_per_element_lt, * ut::expected_failures(2) ) | |
79 | { | |
80 | std::vector<int> a{1, 2, 3}; | |
81 | std::vector<int> b{1, 3, 2}; | |
82 | ||
83 | BOOST_TEST( a < b, tt::per_element() ); | |
84 | } | |
85 | ||
86 | //____________________________________________________________________________// | |
87 | ||
88 | BOOST_AUTO_TEST_CASE( test_per_element_ge, * ut::expected_failures(1) ) | |
89 | { | |
90 | std::vector<int> a{1, 2, 3}; | |
91 | std::vector<int> b{1, 3, 2}; | |
92 | ||
93 | BOOST_TEST( b >= a, tt::per_element() ); | |
94 | } | |
95 | ||
96 | //____________________________________________________________________________// | |
97 | ||
98 | BOOST_AUTO_TEST_CASE( test_lexicographic_lt ) | |
99 | { | |
100 | std::vector<int> a{1, 2, 3}; | |
101 | std::vector<int> b{1, 3, 2}; | |
102 | ||
103 | BOOST_TEST( a < b, tt::lexicographic() ); | |
104 | } | |
105 | ||
106 | //____________________________________________________________________________// | |
107 | ||
108 | BOOST_AUTO_TEST_CASE( test_lexicographic_le ) | |
109 | { | |
110 | std::vector<int> a{1, 2, 3}; | |
111 | std::vector<int> b{1, 3, 2}; | |
112 | ||
113 | BOOST_TEST( a <= b, tt::lexicographic() ); | |
114 | } | |
115 | ||
116 | //____________________________________________________________________________// | |
117 | ||
118 | BOOST_AUTO_TEST_CASE( test_lexicographic_gt ) | |
119 | { | |
120 | std::vector<int> a{1, 2, 3}; | |
121 | std::vector<int> b{1, 3, 2}; | |
122 | ||
123 | BOOST_TEST( b > a, tt::lexicographic() ); | |
124 | } | |
125 | ||
126 | //____________________________________________________________________________// | |
127 | ||
128 | BOOST_AUTO_TEST_CASE( test_lexicographic_ge ) | |
129 | { | |
130 | std::vector<int> a{1, 2, 3}; | |
131 | std::vector<int> b{1, 3, 2}; | |
132 | ||
133 | BOOST_TEST( b >= a, tt::lexicographic() ); | |
134 | } | |
135 | ||
136 | //____________________________________________________________________________// | |
137 | ||
138 | BOOST_AUTO_TEST_CASE( test_collection_of_collection_comp ) | |
139 | { | |
140 | BOOST_TEST( std::string("abc") == std::string("abc") ); | |
141 | } | |
142 | ||
11fdf7f2 TL |
143 | //____________________________________________________________________________// |
144 | ||
145 | // this one does not have const_iterator nor a size, but should be forward iterable | |
146 | // and possible to use in the collection comparison | |
147 | struct fwd_iterable_custom { | |
148 | typedef std::vector<int>::const_iterator custom_iterator; // named "exotic" on purpose | |
149 | ||
150 | custom_iterator begin() const { return values.begin(); } | |
151 | custom_iterator end() const { return values.end(); } | |
152 | ||
153 | #if !defined(BOOST_MSVC) || (BOOST_MSVC_FULL_VER > 180040629) | |
154 | #define MY_TEST_HAS_INIT_LIST | |
155 | // this does not work on VC++ 2013 update 5 | |
156 | fwd_iterable_custom(std::initializer_list<int> ilist) : values{ilist} | |
157 | {} | |
158 | #else | |
159 | fwd_iterable_custom(int v1, int v2, int v3) { | |
160 | values.push_back(v1); | |
161 | values.push_back(v2); | |
162 | values.push_back(v3); | |
163 | } | |
164 | #endif | |
165 | private: | |
166 | std::vector<int> values; | |
167 | }; | |
168 | ||
169 | BOOST_AUTO_TEST_CASE( test_collection_requirement_type ) | |
170 | { | |
171 | #ifdef MY_TEST_HAS_INIT_LIST | |
172 | fwd_iterable_custom a{3,4,5}; | |
173 | fwd_iterable_custom b{3,4,6}; | |
174 | fwd_iterable_custom c{3,4,5}; | |
175 | #else | |
176 | fwd_iterable_custom a(3,4,5); | |
177 | fwd_iterable_custom b(3,4,6); | |
178 | fwd_iterable_custom c(3,4,5); | |
179 | #endif | |
180 | ||
181 | BOOST_TEST( a == a, tt::per_element() ); | |
182 | //BOOST_TEST( a != b, tt::per_element() ); | |
183 | BOOST_TEST( a == c, tt::per_element() ); | |
184 | ||
185 | BOOST_TEST( a < b, tt::lexicographic() ); | |
186 | BOOST_TEST( a <= c, tt::lexicographic() ); | |
187 | BOOST_TEST( b > c, tt::lexicographic() ); | |
188 | ||
189 | BOOST_TEST( a <= b, tt::per_element() ); | |
190 | BOOST_TEST( a <= c, tt::per_element() ); | |
191 | } | |
192 | ||
7c673cae | 193 | // EOF |