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