blob: 0240d5a83728d65cfb98dfacfd2b18c0702b6978 [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
Eric Fiselier2decfad2015-07-18 23:56:04 +000019#include "test_macros.h"
20
Eric Fiselierb4e2e7a2015-10-01 07:05:38 +000021// std::array is explicitly allowed to be initialized with A a = { init-list };.
22// Disable the missing braces warning for this reason.
23#include "disable_missing_braces_warning.h"
Eric Fiselier2decfad2015-07-18 23:56:04 +000024
Marshall Clowe7826952017-01-16 03:02:10 +000025#if TEST_STD_VER > 14
26constexpr bool check_idx( size_t idx, double val )
Stephan T. Lavaveja730ed32017-01-18 20:10:25 +000027{
Marshall Clowe7826952017-01-16 03:02:10 +000028 std::array<double, 3> arr = {1, 2, 3.5};
Stephan T. Lavavej4159db72017-07-29 00:55:10 +000029 return arr.at(idx) == val;
Marshall Clowe7826952017-01-16 03:02:10 +000030}
31#endif
32
JF Bastien2df59c52019-02-04 20:31:13 +000033int main(int, char**)
Marshall Clow8bf1f082013-07-17 18:25:36 +000034{
35 {
36 typedef double T;
37 typedef std::array<T, 3> C;
38 C c = {1, 2, 3.5};
39 C::reference r1 = c.at(0);
40 assert(r1 == 1);
41 r1 = 5.5;
42 assert(c.front() == 5.5);
Eric Fiselier2decfad2015-07-18 23:56:04 +000043
Marshall Clow8bf1f082013-07-17 18:25:36 +000044 C::reference r2 = c.at(2);
45 assert(r2 == 3.5);
46 r2 = 7.5;
47 assert(c.back() == 7.5);
48
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000049#ifndef TEST_HAS_NO_EXCEPTIONS
50 try
51 {
Billy Robert O'Neal IIIba40b052017-11-21 21:37:26 +000052 TEST_IGNORE_NODISCARD c.at(3);
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000053 assert(false);
54 }
Marshall Clow8bf1f082013-07-17 18:25:36 +000055 catch (const std::out_of_range &) {}
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000056#endif
Marshall Clow8bf1f082013-07-17 18:25:36 +000057 }
Eric Fiselier59cdf902018-02-07 21:06:13 +000058#ifndef TEST_HAS_NO_EXCEPTIONS
59 {
60 typedef double T;
61 typedef std::array<T, 0> C;
62 C c = {};
63 C const& cc = c;
64 try
65 {
66 TEST_IGNORE_NODISCARD c.at(0);
67 assert(false);
68 }
69 catch (const std::out_of_range &) {}
70 try
71 {
72 TEST_IGNORE_NODISCARD cc.at(0);
73 assert(false);
74 }
75 catch (const std::out_of_range &) {}
76 }
77#endif
Marshall Clow8bf1f082013-07-17 18:25:36 +000078 {
79 typedef double T;
80 typedef std::array<T, 3> C;
81 const C c = {1, 2, 3.5};
82 C::const_reference r1 = c.at(0);
83 assert(r1 == 1);
Eric Fiselierd04c6852016-06-01 21:35:39 +000084
Marshall Clow8bf1f082013-07-17 18:25:36 +000085 C::const_reference r2 = c.at(2);
86 assert(r2 == 3.5);
87
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000088#ifndef TEST_HAS_NO_EXCEPTIONS
89 try
90 {
Billy Robert O'Neal IIIba40b052017-11-21 21:37:26 +000091 TEST_IGNORE_NODISCARD c.at(3);
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000092 assert(false);
93 }
Marshall Clow8bf1f082013-07-17 18:25:36 +000094 catch (const std::out_of_range &) {}
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000095#endif
Marshall Clow8bf1f082013-07-17 18:25:36 +000096 }
Eric Fiselierd04c6852016-06-01 21:35:39 +000097
Eric Fiselier2decfad2015-07-18 23:56:04 +000098#if TEST_STD_VER > 11
Marshall Clow8bf1f082013-07-17 18:25:36 +000099 {
100 typedef double T;
101 typedef std::array<T, 3> C;
102 constexpr C c = {1, 2, 3.5};
103
104 constexpr T t1 = c.at(0);
105 static_assert (t1 == 1, "");
106
107 constexpr T t2 = c.at(2);
108 static_assert (t2 == 3.5, "");
109 }
110#endif
111
Marshall Clowe7826952017-01-16 03:02:10 +0000112#if TEST_STD_VER > 14
113 {
114 static_assert (check_idx(0, 1), "");
115 static_assert (check_idx(1, 2), "");
116 static_assert (check_idx(2, 3.5), "");
117 }
118#endif
JF Bastien2df59c52019-02-04 20:31:13 +0000119
120 return 0;
Marshall Clow8bf1f082013-07-17 18:25:36 +0000121}