| 1 | // Copyright (C) 2013-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // 25.2.11 [alg.equal] |
| 19 | |
| 20 | // { dg-do run { target c++14 } } |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <testsuite_hooks.h> |
| 24 | #include <testsuite_iterators.h> |
| 25 | |
| 26 | using __gnu_test::test_container; |
| 27 | using __gnu_test::input_iterator_wrapper; |
| 28 | using __gnu_test::random_access_iterator_wrapper; |
| 29 | |
| 30 | typedef test_container<int, input_iterator_wrapper> Container; |
| 31 | typedef test_container<int, random_access_iterator_wrapper> RA_Container; |
| 32 | int array1[] = {0, 1}; |
| 33 | int array2[] = {1, 0}; |
| 34 | int array3[] = {1, 0}; |
| 35 | |
| 36 | struct equal_to |
| 37 | { |
| 38 | static int count; |
| 39 | |
| 40 | bool operator()(int l, int r) |
| 41 | { |
| 42 | ++count; |
| 43 | return l == r; |
| 44 | } |
| 45 | } eq; |
| 46 | |
| 47 | int equal_to::count = 0; |
| 48 | |
| 49 | bool __attribute__((unused)) test = false; |
| 50 | |
| 51 | void test1() |
| 52 | { |
| 53 | const Container con1(array1, array1); |
| 54 | const Container con2(array2, array2); |
| 55 | |
| 56 | auto c1 = con1; |
| 57 | auto c2 = con2; |
| 58 | VERIFY( std::equal(c1.begin(), c1.end(), c2.begin(), c2.end()) ); |
| 59 | VERIFY( equal_to::count == 0 ); |
| 60 | |
| 61 | c1 = con1; |
| 62 | c2 = con2; |
| 63 | VERIFY( std::equal(c1.begin(), c1.end(), c2.begin(), c2.end(), eq) ); |
| 64 | VERIFY( equal_to::count == 0 ); |
| 65 | } |
| 66 | |
| 67 | void test2() |
| 68 | { |
| 69 | const Container con1(array1, array1 + 0); |
| 70 | const Container con2(array2, array2 + 2); |
| 71 | |
| 72 | auto c1 = con1; |
| 73 | auto c2 = con2; |
| 74 | VERIFY( !std::equal(c1.begin(), c1.end(), c2.begin(), c2.end()) ); |
| 75 | |
| 76 | c1 = con1; |
| 77 | c2 = con2; |
| 78 | VERIFY( !std::equal(c1.begin(), c1.end(), c2.begin(), c2.end(), eq) ); |
| 79 | VERIFY( equal_to::count == 0 ); |
| 80 | |
| 81 | c1 = con1; |
| 82 | c2 = con2; |
| 83 | VERIFY( !std::equal(c2.begin(), c2.end(), c1.begin(), c1.end()) ); |
| 84 | |
| 85 | c1 = con1; |
| 86 | c2 = con2; |
| 87 | VERIFY( !std::equal(c2.begin(), c2.end(), c1.begin(), c1.end(), eq) ); |
| 88 | VERIFY( equal_to::count == 0 ); |
| 89 | } |
| 90 | |
| 91 | void test3() |
| 92 | { |
| 93 | const Container con1(array1, array1 + 2); |
| 94 | const Container con2(array2, array2 + 2); |
| 95 | |
| 96 | auto c1 = con1; |
| 97 | auto c2 = con2; |
| 98 | VERIFY( !std::equal(c1.begin(), c1.end(), c2.begin(), c2.end()) ); |
| 99 | |
| 100 | c1 = con1; |
| 101 | c2 = con2; |
| 102 | VERIFY( !std::equal(c1.begin(), c1.end(), c2.begin(), c2.end(), eq) ); |
| 103 | VERIFY( equal_to::count == 1 ); |
| 104 | equal_to::count = 0; |
| 105 | |
| 106 | c1 = con1; |
| 107 | c2 = con2; |
| 108 | VERIFY( !std::equal(c2.begin(), c2.end(), c1.begin(), c1.end()) ); |
| 109 | |
| 110 | c1 = con1; |
| 111 | c2 = con2; |
| 112 | VERIFY( !std::equal(c2.begin(), c2.end(), c1.begin(), c1.end(), eq) ); |
| 113 | VERIFY( equal_to::count == 1 ); |
| 114 | equal_to::count = 0; |
| 115 | } |
| 116 | |
| 117 | void test4() |
| 118 | { |
| 119 | const Container con3(array3, array3 + 2); |
| 120 | const Container con2(array2, array2 + 2); |
| 121 | |
| 122 | auto c3 = con3; |
| 123 | auto c2 = con2; |
| 124 | VERIFY( std::equal(c3.begin(), c3.end(), c2.begin(), c2.end()) ); |
| 125 | |
| 126 | c3 = con3; |
| 127 | c2 = con2; |
| 128 | VERIFY( std::equal(c3.begin(), c3.end(), c2.begin(), c2.end(), eq) ); |
| 129 | VERIFY( equal_to::count == 2 ); |
| 130 | equal_to::count = 0; |
| 131 | |
| 132 | c3 = con3; |
| 133 | c2 = con2; |
| 134 | VERIFY( std::equal(c2.begin(), c2.end(), c3.begin(), c3.end()) ); |
| 135 | |
| 136 | c3 = con3; |
| 137 | c2 = con2; |
| 138 | VERIFY( std::equal(c2.begin(), c2.end(), c3.begin(), c3.end(), eq) ); |
| 139 | VERIFY( equal_to::count == 2 ); |
| 140 | equal_to::count = 0; |
| 141 | } |
| 142 | |
| 143 | void test5() |
| 144 | { |
| 145 | const Container con3(array3, array3 + 1); |
| 146 | const Container con2(array2, array2 + 2); |
| 147 | |
| 148 | auto c3 = con3; |
| 149 | auto c2 = con2; |
| 150 | VERIFY( !std::equal(c3.begin(), c3.end(), c2.begin(), c2.end()) ); |
| 151 | |
| 152 | c3 = con3; |
| 153 | c2 = con2; |
| 154 | VERIFY( !std::equal(c3.begin(), c3.end(), c2.begin(), c2.end(), eq) ); |
| 155 | VERIFY( equal_to::count == 1 ); |
| 156 | equal_to::count = 0; |
| 157 | |
| 158 | c3 = con3; |
| 159 | c2 = con2; |
| 160 | VERIFY( !std::equal(c2.begin(), c2.end(), c3.begin(), c3.end()) ); |
| 161 | |
| 162 | c3 = con3; |
| 163 | c2 = con2; |
| 164 | VERIFY( !std::equal(c2.begin(), c2.end(), c3.begin(), c3.end(), eq) ); |
| 165 | VERIFY( equal_to::count == 1 ); |
| 166 | equal_to::count = 0; |
| 167 | } |
| 168 | |
| 169 | void test6() |
| 170 | { |
| 171 | RA_Container c3(array3, array3 + 1); |
| 172 | RA_Container c2(array2, array2 + 2); |
| 173 | |
| 174 | VERIFY( !std::equal(c3.begin(), c3.end(), c2.begin(), c2.end()) ); |
| 175 | |
| 176 | VERIFY( !std::equal(c3.begin(), c3.end(), c2.begin(), c2.end(), eq) ); |
| 177 | VERIFY( equal_to::count == 0 ); |
| 178 | |
| 179 | VERIFY( !std::equal(c2.begin(), c2.end(), c3.begin(), c3.end()) ); |
| 180 | |
| 181 | VERIFY( !std::equal(c2.begin(), c2.end(), c3.begin(), c3.end(), eq) ); |
| 182 | VERIFY( equal_to::count == 0 ); |
| 183 | } |
| 184 | |
| 185 | int main() |
| 186 | { |
| 187 | test1(); |
| 188 | test2(); |
| 189 | test3(); |
| 190 | test4(); |
| 191 | test5(); |
| 192 | test6(); |
| 193 | } |