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 C 33 make(int size, int start) 34 { 35 C c; 36 for (int i = 0; i < size; ++i) 37 c.push_back(start + i); 38 return c; 39 } 40 41 template <class Vector> 42 void test_get_basic(Vector& c, int start_value) { 43 const int n = static_cast<int>(c.size()); 44 for (int i = 0; i < n; ++i) 45 assert(c[i] == start_value + i); 46 for (int i = 0; i < n; ++i) 47 assert(c.at(i) == start_value + i); 48 49 #ifndef TEST_HAS_NO_EXCEPTIONS 50 try { 51 TEST_IGNORE_NODISCARD c.at(n); 52 assert(false); 53 } catch (const std::out_of_range&) {} 54 #endif 55 56 assert(c.front() == start_value); 57 assert(c.back() == start_value + n - 1); 58 } 59 60 template <class Vector> 61 void test_get() { 62 int start_value = 35; 63 Vector c = make<Vector>(10, start_value); 64 const Vector& cc = c; 65 test_get_basic(c, start_value); 66 test_get_basic(cc, start_value); 67 } 68 69 template <class Vector> 70 void test_set() { 71 int start_value = 35; 72 const int n = 10; 73 Vector c = make<Vector>(n, start_value); 74 75 for (int i = 0; i < n; ++i) { 76 assert(c[i] == start_value + i); 77 c[i] = start_value + i + 1; 78 assert(c[i] == start_value + i + 1); 79 } 80 for (int i = 0; i < n; ++i) { 81 assert(c.at(i) == start_value + i + 1); 82 c.at(i) = start_value + i + 2; 83 assert(c.at(i) == start_value + i + 2); 84 } 85 86 assert(c.front() == start_value + 2); 87 c.front() = start_value + 3; 88 assert(c.front() == start_value + 3); 89 90 assert(c.back() == start_value + n + 1); 91 c.back() = start_value + n + 2; 92 assert(c.back() == start_value + n + 2); 93 } 94 95 template <class Vector> 96 void test() { 97 test_get<Vector>(); 98 test_set<Vector>(); 99 100 Vector c; 101 const Vector& cc = c; 102 ASSERT_SAME_TYPE(typename Vector::reference, decltype(c[0])); 103 ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc[0])); 104 105 ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.at(0))); 106 ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.at(0))); 107 108 ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.front())); 109 ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.front())); 110 111 ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.back())); 112 ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.back())); 113 } 114 115 int main(int, char**) 116 { 117 test<std::vector<int> >(); 118 #if TEST_STD_VER >= 11 119 test<std::vector<int, min_allocator<int> > >(); 120 #endif 121 122 return 0; 123 } 124