From bc5e4d527e0db22194fc4f744519d9f6306893ec Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Tue, 2 Apr 2024 01:02:04 +0800 Subject: [PATCH 1/3] Check and test preconditions for `take_view`'s conctructor Also add test for `drop_view`'s --- stl/inc/ranges | 6 ++++- tests/std/test.lst | 2 ++ .../tests/P0896R4_views_drop_death/env.lst | 4 +++ .../tests/P0896R4_views_drop_death/test.cpp | 27 +++++++++++++++++++ .../tests/P0896R4_views_take_death/env.lst | 4 +++ .../tests/P0896R4_views_take_death/test.cpp | 27 +++++++++++++++++++ 6 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 tests/std/tests/P0896R4_views_drop_death/env.lst create mode 100644 tests/std/tests/P0896R4_views_drop_death/test.cpp create mode 100644 tests/std/tests/P0896R4_views_take_death/env.lst create mode 100644 tests/std/tests/P0896R4_views_take_death/test.cpp diff --git a/stl/inc/ranges b/stl/inc/ranges index 37e9db92824..b6cb1ea0adc 100644 --- a/stl/inc/ranges +++ b/stl/inc/ranges @@ -2948,7 +2948,11 @@ namespace ranges { constexpr explicit take_view(_Vw _Range_, const range_difference_t<_Vw> _Count_) noexcept( is_nothrow_move_constructible_v<_Vw>) // strengthened - : _Range(_STD move(_Range_)), _Count{_Count_} {} + : _Range(_STD move(_Range_)), _Count{_Count_} { +#if _CONTAINER_DEBUG_LEVEL > 0 + _STL_VERIFY(_Count_ >= 0, "Number of elements to take must be non-negative (N4971 [range.take.view]/1"); +#endif // _CONTAINER_DEBUG_LEVEL > 0 + } _NODISCARD constexpr _Vw base() const& noexcept(is_nothrow_copy_constructible_v<_Vw>) /* strengthened */ requires copy_constructible<_Vw> diff --git a/tests/std/test.lst b/tests/std/test.lst index cf8d9bf3ef1..610ee74a6ff 100644 --- a/tests/std/test.lst +++ b/tests/std/test.lst @@ -479,6 +479,7 @@ tests\P0896R4_views_common tests\P0896R4_views_counted tests\P0896R4_views_counted_death tests\P0896R4_views_drop +tests\P0896R4_views_drop_death tests\P0896R4_views_drop_while tests\P0896R4_views_drop_while_death tests\P0896R4_views_elements @@ -494,6 +495,7 @@ tests\P0896R4_views_reverse tests\P0896R4_views_single tests\P0896R4_views_split tests\P0896R4_views_take +tests\P0896R4_views_take_death tests\P0896R4_views_take_while tests\P0896R4_views_take_while_death tests\P0896R4_views_transform diff --git a/tests/std/tests/P0896R4_views_drop_death/env.lst b/tests/std/tests/P0896R4_views_drop_death/env.lst new file mode 100644 index 00000000000..351a8293d9d --- /dev/null +++ b/tests/std/tests/P0896R4_views_drop_death/env.lst @@ -0,0 +1,4 @@ +# Copyright (c) Microsoft Corporation. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +RUNALL_INCLUDE ..\usual_20_matrix.lst diff --git a/tests/std/tests/P0896R4_views_drop_death/test.cpp b/tests/std/tests/P0896R4_views_drop_death/test.cpp new file mode 100644 index 00000000000..9205b10aa75 --- /dev/null +++ b/tests/std/tests/P0896R4_views_drop_death/test.cpp @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#define _CONTAINER_DEBUG_LEVEL 1 + +#include + +#include +using namespace std; + +constexpr int some_ints[] = {0, 1, 2, 3}; + +void test_constructor_negative_size() { + (void) views::drop(some_ints, -3); // Number of elements to drop must be non-negative +} + +int main(int argc, char* argv[]) { + std_testing::death_test_executive exec; + +#ifdef _DEBUG + exec.add_death_tests({ + test_constructor_negative_size, + }); +#endif // _DEBUG + + return exec.run(argc, argv); +} diff --git a/tests/std/tests/P0896R4_views_take_death/env.lst b/tests/std/tests/P0896R4_views_take_death/env.lst new file mode 100644 index 00000000000..351a8293d9d --- /dev/null +++ b/tests/std/tests/P0896R4_views_take_death/env.lst @@ -0,0 +1,4 @@ +# Copyright (c) Microsoft Corporation. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +RUNALL_INCLUDE ..\usual_20_matrix.lst diff --git a/tests/std/tests/P0896R4_views_take_death/test.cpp b/tests/std/tests/P0896R4_views_take_death/test.cpp new file mode 100644 index 00000000000..1c13dd776dc --- /dev/null +++ b/tests/std/tests/P0896R4_views_take_death/test.cpp @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#define _CONTAINER_DEBUG_LEVEL 1 + +#include + +#include +using namespace std; + +constexpr int some_ints[] = {0, 1, 2, 3}; + +void test_constructor_negative_size() { + (void) views::drop(some_ints, -3); // Number of elements to take must be non-negative +} + +int main(int argc, char* argv[]) { + std_testing::death_test_executive exec; + +#ifdef _DEBUG + exec.add_death_tests({ + test_constructor_negative_size, + }); +#endif // _DEBUG + + return exec.run(argc, argv); +} From 8cbc9c3b3a90b4ace1f89c423306ec752aac20f4 Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Tue, 2 Apr 2024 07:04:32 +0800 Subject: [PATCH 2/3] Properly `take` in test --- tests/std/tests/P0896R4_views_take_death/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/std/tests/P0896R4_views_take_death/test.cpp b/tests/std/tests/P0896R4_views_take_death/test.cpp index 1c13dd776dc..54c26053f52 100644 --- a/tests/std/tests/P0896R4_views_take_death/test.cpp +++ b/tests/std/tests/P0896R4_views_take_death/test.cpp @@ -11,7 +11,7 @@ using namespace std; constexpr int some_ints[] = {0, 1, 2, 3}; void test_constructor_negative_size() { - (void) views::drop(some_ints, -3); // Number of elements to take must be non-negative + (void) views::take(some_ints, -3); // Number of elements to take must be non-negative } int main(int argc, char* argv[]) { From 95fc2ceb531c53c17a94a5c54037b2be6b3bc656 Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Wed, 3 Apr 2024 09:21:40 +0800 Subject: [PATCH 3/3] Right parenthesis and N4971 --- stl/inc/ranges | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stl/inc/ranges b/stl/inc/ranges index b6cb1ea0adc..8efb5a1aa7d 100644 --- a/stl/inc/ranges +++ b/stl/inc/ranges @@ -2950,7 +2950,7 @@ namespace ranges { is_nothrow_move_constructible_v<_Vw>) // strengthened : _Range(_STD move(_Range_)), _Count{_Count_} { #if _CONTAINER_DEBUG_LEVEL > 0 - _STL_VERIFY(_Count_ >= 0, "Number of elements to take must be non-negative (N4971 [range.take.view]/1"); + _STL_VERIFY(_Count_ >= 0, "Number of elements to take must be non-negative (N4971 [range.take.view]/1)"); #endif // _CONTAINER_DEBUG_LEVEL > 0 } @@ -3370,7 +3370,7 @@ namespace ranges { is_nothrow_move_constructible_v<_Vw>) // strengthened : _Range(_STD move(_Range_)), _Count{_Count_} { #if _CONTAINER_DEBUG_LEVEL > 0 - _STL_VERIFY(_Count_ >= 0, "Number of elements to drop must be non-negative (N4950 [range.drop.view]/1"); + _STL_VERIFY(_Count_ >= 0, "Number of elements to drop must be non-negative (N4971 [range.drop.view]/1)"); #endif // _CONTAINER_DEBUG_LEVEL > 0 }