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 |
| 15 | |
| 16 | #include <array> |
| 17 | #include <cassert> |
| 18 | |
Stephan T. Lavavej | 5688f16 | 2019-12-13 18:14:09 -0800 | [diff] [blame] | 19 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 20 | #include <stdexcept> |
| 21 | #endif |
| 22 | |
Eric Fiselier | 2decfad | 2015-07-18 23:56:04 +0000 | [diff] [blame] | 23 | #include "test_macros.h" |
| 24 | |
Eric Fiselier | b4e2e7a | 2015-10-01 07:05:38 +0000 | [diff] [blame] | 25 | // std::array is explicitly allowed to be initialized with A a = { init-list };. |
| 26 | // Disable the missing braces warning for this reason. |
| 27 | #include "disable_missing_braces_warning.h" |
Eric Fiselier | 2decfad | 2015-07-18 23:56:04 +0000 | [diff] [blame] | 28 | |
Marshall Clow | e782695 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 29 | #if TEST_STD_VER > 14 |
| 30 | constexpr bool check_idx( size_t idx, double val ) |
Stephan T. Lavavej | a730ed3 | 2017-01-18 20:10:25 +0000 | [diff] [blame] | 31 | { |
Marshall Clow | e782695 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 32 | std::array<double, 3> arr = {1, 2, 3.5}; |
Stephan T. Lavavej | 4159db7 | 2017-07-29 00:55:10 +0000 | [diff] [blame] | 33 | return arr.at(idx) == val; |
Marshall Clow | e782695 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 34 | } |
| 35 | #endif |
| 36 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 37 | int main(int, char**) |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 38 | { |
| 39 | { |
| 40 | typedef double T; |
| 41 | typedef std::array<T, 3> C; |
| 42 | C c = {1, 2, 3.5}; |
| 43 | C::reference r1 = c.at(0); |
| 44 | assert(r1 == 1); |
| 45 | r1 = 5.5; |
| 46 | assert(c.front() == 5.5); |
Eric Fiselier | 2decfad | 2015-07-18 23:56:04 +0000 | [diff] [blame] | 47 | |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 48 | C::reference r2 = c.at(2); |
| 49 | assert(r2 == 3.5); |
| 50 | r2 = 7.5; |
| 51 | assert(c.back() == 7.5); |
| 52 | |
Roger Ferrer Ibanez | 86663cd | 2016-11-29 17:10:29 +0000 | [diff] [blame] | 53 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 54 | try |
| 55 | { |
Billy Robert O'Neal III | ba40b05 | 2017-11-21 21:37:26 +0000 | [diff] [blame] | 56 | TEST_IGNORE_NODISCARD c.at(3); |
Roger Ferrer Ibanez | 86663cd | 2016-11-29 17:10:29 +0000 | [diff] [blame] | 57 | assert(false); |
| 58 | } |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 59 | catch (const std::out_of_range &) {} |
Roger Ferrer Ibanez | 86663cd | 2016-11-29 17:10:29 +0000 | [diff] [blame] | 60 | #endif |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 61 | } |
Eric Fiselier | 59cdf90 | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 62 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 63 | { |
| 64 | typedef double T; |
| 65 | typedef std::array<T, 0> C; |
| 66 | C c = {}; |
| 67 | C const& cc = c; |
| 68 | try |
| 69 | { |
| 70 | TEST_IGNORE_NODISCARD c.at(0); |
| 71 | assert(false); |
| 72 | } |
| 73 | catch (const std::out_of_range &) {} |
| 74 | try |
| 75 | { |
| 76 | TEST_IGNORE_NODISCARD cc.at(0); |
| 77 | assert(false); |
| 78 | } |
| 79 | catch (const std::out_of_range &) {} |
| 80 | } |
| 81 | #endif |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 82 | { |
| 83 | typedef double T; |
| 84 | typedef std::array<T, 3> C; |
| 85 | const C c = {1, 2, 3.5}; |
| 86 | C::const_reference r1 = c.at(0); |
| 87 | assert(r1 == 1); |
Eric Fiselier | d04c685 | 2016-06-01 21:35:39 +0000 | [diff] [blame] | 88 | |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 89 | C::const_reference r2 = c.at(2); |
| 90 | assert(r2 == 3.5); |
| 91 | |
Roger Ferrer Ibanez | 86663cd | 2016-11-29 17:10:29 +0000 | [diff] [blame] | 92 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 93 | try |
| 94 | { |
Billy Robert O'Neal III | ba40b05 | 2017-11-21 21:37:26 +0000 | [diff] [blame] | 95 | TEST_IGNORE_NODISCARD c.at(3); |
Roger Ferrer Ibanez | 86663cd | 2016-11-29 17:10:29 +0000 | [diff] [blame] | 96 | assert(false); |
| 97 | } |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 98 | catch (const std::out_of_range &) {} |
Roger Ferrer Ibanez | 86663cd | 2016-11-29 17:10:29 +0000 | [diff] [blame] | 99 | #endif |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 100 | } |
Eric Fiselier | d04c685 | 2016-06-01 21:35:39 +0000 | [diff] [blame] | 101 | |
Eric Fiselier | 2decfad | 2015-07-18 23:56:04 +0000 | [diff] [blame] | 102 | #if TEST_STD_VER > 11 |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 103 | { |
| 104 | typedef double T; |
| 105 | typedef std::array<T, 3> C; |
| 106 | constexpr C c = {1, 2, 3.5}; |
| 107 | |
| 108 | constexpr T t1 = c.at(0); |
| 109 | static_assert (t1 == 1, ""); |
| 110 | |
| 111 | constexpr T t2 = c.at(2); |
| 112 | static_assert (t2 == 3.5, ""); |
| 113 | } |
| 114 | #endif |
| 115 | |
Marshall Clow | e782695 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 116 | #if TEST_STD_VER > 14 |
| 117 | { |
| 118 | static_assert (check_idx(0, 1), ""); |
| 119 | static_assert (check_idx(1, 2), ""); |
| 120 | static_assert (check_idx(2, 3.5), ""); |
| 121 | } |
| 122 | #endif |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 123 | |
| 124 | return 0; |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 125 | } |