Debian Codegraph @

3rdparty/catch2/tests/SelfTest/UsageTests/MatchersRanges.tests.cpp

raw symbols SCIP
1
2 // Copyright Catch2 Authors
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE.txt or copy at
5 // https://www.boost.org/LICENSE_1_0.txt)
6
7 // SPDX-License-Identifier: BSL-1.0
8
9 #include <catch2/catch_test_macros.hpp>
10 #include <catch2/matchers/catch_matchers_container_properties.hpp>
11 #include <catch2/matchers/catch_matchers_contains.hpp>
12 #include <catch2/matchers/catch_matchers_range_equals.hpp>
13 #include <catch2/matchers/catch_matchers_floating_point.hpp>
14 #include <catch2/matchers/catch_matchers_quantifiers.hpp>
15 #include <catch2/matchers/catch_matchers_predicate.hpp>
16 #include <catch2/matchers/catch_matchers_string.hpp>
17
18 #include <helpers/range_test_helpers.hpp>
19
20 #include <cmath>
21 #include <list>
22 #include <map>
23 #include <type_traits>
24 #include <vector>
25 #include <memory>
26
27 struct MoveOnlyTestElement {
28 int num = 0;
29 MoveOnlyTestElement(int n) :num(n) {}
30
31 MoveOnlyTestElement(MoveOnlyTestElement&& rhs) = default;
32 MoveOnlyTestElement& operator=(MoveOnlyTestElement&& rhs) = default;
33
34 friend bool operator==(MoveOnlyTestElement const& lhs, MoveOnlyTestElement const& rhs) {
35 return lhs.num == rhs.num;
36 }
37
38 friend std::ostream& operator<<(std::ostream& out, MoveOnlyTestElement const& elem) {
39 out << elem.num;
40 return out;
41 }
42 };
43
44 TEST_CASE("Basic use of the Contains range matcher", "[matchers][templated][contains]") {
45 using Catch::Matchers::Contains;
46
47 SECTION("Different argument ranges, same element type, default comparison") {
48 std::array<int, 3> a{ { 1,2,3 } };
49 std::vector<int> b{ 0,1,2 };
50 std::list<int> c{ 4,5,6 };
51
52 // A contains 1
53 REQUIRE_THAT(a, Contains(1));
54 // B contains 1
55 REQUIRE_THAT(b, Contains(1));
56 // C does not contain 1
57 REQUIRE_THAT(c, !Contains(1));
58 }
59
60 SECTION("Different argument ranges, same element type, custom comparison") {
61 std::array<int, 3> a{ { 1,2,3 } };
62 std::vector<int> b{ 0,1,2 };
63 std::list<int> c{ 4,5,6 };
64
65 auto close_enough = [](int lhs, int rhs) { return std::abs(lhs - rhs) <= 1; };
66
67 // A contains 1, which is "close enough" to 0
68 REQUIRE_THAT(a, Contains(0, close_enough));
69 // B contains 0 directly
70 REQUIRE_THAT(b, Contains(0, close_enough));
71 // C does not contain anything "close enough" to 0
72 REQUIRE_THAT(c, !Contains(0, close_enough));
73 }
74
75 SECTION("Different element type, custom comparisons") {
76 std::array<std::string, 3> a{ { "abc", "abcd" , "abcde" } };
77
78 REQUIRE_THAT(a, Contains(4, [](auto&& lhs, size_t sz) {
79 return lhs.size() == sz;
80 }));
81 }
82
83 SECTION("Can handle type that requires ADL-found free function begin and end") {
84 unrelated::needs_ADL_begin<int> in{1, 2, 3, 4, 5};
85
86 REQUIRE_THAT(in, Contains(1));
87 REQUIRE_THAT(in, !Contains(8));
88 }
89
90 SECTION("Initialization with move only types") {
91 std::array<MoveOnlyTestElement, 3> in{ { MoveOnlyTestElement{ 1 }, MoveOnlyTestElement{ 2 }, MoveOnlyTestElement{ 3 } } };
92
93 REQUIRE_THAT(in, Contains(MoveOnlyTestElement{ 2 }));
94 REQUIRE_THAT(in, !Contains(MoveOnlyTestElement{ 9 }));
95 }
96
97 SECTION("Matching using matcher") {
98 std::array<double, 4> in{ {1, 2, 3} };
99
100 REQUIRE_THAT(in, Contains(Catch::Matchers::WithinAbs(0.5, 0.5)));
101 }
102 }
103
104 namespace {
105
106 struct has_empty {
107 bool empty() const { return false; }
108 };
109
110 } // end unnamed namespace
111
112 TEST_CASE("Basic use of the Empty range matcher", "[matchers][templated][empty]") {
113 using Catch::Matchers::IsEmpty;
114 SECTION("Simple, std-provided containers") {
115 std::array<int, 0> empty_array{};
116 std::array<double, 1> non_empty_array{};
117 REQUIRE_THAT(empty_array, IsEmpty());
118 REQUIRE_THAT(non_empty_array, !IsEmpty());
119
120 std::vector<std::string> empty_vec;
121 std::vector<char> non_empty_vec{ 'a', 'b', 'c' };
122 REQUIRE_THAT(empty_vec, IsEmpty());
123 REQUIRE_THAT(non_empty_vec, !IsEmpty());
124
125 std::list<std::list<std::list<int>>> inner_lists_are_empty;
126 inner_lists_are_empty.push_back({});
127 REQUIRE_THAT(inner_lists_are_empty, !IsEmpty());
128 REQUIRE_THAT(inner_lists_are_empty.front(), IsEmpty());
129 }
130 SECTION("Type with empty") {
131 REQUIRE_THAT(has_empty{}, !IsEmpty());
132 }
133 SECTION("Type requires ADL found empty free function") {
134 REQUIRE_THAT(unrelated::ADL_empty{}, IsEmpty());
135 }
136 }
137
138 namespace {
139 class LessThanMatcher final : public Catch::Matchers::MatcherBase<size_t> {
140 size_t m_target;
141 public:
142 explicit LessThanMatcher(size_t target):
143 m_target(target)
144 {}
145
146 bool match(size_t const& size) const override {
147 return size < m_target;
148 }
149
150 std::string describe() const override {
151 return "is less than " + std::to_string(m_target);
152 }
153 };
154
155 LessThanMatcher Lt(size_t sz) {
156 return LessThanMatcher{ sz };
157 }
158
159 struct has_size {
160 size_t size() const {
161 return 13;
162 }
163 };
164
165 } // end unnamed namespace
166
167 TEST_CASE("Usage of the SizeIs range matcher", "[matchers][templated][size]") {
168 using Catch::Matchers::SizeIs;
169 SECTION("Some with stdlib containers") {
170 std::vector<int> empty_vec;
171 REQUIRE_THAT(empty_vec, SizeIs(0));
172 REQUIRE_THAT(empty_vec, !SizeIs(2));
173 REQUIRE_THAT(empty_vec, SizeIs(Lt(2)));
174
175 std::array<int, 2> arr{};
176 REQUIRE_THAT(arr, SizeIs(2));
177 REQUIRE_THAT(arr, SizeIs( Lt(3)));
178 REQUIRE_THAT(arr, !SizeIs(!Lt(3)));
179
180 std::map<int, int> map{ {1, 1}, {2, 2}, {3, 3} };
181 REQUIRE_THAT(map, SizeIs(3));
182 }
183 SECTION("Type requires ADL found size free function") {
184 REQUIRE_THAT(unrelated::ADL_size{}, SizeIs(12));
185 }
186 SECTION("Type has size member") {
187 REQUIRE_THAT(has_size{}, SizeIs(13));
188 }
189 }
190
191
192 TEST_CASE("Usage of AllMatch range matcher", "[matchers][templated][quantifiers]") {
193 using Catch::Matchers::AllMatch;
194 using Catch::Matchers::Predicate;
195
196 SECTION("Basic usage") {
197 using Catch::Matchers::Contains;
198 using Catch::Matchers::SizeIs;
199
200 std::array<std::array<int, 5>, 5> data{{
201 {{ 0, 1, 2, 3, 5 }},
202 {{ 4,-3,-2, 5, 0 }},
203 {{ 0, 0, 0, 5, 0 }},
204 {{ 0,-5, 0, 5, 0 }},
205 {{ 1, 0, 0,-1, 5 }}
206 }};
207
208 REQUIRE_THAT(data, AllMatch(SizeIs(5)));
209 REQUIRE_THAT(data, !AllMatch(Contains(0) && Contains(1)));
210 }
211
212 SECTION("Type requires ADL found begin and end") {
213 unrelated::needs_ADL_begin<int> needs_adl{ 1, 2, 3, 4, 5 };
214 REQUIRE_THAT( needs_adl, AllMatch( Predicate<int>( []( int elem ) {
215 return elem < 6;
216 } ) ) );
217 }
218
219 SECTION("Shortcircuiting") {
220 with_mocked_iterator_access<int> mocked{ 1, 2, 3, 4, 5 };
221 SECTION("All are read") {
222 auto allMatch = AllMatch(Predicate<int>([](int elem) {
223 return elem < 10;
224 }));
225 REQUIRE_THAT(mocked, allMatch);
226 REQUIRE(mocked.m_derefed[0]);
227 REQUIRE(mocked.m_derefed[1]);
228 REQUIRE(mocked.m_derefed[2]);
229 REQUIRE(mocked.m_derefed[3]);
230 REQUIRE(mocked.m_derefed[4]);
231 }
232 SECTION("Short-circuited") {
233 auto allMatch = AllMatch(Predicate<int>([](int elem) {
234 return elem < 3;
235 }));
236 REQUIRE_THAT(mocked, !allMatch);
237 REQUIRE(mocked.m_derefed[0]);
238 REQUIRE(mocked.m_derefed[1]);
239 REQUIRE(mocked.m_derefed[2]);
240 REQUIRE_FALSE(mocked.m_derefed[3]);
241 REQUIRE_FALSE(mocked.m_derefed[4]);
242 }
243 }
244 }
245
246 TEST_CASE("Usage of AnyMatch range matcher", "[matchers][templated][quantifiers]") {
247 using Catch::Matchers::AnyMatch;
248 using Catch::Matchers::Predicate;
249
250 SECTION("Basic usage") {
251 using Catch::Matchers::Contains;
252 using Catch::Matchers::SizeIs;
253
254 std::array<std::array<int, 5>, 5> data{ {
255 {{ 0, 1, 2, 3, 5 }},
256 {{ 4,-3,-2, 5, 0 }},
257 {{ 0, 0, 0, 5, 0 }},
258 {{ 0,-5, 0, 5, 0 }},
259 {{ 1, 0, 0,-1, 5 }}
260 } };
261
262 REQUIRE_THAT(data, AnyMatch(SizeIs(5)));
263 REQUIRE_THAT(data, !AnyMatch(Contains(0) && Contains(10)));
264 }
265
266 SECTION( "Type requires ADL found begin and end" ) {
267 unrelated::needs_ADL_begin<int> needs_adl{ 1, 2, 3, 4, 5 };
268 REQUIRE_THAT( needs_adl, AnyMatch( Predicate<int>( []( int elem ) {
269 return elem < 3;
270 } ) ) );
271 }
272
273 SECTION("Shortcircuiting") {
274 with_mocked_iterator_access<int> mocked{ 1, 2, 3, 4, 5 };
275 SECTION("All are read") {
276 auto anyMatch = AnyMatch(
277 Predicate<int>( []( int elem ) { return elem > 10; } ) );
278 REQUIRE_THAT( mocked, !anyMatch );
279 REQUIRE( mocked.m_derefed[0] );
280 REQUIRE( mocked.m_derefed[1] );
281 REQUIRE( mocked.m_derefed[2] );
282 REQUIRE( mocked.m_derefed[3] );
283 REQUIRE( mocked.m_derefed[4] );
284 }
285 SECTION("Short-circuited") {
286 auto anyMatch = AnyMatch(
287 Predicate<int>( []( int elem ) { return elem < 3; } ) );
288 REQUIRE_THAT( mocked, anyMatch );
289 REQUIRE( mocked.m_derefed[0] );
290 REQUIRE_FALSE( mocked.m_derefed[1] );
291 REQUIRE_FALSE( mocked.m_derefed[2] );
292 REQUIRE_FALSE( mocked.m_derefed[3] );
293 REQUIRE_FALSE( mocked.m_derefed[4] );
294 }
295 }
296 }
297
298 TEST_CASE("Usage of NoneMatch range matcher", "[matchers][templated][quantifiers]") {
299 using Catch::Matchers::NoneMatch;
300 using Catch::Matchers::Predicate;
301
302 SECTION("Basic usage") {
303 using Catch::Matchers::Contains;
304 using Catch::Matchers::SizeIs;
305
306 std::array<std::array<int, 5>, 5> data{ {
307 {{ 0, 1, 2, 3, 5 }},
308 {{ 4,-3,-2, 5, 0 }},
309 {{ 0, 0, 0, 5, 0 }},
310 {{ 0,-5, 0, 5, 0 }},
311 {{ 1, 0, 0,-1, 5 }}
312 } };
313
314 REQUIRE_THAT(data, NoneMatch(SizeIs(6)));
315 REQUIRE_THAT(data, !NoneMatch(Contains(0) && Contains(1)));
316 }
317
318 SECTION( "Type requires ADL found begin and end" ) {
319 unrelated::needs_ADL_begin<int> needs_adl{ 1, 2, 3, 4, 5 };
320 REQUIRE_THAT( needs_adl, NoneMatch( Predicate<int>( []( int elem ) {
321 return elem > 6;
322 } ) ) );
323 }
324
325 SECTION("Shortcircuiting") {
326 with_mocked_iterator_access<int> mocked{ 1, 2, 3, 4, 5 };
327 SECTION("All are read") {
328 auto noneMatch = NoneMatch(
329 Predicate<int>([](int elem) { return elem > 10; }));
330 REQUIRE_THAT(mocked, noneMatch);
331 REQUIRE(mocked.m_derefed[0]);
332 REQUIRE(mocked.m_derefed[1]);
333 REQUIRE(mocked.m_derefed[2]);
334 REQUIRE(mocked.m_derefed[3]);
335 REQUIRE(mocked.m_derefed[4]);
336 }
337 SECTION("Short-circuited") {
338 auto noneMatch = NoneMatch(
339 Predicate<int>([](int elem) { return elem < 3; }));
340 REQUIRE_THAT(mocked, !noneMatch);
341 REQUIRE(mocked.m_derefed[0]);
342 REQUIRE_FALSE(mocked.m_derefed[1]);
343 REQUIRE_FALSE(mocked.m_derefed[2]);
344 REQUIRE_FALSE(mocked.m_derefed[3]);
345 REQUIRE_FALSE(mocked.m_derefed[4]);
346 }
347 }
348 }
349
350 namespace {
351 struct ConvertibleToBool
352 {
353 bool v;
354
355 explicit operator bool() const
356 {
357 return v;
358 }
359 };
360 }
361
362 namespace Catch {
363 template <>
364 struct StringMaker<ConvertibleToBool> {
365 static std::string
366 convert( ConvertibleToBool const& convertible_to_bool ) {
367 return ::Catch::Detail::stringify( convertible_to_bool.v );
368 }
369 };
370 } // namespace Catch
371
372 TEST_CASE("Usage of AllTrue range matcher", "[matchers][templated][quantifiers]") {
373 using Catch::Matchers::AllTrue;
374
375 SECTION( "Basic usage" ) {
376 SECTION( "All true evaluates to true" ) {
377 std::array<bool, 5> const data{ { true, true, true, true, true } };
378 REQUIRE_THAT( data, AllTrue() );
379 }
380 SECTION( "Empty evaluates to true" ) {
381 std::array<bool, 0> const data{};
382 REQUIRE_THAT( data, AllTrue() );
383 }
384 SECTION( "One false evaluates to false" ) {
385 std::array<bool, 5> const data{ { true, true, false, true, true } };
386 REQUIRE_THAT( data, !AllTrue() );
387 }
388 SECTION( "All false evaluates to false" ) {
389 std::array<bool, 5> const data{
390 { false, false, false, false, false } };
391 REQUIRE_THAT( data, !AllTrue() );
392 }
393 }
394
395 SECTION( "Contained type is convertible to bool" ) {
396 SECTION( "All true evaluates to true" ) {
397 std::array<ConvertibleToBool, 5> const data{
398 { { true }, { true }, { true }, { true }, { true } } };
399 REQUIRE_THAT( data, AllTrue() );
400 }
401 SECTION( "One false evaluates to false" ) {
402 std::array<ConvertibleToBool, 5> const data{
403 { { true }, { true }, { false }, { true }, { true } } };
404 REQUIRE_THAT( data, !AllTrue() );
405 }
406 SECTION( "All false evaluates to false" ) {
407 std::array<ConvertibleToBool, 5> const data{
408 { { false }, { false }, { false }, { false }, { false } } };
409 REQUIRE_THAT( data, !AllTrue() );
410 }
411 }
412
413 SECTION( "Shortcircuiting" ) {
414 SECTION( "All are read" ) {
415 with_mocked_iterator_access<bool> const mocked{
416 true, true, true, true, true };
417 REQUIRE_THAT( mocked, AllTrue() );
418 REQUIRE( mocked.m_derefed[0] );
419 REQUIRE( mocked.m_derefed[1] );
420 REQUIRE( mocked.m_derefed[2] );
421 REQUIRE( mocked.m_derefed[3] );
422 REQUIRE( mocked.m_derefed[4] );
423 }
424 SECTION( "Short-circuited" ) {
425 with_mocked_iterator_access<bool> const mocked{
426 true, true, false, true, true };
427 REQUIRE_THAT( mocked, !AllTrue() );
428 REQUIRE( mocked.m_derefed[0] );
429 REQUIRE( mocked.m_derefed[1] );
430 REQUIRE( mocked.m_derefed[2] );
431 REQUIRE_FALSE( mocked.m_derefed[3] );
432 REQUIRE_FALSE( mocked.m_derefed[4] );
433 }
434 }
435 }
436
437 TEST_CASE( "Usage of NoneTrue range matcher", "[matchers][templated][quantifiers]" ) {
438 using Catch::Matchers::NoneTrue;
439
440 SECTION( "Basic usage" ) {
441 SECTION( "All true evaluates to false" ) {
442 std::array<bool, 5> const data{ { true, true, true, true, true } };
443 REQUIRE_THAT( data, !NoneTrue() );
444 }
445 SECTION( "Empty evaluates to true" ) {
446 std::array<bool, 0> const data{};
447 REQUIRE_THAT( data, NoneTrue() );
448 }
449 SECTION( "One true evaluates to false" ) {
450 std::array<bool, 5> const data{
451 { false, false, true, false, false } };
452 REQUIRE_THAT( data, !NoneTrue() );
453 }
454 SECTION( "All false evaluates to true" ) {
455 std::array<bool, 5> const data{
456 { false, false, false, false, false } };
457 REQUIRE_THAT( data, NoneTrue() );
458 }
459 }
460
461 SECTION( "Contained type is convertible to bool" ) {
462 SECTION( "All true evaluates to false" ) {
463 std::array<ConvertibleToBool, 5> const data{
464 { { true }, { true }, { true }, { true }, { true } } };
465 REQUIRE_THAT( data, !NoneTrue() );
466 }
467 SECTION( "One true evaluates to false" ) {
468 std::array<ConvertibleToBool, 5> const data{
469 { { false }, { false }, { true }, { false }, { false } } };
470 REQUIRE_THAT( data, !NoneTrue() );
471 }
472 SECTION( "All false evaluates to true" ) {
473 std::array<ConvertibleToBool, 5> const data{
474 { { false }, { false }, { false }, { false }, { false } } };
475 REQUIRE_THAT( data, NoneTrue() );
476 }
477 }
478
479 SECTION( "Shortcircuiting" ) {
480 SECTION( "All are read" ) {
481 with_mocked_iterator_access<bool> const mocked{
482 false, false, false, false, false };
483 REQUIRE_THAT( mocked, NoneTrue() );
484 REQUIRE( mocked.m_derefed[0] );
485 REQUIRE( mocked.m_derefed[1] );
486 REQUIRE( mocked.m_derefed[2] );
487 REQUIRE( mocked.m_derefed[3] );
488 REQUIRE( mocked.m_derefed[4] );
489 }
490 SECTION( "Short-circuited" ) {
491 with_mocked_iterator_access<bool> const mocked{
492 false, false, true, true, true };
493 REQUIRE_THAT( mocked, !NoneTrue() );
494 REQUIRE( mocked.m_derefed[0] );
495 REQUIRE( mocked.m_derefed[1] );
496 REQUIRE( mocked.m_derefed[2] );
497 REQUIRE_FALSE( mocked.m_derefed[3] );
498 REQUIRE_FALSE( mocked.m_derefed[4] );
499 }
500 }
501 }
502
503 TEST_CASE( "Usage of AnyTrue range matcher", "[matchers][templated][quantifiers]" ) {
504 using Catch::Matchers::AnyTrue;
505
506 SECTION( "Basic usage" ) {
507 SECTION( "All true evaluates to true" ) {
508 std::array<bool, 5> const data{ { true, true, true, true, true } };
509 REQUIRE_THAT( data, AnyTrue() );
510 }
511 SECTION( "Empty evaluates to false" ) {
512 std::array<bool, 0> const data{};
513 REQUIRE_THAT( data, !AnyTrue() );
514 }
515 SECTION( "One true evaluates to true" ) {
516 std::array<bool, 5> const data{
517 { false, false, true, false, false } };
518 REQUIRE_THAT( data, AnyTrue() );
519 }
520 SECTION( "All false evaluates to false" ) {
521 std::array<bool, 5> const data{
522 { false, false, false, false, false } };
523 REQUIRE_THAT( data, !AnyTrue() );
524 }
525 }
526
527 SECTION( "Contained type is convertible to bool" ) {
528 SECTION( "All true evaluates to true" ) {
529 std::array<ConvertibleToBool, 5> const data{
530 { { true }, { true }, { true }, { true }, { true } } };
531 REQUIRE_THAT( data, AnyTrue() );
532 }
533 SECTION( "One true evaluates to true" ) {
534 std::array<ConvertibleToBool, 5> const data{
535 { { false }, { false }, { true }, { false }, { false } } };
536 REQUIRE_THAT( data, AnyTrue() );
537 }
538 SECTION( "All false evaluates to false" ) {
539 std::array<ConvertibleToBool, 5> const data{
540 { { false }, { false }, { false }, { false }, { false } } };
541 REQUIRE_THAT( data, !AnyTrue() );
542 }
543 }
544
545 SECTION( "Shortcircuiting" ) {
546 SECTION( "All are read" ) {
547 with_mocked_iterator_access<bool> const mocked{
548 false, false, false, false, true };
549 REQUIRE_THAT( mocked, AnyTrue() );
550 REQUIRE( mocked.m_derefed[0] );
551 REQUIRE( mocked.m_derefed[1] );
552 REQUIRE( mocked.m_derefed[2] );
553 REQUIRE( mocked.m_derefed[3] );
554 REQUIRE( mocked.m_derefed[4] );
555 }
556 SECTION( "Short-circuited" ) {
557 with_mocked_iterator_access<bool> const mocked{
558 false, false, true, true, true };
559 REQUIRE_THAT( mocked, AnyTrue() );
560 REQUIRE( mocked.m_derefed[0] );
561 REQUIRE( mocked.m_derefed[1] );
562 REQUIRE( mocked.m_derefed[2] );
563 REQUIRE_FALSE( mocked.m_derefed[3] );
564 REQUIRE_FALSE( mocked.m_derefed[4] );
565 }
566 }
567 }
568
569 TEST_CASE("All/Any/None True matchers support types with ADL begin",
570 "[approvals][matchers][quantifiers][templated]") {
571 using Catch::Matchers::AllTrue;
572 using Catch::Matchers::NoneTrue;
573 using Catch::Matchers::AnyTrue;
574
575
576 SECTION( "Type requires ADL found begin and end" ) {
577 unrelated::needs_ADL_begin<bool> const needs_adl{
578 true, true, true, true, true };
579 REQUIRE_THAT( needs_adl, AllTrue() );
580 }
581
582 SECTION( "Type requires ADL found begin and end" ) {
583 unrelated::needs_ADL_begin<bool> const needs_adl{
584 false, false, false, false, false };
585 REQUIRE_THAT( needs_adl, NoneTrue() );
586 }
587
588 SECTION( "Type requires ADL found begin and end" ) {
589 unrelated::needs_ADL_begin<bool> const needs_adl{
590 false, false, true, false, false };
591 REQUIRE_THAT( needs_adl, AnyTrue() );
592 }
593 }
594
595 // Range loop iterating over range with different types for begin and end is a
596 // C++17 feature, and GCC refuses to compile such code unless the lang mode is
597 // set to C++17 or later.
598 #if defined(CATCH_CPP17_OR_GREATER)
599
600 TEST_CASE( "The quantifier range matchers support types with different types returned from begin and end",
601 "[matchers][templated][quantifiers][approvals]" ) {
602 using Catch::Matchers::AllMatch;
603 using Catch::Matchers::AllTrue;
604 using Catch::Matchers::AnyMatch;
605 using Catch::Matchers::AnyTrue;
606 using Catch::Matchers::NoneMatch;
607 using Catch::Matchers::NoneTrue;
608
609 using Catch::Matchers::Predicate;
610
611 SECTION( "AllAnyNoneMatch" ) {
612 has_different_begin_end_types<int> diff_types{ 1, 2, 3, 4, 5 };
613 REQUIRE_THAT( diff_types, !AllMatch( Predicate<int>( []( int elem ) {
614 return elem < 3;
615 } ) ) );
616
617 REQUIRE_THAT( diff_types, AnyMatch( Predicate<int>( []( int elem ) {
618 return elem < 2;
619 } ) ) );
620
621 REQUIRE_THAT( diff_types, !NoneMatch( Predicate<int>( []( int elem ) {
622 return elem < 3;
623 } ) ) );
624 }
625 SECTION( "AllAnyNoneTrue" ) {
626 has_different_begin_end_types<bool> diff_types{ false, false, true, false, false };
627
628 REQUIRE_THAT( diff_types, !AllTrue() );
629 REQUIRE_THAT( diff_types, AnyTrue() );
630 REQUIRE_THAT( diff_types, !NoneTrue() );
631 }
632 }
633
634 TEST_CASE( "RangeEquals supports ranges with different types returned from begin and end",
635 "[matchers][templated][range][approvals] ") {
636 using Catch::Matchers::RangeEquals;
637 using Catch::Matchers::UnorderedRangeEquals;
638
639 has_different_begin_end_types<int> diff_types{ 1, 2, 3, 4, 5 };
640 std::array<int, 5> arr1{ { 1, 2, 3, 4, 5 } }, arr2{ { 2, 3, 4, 5, 6 } };
641
642 REQUIRE_THAT( diff_types, RangeEquals( arr1 ) );
643 REQUIRE_THAT( diff_types, RangeEquals( arr2, []( int l, int r ) {
644 return l + 1 == r;
645 } ) );
646 REQUIRE_THAT( diff_types, UnorderedRangeEquals( diff_types ) );
647 }
648
649 TEST_CASE( "RangeContains supports ranges with different types returned from "
650 "begin and end",
651 "[matchers][templated][range][approvals]" ) {
652 using Catch::Matchers::Contains;
653
654 has_different_begin_end_types<size_t> diff_types{ 1, 2, 3, 4, 5 };
655 REQUIRE_THAT( diff_types, Contains( size_t( 3 ) ) );
656 REQUIRE_THAT( diff_types, Contains( LessThanMatcher( size_t( 4 ) ) ) );
657 }
658
659 #endif
660
661 TEST_CASE( "Usage of RangeEquals range matcher", "[matchers][templated][quantifiers]" ) {
662 using Catch::Matchers::RangeEquals;
663
664 // In these tests, the types are always the same - type conversion is in the next section
665 SECTION( "Basic usage" ) {
666 SECTION( "Empty container matches empty container" ) {
667 const std::vector<int> empty_vector;
668 CHECK_THAT( empty_vector, RangeEquals( empty_vector ) );
669 }
670 SECTION( "Empty container does not match non-empty container" ) {
671 const std::vector<int> empty_vector;
672 const std::vector<int> non_empty_vector{ 1 };
673 CHECK_THAT( empty_vector, !RangeEquals( non_empty_vector ) );
674 // ...and in reverse
675 CHECK_THAT( non_empty_vector, !RangeEquals( empty_vector ) );
676 }
677 SECTION( "Two equal 1-length non-empty containers" ) {
678 const std::array<int, 1> non_empty_array{ { 1 } };
679 CHECK_THAT( non_empty_array, RangeEquals( non_empty_array ) );
680 }
681 SECTION( "Two equal-sized, equal, non-empty containers" ) {
682 const std::array<int, 3> array_a{ { 1, 2, 3 } };
683 CHECK_THAT( array_a, RangeEquals( array_a ) );
684 }
685 SECTION( "Two equal-sized, non-equal, non-empty containers" ) {
686 const std::array<int, 3> array_a{ { 1, 2, 3 } };
687 const std::array<int, 3> array_b{ { 2, 2, 3 } };
688 const std::array<int, 3> array_c{ { 1, 2, 2 } };
689 CHECK_THAT( array_a, !RangeEquals( array_b ) );
690 CHECK_THAT( array_a, !RangeEquals( array_c ) );
691 }
692 SECTION( "Two non-equal-sized, non-empty containers (with same first "
693 "elements)" ) {
694 const std::vector<int> vector_a{ 1, 2, 3 };
695 const std::vector<int> vector_b{ 1, 2, 3, 4 };
696 CHECK_THAT( vector_a, !RangeEquals( vector_b ) );
697 }
698 }
699
700 SECTION( "Custom predicate" ) {
701
702 auto close_enough = []( int lhs, int rhs ) {
703 return std::abs( lhs - rhs ) <= 1;
704 };
705
706 SECTION( "Two equal non-empty containers (close enough)" ) {
707 const std::vector<int> vector_a{ { 1, 2, 3 } };
708 const std::vector<int> vector_a_plus_1{ { 2, 3, 4 } };
709 CHECK_THAT( vector_a, RangeEquals( vector_a_plus_1, close_enough ) );
710 }
711 SECTION( "Two non-equal non-empty containers (close enough)" ) {
712 const std::vector<int> vector_a{ { 1, 2, 3 } };
713 const std::vector<int> vector_b{ { 3, 3, 4 } };
714 CHECK_THAT( vector_a, !RangeEquals( vector_b, close_enough ) );
715 }
716 }
717
718 SECTION( "Ranges that need ADL begin/end" ) {
719 unrelated::needs_ADL_begin<int> const
720 needs_adl1{ 1, 2, 3, 4, 5 },
721 needs_adl2{ 1, 2, 3, 4, 5 },
722 needs_adl3{ 2, 3, 4, 5, 6 };
723
724 REQUIRE_THAT( needs_adl1, RangeEquals( needs_adl2 ) );
725 REQUIRE_THAT( needs_adl1, RangeEquals( needs_adl3, []( int l, int r ) {
726 return l + 1 == r;
727 } ) );
728 }
729
730 SECTION("Check short-circuiting behaviour") {
731 with_mocked_iterator_access<int> const mocked1{ 1, 2, 3, 4 };
732
733 SECTION( "Check short-circuits on failure" ) {
734 std::array<int, 4> arr{ { 1, 2, 4, 4 } };
735
736 REQUIRE_THAT( mocked1, !RangeEquals( arr ) );
737 REQUIRE( mocked1.m_derefed[0] );
738 REQUIRE( mocked1.m_derefed[1] );
739 REQUIRE( mocked1.m_derefed[2] );
740 REQUIRE_FALSE( mocked1.m_derefed[3] );
741 }
742 SECTION("All elements are checked on success") {
743 std::array<int, 4> arr{ { 1, 2, 3, 4 } };
744
745 REQUIRE_THAT( mocked1, RangeEquals( arr ) );
746 REQUIRE( mocked1.m_derefed[0] );
747 REQUIRE( mocked1.m_derefed[1] );
748 REQUIRE( mocked1.m_derefed[2] );
749 REQUIRE( mocked1.m_derefed[3] );
750 }
751 }
752 }
753
754 TEST_CASE( "Usage of UnorderedRangeEquals range matcher",
755 "[matchers][templated][quantifiers]" ) {
756 using Catch::Matchers::UnorderedRangeEquals;
757
758 // In these tests, the types are always the same - type conversion is in the
759 // next section
760 SECTION( "Basic usage" ) {
761 SECTION( "Empty container matches empty container" ) {
762 const std::vector<int> empty_vector;
763 CHECK_THAT( empty_vector, UnorderedRangeEquals( empty_vector ) );
764 }
765 SECTION( "Empty container does not match non-empty container" ) {
766 const std::vector<int> empty_vector;
767 const std::vector<int> non_empty_vector{ 1 };
768 CHECK_THAT( empty_vector,
769 !UnorderedRangeEquals( non_empty_vector ) );
770 // ...and in reverse
771 CHECK_THAT( non_empty_vector,
772 !UnorderedRangeEquals( empty_vector ) );
773 }
774 SECTION( "Two equal 1-length non-empty containers" ) {
775 const std::array<int, 1> non_empty_array{ { 1 } };
776 CHECK_THAT( non_empty_array,
777 UnorderedRangeEquals( non_empty_array ) );
778 }
779 SECTION( "Two equal-sized, equal, non-empty containers" ) {
780 const std::array<int, 3> array_a{ { 1, 2, 3 } };
781 CHECK_THAT( array_a, UnorderedRangeEquals( array_a ) );
782 }
783 SECTION( "Two equal-sized, non-equal, non-empty containers" ) {
784 const std::array<int, 3> array_a{ { 1, 2, 3 } };
785 const std::array<int, 3> array_b{ { 2, 2, 3 } };
786 CHECK_THAT( array_a, !UnorderedRangeEquals( array_b ) );
787 }
788 SECTION( "Two non-equal-sized, non-empty containers" ) {
789 const std::vector<int> vector_a{ 1, 2, 3 };
790 const std::vector<int> vector_b{ 1, 2, 3, 4 };
791 CHECK_THAT( vector_a, !UnorderedRangeEquals( vector_b ) );
792 }
793 }
794
795 SECTION( "Custom predicate" ) {
796
797 auto close_enough = []( int lhs, int rhs ) {
798 return std::abs( lhs - rhs ) <= 1;
799 };
800
801 SECTION( "Two equal non-empty containers (close enough)" ) {
802 const std::vector<int> vector_a{ { 1, 10, 20 } };
803 const std::vector<int> vector_a_plus_1{ { 11, 21, 2 } };
804 CHECK_THAT( vector_a,
805 UnorderedRangeEquals( vector_a_plus_1, close_enough ) );
806 }
807 SECTION( "Two non-equal non-empty containers (close enough)" ) {
808 const std::vector<int> vector_a{ { 1, 10, 21 } };
809 const std::vector<int> vector_b{ { 11, 21, 3 } };
810 CHECK_THAT( vector_a,
811 !UnorderedRangeEquals( vector_b, close_enough ) );
812 }
813 }
814
815
816 SECTION( "Ranges that need ADL begin/end" ) {
817 unrelated::needs_ADL_begin<int> const
818 needs_adl1{ 1, 2, 3, 4, 5 },
819 needs_adl2{ 1, 2, 3, 4, 5 };
820
821 REQUIRE_THAT( needs_adl1, UnorderedRangeEquals( needs_adl2 ) );
822 }
823 }
824
825 /**
826 * Return true if the type given has a random access iterator type.
827 */
828 template <typename Container>
829 static constexpr bool ContainerIsRandomAccess( const Container& ) {
830 using array_iter_category = typename std::iterator_traits<
831 typename Container::iterator>::iterator_category;
832
833 return std::is_base_of<std::random_access_iterator_tag,
834 array_iter_category>::value;
835 }
836
837 TEST_CASE( "Type conversions of RangeEquals and similar",
838 "[matchers][templated][quantifiers]" ) {
839 using Catch::Matchers::RangeEquals;
840 using Catch::Matchers::UnorderedRangeEquals;
841
842 // In these test, we can always test RangeEquals and
843 // UnorderedRangeEquals in the same way, since we're mostly
844 // testing the template type deductions (and RangeEquals
845 // implies UnorderedRangeEquals)
846
847 SECTION( "Container conversions" ) {
848 SECTION( "Two equal containers of different container types" ) {
849 const std::array<int, 3> array_int_a{ { 1, 2, 3 } };
850 const int c_array[3] = { 1, 2, 3 };
851 CHECK_THAT( array_int_a, RangeEquals( c_array ) );
852 CHECK_THAT( array_int_a, UnorderedRangeEquals( c_array ) );
853 }
854 SECTION( "Two equal containers of different container types "
855 "(differ in array N)" ) {
856 const std::array<int, 3> array_int_3{ { 1, 2, 3 } };
857 const std::array<int, 4> array_int_4{ { 1, 2, 3, 4 } };
858 CHECK_THAT( array_int_3, !RangeEquals( array_int_4 ) );
859 CHECK_THAT( array_int_3, !UnorderedRangeEquals( array_int_4 ) );
860 }
861 SECTION( "Two equal containers of different container types and value "
862 "types" ) {
863 const std::array<int, 3> array_int_a{ { 1, 2, 3 } };
864 const std::vector<int> vector_char_a{ 1, 2, 3 };
865 CHECK_THAT( array_int_a, RangeEquals( vector_char_a ) );
866 CHECK_THAT( array_int_a, UnorderedRangeEquals( vector_char_a ) );
867 }
868 SECTION( "Two equal containers, one random access, one not" ) {
869 const std::array<int, 3> array_int_a{ { 1, 2, 3 } };
870 const std::list<int> list_char_a{ 1, 2, 3 };
871
872 // Verify these types really are different in random access nature
873 STATIC_REQUIRE( ContainerIsRandomAccess( array_int_a ) !=
874 ContainerIsRandomAccess( list_char_a ) );
875
876 CHECK_THAT( array_int_a, RangeEquals( list_char_a ) );
877 CHECK_THAT( array_int_a, UnorderedRangeEquals( list_char_a ) );
878 }
879 }
880
881 SECTION( "Value type" ) {
882 SECTION( "Two equal containers of different value types" ) {
883 const std::vector<int> vector_int_a{ 1, 2, 3 };
884 const std::vector<char> vector_char_a{ 1, 2, 3 };
885 CHECK_THAT( vector_int_a, RangeEquals( vector_char_a ) );
886 CHECK_THAT( vector_int_a, UnorderedRangeEquals( vector_char_a ) );
887 }
888 SECTION( "Two non-equal containers of different value types" ) {
889 const std::vector<int> vector_int_a{ 1, 2, 3 };
890 const std::vector<char> vector_char_b{ 1, 2, 2 };
891 CHECK_THAT( vector_int_a, !RangeEquals( vector_char_b ) );
892 CHECK_THAT( vector_int_a, !UnorderedRangeEquals( vector_char_b ) );
893 }
894 }
895
896 SECTION( "Ranges with begin that needs ADL" ) {
897 unrelated::needs_ADL_begin<int> a{ 1, 2, 3 }, b{ 3, 2, 1 };
898 REQUIRE_THAT( a, !RangeEquals( b ) );
899 REQUIRE_THAT( a, UnorderedRangeEquals( b ) );
900 }
901
902 SECTION( "Custom predicate" ) {
903
904 auto close_enough = []( int lhs, int rhs ) {
905 return std::abs( lhs - rhs ) <= 1;
906 };
907
908 SECTION( "Two equal non-empty containers (close enough)" ) {
909 const std::vector<int> vector_a{ { 1, 2, 3 } };
910 const std::array<char, 3> array_a_plus_1{ { 2, 3, 4 } };
911 CHECK_THAT( vector_a,
912 RangeEquals( array_a_plus_1, close_enough ) );
913 CHECK_THAT( vector_a,
914 UnorderedRangeEquals( array_a_plus_1, close_enough ) );
915 }
916 }
917 }