Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc++] Add a missing assertion in std::span's constructor
Also, add missing tests for assertions in span constructors. Now I believe that all of std::span's API should be hardened, and all the assertions should have a corresponding test. Differential Revision: https://reviews.llvm.org/D131681 (cherry picked from commit 8c6319e)
- Loading branch information
Showing
5 changed files
with
154 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
libcxx/test/libcxx/containers/views/views.span/span.cons/assert.iter_sent.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
|
||
// <span> | ||
// | ||
// constexpr span<T, Extent>::span(Iterator it, Sentinel sent); | ||
// | ||
// Check that we ensure `Extent == sent - it` and also that `[it, sent)` is a valid range. | ||
// | ||
// | ||
// constexpr span<T, dynamic_extent>::span(Iterator it, Sentinel sent); | ||
// | ||
// Check that we ensure that `[it, sent)` is a valid range. | ||
|
||
// REQUIRES: has-unix-headers | ||
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}} | ||
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1 | ||
|
||
#include <array> | ||
#include <span> | ||
|
||
#include "check_assertion.h" | ||
|
||
int main(int, char**) { | ||
{ | ||
std::array<int, 3> array{0, 1, 2}; | ||
|
||
auto invalid_range = [&] { std::span<int> const s(array.end(), array.begin()); (void)s; }; | ||
TEST_LIBCPP_ASSERT_FAILURE(invalid_range(), "invalid range in span's constructor (iterator, sentinel)"); | ||
} | ||
{ | ||
std::array<int, 3> array{0, 1, 2}; | ||
|
||
auto invalid_range = [&] { std::span<int, 3> const s(array.end(), array.begin()); (void)s; }; | ||
TEST_LIBCPP_ASSERT_FAILURE(invalid_range(), "invalid range in span's constructor (iterator, sentinel)"); | ||
|
||
auto invalid_size = [&] { std::span<int, 3> const s(array.begin(), array.begin() + 2); (void)s; }; | ||
TEST_LIBCPP_ASSERT_FAILURE(invalid_size(), "invalid range in span's constructor (iterator, sentinel): last - first != extent"); | ||
} | ||
|
||
return 0; | ||
} |
37 changes: 37 additions & 0 deletions
37
libcxx/test/libcxx/containers/views/views.span/span.cons/assert.iter_size.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
|
||
// <span> | ||
// | ||
// constexpr span<T, Extent>::span(Iterator, size_type); | ||
// | ||
// Check that the passed size is equal to the statically known extent. | ||
// Note that it doesn't make sense to validate the incoming size in the | ||
// dynamic_extent version. | ||
|
||
// REQUIRES: has-unix-headers | ||
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}} | ||
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1 | ||
|
||
#include <array> | ||
#include <span> | ||
|
||
#include "check_assertion.h" | ||
|
||
int main(int, char**) { | ||
std::array<int, 3> array{0, 1, 2}; | ||
|
||
auto too_large = [&] { std::span<int, 3> const s(array.data(), 4); (void)s; }; | ||
TEST_LIBCPP_ASSERT_FAILURE(too_large(), "size mismatch in span's constructor (iterator, len)"); | ||
|
||
auto too_small = [&] { std::span<int, 3> const s(array.data(), 2); (void)s; }; | ||
TEST_LIBCPP_ASSERT_FAILURE(too_small(), "size mismatch in span's constructor (iterator, len)"); | ||
|
||
return 0; | ||
} |
33 changes: 33 additions & 0 deletions
33
libcxx/test/libcxx/containers/views/views.span/span.cons/assert.other_span.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
|
||
// <span> | ||
// | ||
// constexpr span<T, Extent>::span(const span<U, dynamic_extent>& other); | ||
// | ||
// Check that we ensure `other.size() == Extent`. | ||
|
||
// REQUIRES: has-unix-headers | ||
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}} | ||
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1 | ||
|
||
#include <array> | ||
#include <span> | ||
|
||
#include "check_assertion.h" | ||
|
||
int main(int, char**) { | ||
std::array<int, 3> array{0, 1, 2}; | ||
std::span<int> other(array.data(), 3); | ||
|
||
auto invalid_source = [&] { std::span<int, 2> const s(other); (void)s; }; | ||
TEST_LIBCPP_ASSERT_FAILURE(invalid_source(), "size mismatch in span's constructor (other span)"); | ||
|
||
return 0; | ||
} |
32 changes: 32 additions & 0 deletions
32
libcxx/test/libcxx/containers/views/views.span/span.cons/assert.range.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
|
||
// <span> | ||
// | ||
// constexpr span<T, Extent>::span(Range&& r); | ||
// | ||
// Check that we ensure `size(r) == Extent`. | ||
|
||
// REQUIRES: has-unix-headers | ||
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}} | ||
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1 | ||
|
||
#include <span> | ||
#include <vector> | ||
|
||
#include "check_assertion.h" | ||
|
||
int main(int, char**) { | ||
std::vector<int> vec{0, 1, 2}; // must use std::vector instead of std::array, because std::span has a special constructor from std::array | ||
|
||
auto invalid_size = [&] { std::span<int, 2> const s(vec); (void)s; }; | ||
TEST_LIBCPP_ASSERT_FAILURE(invalid_size(), "size mismatch in span's constructor (range)"); | ||
|
||
return 0; | ||
} |