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

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