1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// <algorithm>
// template<InputIterator Iter1, InputIterator Iter2>
// requires HasEqualTo<Iter1::value_type, Iter2::value_type>
// constexpr bool // constexpr after c++17
// equal(Iter1 first1, Iter1 last1, Iter2 first2);
//
// Introduced in C++14:
// template<InputIterator Iter1, InputIterator Iter2>
// constexpr bool // constexpr after c++17
// equal(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2);
// We test the cartesian product, so we sometimes compare differently signed types
// ADDITIONAL_COMPILE_FLAGS(gcc-style-warnings): -Wno-sign-compare
// ADDITIONAL_COMPILE_FLAGS(character-conversion-warnings): -Wno-character-conversion
// MSVC warning C4242: 'argument': conversion from 'int' to 'const _Ty', possible loss of data
// MSVC warning C4244: 'argument': conversion from 'wchar_t' to 'const _Ty', possible loss of data
// MSVC warning C4389: '==': signed/unsigned mismatch
// ADDITIONAL_COMPILE_FLAGS(cl-style-warnings): /wd4242 /wd4244 /wd4389
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
#include <algorithm>
#include <cassert>
#include <functional>
#include <vector>
#include "sized_allocator.h"
#include "test_iterators.h"
#include "test_macros.h"
#include "type_algorithms.h"
template <class UnderlyingType, class Iter1>
struct Test {
template <class Iter2>
TEST_CONSTEXPR_CXX20 void operator()() {
UnderlyingType a[] = {0, 1, 2, 3, 4, 5};
const unsigned s = sizeof(a) / sizeof(a[0]);
UnderlyingType b[s] = {0, 1, 2, 5, 4, 5};
assert(std::equal(Iter1(a), Iter1(a + s), Iter2(a)));
assert(!std::equal(Iter1(a), Iter1(a + s), Iter2(b)));
#if TEST_STD_VER >= 14
assert(std::equal(Iter1(a), Iter1(a + s), Iter2(a), std::equal_to<>()));
assert(!std::equal(Iter1(a), Iter1(a + s), Iter2(b), std::equal_to<>()));
assert(std::equal(Iter1(a), Iter1(a + s), Iter2(a), Iter2(a + s)));
assert(!std::equal(Iter1(a), Iter1(a + s), Iter2(a), Iter2(a + s - 1)));
assert(!std::equal(Iter1(a), Iter1(a + s), Iter2(b), Iter2(b + s)));
assert(std::equal(Iter1(a), Iter1(a + s), Iter2(a), Iter2(a + s), std::equal_to<>()));
assert(!std::equal(Iter1(a), Iter1(a + s), Iter2(a), Iter2(a + s - 1), std::equal_to<>()));
assert(!std::equal(Iter1(a), Iter1(a + s), Iter2(b), Iter2(b + s), std::equal_to<>()));
#endif
}
};
struct TestNarrowingEqualTo {
template <class UnderlyingType>
TEST_CONSTEXPR_CXX20 void operator()() {
TEST_DIAGNOSTIC_PUSH
// MSVC warning C4310: cast truncates constant value
TEST_MSVC_DIAGNOSTIC_IGNORED(4310)
UnderlyingType a[] = {
UnderlyingType(0x1000),
UnderlyingType(0x1001),
UnderlyingType(0x1002),
UnderlyingType(0x1003),
UnderlyingType(0x1004)};
UnderlyingType b[] = {
UnderlyingType(0x1600),
UnderlyingType(0x1601),
UnderlyingType(0x1602),
UnderlyingType(0x1603),
UnderlyingType(0x1604)};
TEST_DIAGNOSTIC_POP
assert(std::equal(a, a + 5, b, std::equal_to<char>()));
#if TEST_STD_VER >= 14
assert(std::equal(a, a + 5, b, b + 5, std::equal_to<char>()));
#endif
}
};
template <class UnderlyingType, class TypeList>
struct TestIter2 {
template <class Iter1>
TEST_CONSTEXPR_CXX20 void operator()() {
types::for_each(TypeList(), Test<UnderlyingType, Iter1>());
}
};
struct AddressCompare {
int i = 0;
TEST_CONSTEXPR_CXX20 AddressCompare(int) {}
operator char() { return static_cast<char>(i); }
friend TEST_CONSTEXPR_CXX20 bool operator==(const AddressCompare& lhs, const AddressCompare& rhs) {
return &lhs == &rhs;
}
friend TEST_CONSTEXPR_CXX20 bool operator!=(const AddressCompare& lhs, const AddressCompare& rhs) {
return &lhs != &rhs;
}
};
#if TEST_STD_VER >= 20
class trivially_equality_comparable {
public:
constexpr trivially_equality_comparable(int i) : i_(i) {}
bool operator==(const trivially_equality_comparable&) const = default;
private:
int i_;
};
#endif
template <std::size_t N>
TEST_CONSTEXPR_CXX20 void test_vector_bool() {
std::vector<bool> in(N, false);
for (std::size_t i = 0; i < N; i += 2)
in[i] = true;
{ // Test equal() with aligned bytes
std::vector<bool> out = in;
assert(std::equal(in.begin(), in.end(), out.begin()));
#if TEST_STD_VER >= 14
assert(std::equal(in.begin(), in.end(), out.begin(), out.end()));
#endif
}
{ // Test equal() with unaligned bytes
std::vector<bool> out(N + 8);
std::copy(in.begin(), in.end(), out.begin() + 4);
assert(std::equal(in.begin(), in.end(), out.begin() + 4));
#if TEST_STD_VER >= 14
assert(std::equal(in.begin(), in.end(), out.begin() + 4, out.end() - 4));
#endif
}
}
TEST_CONSTEXPR_CXX20 bool test() {
types::for_each(types::cpp17_input_iterator_list<int*>(), TestIter2<int, types::cpp17_input_iterator_list<int*> >());
types::for_each(
types::cpp17_input_iterator_list<char*>(), TestIter2<char, types::cpp17_input_iterator_list<char*> >());
types::for_each(types::cpp17_input_iterator_list<AddressCompare*>(),
TestIter2<AddressCompare, types::cpp17_input_iterator_list<AddressCompare*> >());
types::for_each(types::integral_types(), TestNarrowingEqualTo());
#if TEST_STD_VER >= 20
types::for_each(
types::cpp17_input_iterator_list<trivially_equality_comparable*>{},
TestIter2<trivially_equality_comparable, types::cpp17_input_iterator_list<trivially_equality_comparable*>>{});
#endif
{ // Test vector<bool>::iterator optimization
test_vector_bool<8>();
test_vector_bool<19>();
test_vector_bool<32>();
test_vector_bool<49>();
test_vector_bool<64>();
test_vector_bool<199>();
test_vector_bool<256>();
}
// Make sure std::equal behaves properly with std::vector<bool> iterators with custom size types.
// See issue: https://github.com/llvm/llvm-project/issues/126369.
{
//// Tests for std::equal with aligned bits
{ // Test the first (partial) word for uint8_t
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
std::vector<bool, Alloc> in(6, true, Alloc(1));
std::vector<bool, Alloc> expected(8, true, Alloc(1));
assert(std::equal(in.begin() + 4, in.end(), expected.begin() + 4));
}
{ // Test the last word for uint8_t
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
std::vector<bool, Alloc> in(12, true, Alloc(1));
std::vector<bool, Alloc> expected(16, true, Alloc(1));
assert(std::equal(in.begin(), in.end(), expected.begin()));
}
{ // Test middle words for uint8_t
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
std::vector<bool, Alloc> in(24, true, Alloc(1));
std::vector<bool, Alloc> expected(29, true, Alloc(1));
assert(std::equal(in.begin(), in.end(), expected.begin()));
}
{ // Test the first (partial) word for uint16_t
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
std::vector<bool, Alloc> in(12, true, Alloc(1));
std::vector<bool, Alloc> expected(16, true, Alloc(1));
assert(std::equal(in.begin() + 4, in.end(), expected.begin() + 4));
}
{ // Test the last word for uint16_t
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
std::vector<bool, Alloc> in(24, true, Alloc(1));
std::vector<bool, Alloc> expected(32, true, Alloc(1));
assert(std::equal(in.begin(), in.end(), expected.begin()));
}
{ // Test middle words for uint16_t
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
std::vector<bool, Alloc> in(48, true, Alloc(1));
std::vector<bool, Alloc> expected(55, true, Alloc(1));
assert(std::equal(in.begin(), in.end(), expected.begin()));
}
//// Tests for std::equal with unaligned bits
{ // Test the first (partial) word for uint8_t
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
std::vector<bool, Alloc> in(6, true, Alloc(1));
std::vector<bool, Alloc> expected(8, true, Alloc(1));
assert(std::equal(in.begin() + 4, in.end(), expected.begin()));
}
{ // Test the last word for uint8_t
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
std::vector<bool, Alloc> in(4, true, Alloc(1));
std::vector<bool, Alloc> expected(8, true, Alloc(1));
assert(std::equal(in.begin(), in.end(), expected.begin() + 3));
}
{ // Test middle words for uint8_t
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
std::vector<bool, Alloc> in(16, true, Alloc(1));
std::vector<bool, Alloc> expected(24, true, Alloc(1));
assert(std::equal(in.begin(), in.end(), expected.begin() + 4));
}
{ // Test the first (partial) word for uint16_t
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
std::vector<bool, Alloc> in(12, true, Alloc(1));
std::vector<bool, Alloc> expected(16, true, Alloc(1));
assert(std::equal(in.begin() + 4, in.end(), expected.begin()));
}
{ // Test the last word for uint16_t
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
std::vector<bool, Alloc> in(12, true, Alloc(1));
std::vector<bool, Alloc> expected(16, true, Alloc(1));
assert(std::equal(in.begin(), in.end(), expected.begin() + 3));
}
{ // Test the middle words for uint16_t
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
std::vector<bool, Alloc> in(32, true, Alloc(1));
std::vector<bool, Alloc> expected(64, true, Alloc(1));
assert(std::equal(in.begin(), in.end(), expected.begin() + 4));
}
}
return true;
}
struct Base {};
struct Derived : virtual Base {};
struct TestTypes {
template <class T>
struct Test {
template <class U>
void operator()() {
T a[] = {1, 2, 3, 4, 5, 6};
U b[] = {1, 2, 3, 4, 5, 6};
assert(std::equal(a, a + 6, b));
}
};
template <class T>
void operator()() {
types::for_each(types::integer_types(), Test<T>());
}
};
int main(int, char**) {
test();
#if TEST_STD_VER >= 20
static_assert(test());
#endif
types::for_each(types::integer_types(), TestTypes());
types::for_each(types::as_pointers<types::cv_qualified_versions<int> >(),
TestIter2<int, types::as_pointers<types::cv_qualified_versions<int> > >());
types::for_each(types::as_pointers<types::cv_qualified_versions<char> >(),
TestIter2<char, types::as_pointers<types::cv_qualified_versions<char> > >());
{
Derived d;
Derived* a[] = {&d, nullptr};
Base* b[] = {&d, nullptr};
assert(std::equal(a, a + 2, b));
#if TEST_STD_VER >= 14
assert(std::equal(a, a + 2, b, b + 2));
#endif
}
return 0;
}
|