xref: /llvm-project/libcxx/test/std/containers/sequences/array/indexing.pass.cpp (revision 2fa901c4716fb306d921da163631bc235b3beca8)
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 // <array>
10 
11 // reference operator[] (size_type)
12 // const_reference operator[] (size_type); // constexpr in C++14
13 // reference at (size_type)
14 // const_reference at (size_type); // constexpr in C++14
15 // Libc++ marks these as noexcept
16 
17 #include <array>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 
22 // std::array is explicitly allowed to be initialized with A a = { init-list };.
23 // Disable the missing braces warning for this reason.
24 #include "disable_missing_braces_warning.h"
25 
26 #if TEST_STD_VER > 14
27 constexpr bool check_idx( size_t idx, double val )
28 {
29     std::array<double, 3> arr = {1, 2, 3.5};
30     return arr[idx] == val;
31 }
32 #endif
33 
34 int main(int, char**)
35 {
36     {
37         typedef double T;
38         typedef std::array<T, 3> C;
39         C c = {1, 2, 3.5};
40         LIBCPP_ASSERT_NOEXCEPT(c[0]);
41         ASSERT_SAME_TYPE(C::reference, decltype(c[0]));
42         C::reference r1 = c[0];
43         assert(r1 == 1);
44         r1 = 5.5;
45         assert(c.front() == 5.5);
46 
47         C::reference r2 = c[2];
48         assert(r2 == 3.5);
49         r2 = 7.5;
50         assert(c.back() == 7.5);
51     }
52     {
53         typedef double T;
54         typedef std::array<T, 3> C;
55         const C c = {1, 2, 3.5};
56         LIBCPP_ASSERT_NOEXCEPT(c[0]);
57         ASSERT_SAME_TYPE(C::const_reference, decltype(c[0]));
58         C::const_reference r1 = c[0];
59         assert(r1 == 1);
60         C::const_reference r2 = c[2];
61         assert(r2 == 3.5);
62     }
63     { // Test operator[] "works" on zero sized arrays
64         typedef double T;
65         typedef std::array<T, 0> C;
66         C c = {};
67         C const& cc = c;
68         LIBCPP_ASSERT_NOEXCEPT(c[0]);
69         LIBCPP_ASSERT_NOEXCEPT(cc[0]);
70         ASSERT_SAME_TYPE(C::reference, decltype(c[0]));
71         ASSERT_SAME_TYPE(C::const_reference, decltype(cc[0]));
72         if (c.size() > (0)) { // always false
73           C::reference r1 = c[0];
74           C::const_reference r2 = cc[0];
75           ((void)r1);
76           ((void)r2);
77         }
78     }
79     { // Test operator[] "works" on zero sized arrays
80         typedef double T;
81         typedef std::array<const T, 0> C;
82         C c = {{}};
83         C const& cc = c;
84         LIBCPP_ASSERT_NOEXCEPT(c[0]);
85         LIBCPP_ASSERT_NOEXCEPT(cc[0]);
86         ASSERT_SAME_TYPE(C::reference, decltype(c[0]));
87         ASSERT_SAME_TYPE(C::const_reference, decltype(cc[0]));
88         if (c.size() > (0)) { // always false
89           C::reference r1 = c[0];
90           C::const_reference r2 = cc[0];
91           ((void)r1);
92           ((void)r2);
93         }
94     }
95 #if TEST_STD_VER > 11
96     {
97         typedef double T;
98         typedef std::array<T, 3> C;
99         constexpr C c = {1, 2, 3.5};
100         LIBCPP_ASSERT_NOEXCEPT(c[0]);
101         ASSERT_SAME_TYPE(C::const_reference, decltype(c[0]));
102 
103         constexpr T t1 = c[0];
104         static_assert (t1 == 1, "");
105 
106         constexpr T t2 = c[2];
107         static_assert (t2 == 3.5, "");
108     }
109 #endif
110 
111 #if TEST_STD_VER > 14
112     {
113         static_assert (check_idx(0, 1), "");
114         static_assert (check_idx(1, 2), "");
115         static_assert (check_idx(2, 3.5), "");
116     }
117 #endif
118 
119   return 0;
120 }
121