File: eq.pass.cpp

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (97 lines) | stat: -rw-r--r-- 3,691 bytes parent folder | download
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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-has-no-incomplete-ranges

// template<bool OtherConst>
//   requires sentinel_for<sentinel_t<Base>, iterator_t<maybe-const<OtherConst, V>>>
// friend constexpr bool operator==(const iterator<OtherConst>& x, const sentinel& y);

#include <cassert>
#include <concepts>
#include <functional>
#include <ranges>

#include "../types.h"

template <class Iter, class Sent>
concept EqualityComparable = std::invocable<std::equal_to<>, const Iter&, const Sent&> ;

using Iterator = random_access_iterator<BufferView<int*>*>;
using ConstIterator = random_access_iterator<const BufferView<int*>*>;

template <bool Const>
struct ConstComparableSentinel {

  using Iter = std::conditional_t<Const, ConstIterator, Iterator>;
  Iter iter_;

  explicit ConstComparableSentinel() = default;
  constexpr explicit ConstComparableSentinel(const Iter& it) : iter_(it) {}

  constexpr friend bool operator==(const Iterator& i, const ConstComparableSentinel& s) {
    return base(i) == base(s.iter_);
  }

  constexpr friend bool operator==(const ConstIterator& i, const ConstComparableSentinel& s) {
    return base(i) == base(s.iter_);
  }
};

struct ConstComparableView : BufferView<BufferView<int*>*> {
  using BufferView<BufferView<int*>*>::BufferView;

  constexpr auto begin() { return Iterator(data_); }
  constexpr auto begin() const { return ConstIterator(data_); }
  constexpr auto end() { return ConstComparableSentinel<false>(Iterator(data_ + size_)); }
  constexpr auto end() const { return ConstComparableSentinel<true>(ConstIterator(data_ + size_)); }
};

static_assert(EqualityComparable<std::ranges::iterator_t<ConstComparableView>,
                                 std::ranges::sentinel_t<const ConstComparableView>>);
static_assert(EqualityComparable<std::ranges::iterator_t<const ConstComparableView>,
                                 std::ranges::sentinel_t<ConstComparableView>>);

constexpr bool test() {
  int buffer[4][4] = {{1111, 2222, 3333, 4444}, {555, 666, 777, 888}, {99, 1010, 1111, 1212}, {13, 14, 15, 16}};

  {
    ChildView children[4] = {ChildView(buffer[0]), ChildView(buffer[1]), ChildView(buffer[2]), ChildView(buffer[3])};
    auto jv = std::ranges::join_view(ParentView(children));
    assert(jv.end() == std::ranges::next(jv.begin(), 16));
    static_assert(!EqualityComparable<decltype(std::as_const(jv).begin()), decltype(jv.end())>);
    static_assert(!EqualityComparable<decltype(jv.begin()), decltype(std::as_const(jv).end())>);
  }

  {
    CopyableChild children[4] = {CopyableChild(buffer[0]), CopyableChild(buffer[1]), CopyableChild(buffer[2]),
                                 CopyableChild(buffer[3])};
    const auto jv = std::ranges::join_view(ParentView(children));
    assert(jv.end() == std::ranges::next(jv.begin(), 16));
  }

  // test iterator<Const> == sentinel<!Const>
  {
    BufferView<int*> inners[] = {buffer[0], buffer[1]};
    ConstComparableView outer(inners);
    auto jv = std::ranges::join_view(outer);
    assert(jv.end() == std::ranges::next(jv.begin(), 8));
    assert(std::as_const(jv).end() == std::ranges::next(jv.begin(), 8));
    assert(jv.end() == std::ranges::next(std::as_const(jv).begin(), 8));
  }

  return true;
}

int main(int, char**) {
  test();
  static_assert(test());

  return 0;
}