OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libstdc++-v3/] [testsuite/] [ext/] [ext_pointer/] [1.cc] - Blame information for rev 776

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 742 jeremybenn
// Test for Container using non-standard pointer types.
2
 
3
// Copyright (C) 2008, 2009
4
// Free Software Foundation, Inc.
5
//
6
// This file is part of the GNU ISO C++ Library.  This library is free
7
// software; you can redistribute it and/or modify it under the
8
// terms of the GNU General Public License as published by the
9
// Free Software Foundation; either version 3, or (at your option)
10
// any later version.
11
 
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
// GNU General Public License for more details.
16
 
17
// You should have received a copy of the GNU General Public License along
18
// with this library; see the file COPYING3.  If not see
19
// <http://www.gnu.org/licenses/>.
20
 
21
 
22
#include <algorithm>
23
#include <testsuite_hooks.h>
24
#include <ext/cast.h>
25
#include <ext/pointer.h>
26
 
27
using __gnu_cxx::_Pointer_adapter;
28
using __gnu_cxx::_Relative_pointer_impl;
29
using __gnu_cxx::__static_pointer_cast;
30
using __gnu_cxx::__const_pointer_cast;
31
 
32
 
33
void
34
test01() {
35
  bool test __attribute__((unused)) = true;
36
 
37
  typedef _Pointer_adapter<_Relative_pointer_impl<int> >       pointer;
38
  typedef _Pointer_adapter<_Relative_pointer_impl<const int> > const_pointer;
39
 
40
  int A[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
41
 
42
  // basic pointer assignment/access tests.
43
  pointer x = &A[0];
44
  VERIFY(*x == 0);
45
  VERIFY(std::equal(x, x+10, A));
46
  pointer y(&A[9]);
47
  VERIFY(*y == 9);
48
 
49
  // assignability
50
  pointer z(x);
51
  VERIFY(z==x);
52
  VERIFY(*z == 0);
53
 
54
  z = y;
55
  VERIFY(z==y);
56
  VERIFY(z!=x);
57
  VERIFY(z>x);
58
  VERIFY(*z == 9);
59
 
60
  // pointer arithmetic
61
  VERIFY(*++x == 1);
62
  VERIFY(*--x == 0);
63
  VERIFY(*(x++) == 0);
64
  VERIFY(*(x--) == 1);
65
  VERIFY(*(x+2) == 2);
66
  VERIFY(*(2+x) == 2);
67
  VERIFY(*(y-2) == 7);
68
  VERIFY(y - x == 9);
69
  VERIFY(&*y - x == 9);
70
  VERIFY(y - &*x == 9);
71
 
72
  size_t s(y - x);
73
  VERIFY(s == 9);
74
}
75
 
76
 
77
struct A {
78
  mutable int i;
79
};
80
struct B : public A{
81
  mutable int j;
82
};
83
typedef _Pointer_adapter<_Relative_pointer_impl<B> >       B_pointer;
84
typedef _Pointer_adapter<_Relative_pointer_impl<A> >       A_pointer;
85
typedef _Pointer_adapter<_Relative_pointer_impl<const A> > const_A_pointer;
86
typedef _Pointer_adapter<_Relative_pointer_impl<const B> > const_B_pointer;
87
 
88
 
89
// Test implicit conversion from B* to A*
90
void inc(_Pointer_adapter<_Relative_pointer_impl<A> > a) {
91
  a->i++;
92
}
93
// Test implicit conversion from B* to const B*
94
void inc2(_Pointer_adapter<_Relative_pointer_impl<const B> > b) {
95
  b->i++;
96
  b->j++;
97
}
98
// Test implicit conversion from B* to const A*
99
void inc3(_Pointer_adapter<_Relative_pointer_impl<const A> > a) {
100
  a->i++;
101
}
102
 
103
void test02() {
104
  bool test __attribute__((unused)) = true;
105
 
106
  B b;
107
  b.i = 2;
108
  b.j = 2;
109
 
110
  B_pointer Bptr(&b);
111
  VERIFY(Bptr->i == 2);
112
  Bptr->i++;
113
  VERIFY(b.i == 3);
114
 
115
  const_B_pointer cBptr(&b);
116
  b.i++;
117
  VERIFY(cBptr->i == 4);
118
 
119
  A_pointer Aptr(&b);
120
  b.i++;
121
  VERIFY(Aptr->i == 5);
122
  Aptr->i++;
123
  VERIFY(b.i == 6);
124
 
125
  const_A_pointer cAptr(&b);
126
  b.i++;
127
  VERIFY(cAptr->i == 7);
128
 
129
  const_B_pointer cBptr2(Bptr);
130
  b.i++;
131
  VERIFY(cBptr2->i == 8);
132
 
133
  A_pointer Aptr2(Bptr);
134
  b.i++;
135
  VERIFY(Aptr2->i == 9);
136
  Aptr2->i++;
137
  VERIFY(b.i == 10);
138
 
139
  const_A_pointer cAptr2(Bptr);
140
  b.i++;
141
  VERIFY(cAptr2->i == 11);
142
 
143
  // Implicit casting during invocation
144
  inc(Bptr);
145
  VERIFY(Bptr->i == 12);
146
  inc2(Bptr);
147
  VERIFY(Bptr->i == 13);
148
  VERIFY(Bptr->j == 3);
149
  inc3(Bptr);
150
  VERIFY(Bptr->i == 14);
151
}
152
 
153
void test03() {
154
  bool test __attribute__((unused)) = true;
155
 
156
  B b;
157
  B* bPtr = &b;
158
  A* aPtr __attribute__((unused)) = __static_pointer_cast<A*>(bPtr);
159
  const A *caPtr __attribute__((unused)) = __static_pointer_cast<const A*>(bPtr);
160
  const B *cbPtr __attribute__((unused)) = __static_pointer_cast<const B*>(bPtr);
161
 
162
  B_pointer Bptr2 = &b;
163
 
164
  const A* caPtr2 __attribute__((unused)) = __static_pointer_cast<const A*>(Bptr2);
165
  A * aPtr2 __attribute__((unused)) = __static_pointer_cast<A*>(Bptr2);
166
  const B* cbPtr2 __attribute__((unused)) = __const_pointer_cast<const B*>(Bptr2);
167
 
168
  const_A_pointer caPtr3 __attribute__((unused)) = __static_pointer_cast<const A*>(Bptr2);
169
  A_pointer aPtr3 __attribute__((unused)) = __static_pointer_cast<A*>(Bptr2);
170
  const_B_pointer cbPtr3 __attribute__((unused)) = __const_pointer_cast<const B*>(Bptr2);
171
}
172
 
173
// Confirm the usability of the __static_pointer_cast<> template function
174
// to transform between _Pointer_adapter and standard versions.
175
void test04() {
176
  bool test __attribute__((unused)) = true;
177
 
178
  B b;
179
  B_pointer bPtr = &b;
180
 
181
  A_pointer aPtr = __static_pointer_cast<A_pointer>(bPtr);
182
  VERIFY(aPtr == bPtr);
183
  B_pointer bPtr2 = __static_pointer_cast<B_pointer>(aPtr);
184
  VERIFY(bPtr2 == aPtr);
185
 
186
  A* aPtr3 = __static_pointer_cast<A*>(bPtr);
187
  VERIFY(aPtr3 == bPtr);
188
  B* bPtr3 = __static_pointer_cast<B*>(aPtr);
189
  VERIFY(bPtr3 == aPtr);
190
}
191
 
192
int main()
193
{
194
  test01();
195
  test02();
196
  test03();
197
  test04();
198
  return 0;
199
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.