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 | |
Louis Dionne | 77b9abf | 2020-05-22 09:59:48 -0400 | [diff] [blame] | 11 | // reference at (size_type); // constexpr in C++17 |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 12 | |
| 13 | #include <array> |
| 14 | #include <cassert> |
| 15 | |
Stephan T. Lavavej | 5688f16 | 2019-12-13 18:14:09 -0800 | [diff] [blame] | 16 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 17 | #include <stdexcept> |
| 18 | #endif |
| 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 | |
Louis Dionne | 77b9abf | 2020-05-22 09:59:48 -0400 | [diff] [blame] | 27 | TEST_CONSTEXPR_CXX17 bool tests() |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 28 | { |
| 29 | { |
| 30 | typedef double T; |
| 31 | typedef std::array<T, 3> C; |
| 32 | C c = {1, 2, 3.5}; |
Louis Dionne | 77b9abf | 2020-05-22 09:59:48 -0400 | [diff] [blame] | 33 | typename C::reference r1 = c.at(0); |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 34 | assert(r1 == 1); |
| 35 | r1 = 5.5; |
Louis Dionne | 77b9abf | 2020-05-22 09:59:48 -0400 | [diff] [blame] | 36 | assert(c[0] == 5.5); |
Eric Fiselier | 2decfad | 2015-07-18 23:56:04 +0000 | [diff] [blame] | 37 | |
Louis Dionne | 77b9abf | 2020-05-22 09:59:48 -0400 | [diff] [blame] | 38 | typename C::reference r2 = c.at(2); |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 39 | assert(r2 == 3.5); |
| 40 | r2 = 7.5; |
Louis Dionne | 77b9abf | 2020-05-22 09:59:48 -0400 | [diff] [blame] | 41 | assert(c[2] == 7.5); |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 42 | } |
Louis Dionne | 77b9abf | 2020-05-22 09:59:48 -0400 | [diff] [blame] | 43 | return true; |
| 44 | } |
| 45 | |
| 46 | void test_exceptions() |
| 47 | { |
Eric Fiselier | 59cdf90 | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 48 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 49 | { |
Louis Dionne | 77b9abf | 2020-05-22 09:59:48 -0400 | [diff] [blame] | 50 | std::array<int, 4> array = {1, 2, 3, 4}; |
| 51 | |
| 52 | try { |
| 53 | TEST_IGNORE_NODISCARD array.at(4); |
| 54 | assert(false); |
| 55 | } catch (std::out_of_range const&) { |
| 56 | // pass |
| 57 | } catch (...) { |
Eric Fiselier | 59cdf90 | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 58 | assert(false); |
| 59 | } |
Louis Dionne | 77b9abf | 2020-05-22 09:59:48 -0400 | [diff] [blame] | 60 | |
| 61 | try { |
| 62 | TEST_IGNORE_NODISCARD array.at(5); |
| 63 | assert(false); |
| 64 | } catch (std::out_of_range const&) { |
| 65 | // pass |
| 66 | } catch (...) { |
Eric Fiselier | 59cdf90 | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 67 | assert(false); |
| 68 | } |
Eric Fiselier | d04c685 | 2016-06-01 21:35:39 +0000 | [diff] [blame] | 69 | |
Louis Dionne | 77b9abf | 2020-05-22 09:59:48 -0400 | [diff] [blame] | 70 | try { |
| 71 | TEST_IGNORE_NODISCARD array.at(6); |
| 72 | assert(false); |
| 73 | } catch (std::out_of_range const&) { |
| 74 | // pass |
| 75 | } catch (...) { |
Roger Ferrer Ibanez | 86663cd | 2016-11-29 17:10:29 +0000 | [diff] [blame] | 76 | assert(false); |
| 77 | } |
Louis Dionne | 77b9abf | 2020-05-22 09:59:48 -0400 | [diff] [blame] | 78 | |
| 79 | try { |
| 80 | TEST_IGNORE_NODISCARD array.at(-1); |
| 81 | assert(false); |
| 82 | } catch (std::out_of_range const&) { |
| 83 | // pass |
| 84 | } catch (...) { |
| 85 | assert(false); |
| 86 | } |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 87 | } |
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 | { |
Louis Dionne | 77b9abf | 2020-05-22 09:59:48 -0400 | [diff] [blame] | 90 | std::array<int, 0> array = {}; |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 91 | |
Louis Dionne | 77b9abf | 2020-05-22 09:59:48 -0400 | [diff] [blame] | 92 | try { |
| 93 | TEST_IGNORE_NODISCARD array.at(0); |
| 94 | assert(false); |
| 95 | } catch (std::out_of_range const&) { |
| 96 | // pass |
| 97 | } catch (...) { |
| 98 | assert(false); |
| 99 | } |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 100 | } |
| 101 | #endif |
Louis Dionne | 77b9abf | 2020-05-22 09:59:48 -0400 | [diff] [blame] | 102 | } |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 103 | |
Louis Dionne | 77b9abf | 2020-05-22 09:59:48 -0400 | [diff] [blame] | 104 | int main(int, char**) |
| 105 | { |
| 106 | tests(); |
| 107 | test_exceptions(); |
| 108 | |
| 109 | #if TEST_STD_VER >= 17 |
| 110 | static_assert(tests(), ""); |
Marshall Clow | e782695 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 111 | #endif |
Louis Dionne | 77b9abf | 2020-05-22 09:59:48 -0400 | [diff] [blame] | 112 | return 0; |
Marshall Clow | 8bf1f08 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 113 | } |