blob: c4f7acb9f2a8100d31372f4b74d07bbdc98c38ba [file] [log] [blame]
Marshall Clow8fc4f5a2013-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 Fiselier02bb4bd2015-07-18 23:56:04 +000020#include "test_macros.h"
21
Dan Albert1d4a1ed2016-05-25 22:36:09 -070022#include "suppress_array_warnings.h"
Eric Fiselier02bb4bd2015-07-18 23:56:04 +000023
Marshall Clow8fc4f5a2013-07-17 18:25:36 +000024int main()
25{
26 {
27 typedef double T;
28 typedef std::array<T, 3> C;
29 C c = {1, 2, 3.5};
30 C::reference r1 = c.at(0);
31 assert(r1 == 1);
32 r1 = 5.5;
33 assert(c.front() == 5.5);
Eric Fiselier02bb4bd2015-07-18 23:56:04 +000034
Marshall Clow8fc4f5a2013-07-17 18:25:36 +000035 C::reference r2 = c.at(2);
36 assert(r2 == 3.5);
37 r2 = 7.5;
38 assert(c.back() == 7.5);
39
40 try { (void) c.at(3); }
41 catch (const std::out_of_range &) {}
42 }
43 {
44 typedef double T;
45 typedef std::array<T, 3> C;
46 const C c = {1, 2, 3.5};
47 C::const_reference r1 = c.at(0);
48 assert(r1 == 1);
49
50 C::const_reference r2 = c.at(2);
51 assert(r2 == 3.5);
52
53 try { (void) c.at(3); }
54 catch (const std::out_of_range &) {}
55 }
56
Eric Fiselier02bb4bd2015-07-18 23:56:04 +000057#if TEST_STD_VER > 11
Marshall Clow8fc4f5a2013-07-17 18:25:36 +000058 {
59 typedef double T;
60 typedef std::array<T, 3> C;
61 constexpr C c = {1, 2, 3.5};
62
63 constexpr T t1 = c.at(0);
64 static_assert (t1 == 1, "");
65
66 constexpr T t2 = c.at(2);
67 static_assert (t2 == 3.5, "");
68 }
69#endif
70
71}