2015-05-05 Yvan Roux <yvan.roux@linaro.org>
[official-gcc.git] / libstdc++-v3 / testsuite / ext / ext_pointer / 1.cc
blobc562274a5739d44454fb37f8fbf137a65708b77d
1 // Test for Container using non-standard pointer types.
3 // Copyright (C) 2008-2015 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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/>.
21 #include <algorithm>
22 #include <testsuite_hooks.h>
23 #include <ext/cast.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;
32 void
33 test01() {
34 bool test __attribute__((unused)) = true;
36 typedef _Pointer_adapter<_Relative_pointer_impl<int> > pointer;
37 typedef _Pointer_adapter<_Relative_pointer_impl<const int> > const_pointer;
39 int A[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
41 // basic pointer assignment/access tests.
42 pointer x = &A[0];
43 VERIFY(*x == 0);
44 VERIFY(std::equal(x, x+10, A));
45 pointer y(&A[9]);
46 VERIFY(*y == 9);
48 // assignability
49 pointer z(x);
50 VERIFY(z==x);
51 VERIFY(*z == 0);
53 z = y;
54 VERIFY(z==y);
55 VERIFY(z!=x);
56 VERIFY(z>x);
57 VERIFY(*z == 9);
59 // pointer arithmetic
60 VERIFY(*++x == 1);
61 VERIFY(*--x == 0);
62 VERIFY(*(x++) == 0);
63 VERIFY(*(x--) == 1);
64 VERIFY(*(x+2) == 2);
65 VERIFY(*(2+x) == 2);
66 VERIFY(*(y-2) == 7);
67 VERIFY(y - x == 9);
68 VERIFY(&*y - x == 9);
69 VERIFY(y - &*x == 9);
71 size_t s(y - x);
72 VERIFY(s == 9);
76 struct A {
77 mutable int i;
79 struct B : public A{
80 mutable int j;
82 typedef _Pointer_adapter<_Relative_pointer_impl<B> > B_pointer;
83 typedef _Pointer_adapter<_Relative_pointer_impl<A> > A_pointer;
84 typedef _Pointer_adapter<_Relative_pointer_impl<const A> > const_A_pointer;
85 typedef _Pointer_adapter<_Relative_pointer_impl<const B> > const_B_pointer;
88 // Test implicit conversion from B* to A*
89 void inc(_Pointer_adapter<_Relative_pointer_impl<A> > a) {
90 a->i++;
92 // Test implicit conversion from B* to const B*
93 void inc2(_Pointer_adapter<_Relative_pointer_impl<const B> > b) {
94 b->i++;
95 b->j++;
97 // Test implicit conversion from B* to const A*
98 void inc3(_Pointer_adapter<_Relative_pointer_impl<const A> > a) {
99 a->i++;
102 void test02() {
103 bool test __attribute__((unused)) = true;
105 B b;
106 b.i = 2;
107 b.j = 2;
109 B_pointer Bptr(&b);
110 VERIFY(Bptr->i == 2);
111 Bptr->i++;
112 VERIFY(b.i == 3);
114 const_B_pointer cBptr(&b);
115 b.i++;
116 VERIFY(cBptr->i == 4);
118 A_pointer Aptr(&b);
119 b.i++;
120 VERIFY(Aptr->i == 5);
121 Aptr->i++;
122 VERIFY(b.i == 6);
124 const_A_pointer cAptr(&b);
125 b.i++;
126 VERIFY(cAptr->i == 7);
128 const_B_pointer cBptr2(Bptr);
129 b.i++;
130 VERIFY(cBptr2->i == 8);
132 A_pointer Aptr2(Bptr);
133 b.i++;
134 VERIFY(Aptr2->i == 9);
135 Aptr2->i++;
136 VERIFY(b.i == 10);
138 const_A_pointer cAptr2(Bptr);
139 b.i++;
140 VERIFY(cAptr2->i == 11);
142 // Implicit casting during invocation
143 inc(Bptr);
144 VERIFY(Bptr->i == 12);
145 inc2(Bptr);
146 VERIFY(Bptr->i == 13);
147 VERIFY(Bptr->j == 3);
148 inc3(Bptr);
149 VERIFY(Bptr->i == 14);
152 void test03() {
153 bool test __attribute__((unused)) = true;
155 B b;
156 B* bPtr = &b;
157 A* aPtr __attribute__((unused)) = __static_pointer_cast<A*>(bPtr);
158 const A *caPtr __attribute__((unused)) = __static_pointer_cast<const A*>(bPtr);
159 const B *cbPtr __attribute__((unused)) = __static_pointer_cast<const B*>(bPtr);
161 B_pointer Bptr2 = &b;
163 const A* caPtr2 __attribute__((unused)) = __static_pointer_cast<const A*>(Bptr2);
164 A * aPtr2 __attribute__((unused)) = __static_pointer_cast<A*>(Bptr2);
165 const B* cbPtr2 __attribute__((unused)) = __const_pointer_cast<const B*>(Bptr2);
167 const_A_pointer caPtr3 __attribute__((unused)) = __static_pointer_cast<const A*>(Bptr2);
168 A_pointer aPtr3 __attribute__((unused)) = __static_pointer_cast<A*>(Bptr2);
169 const_B_pointer cbPtr3 __attribute__((unused)) = __const_pointer_cast<const B*>(Bptr2);
172 // Confirm the usability of the __static_pointer_cast<> template function
173 // to transform between _Pointer_adapter and standard versions.
174 void test04() {
175 bool test __attribute__((unused)) = true;
177 B b;
178 B_pointer bPtr = &b;
180 A_pointer aPtr = __static_pointer_cast<A_pointer>(bPtr);
181 VERIFY(aPtr == bPtr);
182 B_pointer bPtr2 = __static_pointer_cast<B_pointer>(aPtr);
183 VERIFY(bPtr2 == aPtr);
185 A* aPtr3 = __static_pointer_cast<A*>(bPtr);
186 VERIFY(aPtr3 == bPtr);
187 B* bPtr3 = __static_pointer_cast<B*>(aPtr);
188 VERIFY(bPtr3 == aPtr);
191 int main()
193 test01();
194 test02();
195 test03();
196 test04();
197 return 0;