Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / test / std / iterators / predef.iterators / reverse.iterators / reverse.iter.nonmember / minus.pass.cpp
blob676f6e1b491695b81f5955dcf09376135e83c572
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 // <iterator>
11 // reverse_iterator
13 // template <RandomAccessIterator Iter1, RandomAccessIterator Iter2>
14 // requires HasMinus<Iter2, Iter1>
15 // auto operator-(const reverse_iterator<Iter1>& x, const reverse_iterator<Iter2>& y) // constexpr in C++17
16 // -> decltype(y.base() - x.base());
18 #include <iterator>
19 #include <cstddef>
20 #include <cassert>
21 #include <type_traits>
23 #include "test_macros.h"
24 #include "test_iterators.h"
26 template <class, class, class = void>
27 struct HasMinus : std::false_type {};
28 template <class R1, class R2>
29 struct HasMinus<R1, R2, decltype((R1() - R2(), void()))> : std::true_type {};
31 // Test non-subtractable base iterator types
32 static_assert(HasMinus<std::reverse_iterator<int*>, std::reverse_iterator<int*> >::value, "");
33 static_assert(HasMinus<std::reverse_iterator<int*>, std::reverse_iterator<const int*> >::value, "");
35 #if TEST_STD_VER >= 11
36 static_assert(!HasMinus<std::reverse_iterator<int*>, std::reverse_iterator<char*> >::value, "");
37 static_assert(!HasMinus<std::reverse_iterator<bidirectional_iterator<int*> >,
38 std::reverse_iterator<bidirectional_iterator<int*> > >::value,
39 "");
40 #endif
42 template <class It1, class It2>
43 TEST_CONSTEXPR_CXX17 void test_one(It1 l, It2 r, std::ptrdiff_t x) {
44 const std::reverse_iterator<It1> r1(l);
45 const std::reverse_iterator<It2> r2(r);
46 assert((r1 - r2) == x);
49 template <class Iter>
50 TEST_CONSTEXPR_CXX17 void test() {
51 // Test same base iterator type
52 char s[3] = {0};
54 test_one(Iter(s), Iter(s), 0);
55 test_one(Iter(s), Iter(s + 1), 1);
56 test_one(Iter(s + 1), Iter(s), -1);
59 TEST_CONSTEXPR_CXX17 bool tests() {
61 test<char*>();
62 test<random_access_iterator<char*> >();
63 #if TEST_STD_VER >= 20
64 test<cpp20_random_access_iterator<char*>>();
65 #endif
68 // Test different (but subtractable) base iterator types
69 using PC = const char*;
70 char s[3] = {0};
71 test_one(PC(s), s, 0);
72 test_one(PC(s), s + 1, 1);
73 test_one(PC(s + 1), s, -1);
76 return true;
79 int main(int, char**) {
80 tests();
81 #if TEST_STD_VER > 14
82 static_assert(tests(), "");
83 #endif
84 return 0;