| 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 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // <vector> |
| 10 | |
| 11 | // reference operator[](size_type __i); |
| 12 | // const_reference operator[](size_type __i) const; |
| 13 | // |
| 14 | // reference at(size_type __i); |
| 15 | // const_reference at(size_type __i) const; |
| 16 | // |
| 17 | // reference front(); |
| 18 | // const_reference front() const; |
| 19 | // |
| 20 | // reference back(); |
| 21 | // const_reference back() const; |
| 22 | // libc++ marks these as 'noexcept' (except 'at') |
| 23 | |
| 24 | #include <vector> |
| 25 | #include <cassert> |
| 26 | #include <stdexcept> |
| 27 | |
| 28 | #include "min_allocator.h" |
| 29 | #include "test_macros.h" |
| 30 | |
| 31 | template <class C> |
| 32 | TEST_CONSTEXPR_CXX20 C make(int size, int start) { |
| 33 | C c; |
| 34 | for (int i = 0; i < size; ++i) |
| 35 | c.push_back(start + i); |
| 36 | return c; |
| 37 | } |
| 38 | |
| 39 | template <class Vector> |
| 40 | TEST_CONSTEXPR_CXX20 void test_get_basic(Vector& c, int start_value) { |
| 41 | const int n = static_cast<int>(c.size()); |
| 42 | for (int i = 0; i < n; ++i) |
| 43 | assert(c[i] == start_value + i); |
| 44 | for (int i = 0; i < n; ++i) |
| 45 | assert(c.at(i) == start_value + i); |
| 46 | |
| 47 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 48 | if (!TEST_IS_CONSTANT_EVALUATED) { |
| 49 | try { |
| 50 | TEST_IGNORE_NODISCARD c.at(n); |
| 51 | assert(false); |
| 52 | } catch (const std::out_of_range&) { |
| 53 | } |
| 54 | } |
| 55 | #endif |
| 56 | |
| 57 | assert(c.front() == start_value); |
| 58 | assert(c.back() == start_value + n - 1); |
| 59 | } |
| 60 | |
| 61 | template <class Vector> |
| 62 | TEST_CONSTEXPR_CXX20 void test_get() { |
| 63 | int start_value = 35; |
| 64 | Vector c = make<Vector>(10, start_value); |
| 65 | const Vector& cc = c; |
| 66 | test_get_basic(c, start_value); |
| 67 | test_get_basic(cc, start_value); |
| 68 | } |
| 69 | |
| 70 | template <class Vector> |
| 71 | TEST_CONSTEXPR_CXX20 void test_set() { |
| 72 | int start_value = 35; |
| 73 | const int n = 10; |
| 74 | Vector c = make<Vector>(n, start_value); |
| 75 | |
| 76 | for (int i = 0; i < n; ++i) { |
| 77 | assert(c[i] == start_value + i); |
| 78 | c[i] = start_value + i + 1; |
| 79 | assert(c[i] == start_value + i + 1); |
| 80 | } |
| 81 | for (int i = 0; i < n; ++i) { |
| 82 | assert(c.at(i) == start_value + i + 1); |
| 83 | c.at(i) = start_value + i + 2; |
| 84 | assert(c.at(i) == start_value + i + 2); |
| 85 | } |
| 86 | |
| 87 | assert(c.front() == start_value + 2); |
| 88 | c.front() = start_value + 3; |
| 89 | assert(c.front() == start_value + 3); |
| 90 | |
| 91 | assert(c.back() == start_value + n + 1); |
| 92 | c.back() = start_value + n + 2; |
| 93 | assert(c.back() == start_value + n + 2); |
| 94 | } |
| 95 | |
| 96 | template <class Vector> |
| 97 | TEST_CONSTEXPR_CXX20 void test() { |
| 98 | test_get<Vector>(); |
| 99 | test_set<Vector>(); |
| 100 | |
| 101 | Vector c; |
| 102 | const Vector& cc = c; |
| 103 | ASSERT_SAME_TYPE(typename Vector::reference, decltype(c[0])); |
| 104 | ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc[0])); |
| 105 | |
| 106 | ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.at(0))); |
| 107 | ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.at(0))); |
| 108 | |
| 109 | ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.front())); |
| 110 | ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.front())); |
| 111 | |
| 112 | ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.back())); |
| 113 | ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.back())); |
| 114 | } |
| 115 | |
| 116 | TEST_CONSTEXPR_CXX20 bool tests() { |
| 117 | test<std::vector<int> >(); |
| 118 | #if TEST_STD_VER >= 11 |
| 119 | test<std::vector<int, min_allocator<int> > >(); |
| 120 | test<std::vector<int, safe_allocator<int> > >(); |
| 121 | #endif |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | int main(int, char**) { |
| 126 | tests(); |
| 127 | #if TEST_STD_VER > 17 |
| 128 | static_assert(tests()); |
| 129 | #endif |
| 130 | return 0; |
| 131 | } |
| 132 | |