blob: 0df672d8b0196e5019c4a21261acfeb810caf6db [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
Marshall Clow5f6a5ac2019-03-14 21:56:57 +000015// Libc++ marks these as noexcept
Marshall Clow8bf1f082013-07-17 18:25:36 +000016
17#include <array>
18#include <cassert>
19
Eric Fiselier2decfad2015-07-18 23:56:04 +000020#include "test_macros.h"
21
Eric Fiselierb4e2e7a2015-10-01 07:05:38 +000022// std::array is explicitly allowed to be initialized with A a = { init-list };.
23// Disable the missing braces warning for this reason.
24#include "disable_missing_braces_warning.h"
Eric Fiselier2decfad2015-07-18 23:56:04 +000025
Marshall Clowe7826952017-01-16 03:02:10 +000026#if TEST_STD_VER > 14
27constexpr bool check_idx( size_t idx, double val )
Stephan T. Lavaveja730ed32017-01-18 20:10:25 +000028{
Marshall Clowe7826952017-01-16 03:02:10 +000029 std::array<double, 3> arr = {1, 2, 3.5};
Stephan T. Lavavej4159db72017-07-29 00:55:10 +000030 return arr[idx] == val;
Marshall Clowe7826952017-01-16 03:02:10 +000031}
32#endif
33
JF Bastien2df59c52019-02-04 20:31:13 +000034int main(int, char**)
Marshall Clow8bf1f082013-07-17 18:25:36 +000035{
36 {
37 typedef double T;
38 typedef std::array<T, 3> C;
39 C c = {1, 2, 3.5};
Marshall Clow5f6a5ac2019-03-14 21:56:57 +000040 LIBCPP_ASSERT_NOEXCEPT(c[0]);
41 ASSERT_SAME_TYPE(C::reference, decltype(c[0]));
Marshall Clow8bf1f082013-07-17 18:25:36 +000042 C::reference r1 = c[0];
43 assert(r1 == 1);
44 r1 = 5.5;
45 assert(c.front() == 5.5);
Eric Fiselierd04c6852016-06-01 21:35:39 +000046
Marshall Clow8bf1f082013-07-17 18:25:36 +000047 C::reference r2 = c[2];
48 assert(r2 == 3.5);
49 r2 = 7.5;
50 assert(c.back() == 7.5);
51 }
52 {
53 typedef double T;
54 typedef std::array<T, 3> C;
55 const C c = {1, 2, 3.5};
Marshall Clow5f6a5ac2019-03-14 21:56:57 +000056 LIBCPP_ASSERT_NOEXCEPT(c[0]);
57 ASSERT_SAME_TYPE(C::const_reference, decltype(c[0]));
Marshall Clow8bf1f082013-07-17 18:25:36 +000058 C::const_reference r1 = c[0];
59 assert(r1 == 1);
60 C::const_reference r2 = c[2];
61 assert(r2 == 3.5);
62 }
Eric Fiselier59cdf902018-02-07 21:06:13 +000063 { // Test operator[] "works" on zero sized arrays
64 typedef double T;
65 typedef std::array<T, 0> C;
66 C c = {};
67 C const& cc = c;
Marshall Clow2fa901c2019-03-15 15:00:41 +000068 LIBCPP_ASSERT_NOEXCEPT(c[0]);
69 LIBCPP_ASSERT_NOEXCEPT(cc[0]);
70 ASSERT_SAME_TYPE(C::reference, decltype(c[0]));
71 ASSERT_SAME_TYPE(C::const_reference, decltype(cc[0]));
Eric Fiselier59cdf902018-02-07 21:06:13 +000072 if (c.size() > (0)) { // always false
73 C::reference r1 = c[0];
74 C::const_reference r2 = cc[0];
75 ((void)r1);
76 ((void)r2);
77 }
78 }
79 { // Test operator[] "works" on zero sized arrays
80 typedef double T;
81 typedef std::array<const T, 0> C;
82 C c = {{}};
83 C const& cc = c;
Marshall Clow2fa901c2019-03-15 15:00:41 +000084 LIBCPP_ASSERT_NOEXCEPT(c[0]);
85 LIBCPP_ASSERT_NOEXCEPT(cc[0]);
86 ASSERT_SAME_TYPE(C::reference, decltype(c[0]));
87 ASSERT_SAME_TYPE(C::const_reference, decltype(cc[0]));
Eric Fiselier59cdf902018-02-07 21:06:13 +000088 if (c.size() > (0)) { // always false
89 C::reference r1 = c[0];
90 C::const_reference r2 = cc[0];
91 ((void)r1);
92 ((void)r2);
93 }
94 }
Eric Fiselier2decfad2015-07-18 23:56:04 +000095#if TEST_STD_VER > 11
Marshall Clow8bf1f082013-07-17 18:25:36 +000096 {
97 typedef double T;
98 typedef std::array<T, 3> C;
99 constexpr C c = {1, 2, 3.5};
Marshall Clow5f6a5ac2019-03-14 21:56:57 +0000100 LIBCPP_ASSERT_NOEXCEPT(c[0]);
101 ASSERT_SAME_TYPE(C::const_reference, decltype(c[0]));
Eric Fiselier2decfad2015-07-18 23:56:04 +0000102
Marshall Clow8bf1f082013-07-17 18:25:36 +0000103 constexpr T t1 = c[0];
104 static_assert (t1 == 1, "");
105
106 constexpr T t2 = c[2];
107 static_assert (t2 == 3.5, "");
108 }
109#endif
110
Marshall Clowe7826952017-01-16 03:02:10 +0000111#if TEST_STD_VER > 14
112 {
113 static_assert (check_idx(0, 1), "");
114 static_assert (check_idx(1, 2), "");
115 static_assert (check_idx(2, 3.5), "");
116 }
117#endif
JF Bastien2df59c52019-02-04 20:31:13 +0000118
119 return 0;
Marshall Clow8bf1f082013-07-17 18:25:36 +0000120}