blob: 0454643541c9d47efc491a76d5cc5fa70eceb706 [file] [log] [blame]
Marshall Clow8bf1f082013-07-17 18:25:36 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Marshall Clow8bf1f082013-07-17 18:25:36 +00006//
7//===----------------------------------------------------------------------===//
8
9// <array>
10
11// reference operator[] (size_type)
12// const_reference operator[] (size_type); // constexpr in C++14
13// reference at (size_type)
14// const_reference at (size_type); // constexpr in C++14
15
16#include <array>
17#include <cassert>
18
Stephan T. Lavavej5688f162019-12-13 18:14:09 -080019#ifndef TEST_HAS_NO_EXCEPTIONS
20#include <stdexcept>
21#endif
22
Eric Fiselier2decfad2015-07-18 23:56:04 +000023#include "test_macros.h"
24
Eric Fiselierb4e2e7a2015-10-01 07:05:38 +000025// 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"
Eric Fiselier2decfad2015-07-18 23:56:04 +000028
Marshall Clowe7826952017-01-16 03:02:10 +000029#if TEST_STD_VER > 14
30constexpr bool check_idx( size_t idx, double val )
Stephan T. Lavaveja730ed32017-01-18 20:10:25 +000031{
Marshall Clowe7826952017-01-16 03:02:10 +000032 std::array<double, 3> arr = {1, 2, 3.5};
Stephan T. Lavavej4159db72017-07-29 00:55:10 +000033 return arr.at(idx) == val;
Marshall Clowe7826952017-01-16 03:02:10 +000034}
35#endif
36
JF Bastien2df59c52019-02-04 20:31:13 +000037int main(int, char**)
Marshall Clow8bf1f082013-07-17 18:25:36 +000038{
39 {
40 typedef double T;
41 typedef std::array<T, 3> C;
42 C c = {1, 2, 3.5};
43 C::reference r1 = c.at(0);
44 assert(r1 == 1);
45 r1 = 5.5;
46 assert(c.front() == 5.5);
Eric Fiselier2decfad2015-07-18 23:56:04 +000047
Marshall Clow8bf1f082013-07-17 18:25:36 +000048 C::reference r2 = c.at(2);
49 assert(r2 == 3.5);
50 r2 = 7.5;
51 assert(c.back() == 7.5);
52
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000053#ifndef TEST_HAS_NO_EXCEPTIONS
54 try
55 {
Billy Robert O'Neal IIIba40b052017-11-21 21:37:26 +000056 TEST_IGNORE_NODISCARD c.at(3);
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000057 assert(false);
58 }
Marshall Clow8bf1f082013-07-17 18:25:36 +000059 catch (const std::out_of_range &) {}
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000060#endif
Marshall Clow8bf1f082013-07-17 18:25:36 +000061 }
Eric Fiselier59cdf902018-02-07 21:06:13 +000062#ifndef TEST_HAS_NO_EXCEPTIONS
63 {
64 typedef double T;
65 typedef std::array<T, 0> C;
66 C c = {};
67 C const& cc = c;
68 try
69 {
70 TEST_IGNORE_NODISCARD c.at(0);
71 assert(false);
72 }
73 catch (const std::out_of_range &) {}
74 try
75 {
76 TEST_IGNORE_NODISCARD cc.at(0);
77 assert(false);
78 }
79 catch (const std::out_of_range &) {}
80 }
81#endif
Marshall Clow8bf1f082013-07-17 18:25:36 +000082 {
83 typedef double T;
84 typedef std::array<T, 3> C;
85 const C c = {1, 2, 3.5};
86 C::const_reference r1 = c.at(0);
87 assert(r1 == 1);
Eric Fiselierd04c6852016-06-01 21:35:39 +000088
Marshall Clow8bf1f082013-07-17 18:25:36 +000089 C::const_reference r2 = c.at(2);
90 assert(r2 == 3.5);
91
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000092#ifndef TEST_HAS_NO_EXCEPTIONS
93 try
94 {
Billy Robert O'Neal IIIba40b052017-11-21 21:37:26 +000095 TEST_IGNORE_NODISCARD c.at(3);
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000096 assert(false);
97 }
Marshall Clow8bf1f082013-07-17 18:25:36 +000098 catch (const std::out_of_range &) {}
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000099#endif
Marshall Clow8bf1f082013-07-17 18:25:36 +0000100 }
Eric Fiselierd04c6852016-06-01 21:35:39 +0000101
Eric Fiselier2decfad2015-07-18 23:56:04 +0000102#if TEST_STD_VER > 11
Marshall Clow8bf1f082013-07-17 18:25:36 +0000103 {
104 typedef double T;
105 typedef std::array<T, 3> C;
106 constexpr C c = {1, 2, 3.5};
107
108 constexpr T t1 = c.at(0);
109 static_assert (t1 == 1, "");
110
111 constexpr T t2 = c.at(2);
112 static_assert (t2 == 3.5, "");
113 }
114#endif
115
Marshall Clowe7826952017-01-16 03:02:10 +0000116#if TEST_STD_VER > 14
117 {
118 static_assert (check_idx(0, 1), "");
119 static_assert (check_idx(1, 2), "");
120 static_assert (check_idx(2, 3.5), "");
121 }
122#endif
JF Bastien2df59c52019-02-04 20:31:13 +0000123
124 return 0;
Marshall Clow8bf1f082013-07-17 18:25:36 +0000125}