Differential D127401 Diff 435519 libcxx/test/libcxx/containers/views/views.span/debug.iterator-indexing.pass.cpp
Changeset View
Changeset View
Standalone View
Standalone View
libcxx/test/libcxx/containers/views/views.span/debug.iterator-indexing.pass.cpp
- This file was added.
//===----------------------------------------------------------------------===// | |||||
// | |||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||||
// See https://llvm.org/LICENSE.txt for license information. | |||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||||
// | |||||
//===----------------------------------------------------------------------===// | |||||
// UNSUPPORTED: c++03, c++11, c++14, c++17 | |||||
// Make sure that std::span's iterators check for OOB accesses when the debug mode is enabled. | |||||
// REQUIRES: has-unix-headers | |||||
// UNSUPPORTED: !libcpp-has-debug-mode, c++03 | |||||
#include <span> | |||||
#include "check_assertion.h" | |||||
int main(int, char**) { | |||||
auto check = [](auto const& span) { | |||||
{ | |||||
auto it = span.begin(); | |||||
TEST_LIBCPP_ASSERT_FAILURE(it + 4, "Attempted to add/subtract an iterator outside its valid range"); | |||||
} | |||||
{ | |||||
auto it = span.begin(); | |||||
TEST_LIBCPP_ASSERT_FAILURE(it - 4, "Attempted to add/subtract an iterator outside its valid range"); | |||||
} | |||||
{ | |||||
auto it = span.begin(); | |||||
TEST_LIBCPP_ASSERT_FAILURE(--it, "Attempted to decrement a non-decrementable iterator"); | |||||
} | |||||
{ | |||||
auto it = span.begin(); | |||||
TEST_LIBCPP_ASSERT_FAILURE(it--, "Attempted to decrement a non-decrementable iterator"); | |||||
} | |||||
{ | |||||
auto it = span.end(); | |||||
TEST_LIBCPP_ASSERT_FAILURE(++it, "Attempted to increment a non-incrementable iterator"); | |||||
} | |||||
{ | |||||
auto it = span.end(); | |||||
TEST_LIBCPP_ASSERT_FAILURE(it++, "Attempted to increment a non-incrementable iterator"); | |||||
} | |||||
{ | |||||
auto it = span.end(); | |||||
TEST_LIBCPP_ASSERT_FAILURE(*it, "Attempted to dereference a non-dereferenceable iterator"); | |||||
} | |||||
}; | |||||
{ | |||||
int array[] = {0, 1, 2}; | |||||
std::span<int> const span(array, 3); | |||||
check(span); | |||||
} | |||||
{ | |||||
int array[] = {0, 1, 2}; | |||||
std::span<int, 3> const span(array, 3); | |||||
check(span); | |||||
} | |||||
return 0; | |||||
} |