File: ends_with.cpp

package info (click to toggle)
range-v3 0.12.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,620 kB
  • sloc: cpp: 76,839; xml: 226; sh: 89; python: 34; makefile: 16; perl: 15
file content (111 lines) | stat: -rw-r--r-- 4,804 bytes parent folder | download | duplicates (3)
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
// Range v3 library
//
//  Copyright Johel Guerrero 2019
//
//  Use, modification and distribution is subject to the
//  Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ericniebler/range-v3
//

#include <initializer_list>
#include <range/v3/algorithm/ends_with.hpp>
#include <range/v3/iterator/operations.hpp>
#include <range/v3/view/subrange.hpp>
#include "../simple_test.hpp"
#include "../test_iterators.hpp"

int comparison_count = 0;

template<typename T>
bool counting_equals(const T &a, const T &b)
{
    ++comparison_count;
    return a == b;
}

int main()
{
    using namespace ranges;
    int ia[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    constexpr auto as = distance(ia);
    int ib[] = {5, 6, 7, 8, 9};
    constexpr auto bs = distance(ib);
    CHECK(ends_with(RandomAccessIterator<const int*>(ia),
                     RandomAccessIterator<const int*>(ia + as),
                     RandomAccessIterator<const int*>(ib),
                     RandomAccessIterator<const int*>(ib + bs)));
    CHECK(!ends_with(InputIterator<const int*>(ia),
                      InputIterator<const int*, true>(ia + as),
                      InputIterator<const int*>(ib),
                      InputIterator<const int*, true>(ib + bs - 1)));
    CHECK(!ends_with(ForwardIterator<const int*>(ia),
                      Sentinel<const int*>(ia + as),
                      ForwardIterator<const int*>(ib),
                      Sentinel<const int*>(ib + bs - 1)));
    CHECK(!ends_with(make_subrange(RandomAccessIterator<const int*>(ia),
                      RandomAccessIterator<const int*>(ia + as)),
                      make_subrange(RandomAccessIterator<const int*>(ib),
                      RandomAccessIterator<const int*>(ib + bs - 1))));
    CHECK(ends_with(make_subrange(InputIterator<const int*>(ia),
                     InputIterator<const int*, true>(ia + as)),
                     make_subrange(InputIterator<const int*>(ib),
                     InputIterator<const int*, true>(ib + bs))));
    CHECK(ends_with(make_subrange(ForwardIterator<const int*>(ia),
                     Sentinel<const int*>(ia + as)),
                     make_subrange(ForwardIterator<const int*>(ib),
                     Sentinel<const int*>(ib + bs))));
    comparison_count = 0;
    CHECK(!ends_with(RandomAccessIterator<const int*>(ib),
                      RandomAccessIterator<const int*>(ib + bs),
                      RandomAccessIterator<const int*>(ia),
                      RandomAccessIterator<const int*>(ia + as),
                      counting_equals<int>));
    CHECK(comparison_count == 0);
    comparison_count = 0;
    CHECK(ends_with(InputIterator<const int*>(ia),
                     InputIterator<const int*, true>(ia + as),
                     InputIterator<const int*>(ib),
                     InputIterator<const int*, true>(ib + bs),
                     counting_equals<int>));
    CHECK(comparison_count > 0);
    comparison_count = 0;
    CHECK(ends_with(ForwardIterator<const int*>(ia),
                     Sentinel<const int*>(ia + as),
                     ForwardIterator<const int*>(ib),
                     Sentinel<const int*>(ib + bs),
                     counting_equals<int>));
    CHECK(comparison_count > 0);
    comparison_count = 0;
    CHECK(ends_with(make_subrange(RandomAccessIterator<const int*>(ia),
                     RandomAccessIterator<const int*>(ia + as)),
                     make_subrange(RandomAccessIterator<const int*>(ib),
                     RandomAccessIterator<const int*>(ib + bs)),
                     counting_equals<int>));
    CHECK(comparison_count > 0);
    comparison_count = 0;
    CHECK(!ends_with(make_subrange(InputIterator<const int*>(ib),
                      InputIterator<const int*, true>(ib + bs - 1)),
                      make_subrange(InputIterator<const int*>(ib),
                      InputIterator<const int*, true>(ib + bs)),
                      counting_equals<int>));
    CHECK(comparison_count == 0);
    comparison_count = 0;
    CHECK(!ends_with(make_subrange(ForwardIterator<const int*>(ia),
                      Sentinel<const int*>(ia)),
                      make_subrange(ForwardIterator<const int*>(ib),
                      Sentinel<const int*>(ib + bs)),
                      counting_equals<int>));
    CHECK(comparison_count == 0);

#if RANGES_CXX_CONSTEXPR >= RANGES_CXX_CONSTEXPR_14 && RANGES_CONSTEXPR_INVOKE
    using IL = std::initializer_list<int>;
    static_assert(ends_with(IL{0, 1, 2, 3, 4}, IL{3, 4}), "");
    static_assert(!ends_with(IL{0, 1, 2, 3, 4}, IL{2, 3}), "");
    static_assert(ends_with(IL{}, IL{}), "");
#endif

    return ::test_result();
}