1//===----------------------------------------------------------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8
9// REQUIRES: has-unix-headers
10// UNSUPPORTED: c++03
11// UNSUPPORTED: libcpp-hardening-mode=none
12// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
13
14// <array>
15
16// Test that operator[] triggers an assertion when accessing the array out-of-bounds.
17
18#include <array>
19
20#include "check_assertion.h"
21
22int main(int, char**) {
23 // Check with an empty array
24 {
25 {
26 using Array = std::array<int, 0>;
27 Array c = {};
28 Array const& cc = c;
29 TEST_LIBCPP_ASSERT_FAILURE(c[0], "cannot call array<T, 0>::operator[] on a zero-sized array");
30 TEST_LIBCPP_ASSERT_FAILURE(c[1], "cannot call array<T, 0>::operator[] on a zero-sized array");
31 TEST_LIBCPP_ASSERT_FAILURE(cc[0], "cannot call array<T, 0>::operator[] on a zero-sized array");
32 TEST_LIBCPP_ASSERT_FAILURE(cc[1], "cannot call array<T, 0>::operator[] on a zero-sized array");
33 }
34 {
35 using Array = std::array<const int, 0>;
36 Array c = {{}};
37 Array const& cc = c;
38 TEST_LIBCPP_ASSERT_FAILURE(c[0], "cannot call array<T, 0>::operator[] on a zero-sized array");
39 TEST_LIBCPP_ASSERT_FAILURE(c[1], "cannot call array<T, 0>::operator[] on a zero-sized array");
40 TEST_LIBCPP_ASSERT_FAILURE(cc[0], "cannot call array<T, 0>::operator[] on a zero-sized array");
41 TEST_LIBCPP_ASSERT_FAILURE(cc[1], "cannot call array<T, 0>::operator[] on a zero-sized array");
42 }
43 }
44
45 // Check with non-empty arrays
46 {
47 {
48 using Array = std::array<int, 1>;
49 Array c = {};
50 Array const& cc = c;
51 TEST_LIBCPP_ASSERT_FAILURE(c[2], "out-of-bounds access in std::array<T, N>");
52 TEST_LIBCPP_ASSERT_FAILURE(cc[2], "out-of-bounds access in std::array<T, N>");
53 }
54 {
55 using Array = std::array<const int, 1>;
56 Array c = {{}};
57 Array const& cc = c;
58 TEST_LIBCPP_ASSERT_FAILURE(c[2], "out-of-bounds access in std::array<T, N>");
59 TEST_LIBCPP_ASSERT_FAILURE(cc[2], "out-of-bounds access in std::array<T, N>");
60 }
61
62 {
63 using Array = std::array<int, 5>;
64 Array c = {};
65 Array const& cc = c;
66 TEST_LIBCPP_ASSERT_FAILURE(c[99], "out-of-bounds access in std::array<T, N>");
67 TEST_LIBCPP_ASSERT_FAILURE(cc[99], "out-of-bounds access in std::array<T, N>");
68 }
69 {
70 using Array = std::array<const int, 5>;
71 Array c = {{}};
72 Array const& cc = c;
73 TEST_LIBCPP_ASSERT_FAILURE(c[99], "out-of-bounds access in std::array<T, N>");
74 TEST_LIBCPP_ASSERT_FAILURE(cc[99], "out-of-bounds access in std::array<T, N>");
75 }
76 }
77
78 return 0;
79}
80

source code of libcxx/test/std/containers/sequences/array/assert.indexing.pass.cpp