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