Lumiera  0.pre.03
»edit your freedom«
access-casted-test.cpp
Go to the documentation of this file.
1 /*
2  AccessCasted(Test) - verify helper to cast or convert as appropriate
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program 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
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
29 #include "lib/test/run.hpp"
30 #include "lib/test/test-helper.hpp"
31 #include "lib/access-casted.hpp"
32 #include "lib/format-cout.hpp"
33 #include "lib/util.hpp"
34 
35 #include <utility>
36 #include <string>
37 
38 using std::move;
39 using std::string;
40 using std::ostream;
41 
42 using util::isSameObject;
43 
44 using LERR_(BOTTOM_VALUE);
45 using LERR_(WRONG_TYPE);
46 
47 
48 namespace util {
49 namespace test {
50 
51 
52 
53  namespace { // Test fixture...
54 
55  struct B {};
56  struct D : B {};
57 
58  struct E : D
59  {
60  virtual ~E() {};
61  };
62 
63  struct X
64  {
65  char x = 'x';
66  };
67 
68  struct F
69  : X
70  , E
71  { };
72 
73 
74  ostream& operator<< (ostream& s, const B& b) { return s << "B{} adr="<<&b; }
75  ostream& operator<< (ostream& s, const D& d) { return s << "D{} adr="<<&d; }
76  ostream& operator<< (ostream& s, const E& e) { return s << "E{} adr="<<&e; }
77  ostream& operator<< (ostream& s, const F& f) { return s << "F{} adr="<<&f; }
78 
79  }//(End)Test fixture
80 
81 
82 
83 
84 
85 
86  /*************************************************************************************************/
95  class AccessCasted_test : public Test
96  {
97  virtual void
98  run (Arg)
99  {
100 
101  D d;
102  D* pD =&d;
103  B* pB =pD;
104  D& rD = *pD;
105  B& rB = *pB;
106 
107  D*& rpD = pD;
108  B*& rpB = pB;
109 
110  E e;
111  E& rE = e;
112  F f;
113  E& rEF = f;
114  E* pEF = &f;
115  X* pXF = &f;
116  F* pF = &f;
117 
118  cout << "can_downcast<B,D> = " << can_downcast<B,D>::value <<endl;
119  cout << "can_downcast<B*,D*> = " << can_downcast<B*,D*>::value <<endl;
120  cout << "can_downcast<B&,D&> = " << can_downcast<B&,D&>::value <<endl;
121  cout << "can_downcast<B&,D*> = " << can_downcast<B&,D*>::value <<endl;
122  cout << "can_downcast<B*,D&> = " << can_downcast<B*,D&>::value <<endl;
123  cout << "can_downcast<B*&,D*&> = " << can_downcast<B*&,D*&>::value <<endl;
124  cout << "can_downcast<D*&,D*&> = " << can_downcast<D*&,D*&>::value <<endl;
125 
126  cout << "can_downcast<D*,E*> = " << can_downcast<D*,E*>::value <<endl;
127  cout << "can_downcast<E*,F*> = " << can_downcast<E*,F*>::value <<endl;
128 
129  cout << "has_RTTI<D*> = " << has_RTTI<D*>::value <<endl;
130  cout << "has_RTTI<E*> = " << has_RTTI<E*>::value <<endl;
131  cout << "has_RTTI<F*> = " << has_RTTI<F*>::value <<endl;
132 
133  cout << "is_convertible<D,D&> = " << std::is_convertible<D,D&>::value <<endl;
134  cout << "is_convertible<D&,D> = " << std::is_convertible<D&,D>::value <<endl;
135 
136 
137  cout << "can_use_dynamic_downcast<D,D&> = " << can_use_dynamic_downcast<D,D&>::value <<endl;
138  cout << "can_use_conversion<D,D&> = " << can_use_conversion<D,D&>::value <<endl;
139  cout << "can_use_dynamic_downcast<B*,D*> = " << can_use_dynamic_downcast<B*,D*>::value <<endl;
140  cout << "can_use_conversion<D*,B*> = " << can_use_conversion<D*,B*>::value <<endl;
141 
142  cout << "can_use_dynamic_downcast<D*&,D*&> = " << can_use_dynamic_downcast<D*&,D*&>::value <<endl;
143  cout << "can_use_conversion<D*&,D*&> = " << can_use_conversion<D*&,D*&>::value <<endl;
144  cout << "can_use_conversion<D*,E*> = " << can_use_conversion<D*,E*>::value <<endl;
145  cout << "can_use_dynamic_downcast<D*&,E*> = " << can_use_dynamic_downcast<D*&,E*>::value <<endl;
146  cout << "can_use_conversion<E*,F*> = " << can_use_conversion<E*,F*>::value <<endl;
147  cout << "can_use_dynamic_downcast<E*,F*> = " << can_use_dynamic_downcast<E*,F*>::value <<endl;
148 
149 
150 
151  cout << "=== standard case: References ==="<<endl;
152  cout << "Access(D as D&) --->" << AccessCasted<D&>::access(d) <<endl;
153  cout << "Access(D& as D&) --->" << AccessCasted<D&>::access(rD) <<endl;
154  D dd1(d);
155  // AccessCasted<D&>::access(move(dd1)); // does not compile since it would be dangerous; we can't take a l-value ref
156  // AccessCasted<D&&>::access(rD); // from a r-value (move) reference and we can't move a l-value ref
157  // AccessCasted<D&&>::access(d); //
158 
159  cout << "=== build a value object ==="<<endl;
160  cout << "Access(D as D) --->" << AccessCasted<D>::access(d) <<endl;
161  cout << "Access(D& as D) --->" << AccessCasted<D>::access(rD) <<endl;
162  cout << "Access(D&& as D) --->" << AccessCasted<D>::access(move(dd1)) <<endl;
163 
164  cout << "=== take a pointer ==="<<endl;
165  cout << "Access(D as D*) --->" << AccessCasted<D*>::access(d) <<endl;
166  cout << "Access(D& as D*) --->" << AccessCasted<D*>::access(rD) <<endl;
167  // AccessCasted<D*>::access(move(dd1)); // should not take value moved by r-value-ref as pointer, otherwise the moved object would be lost
168 
169  cout << "=== dereference a pointer ==="<<endl;
170  cout << "Access(D* as D&) --->" << AccessCasted<D&>::access(pD) <<endl;
171  cout << "Access(D* as D) --->" << AccessCasted<D>::access(pD) <<endl;
172  D* pdd1(pD);
173  cout << "Access(D*&& as D) --->" << AccessCasted<D>::access(move(pdd1)) <<endl;
174  D* pNull(0);
175  VERIFY_ERROR (BOTTOM_VALUE, AccessCasted<D>::access(pNull)); // run-time NULL check
176  // AccessCasted<D&&>::access(pD); // should not move away a value accessed through a pointer, there might be other users
177 
178  cout << "=== const correctness ==="<<endl;
179  cout << "Access(D as D const&) --->" << AccessCasted<D const&>::access(d) <<endl;
180  cout << "Access(D& as D const&) --->" << AccessCasted<D const&>::access(rD) <<endl;
181  cout << "Access(D as const D) --->" << AccessCasted<const D>::access(d) <<endl;
182  cout << "Access(D& as const D) --->" << AccessCasted<const D>::access(rD) <<endl;
183  cout << "Access(D as const D*) --->" << AccessCasted<const D*>::access(d) <<endl;
184  cout << "Access(D& as const D*) --->" << AccessCasted<const D*>::access(rD) <<endl;
185  cout << "Access(D* as D const&) --->" << AccessCasted<D const&>::access(pD) <<endl;
186  cout << "Access(D* as const D) --->" << AccessCasted<const D>::access(pD) <<endl;
187  const D cD(d);
188  D const& rcD(d);
189  const D* pcD(&cD);
190  cout << "Access(const D as D const&) --->" << AccessCasted<D const&>::access(cD) <<endl;
191  cout << "Access(D const& as D const&) --->" << AccessCasted<D const&>::access(rcD) <<endl;
192  cout << "Access(const D as const D) --->" << AccessCasted<const D>::access(cD) <<endl;
193  cout << "Access(D const& as const D) --->" << AccessCasted<const D>::access(rcD) <<endl;
194  cout << "Access(const D as const D*) --->" << AccessCasted<const D*>::access(cD) <<endl;
195  cout << "Access(D const& as const D*) --->" << AccessCasted<const D*>::access(rcD) <<endl;
196  cout << "Access(const D* as D const&) --->" << AccessCasted<D const&>::access(pcD) <<endl;
197  cout << "Access(const D* as const D) --->" << AccessCasted<const D>::access(pcD) <<endl;
198  cout << "Access(D const& as D) --->" << AccessCasted<D>::access(rcD) <<endl; // it's OK to construct a new (non-const) object from const ref
199  const D cD1(cD); // likewise it's OK to construct from move-ref. Actually, we're not
200  cout << "Access(D const&& as D) --->" << AccessCasted<D>::access(move(cD1)) <<endl; // moving anything, but it's up to the receiving ctor to prevent that
201  // AccessCasted<D&>::access(rcD); // normal ref from const ref is not const correct
202  // AccessCasted<D*>::access(rcD); // likewise, regular pointer from const ref prohibited
203  // AccessCasted<D&>::access(pcD); // likewise, regular ref from pointer-to-const
204  // AccessCasted<D*>::access(pcD); // and regular pointer from pointer-to-const
205  // AccessCasted<D&&>::access(rcD); // ruled out already because moving a reference is invalid
206  // AccessCasted<D&&>::access(pcD); // ruled out already because moving a dereferenced pointer is invalid
207  // AccessCasted<D&>::access(move(cD)); // ruled out already because taking reference from moved value is invalid
208  // AccessCasted<D*>::access(move(cD)); // and same for taking pointer from a moved value.
209 
210  cout << "=== work cases: actual conversions ==="<<endl;
211  cout << "Access(B& as B&) --->" << AccessCasted<B&>::access(rB) <<endl;
212  cout << "Access(D& as B&) --->" << AccessCasted<B&>::access(rD) <<endl;
213  cout << "Access(B* as B*) --->" << AccessCasted<B*>::access(pB) <<endl;
214  cout << "Access(D* as B*) --->" << AccessCasted<B*>::access(pD) <<endl;
215  cout << "Access(D& as B*) --->" << AccessCasted<B*>::access(rD) <<endl;
216  cout << "Access(D* as B&) --->" << AccessCasted<B&>::access(pD) <<endl;
217  cout << "Access(B*& as B*&) --->" << AccessCasted<B*&>::access(rpB) <<endl;
218  cout << "Access(D*& as D*&) --->" << AccessCasted<D*&>::access(rpD) <<endl;
219  cout << "Access(D& as const B*) --->" << AccessCasted<const B*>::access(rD) <<endl;
220  cout << "Access(D* as B const&) --->" << AccessCasted<B const&>::access(pD) <<endl;
221  cout << "Access(D const& as const B*) --->" << AccessCasted<const B*>::access(rcD) <<endl;
222  cout << "Access(const D* as B const&) --->" << AccessCasted<B const&>::access(pcD) <<endl;
223  // AccessCasted<B*&>::access(rpD); // ruled out, since it would allow to sneak-in a non-D object into the D*
224  // AccessCasted<D&>::access(rB); // any down-casts are ruled out,
225  // AccessCasted<D*>::access(pB); // since neither B nor D has RTTI
226  // AccessCasted<D&>::access(pB); //
227  // AccessCasted<D*>::access(rB); //
228  // AccessCasted<E&>::access(rD); // we need RTTI on both ends to perform a safe dynamic downcast.
229  // AccessCasted<D*>::access((B*)pD); // dangerous, since we have no way to know for sure it's indeed a D object
230  // AccessCasted<E*>::access(pDE); // same here, since E has RTTI but D hasn't, we have no way to find out the real type
231 
232  VERIFY_ERROR (WRONG_TYPE, AccessCasted<F&>::access(rE)); // allowed by typing, but fails at runtime since it isn't an F-object
233  cout << "Access(E(F)& as F&) --->" << AccessCasted<F&>::access(rEF) <<endl;
234  cout << "Access(E(F)* as F*) --->" << AccessCasted<F*>::access(pEF) <<endl;
235  cout << "Access(E(F)* as F&) --->" << AccessCasted<F&>::access(pEF) <<endl;
236  cout << "Access(E(F)& as F*) --->" << AccessCasted<F*>::access(pEF) <<endl;
237  cout << "Access(F* as X*) --->" << AccessCasted<X*>::access(pF) <<endl; // upcast to the other mixin is OK
238  cout << "Access(X(F)* as X*) --->" << AccessCasted<X*>::access(pXF) <<endl; // (and note: address adjustment due to mixin layout)
239  cout << "Access(F* as B&) --->" << AccessCasted<B&>::access(pF) <<endl; // upcast to base
240  cout << "Access(F* as E&) --->" << AccessCasted<E&>::access(pF) <<endl; // upcast to parent (retaining the RTTI)
241  // AccessCasted<X*>::access(pEF); // cross-cast not supported (to complicated to implement)
242  // AccessCasted<F*>::access(pXF); // downcast not possible, since X does not provide RTTI
243 
244  int i = 2;
245  float fp = 3.1415;
246  cout << "Access(int as double) --->" << AccessCasted<double>::access(i) <<endl;
247  cout << "Access(float as long) --->" << AccessCasted<long>::access(fp) <<endl;
248  // AccessCasted<double&>::access(i); // would undermine the type system, thus ruled out
249  // AccessCasted<double const&>::access(i); // allowed, but warning: returning reference to temporary (and the warning is justified)
250 
251  CHECK (isSameObject (d, AccessCasted<B&>::access(d)));
252  CHECK (isSameObject (rD, AccessCasted<B&>::access(pD)));
254  CHECK (!isSameObject (d, AccessCasted<B>::access(rD)));
255 
256  CHECK (isSameObject (f, AccessCasted<F&>::access(rEF)));
257  CHECK (!isSameObject (f, AccessCasted<X&>::access(pF))); // note: address adjustment due to layout of mixin object X
258  }
259  };
260 
261 
263  LAUNCHER (AccessCasted_test, "unit common");
264 
265 
266 
267 }} // namespace lib::meta::test
Helper template to access a given value, possibly converted or casted in a safe way.
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:49
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Simple test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
A collection of frequently used helper functions to support unit testing.
Helper for accessing a value, employing either a conversion or downcast, depending on the relation of...
bool isSameObject(A const &a, B const &b)
compare plain object identity, bypassing any custom comparison operators.
Definition: util.hpp:372