Debian Codegraph @

src/external/catch2/tests/SelfTest/IntrospectiveTests/Algorithms.tests.cpp

raw
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/internal/catch_is_permutation.hpp>
11
12 #include <helpers/range_test_helpers.hpp>
13
14 #include <array>
15
16 namespace {
17 template <typename Range1, typename Range2>
18 static bool is_permutation(Range1 const& r1, Range2 const& r2) {
19 using std::begin; using std::end;
20 return Catch::Detail::is_permutation(
21 begin( r1 ), end( r1 ), begin( r2 ), end( r2 ), std::equal_to<>{} );
22 }
23 }
24
25 TEST_CASE("is_permutation", "[algorithms][approvals]") {
26 SECTION( "Handle empty ranges" ) {
27 std::array<int, 0> empty;
28 std::array<int, 2> non_empty{ { 2, 3 } };
29 REQUIRE( is_permutation( empty, empty ) );
30 REQUIRE_FALSE( is_permutation( empty, non_empty ) );
31 REQUIRE_FALSE( is_permutation( non_empty, empty ) );
32 }
33 SECTION( "Different length ranges" ) {
34 std::array<int, 6> arr1{ { 1, 3, 5, 7, 8, 9 } };
35 // arr2 is prefix of arr1
36 std::array<int, 4> arr2{ { 1, 3, 5, 7 } };
37 // arr3 shares prefix with arr1 and arr2, but is not a permutation
38 std::array<int, 5> arr3{ { 1, 3, 5, 9, 8 } };
39 REQUIRE_FALSE( is_permutation( arr1, arr2 ) );
40 REQUIRE_FALSE( is_permutation( arr1, arr3 ) );
41 REQUIRE_FALSE( is_permutation( arr2, arr3 ) );
42 }
43 SECTION( "Same length ranges" ) {
44 SECTION( "Shared elements, but different counts" ) {
45 const std::array<int, 6>
46 arr1{ { 1, 1, 1, 1, 2, 2 } },
47 arr2{ { 1, 1, 2, 2, 2, 2 } };
48 REQUIRE_FALSE( is_permutation( arr1, arr2 ) );
49 }
50 SECTION( "Identical ranges" ) {
51 const std::array<int, 6>
52 arr1{ { 1, 1, 1, 1, 2, 2 } },
53 arr2{ { 1, 1, 2, 2, 2, 2 } };
54 REQUIRE( is_permutation( arr1, arr1 ) );
55 REQUIRE( is_permutation( arr2, arr2 ) );
56 }
57 SECTION( "Completely distinct elements" ) {
58 // Completely distinct elements
59 const std::array<int, 4>
60 arr1{ { 1, 2, 3, 4 } },
61 arr2{ { 10, 20, 30, 40 } };
62 REQUIRE_FALSE( is_permutation( arr1, arr2 ) );
63 }
64 SECTION( "Reverse ranges" ) {
65 const std::array<int, 5>
66 arr1{ { 1, 2, 3, 4, 5 } },
67 arr2{ { 5, 4, 3, 2, 1 } };
68 REQUIRE( is_permutation( arr1, arr2 ) );
69 }
70 SECTION( "Shared prefix & permuted elements" ) {
71 const std::array<int, 5>
72 arr1{ { 1, 1, 2, 3, 4 } },
73 arr2{ { 1, 1, 4, 2, 3 } };
74 REQUIRE( is_permutation( arr1, arr2 ) );
75 }
76 SECTION( "Permutations with element count > 1" ) {
77 const std::array<int, 7>
78 arr1{ { 2, 2, 3, 3, 3, 1, 1 } },
79 arr2{ { 3, 2, 1, 3, 2, 1, 3 } };
80 REQUIRE( is_permutation( arr1, arr2 ) );
81 }
82 }
83 }
84
85 TEST_CASE("is_permutation supports iterator + sentinel pairs",
86 "[algorithms][is-permutation][approvals]") {
87 const has_different_begin_end_types<int>
88 range_1{ 1, 2, 3, 4 },
89 range_2{ 4, 3, 2, 1 };
90 REQUIRE( is_permutation( range_1, range_2 ) );
91
92 const has_different_begin_end_types<int> range_3{ 3, 3, 2, 1 };
93 REQUIRE_FALSE( is_permutation( range_1, range_3 ) );
94 }