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

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