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 at (size_type); // constexpr in C++17
12
13 // GCC 5 doesn't implement the required constexpr support
14 // UNSUPPORTED: gcc-5
15
16 #include <array>
17 #include <cassert>
18
19 #ifndef TEST_HAS_NO_EXCEPTIONS
20 #include <stdexcept>
21 #endif
22
23 #include "test_macros.h"
24
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"
28
29
tests()30 TEST_CONSTEXPR_CXX17 bool tests()
31 {
32 {
33 typedef double T;
34 typedef std::array<T, 3> C;
35 C c = {1, 2, 3.5};
36 typename C::reference r1 = c.at(0);
37 assert(r1 == 1);
38 r1 = 5.5;
39 assert(c[0] == 5.5);
40
41 typename C::reference r2 = c.at(2);
42 assert(r2 == 3.5);
43 r2 = 7.5;
44 assert(c[2] == 7.5);
45 }
46 return true;
47 }
48
test_exceptions()49 void test_exceptions()
50 {
51 #ifndef TEST_HAS_NO_EXCEPTIONS
52 {
53 std::array<int, 4> array = {1, 2, 3, 4};
54
55 try {
56 TEST_IGNORE_NODISCARD array.at(4);
57 assert(false);
58 } catch (std::out_of_range const&) {
59 // pass
60 } catch (...) {
61 assert(false);
62 }
63
64 try {
65 TEST_IGNORE_NODISCARD array.at(5);
66 assert(false);
67 } catch (std::out_of_range const&) {
68 // pass
69 } catch (...) {
70 assert(false);
71 }
72
73 try {
74 TEST_IGNORE_NODISCARD array.at(6);
75 assert(false);
76 } catch (std::out_of_range const&) {
77 // pass
78 } catch (...) {
79 assert(false);
80 }
81
82 try {
83 using size_type = decltype(array)::size_type;
84 TEST_IGNORE_NODISCARD array.at(static_cast<size_type>(-1));
85 assert(false);
86 } catch (std::out_of_range const&) {
87 // pass
88 } catch (...) {
89 assert(false);
90 }
91 }
92
93 {
94 std::array<int, 0> array = {};
95
96 try {
97 TEST_IGNORE_NODISCARD array.at(0);
98 assert(false);
99 } catch (std::out_of_range const&) {
100 // pass
101 } catch (...) {
102 assert(false);
103 }
104 }
105 #endif
106 }
107
main(int,char **)108 int main(int, char**)
109 {
110 tests();
111 test_exceptions();
112
113 #if TEST_STD_VER >= 17
114 static_assert(tests(), "");
115 #endif
116 return 0;
117 }
118