blob: ed4ab80fb0cd6ca88e5880cba90748b45a968388 [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 at (size_type); // constexpr in C++17
Marshall Clow8bf1f082013-07-17 18:25:36 +000012
13#include <array>
14#include <cassert>
15
Stephan T. Lavavej5688f162019-12-13 18:14:09 -080016#ifndef TEST_HAS_NO_EXCEPTIONS
17#include <stdexcept>
18#endif
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
Louis Dionne77b9abf2020-05-22 09:59:48 -040027TEST_CONSTEXPR_CXX17 bool tests()
Marshall Clow8bf1f082013-07-17 18:25:36 +000028{
29 {
30 typedef double T;
31 typedef std::array<T, 3> C;
32 C c = {1, 2, 3.5};
Louis Dionne77b9abf2020-05-22 09:59:48 -040033 typename C::reference r1 = c.at(0);
Marshall Clow8bf1f082013-07-17 18:25:36 +000034 assert(r1 == 1);
35 r1 = 5.5;
Louis Dionne77b9abf2020-05-22 09:59:48 -040036 assert(c[0] == 5.5);
Eric Fiselier2decfad2015-07-18 23:56:04 +000037
Louis Dionne77b9abf2020-05-22 09:59:48 -040038 typename C::reference r2 = c.at(2);
Marshall Clow8bf1f082013-07-17 18:25:36 +000039 assert(r2 == 3.5);
40 r2 = 7.5;
Louis Dionne77b9abf2020-05-22 09:59:48 -040041 assert(c[2] == 7.5);
Marshall Clow8bf1f082013-07-17 18:25:36 +000042 }
Louis Dionne77b9abf2020-05-22 09:59:48 -040043 return true;
44}
45
46void test_exceptions()
47{
Eric Fiselier59cdf902018-02-07 21:06:13 +000048#ifndef TEST_HAS_NO_EXCEPTIONS
49 {
Louis Dionne77b9abf2020-05-22 09:59:48 -040050 std::array<int, 4> array = {1, 2, 3, 4};
51
52 try {
53 TEST_IGNORE_NODISCARD array.at(4);
54 assert(false);
55 } catch (std::out_of_range const&) {
56 // pass
57 } catch (...) {
Eric Fiselier59cdf902018-02-07 21:06:13 +000058 assert(false);
59 }
Louis Dionne77b9abf2020-05-22 09:59:48 -040060
61 try {
62 TEST_IGNORE_NODISCARD array.at(5);
63 assert(false);
64 } catch (std::out_of_range const&) {
65 // pass
66 } catch (...) {
Eric Fiselier59cdf902018-02-07 21:06:13 +000067 assert(false);
68 }
Eric Fiselierd04c6852016-06-01 21:35:39 +000069
Louis Dionne77b9abf2020-05-22 09:59:48 -040070 try {
71 TEST_IGNORE_NODISCARD array.at(6);
72 assert(false);
73 } catch (std::out_of_range const&) {
74 // pass
75 } catch (...) {
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000076 assert(false);
77 }
Louis Dionne77b9abf2020-05-22 09:59:48 -040078
79 try {
80 TEST_IGNORE_NODISCARD array.at(-1);
81 assert(false);
82 } catch (std::out_of_range const&) {
83 // pass
84 } catch (...) {
85 assert(false);
86 }
Marshall Clow8bf1f082013-07-17 18:25:36 +000087 }
Eric Fiselierd04c6852016-06-01 21:35:39 +000088
Marshall Clow8bf1f082013-07-17 18:25:36 +000089 {
Louis Dionne77b9abf2020-05-22 09:59:48 -040090 std::array<int, 0> array = {};
Marshall Clow8bf1f082013-07-17 18:25:36 +000091
Louis Dionne77b9abf2020-05-22 09:59:48 -040092 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 }
Marshall Clow8bf1f082013-07-17 18:25:36 +0000100 }
101#endif
Louis Dionne77b9abf2020-05-22 09:59:48 -0400102}
Marshall Clow8bf1f082013-07-17 18:25:36 +0000103
Louis Dionne77b9abf2020-05-22 09:59:48 -0400104int main(int, char**)
105{
106 tests();
107 test_exceptions();
108
109#if TEST_STD_VER >= 17
110 static_assert(tests(), "");
Marshall Clowe7826952017-01-16 03:02:10 +0000111#endif
Louis Dionne77b9abf2020-05-22 09:59:48 -0400112 return 0;
Marshall Clow8bf1f082013-07-17 18:25:36 +0000113}