xref: /llvm-project/libcxx/test/std/containers/sequences/vector/access.pass.cpp (revision e3dd9f7e66fec22986605da2dcd8120a7864455d)
15bcca9ffSMarshall Clow //===----------------------------------------------------------------------===//
25bcca9ffSMarshall Clow //
35bcca9ffSMarshall Clow // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45bcca9ffSMarshall Clow // See https://llvm.org/LICENSE.txt for license information.
55bcca9ffSMarshall Clow // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65bcca9ffSMarshall Clow //
75bcca9ffSMarshall Clow //===----------------------------------------------------------------------===//
85bcca9ffSMarshall Clow 
95bcca9ffSMarshall Clow // <vector>
105bcca9ffSMarshall Clow 
115bcca9ffSMarshall Clow //       reference operator[](size_type __i);
125bcca9ffSMarshall Clow // const_reference operator[](size_type __i) const;
135bcca9ffSMarshall Clow //
145bcca9ffSMarshall Clow //       reference at(size_type __i);
155bcca9ffSMarshall Clow // const_reference at(size_type __i) const;
165bcca9ffSMarshall Clow //
175bcca9ffSMarshall Clow //       reference front();
185bcca9ffSMarshall Clow // const_reference front() const;
195bcca9ffSMarshall Clow //
205bcca9ffSMarshall Clow //       reference back();
215bcca9ffSMarshall Clow // const_reference back() const;
225bcca9ffSMarshall Clow // libc++ marks these as 'noexcept' (except 'at')
235bcca9ffSMarshall Clow 
245bcca9ffSMarshall Clow #include <vector>
255bcca9ffSMarshall Clow #include <cassert>
26cb71d77cSCasey Carter #include <stdexcept>
275bcca9ffSMarshall Clow 
285bcca9ffSMarshall Clow #include "min_allocator.h"
295bcca9ffSMarshall Clow #include "test_macros.h"
305bcca9ffSMarshall Clow 
315bcca9ffSMarshall Clow template <class C>
make(int size,int start)3298d3d5b5SNikolas Klauser TEST_CONSTEXPR_CXX20 C make(int size, int start) {
335bcca9ffSMarshall Clow     C c;
345bcca9ffSMarshall Clow     for (int i = 0; i < size; ++i)
355bcca9ffSMarshall Clow         c.push_back(start + i);
365bcca9ffSMarshall Clow     return c;
375bcca9ffSMarshall Clow }
385bcca9ffSMarshall Clow 
398c6b2489SKonstantin Boyarinov template <class Vector>
test_get_basic(Vector & c,int start_value)4098d3d5b5SNikolas Klauser TEST_CONSTEXPR_CXX20 void test_get_basic(Vector& c, int start_value) {
41cb71d77cSCasey Carter     const int n = static_cast<int>(c.size());
428c6b2489SKonstantin Boyarinov     for (int i = 0; i < n; ++i)
438c6b2489SKonstantin Boyarinov         assert(c[i] == start_value + i);
448c6b2489SKonstantin Boyarinov     for (int i = 0; i < n; ++i)
458c6b2489SKonstantin Boyarinov         assert(c.at(i) == start_value + i);
468c6b2489SKonstantin Boyarinov 
478c6b2489SKonstantin Boyarinov #ifndef TEST_HAS_NO_EXCEPTIONS
4898d3d5b5SNikolas Klauser     if (!TEST_IS_CONSTANT_EVALUATED) {
498c6b2489SKonstantin Boyarinov         try {
50cb71d77cSCasey Carter             TEST_IGNORE_NODISCARD c.at(n);
518c6b2489SKonstantin Boyarinov             assert(false);
528c6b2489SKonstantin Boyarinov         } catch (const std::out_of_range&) {}
5398d3d5b5SNikolas Klauser     }
548c6b2489SKonstantin Boyarinov #endif
558c6b2489SKonstantin Boyarinov 
568c6b2489SKonstantin Boyarinov     assert(c.front() == start_value);
578c6b2489SKonstantin Boyarinov     assert(c.back() == start_value + n - 1);
588c6b2489SKonstantin Boyarinov }
598c6b2489SKonstantin Boyarinov 
608c6b2489SKonstantin Boyarinov template <class Vector>
test_get()6198d3d5b5SNikolas Klauser TEST_CONSTEXPR_CXX20 void test_get() {
628c6b2489SKonstantin Boyarinov     int start_value = 35;
638c6b2489SKonstantin Boyarinov     Vector c = make<Vector>(10, start_value);
648c6b2489SKonstantin Boyarinov     const Vector& cc = c;
658c6b2489SKonstantin Boyarinov     test_get_basic(c, start_value);
668c6b2489SKonstantin Boyarinov     test_get_basic(cc, start_value);
678c6b2489SKonstantin Boyarinov }
688c6b2489SKonstantin Boyarinov 
698c6b2489SKonstantin Boyarinov template <class Vector>
test_set()7098d3d5b5SNikolas Klauser TEST_CONSTEXPR_CXX20 void test_set() {
718c6b2489SKonstantin Boyarinov     int start_value = 35;
728c6b2489SKonstantin Boyarinov     const int n = 10;
738c6b2489SKonstantin Boyarinov     Vector c = make<Vector>(n, start_value);
748c6b2489SKonstantin Boyarinov 
758c6b2489SKonstantin Boyarinov     for (int i = 0; i < n; ++i) {
768c6b2489SKonstantin Boyarinov         assert(c[i] == start_value + i);
778c6b2489SKonstantin Boyarinov         c[i] = start_value + i + 1;
788c6b2489SKonstantin Boyarinov         assert(c[i] == start_value + i + 1);
798c6b2489SKonstantin Boyarinov     }
808c6b2489SKonstantin Boyarinov     for (int i = 0; i < n; ++i) {
818c6b2489SKonstantin Boyarinov         assert(c.at(i) == start_value + i + 1);
828c6b2489SKonstantin Boyarinov         c.at(i) = start_value + i + 2;
838c6b2489SKonstantin Boyarinov         assert(c.at(i) == start_value + i + 2);
848c6b2489SKonstantin Boyarinov     }
858c6b2489SKonstantin Boyarinov 
868c6b2489SKonstantin Boyarinov     assert(c.front() == start_value + 2);
878c6b2489SKonstantin Boyarinov     c.front() = start_value + 3;
888c6b2489SKonstantin Boyarinov     assert(c.front() == start_value + 3);
898c6b2489SKonstantin Boyarinov 
908c6b2489SKonstantin Boyarinov     assert(c.back() == start_value + n + 1);
918c6b2489SKonstantin Boyarinov     c.back() = start_value + n + 2;
928c6b2489SKonstantin Boyarinov     assert(c.back() == start_value + n + 2);
938c6b2489SKonstantin Boyarinov }
948c6b2489SKonstantin Boyarinov 
958c6b2489SKonstantin Boyarinov template <class Vector>
test()9698d3d5b5SNikolas Klauser TEST_CONSTEXPR_CXX20 void test() {
978c6b2489SKonstantin Boyarinov     test_get<Vector>();
988c6b2489SKonstantin Boyarinov     test_set<Vector>();
998c6b2489SKonstantin Boyarinov 
1008c6b2489SKonstantin Boyarinov     Vector c;
1018c6b2489SKonstantin Boyarinov     const Vector& cc = c;
1028c6b2489SKonstantin Boyarinov     ASSERT_SAME_TYPE(typename Vector::reference, decltype(c[0]));
1038c6b2489SKonstantin Boyarinov     ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc[0]));
1048c6b2489SKonstantin Boyarinov 
1058c6b2489SKonstantin Boyarinov     ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.at(0)));
1068c6b2489SKonstantin Boyarinov     ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.at(0)));
1078c6b2489SKonstantin Boyarinov 
1088c6b2489SKonstantin Boyarinov     ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.front()));
1098c6b2489SKonstantin Boyarinov     ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.front()));
1108c6b2489SKonstantin Boyarinov 
1118c6b2489SKonstantin Boyarinov     ASSERT_SAME_TYPE(typename Vector::reference, decltype(c.back()));
1128c6b2489SKonstantin Boyarinov     ASSERT_SAME_TYPE(typename Vector::const_reference, decltype(cc.back()));
1138c6b2489SKonstantin Boyarinov }
1148c6b2489SKonstantin Boyarinov 
tests()11598d3d5b5SNikolas Klauser TEST_CONSTEXPR_CXX20 bool tests() {
1168c6b2489SKonstantin Boyarinov     test<std::vector<int> >();
1175bcca9ffSMarshall Clow #if TEST_STD_VER >= 11
1188c6b2489SKonstantin Boyarinov     test<std::vector<int, min_allocator<int> > >();
119*e3dd9f7eSAdvenam Tacet     test<std::vector<int, safe_allocator<int> > >();
1205bcca9ffSMarshall Clow #endif
12198d3d5b5SNikolas Klauser     return true;
12298d3d5b5SNikolas Klauser }
1235bcca9ffSMarshall Clow 
main(int,char **)12498d3d5b5SNikolas Klauser int main(int, char**) {
12598d3d5b5SNikolas Klauser     tests();
12698d3d5b5SNikolas Klauser #if TEST_STD_VER > 17
12798d3d5b5SNikolas Klauser     static_assert(tests());
12898d3d5b5SNikolas Klauser #endif
1295bcca9ffSMarshall Clow     return 0;
1305bcca9ffSMarshall Clow }
131