Skip to content

Commit 6e80158

Browse files
committed
Address feedback (test iterator operations)
1 parent 4b7d2e8 commit 6e80158

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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=unchecked
12+
// XFAIL: availability-verbose_abort-missing
13+
14+
#include <iterator>
15+
16+
#include "check_assertion.h"
17+
#include "test_iterators.h"
18+
19+
int main(int, char**) {
20+
using Iter = std::counted_iterator<int*>;
21+
int a[] = {1, 2, 3};
22+
Iter valid_i(a, 1);
23+
24+
{
25+
Iter i;
26+
27+
TEST_LIBCPP_ASSERT_FAILURE(*i, "Iterator is equal to or past end.");
28+
TEST_LIBCPP_ASSERT_FAILURE(i[999], "Subscript argument must be less than size.");
29+
TEST_LIBCPP_ASSERT_FAILURE(std::ranges::iter_move(i), "Iterator must not be past end of range.");
30+
TEST_LIBCPP_ASSERT_FAILURE(std::ranges::iter_swap(i, valid_i), "Iterators must not be past end of range.");
31+
TEST_LIBCPP_ASSERT_FAILURE(std::ranges::iter_swap(valid_i, i), "Iterators must not be past end of range.");
32+
std::ranges::iter_swap(valid_i, valid_i); // Ok
33+
}
34+
35+
{ // Check the `const` overload of `operator*`.
36+
const Iter i;
37+
38+
TEST_LIBCPP_ASSERT_FAILURE(*i, "Iterator is equal to or past end.");
39+
}
40+
41+
return 0;
42+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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=unchecked
12+
// XFAIL: availability-verbose_abort-missing
13+
14+
#include <iterator>
15+
16+
#include "check_assertion.h"
17+
#include "test_iterators.h"
18+
19+
int main(int, char**) {
20+
using Iter = std::common_iterator<int*, sentinel_wrapper<int*>>;
21+
int a[] = {1, 2, 3};
22+
sentinel_wrapper<int*> s;
23+
Iter valid_i = a;
24+
25+
{
26+
Iter i = s;
27+
28+
TEST_LIBCPP_ASSERT_FAILURE(*i, "Attempted to dereference a non-dereferenceable common_iterator");
29+
30+
TEST_LIBCPP_ASSERT_FAILURE(++i, "Attempted to increment a non-dereferenceable common_iterator");
31+
TEST_LIBCPP_ASSERT_FAILURE(i++, "Attempted to increment a non-dereferenceable common_iterator");
32+
33+
TEST_LIBCPP_ASSERT_FAILURE(std::ranges::iter_move(i), "Attempted to iter_move a non-dereferenceable common_iterator");
34+
35+
TEST_LIBCPP_ASSERT_FAILURE(std::ranges::iter_swap(i, valid_i), "Attempted to iter_swap a non-dereferenceable common_iterator");
36+
TEST_LIBCPP_ASSERT_FAILURE(std::ranges::iter_swap(valid_i, i), "Attempted to iter_swap a non-dereferenceable common_iterator");
37+
std::ranges::iter_swap(valid_i, valid_i); // Ok
38+
}
39+
40+
{ // Check the `const` overload of `operator*`.
41+
const Iter i = s;
42+
TEST_LIBCPP_ASSERT_FAILURE(*i, "Attempted to dereference a non-dereferenceable common_iterator");
43+
}
44+
45+
{ // Check `operator->`.
46+
struct Foo {
47+
int x = 0;
48+
};
49+
50+
std::common_iterator<Foo*, sentinel_wrapper<Foo*>> i = sentinel_wrapper<Foo*>();
51+
TEST_LIBCPP_ASSERT_FAILURE(i->x, "Attempted to dereference a non-dereferenceable common_iterator");
52+
}
53+
54+
// TODO: check `valueless_by_exception
55+
56+
return 0;
57+
}

0 commit comments

Comments
 (0)