File: access.pass.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (130 lines) | stat: -rw-r--r-- 3,656 bytes parent folder | download | duplicates (8)
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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <vector>

//       reference operator[](size_type __i);
// const_reference operator[](size_type __i) const;
//
//       reference at(size_type __i);
// const_reference at(size_type __i) const;
//
//       reference front();
// const_reference front() const;
//
//       reference back();
// const_reference back() const;
// libc++ marks these as 'noexcept' (except 'at')

#include <vector>
#include <cassert>
#include <stdexcept>

#include "min_allocator.h"
#include "test_macros.h"

template <class C>
TEST_CONSTEXPR_CXX20 C make(int size, int start) {
    C c;
    for (int i = 0; i < size; ++i)
        c.push_back(start + i);
    return c;
}

template <class Vector>
TEST_CONSTEXPR_CXX20 void test_get_basic(Vector& c, int start_value) {
    const int n = static_cast<int>(c.size());
    for (int i = 0; i < n; ++i)
        assert(c[i] == start_value + i);
    for (int i = 0; i < n; ++i)
        assert(c.at(i) == start_value + i);

#ifndef TEST_HAS_NO_EXCEPTIONS
    if (!TEST_IS_CONSTANT_EVALUATED) {
        try {
            TEST_IGNORE_NODISCARD c.at(n);
            assert(false);
        } catch (const std::out_of_range&) {}
    }
#endif

    assert(c.front() == start_value);
    assert(c.back() == start_value + n - 1);
}

template <class Vector>
TEST_CONSTEXPR_CXX20 void test_get() {
    int start_value = 35;
    Vector c = make<Vector>(10, start_value);
    const Vector& cc = c;
    test_get_basic(c, start_value);
    test_get_basic(cc, start_value);
}

template <class Vector>
TEST_CONSTEXPR_CXX20 void test_set() {
    int start_value = 35;
    const int n = 10;
    Vector c = make<Vector>(n, start_value);

    for (int i = 0; i < n; ++i) {
        assert(c[i] == start_value + i);
        c[i] = start_value + i + 1;
        assert(c[i] == start_value + i + 1);
    }
    for (int i = 0; i < n; ++i) {
        assert(c.at(i) == start_value + i + 1);
        c.at(i) = start_value + i + 2;
        assert(c.at(i) == start_value + i + 2);
    }

    assert(c.front() == start_value + 2);
    c.front() = start_value + 3;
    assert(c.front() == start_value + 3);

    assert(c.back() == start_value + n + 1);
    c.back() = start_value + n + 2;
    assert(c.back() == start_value + n + 2);
}

template <class Vector>
TEST_CONSTEXPR_CXX20 void test() {
    test_get<Vector>();
    test_set<Vector>();

    Vector c;
    const Vector& cc = c;
    ASSERT_SAME_TYPE(typename Vector::reference, decltype(c[0]));
    ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc[0]));

    ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.at(0)));
    ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.at(0)));

    ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.front()));
    ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.front()));

    ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.back()));
    ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.back()));
}

TEST_CONSTEXPR_CXX20 bool tests() {
    test<std::vector<int> >();
#if TEST_STD_VER >= 11
    test<std::vector<int, min_allocator<int> > >();
    test<std::vector<int, safe_allocator<int> > >();
#endif
    return true;
}

int main(int, char**) {
    tests();
#if TEST_STD_VER > 17
    static_assert(tests());
#endif
    return 0;
}