blob: 6b5d8486fda22488021955e6173d1a1c62ab1031 [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
Louis Dionne77b9abf2020-05-22 09:59:48 -040011// reference operator[](size_type); // constexpr in C++17
12// Libc++ marks it as noexcept
Marshall Clow8bf1f082013-07-17 18:25:36 +000013
14#include <array>
15#include <cassert>
16
Eric Fiselier2decfad2015-07-18 23:56:04 +000017#include "test_macros.h"
18
Eric Fiselierb4e2e7a2015-10-01 07:05:38 +000019// std::array is explicitly allowed to be initialized with A a = { init-list };.
20// Disable the missing braces warning for this reason.
21#include "disable_missing_braces_warning.h"
Eric Fiselier2decfad2015-07-18 23:56:04 +000022
Marshall Clowe7826952017-01-16 03:02:10 +000023
Louis Dionne77b9abf2020-05-22 09:59:48 -040024TEST_CONSTEXPR_CXX17 bool tests()
Marshall Clow8bf1f082013-07-17 18:25:36 +000025{
26 {
27 typedef double T;
28 typedef std::array<T, 3> C;
29 C c = {1, 2, 3.5};
Marshall Clow5f6a5ac2019-03-14 21:56:57 +000030 LIBCPP_ASSERT_NOEXCEPT(c[0]);
31 ASSERT_SAME_TYPE(C::reference, decltype(c[0]));
Marshall Clow8bf1f082013-07-17 18:25:36 +000032 C::reference r1 = c[0];
33 assert(r1 == 1);
34 r1 = 5.5;
35 assert(c.front() == 5.5);
Eric Fiselierd04c6852016-06-01 21:35:39 +000036
Marshall Clow8bf1f082013-07-17 18:25:36 +000037 C::reference r2 = c[2];
38 assert(r2 == 3.5);
39 r2 = 7.5;
40 assert(c.back() == 7.5);
41 }
Louis Dionne77b9abf2020-05-22 09:59:48 -040042
43 // Test operator[] "works" on zero sized arrays
Marshall Clow8bf1f082013-07-17 18:25:36 +000044 {
Louis Dionne77b9abf2020-05-22 09:59:48 -040045 {
46 typedef double T;
47 typedef std::array<T, 0> C;
48 C c = {};
49 LIBCPP_ASSERT_NOEXCEPT(c[0]);
50 ASSERT_SAME_TYPE(C::reference, decltype(c[0]));
51 if (c.size() > (0)) { // always false
52 C::reference r = c[0];
53 (void)r;
54 }
55 }
56 {
57 typedef double T;
58 typedef std::array<const T, 0> C;
59 C c = {};
60 LIBCPP_ASSERT_NOEXCEPT(c[0]);
61 ASSERT_SAME_TYPE(C::reference, decltype(c[0]));
62 if (c.size() > (0)) { // always false
63 C::reference r = c[0];
64 (void)r;
65 }
Eric Fiselier59cdf902018-02-07 21:06:13 +000066 }
67 }
Eric Fiselier2decfad2015-07-18 23:56:04 +000068
Louis Dionne77b9abf2020-05-22 09:59:48 -040069 return true;
70}
Marshall Clow8bf1f082013-07-17 18:25:36 +000071
Louis Dionne77b9abf2020-05-22 09:59:48 -040072int main(int, char**)
73{
74 tests();
75#if TEST_STD_VER >= 17
76 static_assert(tests(), "");
Marshall Clow8bf1f082013-07-17 18:25:36 +000077#endif
Louis Dionne77b9abf2020-05-22 09:59:48 -040078 return 0;
Marshall Clow8bf1f082013-07-17 18:25:36 +000079}