1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // REQUIRES: has-unix-headers
11 // UNSUPPORTED: libcpp-hardening-mode=none
12 // XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
16 // Test that operator[] triggers an assertion when accessing the array out-of-bounds.
20 #include "check_assertion.h"
22 int main(int, char**) {
23 // Check with an empty array
26 using Array
= std::array
<int, 0>;
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");
35 using Array
= std::array
<const int, 0>;
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");
45 // Check with non-empty arrays
48 using Array
= std::array
<int, 1>;
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>");
55 using Array
= std::array
<const int, 1>;
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>");
63 using Array
= std::array
<int, 5>;
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>");
70 using Array
= std::array
<const int, 5>;
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>");