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, c++11, c++14, c++17
11 // UNSUPPORTED: libcpp-hardening-mode=none
12 // XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
13 
14 #include <iterator>
15 
16 #include "check_assertion.h"
17 #include "test_iterators.h"
18 
main(int,char **)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(
34         std::ranges::iter_move(i), "Attempted to iter_move a non-dereferenceable common_iterator");
35 
36     TEST_LIBCPP_ASSERT_FAILURE(
37         std::ranges::iter_swap(i, valid_i), "Attempted to iter_swap a non-dereferenceable common_iterator");
38     TEST_LIBCPP_ASSERT_FAILURE(
39         std::ranges::iter_swap(valid_i, i), "Attempted to iter_swap a non-dereferenceable common_iterator");
40     std::ranges::iter_swap(valid_i, valid_i); // Ok
41   }
42 
43   { // Check the `const` overload of `operator*`.
44     const Iter i = s;
45     TEST_LIBCPP_ASSERT_FAILURE(*i, "Attempted to dereference a non-dereferenceable common_iterator");
46   }
47 
48   { // Check `operator->`.
49     struct Foo {
50       int x = 0;
51     };
52 
53     std::common_iterator<Foo*, sentinel_wrapper<Foo*>> i = sentinel_wrapper<Foo*>();
54     TEST_LIBCPP_ASSERT_FAILURE(i->x, "Attempted to dereference a non-dereferenceable common_iterator");
55   }
56 
57   return 0;
58 }
59