1 | // -*- C++ -*- |
2 | //===----------------------------------------------------------------------===// |
3 | // |
4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
5 | // See https://llvm.org/LICENSE.txt for license information. |
6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
7 | // |
8 | //===----------------------------------------------------------------------===// |
9 | |
10 | // UNSUPPORTED: c++03, c++11, c++14 |
11 | // REQUIRES: c++experimental |
12 | |
13 | // <experimental/memory> |
14 | |
15 | // observer_ptr |
16 | // |
17 | // template <class W1, class W2> |
18 | // bool operator==(const observer_ptr<W1>& p1, const observer_ptr<W2>& p2); |
19 | // |
20 | // template <class W1, class W2> |
21 | // bool operator!=(const observer_ptr<W1>& p1, const observer_ptr<W2>& p2); |
22 | // |
23 | // template <class W> |
24 | // bool operator==(const observer_ptr<W>& p, std::nullptr_t) noexcept; |
25 | // |
26 | // template <class W> |
27 | // bool operator==(std::nullptr_t, const observer_ptr<W>& p) noexcept; |
28 | // |
29 | // template <class W> |
30 | // bool operator!=(const observer_ptr<W>& p, std::nullptr_t) noexcept; |
31 | // |
32 | // template <class W> |
33 | // bool operator!=(std::nullptr_t, const observer_ptr<W>& p) noexcept; |
34 | // |
35 | // template <class W1, class W2> |
36 | // bool operator<(const observer_ptr<W1>& p1, const observer_ptr<W2>& p2); |
37 | // |
38 | // template <class W1, class W2> |
39 | // bool operator>(const observer_ptr<W1>& p1, const observer_ptr<W2>& p2); |
40 | // |
41 | // template <class W1, class W2> |
42 | // bool operator<=(const observer_ptr<W1>& p1, const observer_ptr<W2>& p2); |
43 | // |
44 | // template <class W1, class W2> |
45 | // bool operator>=(const observer_ptr<W1>& p1, const observer_ptr<W2>& p2); |
46 | |
47 | #include <experimental/memory> |
48 | #include <cassert> |
49 | |
50 | void test() { |
51 | using T = int; |
52 | using Ptr = std::experimental::observer_ptr<T>; |
53 | using VoidPtr = std::experimental::observer_ptr<void>; |
54 | |
55 | // operator==(observer_ptr, observer_ptr) |
56 | { |
57 | T obj1, obj2; |
58 | Ptr ptr1(&obj1), ptr1_x(&obj1); |
59 | Ptr ptr2(&obj2); |
60 | VoidPtr ptr3(&obj1); |
61 | |
62 | assert(!(ptr1 == ptr2)); |
63 | assert(ptr1 == ptr1_x); |
64 | |
65 | assert(ptr1 == ptr3); |
66 | } |
67 | |
68 | // operator!=(observer_ptr, observer_ptr) |
69 | { |
70 | T obj1, obj2; |
71 | Ptr ptr1(&obj1), ptr1_x(&obj1); |
72 | Ptr ptr2(&obj2); |
73 | VoidPtr ptr3(&obj1); |
74 | |
75 | assert(ptr1 != ptr2); |
76 | assert(!(ptr1 != ptr1_x)); |
77 | |
78 | assert(ptr2 != ptr3); |
79 | } |
80 | |
81 | // operator==(observer_ptr, nullptr_t) |
82 | { |
83 | T obj1; |
84 | Ptr ptr1(&obj1); |
85 | Ptr ptr2(nullptr); |
86 | |
87 | assert(!(ptr1 == nullptr)); |
88 | assert(ptr2 == nullptr); |
89 | } |
90 | |
91 | // operator==(nullptr_t, observer_ptr) |
92 | { |
93 | T obj1; |
94 | Ptr ptr1(&obj1); |
95 | Ptr ptr2(nullptr); |
96 | |
97 | assert(!(nullptr == ptr1)); |
98 | assert(nullptr == ptr2); |
99 | } |
100 | |
101 | // operator!=(observer_ptr, nullptr_t) |
102 | { |
103 | T obj1; |
104 | Ptr ptr1(&obj1); |
105 | Ptr ptr2(nullptr); |
106 | |
107 | assert(ptr1 != nullptr); |
108 | assert(!(ptr2 != nullptr)); |
109 | } |
110 | |
111 | // operator!=(nullptr_t, observer_ptr) |
112 | { |
113 | T obj1; |
114 | Ptr ptr1(&obj1); |
115 | Ptr ptr2(nullptr); |
116 | |
117 | assert(nullptr != ptr1); |
118 | assert(!(nullptr != ptr2)); |
119 | } |
120 | |
121 | // operator<(observer_ptr, observer_ptr) |
122 | { |
123 | T obj1, obj2; |
124 | Ptr ptr1(&obj1); |
125 | Ptr ptr2(&obj2); |
126 | VoidPtr ptr3(&obj1); |
127 | |
128 | assert(!(ptr1 < ptr1)); |
129 | assert((ptr1 < ptr2) == (&obj1 < &obj2)); |
130 | |
131 | assert(!(ptr1 < ptr3)); |
132 | } |
133 | |
134 | // operator>(observer_ptr, observer_ptr) |
135 | { |
136 | T obj1, obj2; |
137 | Ptr ptr1(&obj1); |
138 | Ptr ptr2(&obj2); |
139 | VoidPtr ptr3(&obj1); |
140 | |
141 | assert(!(ptr1 > ptr1)); |
142 | assert((ptr1 > ptr2) == (&obj1 > &obj2)); |
143 | |
144 | assert(!(ptr1 > ptr3)); |
145 | } |
146 | |
147 | // operator<=(observer_ptr, observer_ptr) |
148 | { |
149 | T obj1, obj2; |
150 | Ptr ptr1(&obj1); |
151 | Ptr ptr2(&obj2); |
152 | VoidPtr ptr3(&obj1); |
153 | |
154 | assert(ptr1 <= ptr1); |
155 | assert((ptr1 <= ptr2) == (&obj1 <= &obj2)); |
156 | |
157 | assert(ptr1 <= ptr3); |
158 | } |
159 | |
160 | // operator>=(observer_ptr, observer_ptr) |
161 | { |
162 | T obj1, obj2; |
163 | Ptr ptr1(&obj1); |
164 | Ptr ptr2(&obj2); |
165 | VoidPtr ptr3(&obj1); |
166 | |
167 | assert(ptr1 >= ptr1); |
168 | assert((ptr1 >= ptr2) == (&obj1 >= &obj2)); |
169 | |
170 | assert(ptr1 >= ptr3); |
171 | } |
172 | } |
173 | |
174 | int main(int, char**) { |
175 | // Note: this is not constexpr in the spec |
176 | test(); |
177 | |
178 | return 0; |
179 | } |