| 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); // constexpr in C++17 |
| 12 | // Libc++ marks it as noexcept |
| 13 | |
| 14 | #include <array> |
| 15 | #include <cassert> |
| 16 | |
| 17 | #include "test_macros.h" |
| 18 | |
| 19 | TEST_CONSTEXPR_CXX17 bool tests() { |
| 20 | { |
| 21 | typedef double T; |
| 22 | typedef std::array<T, 3> C; |
| 23 | C c = {1, 2, 3.5}; |
| 24 | LIBCPP_ASSERT_NOEXCEPT(c[0]); |
| 25 | ASSERT_SAME_TYPE(C::reference, decltype(c[0])); |
| 26 | C::reference r1 = c[0]; |
| 27 | assert(r1 == 1); |
| 28 | r1 = 5.5; |
| 29 | assert(c.front() == 5.5); |
| 30 | |
| 31 | C::reference r2 = c[2]; |
| 32 | assert(r2 == 3.5); |
| 33 | r2 = 7.5; |
| 34 | assert(c.back() == 7.5); |
| 35 | } |
| 36 | |
| 37 | // Test operator[] "works" on zero sized arrays |
| 38 | { |
| 39 | { |
| 40 | typedef double T; |
| 41 | typedef std::array<T, 0> C; |
| 42 | C c = {}; |
| 43 | LIBCPP_ASSERT_NOEXCEPT(c[0]); |
| 44 | ASSERT_SAME_TYPE(C::reference, decltype(c[0])); |
| 45 | if (c.size() > (0)) { // always false |
| 46 | C::reference r = c[0]; |
| 47 | (void)r; |
| 48 | } |
| 49 | } |
| 50 | { |
| 51 | typedef double T; |
| 52 | typedef std::array<const T, 0> C; |
| 53 | C c = {}; |
| 54 | LIBCPP_ASSERT_NOEXCEPT(c[0]); |
| 55 | ASSERT_SAME_TYPE(C::reference, decltype(c[0])); |
| 56 | if (c.size() > (0)) { // always false |
| 57 | C::reference r = c[0]; |
| 58 | (void)r; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | int main(int, char**) { |
| 67 | tests(); |
| 68 | #if TEST_STD_VER >= 17 |
| 69 | static_assert(tests(), "" ); |
| 70 | #endif |
| 71 | return 0; |
| 72 | } |
| 73 | |