]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/ext/ext_pointer/1.cc
1 // Test for Container using non-standard pointer types.
3 // Copyright (C) 2008-2021 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
22 #include <testsuite_hooks.h>
24 #include <ext/pointer.h>
26 using __gnu_cxx::_Pointer_adapter
;
27 using __gnu_cxx::_Relative_pointer_impl
;
28 using __gnu_cxx::__static_pointer_cast
;
29 using __gnu_cxx::__const_pointer_cast
;
34 typedef _Pointer_adapter
<_Relative_pointer_impl
<int> > pointer
;
35 typedef _Pointer_adapter
<_Relative_pointer_impl
<const int> > const_pointer
;
37 int A
[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
39 // basic pointer assignment/access tests.
42 VERIFY(std::equal(x
, x
+10, A
));
80 typedef _Pointer_adapter
<_Relative_pointer_impl
<B
> > B_pointer
;
81 typedef _Pointer_adapter
<_Relative_pointer_impl
<A
> > A_pointer
;
82 typedef _Pointer_adapter
<_Relative_pointer_impl
<const A
> > const_A_pointer
;
83 typedef _Pointer_adapter
<_Relative_pointer_impl
<const B
> > const_B_pointer
;
86 // Test implicit conversion from B* to A*
87 void inc(_Pointer_adapter
<_Relative_pointer_impl
<A
> > a
) {
90 // Test implicit conversion from B* to const B*
91 void inc2(_Pointer_adapter
<_Relative_pointer_impl
<const B
> > b
) {
95 // Test implicit conversion from B* to const A*
96 void inc3(_Pointer_adapter
<_Relative_pointer_impl
<const A
> > a
) {
106 VERIFY(Bptr
->i
== 2);
110 const_B_pointer
cBptr(&b
);
112 VERIFY(cBptr
->i
== 4);
116 VERIFY(Aptr
->i
== 5);
120 const_A_pointer
cAptr(&b
);
122 VERIFY(cAptr
->i
== 7);
124 const_B_pointer
cBptr2(Bptr
);
126 VERIFY(cBptr2
->i
== 8);
128 A_pointer
Aptr2(Bptr
);
130 VERIFY(Aptr2
->i
== 9);
134 const_A_pointer
cAptr2(Bptr
);
136 VERIFY(cAptr2
->i
== 11);
138 // Implicit casting during invocation
140 VERIFY(Bptr
->i
== 12);
142 VERIFY(Bptr
->i
== 13);
143 VERIFY(Bptr
->j
== 3);
145 VERIFY(Bptr
->i
== 14);
151 A
* aPtr
__attribute__((unused
)) = __static_pointer_cast
<A
*>(bPtr
);
152 const A
*caPtr
__attribute__((unused
)) = __static_pointer_cast
<const A
*>(bPtr
);
153 const B
*cbPtr
__attribute__((unused
)) = __static_pointer_cast
<const B
*>(bPtr
);
155 B_pointer Bptr2
= &b
;
157 const A
* caPtr2
__attribute__((unused
)) = __static_pointer_cast
<const A
*>(Bptr2
);
158 A
* aPtr2
__attribute__((unused
)) = __static_pointer_cast
<A
*>(Bptr2
);
159 const B
* cbPtr2
__attribute__((unused
)) = __const_pointer_cast
<const B
*>(Bptr2
);
161 const_A_pointer caPtr3
__attribute__((unused
)) = __static_pointer_cast
<const A
*>(Bptr2
);
162 A_pointer aPtr3
__attribute__((unused
)) = __static_pointer_cast
<A
*>(Bptr2
);
163 const_B_pointer cbPtr3
__attribute__((unused
)) = __const_pointer_cast
<const B
*>(Bptr2
);
166 // Confirm the usability of the __static_pointer_cast<> template function
167 // to transform between _Pointer_adapter and standard versions.
172 A_pointer aPtr
= __static_pointer_cast
<A_pointer
>(bPtr
);
173 VERIFY(aPtr
== bPtr
);
174 B_pointer bPtr2
= __static_pointer_cast
<B_pointer
>(aPtr
);
175 VERIFY(bPtr2
== aPtr
);
177 A
* aPtr3
= __static_pointer_cast
<A
*>(bPtr
);
178 VERIFY(aPtr3
== bPtr
);
179 B
* bPtr3
= __static_pointer_cast
<B
*>(aPtr
);
180 VERIFY(bPtr3
== aPtr
);
183 // Check that long long values can be used for pointer arithmetic.
188 A_pointer q
= p
+ 0ull;
193 VERIFY( q
->i
== p
[1ll].i
);