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

source code of libcxx/test/std/containers/sequences/array/at.pass.cpp