Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / test / std / iterators / predef.iterators / iterators.common / assign.pass.cpp
blob18e717687ce190217b024ca49ad669e9c09c8c36
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
11 // template<class I2, class S2>
12 // requires convertible_to<const I2&, I> && convertible_to<const S2&, S> &&
13 // assignable_from<I&, const I2&> && assignable_from<S&, const S2&>
14 // common_iterator& operator=(const common_iterator<I2, S2>& x);
16 #include <iterator>
17 #include <ranges>
18 #include <cassert>
20 #include "test_macros.h"
21 #include "types.h"
23 void test() {
24 int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
27 auto iter1 = cpp17_input_iterator<int*>(buffer);
28 auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1);
29 auto commonIter2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(cpp17_input_iterator<int*>(buffer + 1));
31 assert(*commonIter1 == 1);
32 assert(*commonIter2 == 2);
33 assert(commonIter1 != commonIter2);
35 commonIter1 = commonIter2;
37 assert(*commonIter1 == 2);
38 assert(*commonIter2 == 2);
39 assert(commonIter1 == commonIter2);
42 auto iter1 = forward_iterator<int*>(buffer);
43 auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1);
44 auto commonIter2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(forward_iterator<int*>(buffer + 1));
46 assert(*commonIter1 == 1);
47 assert(*commonIter2 == 2);
48 assert(commonIter1 != commonIter2);
50 commonIter1 = commonIter2;
52 assert(*commonIter1 == 2);
53 assert(*commonIter2 == 2);
54 assert(commonIter1 == commonIter2);
57 auto iter1 = random_access_iterator<int*>(buffer);
58 auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1);
59 auto commonSent1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8});
61 auto commonIter2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1 + 1);
62 auto commonSent2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 7});
64 assert(*commonIter1 == 1);
65 assert(*commonIter2 == 2);
66 assert(commonIter1 != commonIter2);
68 commonIter1 = commonIter2;
70 assert(*commonIter1 == 2);
71 assert(*commonIter2 == 2);
72 assert(commonIter1 == commonIter2);
74 assert(std::next(commonIter1, 6) != commonSent1);
75 assert(std::next(commonIter1, 6) == commonSent2);
77 commonSent1 = commonSent2;
79 assert(std::next(commonIter1, 6) == commonSent1);
80 assert(std::next(commonIter1, 6) == commonSent2);
83 auto iter1 = assignable_iterator<int*>(buffer);
84 auto iter2 = forward_iterator<int*>(buffer + 1);
85 auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1);
86 auto commonSent1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8});
88 auto commonIter2 = std::common_iterator<decltype(iter2), sentinel_type<int*>>(iter2);
89 auto commonSent2 = std::common_iterator<decltype(iter2), sentinel_type<int*>>(sentinel_type<int*>{buffer + 7});
91 assert(*commonIter1 == 1);
92 assert(*commonIter2 == 2);
94 commonIter1 = commonIter2;
96 assert(*commonIter1 == 2);
97 assert(*commonIter2 == 2);
98 assert(commonIter1 == commonIter2);
100 assert(std::next(commonIter1, 6) != commonSent1);
101 assert(std::next(commonIter1, 6) == commonSent2);
103 commonSent1 = commonSent2;
105 assert(std::next(commonIter1, 6) == commonSent1);
106 assert(std::next(commonIter1, 6) == commonSent2);
108 commonIter1 = commonSent1;
110 assert(commonIter1 == commonSent2);
112 commonIter1 = commonSent2;
114 assert(commonIter1 == commonSent2);
116 #ifndef TEST_HAS_NO_EXCEPTIONS
118 auto iter1 = maybe_valueless_iterator<int*>(buffer);
119 auto iter2 = forward_iterator<int*>(buffer);
120 auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1);
121 auto commonSent2 = std::common_iterator<decltype(iter1),
122 sentinel_throws_on_convert<int*>>(sentinel_throws_on_convert<int*>{buffer + 8});
123 auto commonIter2 = std::common_iterator<decltype(iter2), sentinel_type<int*>>(iter2);
125 try {
126 commonIter1 = commonSent2;
127 assert(false);
128 } catch (int x) {
129 assert(x == 42);
130 commonIter1 = commonIter2;
133 assert(*commonIter1 == 1);
135 #endif // TEST_HAS_NO_EXCEPTIONS
138 int main(int, char**) {
139 test();
141 return 0;