Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 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 |
Marshall Clow | 5f6a5ac | 2019-03-14 21:56:57 +0000 | [diff] [blame] | 15 | // Libc++ marks these as noexcept |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 16 | |
| 17 | #include <array> |
| 18 | #include <cassert> |
| 19 | |
Eric Fiselier | 2decfad | 2015-07-18 23:56:04 +0000 | [diff] [blame] | 20 | #include "test_macros.h" |
| 21 | |
Eric Fiselier | b4e2e7a | 2015-10-01 07:05:38 +0000 | [diff] [blame] | 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" |
Eric Fiselier | 2decfad | 2015-07-18 23:56:04 +0000 | [diff] [blame] | 25 | |
Marshall Clow | e782695 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 26 | #if TEST_STD_VER > 14 |
| 27 | constexpr bool check_idx( size_t idx, double val ) |
Stephan T. Lavavej | a730ed3 | 2017-01-18 20:10:25 +0000 | [diff] [blame] | 28 | { |
Marshall Clow | e782695 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 29 | std::array<double, 3> arr = {1, 2, 3.5}; |
Stephan T. Lavavej | 4159db7 | 2017-07-29 00:55:10 +0000 | [diff] [blame] | 30 | return arr[idx] == val; |
Marshall Clow | e782695 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 31 | } |
| 32 | #endif |
| 33 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 34 | int main(int, char**) |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 35 | { |
| 36 | { |
| 37 | typedef double T; |
| 38 | typedef std::array<T, 3> C; |
| 39 | C c = {1, 2, 3.5}; |
Marshall Clow | 5f6a5ac | 2019-03-14 21:56:57 +0000 | [diff] [blame] | 40 | LIBCPP_ASSERT_NOEXCEPT(c[0]); |
| 41 | ASSERT_SAME_TYPE(C::reference, decltype(c[0])); |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 42 | C::reference r1 = c[0]; |
| 43 | assert(r1 == 1); |
| 44 | r1 = 5.5; |
| 45 | assert(c.front() == 5.5); |
Eric Fiselier | d04c685 | 2016-06-01 21:35:39 +0000 | [diff] [blame] | 46 | |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 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}; |
Marshall Clow | 5f6a5ac | 2019-03-14 21:56:57 +0000 | [diff] [blame] | 56 | LIBCPP_ASSERT_NOEXCEPT(c[0]); |
| 57 | ASSERT_SAME_TYPE(C::const_reference, decltype(c[0])); |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 58 | C::const_reference r1 = c[0]; |
| 59 | assert(r1 == 1); |
| 60 | C::const_reference r2 = c[2]; |
| 61 | assert(r2 == 3.5); |
| 62 | } |
Eric Fiselier | 59cdf90 | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 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; |
Marshall Clow | 2fa901c | 2019-03-15 15:00:41 +0000 | [diff] [blame] | 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])); |
Eric Fiselier | 59cdf90 | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 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; |
Marshall Clow | 2fa901c | 2019-03-15 15:00:41 +0000 | [diff] [blame] | 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])); |
Eric Fiselier | 59cdf90 | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 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 | } |
Eric Fiselier | 2decfad | 2015-07-18 23:56:04 +0000 | [diff] [blame] | 95 | #if TEST_STD_VER > 11 |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 96 | { |
| 97 | typedef double T; |
| 98 | typedef std::array<T, 3> C; |
| 99 | constexpr C c = {1, 2, 3.5}; |
Marshall Clow | 5f6a5ac | 2019-03-14 21:56:57 +0000 | [diff] [blame] | 100 | LIBCPP_ASSERT_NOEXCEPT(c[0]); |
| 101 | ASSERT_SAME_TYPE(C::const_reference, decltype(c[0])); |
Eric Fiselier | 2decfad | 2015-07-18 23:56:04 +0000 | [diff] [blame] | 102 | |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 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 | |
Marshall Clow | e782695 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 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 |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 118 | |
| 119 | return 0; |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 120 | } |