blob: 712a4da51f7e8958b8a140e7ddd7c83e66e55751 [file] [log] [blame]
Marshall Clow8bf1f082013-07-17 18:25:36 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <array>
11
12// reference operator[] (size_type)
13// const_reference operator[] (size_type); // constexpr in C++14
14// reference at (size_type)
15// const_reference at (size_type); // constexpr in C++14
16
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.at(idx) == val;
Marshall Clowe7826952017-01-16 03:02:10 +000031}
32#endif
33
Marshall Clow8bf1f082013-07-17 18:25:36 +000034int main()
35{
36 {
37 typedef double T;
38 typedef std::array<T, 3> C;
39 C c = {1, 2, 3.5};
40 C::reference r1 = c.at(0);
41 assert(r1 == 1);
42 r1 = 5.5;
43 assert(c.front() == 5.5);
Eric Fiselier2decfad2015-07-18 23:56:04 +000044
Marshall Clow8bf1f082013-07-17 18:25:36 +000045 C::reference r2 = c.at(2);
46 assert(r2 == 3.5);
47 r2 = 7.5;
48 assert(c.back() == 7.5);
49
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000050#ifndef TEST_HAS_NO_EXCEPTIONS
51 try
52 {
53 (void) c.at(3);
54 assert(false);
55 }
Marshall Clow8bf1f082013-07-17 18:25:36 +000056 catch (const std::out_of_range &) {}
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000057#endif
Marshall Clow8bf1f082013-07-17 18:25:36 +000058 }
59 {
60 typedef double T;
61 typedef std::array<T, 3> C;
62 const C c = {1, 2, 3.5};
63 C::const_reference r1 = c.at(0);
64 assert(r1 == 1);
Eric Fiselierd04c6852016-06-01 21:35:39 +000065
Marshall Clow8bf1f082013-07-17 18:25:36 +000066 C::const_reference r2 = c.at(2);
67 assert(r2 == 3.5);
68
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000069#ifndef TEST_HAS_NO_EXCEPTIONS
70 try
71 {
72 (void) c.at(3);
73 assert(false);
74 }
Marshall Clow8bf1f082013-07-17 18:25:36 +000075 catch (const std::out_of_range &) {}
Roger Ferrer Ibanez86663cd2016-11-29 17:10:29 +000076#endif
Marshall Clow8bf1f082013-07-17 18:25:36 +000077 }
Eric Fiselierd04c6852016-06-01 21:35:39 +000078
Eric Fiselier2decfad2015-07-18 23:56:04 +000079#if TEST_STD_VER > 11
Marshall Clow8bf1f082013-07-17 18:25:36 +000080 {
81 typedef double T;
82 typedef std::array<T, 3> C;
83 constexpr C c = {1, 2, 3.5};
84
85 constexpr T t1 = c.at(0);
86 static_assert (t1 == 1, "");
87
88 constexpr T t2 = c.at(2);
89 static_assert (t2 == 3.5, "");
90 }
91#endif
92
Marshall Clowe7826952017-01-16 03:02:10 +000093#if TEST_STD_VER > 14
94 {
95 static_assert (check_idx(0, 1), "");
96 static_assert (check_idx(1, 2), "");
97 static_assert (check_idx(2, 3.5), "");
98 }
99#endif
Marshall Clow8bf1f082013-07-17 18:25:36 +0000100}