Lumiera  0.pre.03
»edityourfreedom«
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 lumiera::error::LERR_(BOTTOM_VALUE);
45 using lumiera::error::LERR_(WRONG_TYPE);
46 
47 
48 namespace util {
49 namespace test {
50 
51 
52 
53 
54 
55 
56  namespace { // Test fixture...
57 
58  struct B {};
59  struct D : B {};
60 
61  struct E : D
62  {
63  virtual ~E() {};
64  };
65 
66  struct X
67  {
68  char x = 'x';
69  };
70 
71  struct F
72  : X
73  , E
74  { };
75 
76 
77  ostream& operator<< (ostream& s, const B& b) { return s << "B{} adr="<<&b; }
78  ostream& operator<< (ostream& s, const D& d) { return s << "D{} adr="<<&d; }
79  ostream& operator<< (ostream& s, const E& e) { return s << "E{} adr="<<&e; }
80  ostream& operator<< (ostream& s, const F& f) { return s << "F{} adr="<<&f; }
81 
82  }//(End)Test fixture
83 
84 
85 
86 
87 
88 
89  /*************************************************************************************************/
98  class AccessCasted_test : public Test
99  {
100  virtual void
102  {
103 
104  D d;
105  D* pD =&d;
106  B* pB =pD;
107  D& rD = *pD;
108  B& rB = *pB;
109 
110  D*& rpD = pD;
111  B*& rpB = pB;
112 
113  E e;
114  E& rE = e;
115  F f;
116  E& rEF = f;
117  E* pEF = &f;
118  X* pXF = &f;
119  F* pF = &f;
120 
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<B&,D*> = " << can_downcast<B&,D*>::value <<endl;
125  cout << "can_downcast<B*,D&> = " << can_downcast<B*,D&>::value <<endl;
126  cout << "can_downcast<B*&,D*&> = " << can_downcast<B*&,D*&>::value <<endl;
127  cout << "can_downcast<D*&,D*&> = " << can_downcast<D*&,D*&>::value <<endl;
128 
129  cout << "can_downcast<D*,E*> = " << can_downcast<D*,E*>::value <<endl;
130  cout << "can_downcast<E*,F*> = " << can_downcast<E*,F*>::value <<endl;
131 
132  cout << "has_RTTI<D*> = " << has_RTTI<D*>::value <<endl;
133  cout << "has_RTTI<E*> = " << has_RTTI<E*>::value <<endl;
134  cout << "has_RTTI<F*> = " << has_RTTI<F*>::value <<endl;
135 
136  cout << "is_convertible<D,D&> = " << std::is_convertible<D,D&>::value <<endl;
137  cout << "is_convertible<D&,D> = " << std::is_convertible<D&,D>::value <<endl;
138 
139 
140  cout << "can_use_dynamic_downcast<D,D&> = " << can_use_dynamic_downcast<D,D&>::value <<endl;
141  cout << "can_use_conversion<D,D&> = " << can_use_conversion<D,D&>::value <<endl;
142  cout << "can_use_dynamic_downcast<B*,D*> = " << can_use_dynamic_downcast<B*,D*>::value <<endl;
143  cout << "can_use_conversion<D*,B*> = " << can_use_conversion<D*,B*>::value <<endl;
144 
145  cout << "can_use_dynamic_downcast<D*&,D*&> = " << can_use_dynamic_downcast<D*&,D*&>::value <<endl;
146  cout << "can_use_conversion<D*&,D*&> = " << can_use_conversion<D*&,D*&>::value <<endl;
147  cout << "can_use_conversion<D*,E*> = " << can_use_conversion<D*,E*>::value <<endl;
148  cout << "can_use_dynamic_downcast<D*&,E*> = " << can_use_dynamic_downcast<D*&,E*>::value <<endl;
149  cout << "can_use_conversion<E*,F*> = " << can_use_conversion<E*,F*>::value <<endl;
150  cout << "can_use_dynamic_downcast<E*,F*> = " << can_use_dynamic_downcast<E*,F*>::value <<endl;
151 
152 
153 
154  cout << "=== standard case: References ==="<<endl;
155  cout << "Access(D as D&) --->" << AccessCasted<D&>::access(d) <<endl;
156  cout << "Access(D& as D&) --->" << AccessCasted<D&>::access(rD) <<endl;
157  D dd1(d);
158  // AccessCasted<D&>::access(move(dd1)); // does not compile since it would be dangerous; we can't take a l-value ref
159  // AccessCasted<D&&>::access(rD); // from a r-value (move) reference and we can't move a l-value ref
160  // AccessCasted<D&&>::access(d); //
161 
162  cout << "=== build a value object ==="<<endl;
163  cout << "Access(D as D) --->" << AccessCasted<D>::access(d) <<endl;
164  cout << "Access(D& as D) --->" << AccessCasted<D>::access(rD) <<endl;
165  cout << "Access(D&& as D) --->" << AccessCasted<D>::access(move(dd1)) <<endl;
166 
167  cout << "=== take a pointer ==="<<endl;
168  cout << "Access(D as D*) --->" << AccessCasted<D*>::access(d) <<endl;
169  cout << "Access(D& as D*) --->" << AccessCasted<D*>::access(rD) <<endl;
170  // AccessCasted<D*>::access(move(dd1)); // should not take value moved by r-value-ref as pointer, otherwise the moved object would be lost
171 
172  cout << "=== dereference a pointer ==="<<endl;
173  cout << "Access(D* as D&) --->" << AccessCasted<D&>::access(pD) <<endl;
174  cout << "Access(D* as D) --->" << AccessCasted<D>::access(pD) <<endl;
175  D* pdd1(pD);
176  cout << "Access(D*&& as D) --->" << AccessCasted<D>::access(move(pdd1)) <<endl;
177  D* pNull(0);
178  VERIFY_ERROR (BOTTOM_VALUE, AccessCasted<D>::access(pNull)); // run-time NULL check
179  // AccessCasted<D&&>::access(pD); // should not move away a value accessed through a pointer, there might be other users
180 
181  cout << "=== const correctness ==="<<endl;
182  cout << "Access(D as D const&) --->" << AccessCasted<D const&>::access(d) <<endl;
183  cout << "Access(D& as D const&) --->" << AccessCasted<D const&>::access(rD) <<endl;
184  cout << "Access(D as const D) --->" << AccessCasted<const D>::access(d) <<endl;
185  cout << "Access(D& as const D) --->" << AccessCasted<const D>::access(rD) <<endl;
186  cout << "Access(D as const D*) --->" << AccessCasted<const D*>::access(d) <<endl;
187  cout << "Access(D& as const D*) --->" << AccessCasted<const D*>::access(rD) <<endl;
188  cout << "Access(D* as D const&) --->" << AccessCasted<D const&>::access(pD) <<endl;
189  cout << "Access(D* as const D) --->" << AccessCasted<const D>::access(pD) <<endl;
190  const D cD(d);
191  D const& rcD(d);
192  const D* pcD(&cD);
193  cout << "Access(const D as D const&) --->" << AccessCasted<D const&>::access(cD) <<endl;
194  cout << "Access(D const& as D const&) --->" << AccessCasted<D const&>::access(rcD) <<endl;
195  cout << "Access(const D as const D) --->" << AccessCasted<const D>::access(cD) <<endl;
196  cout << "Access(D const& as const D) --->" << AccessCasted<const D>::access(rcD) <<endl;
197  cout << "Access(const D as const D*) --->" << AccessCasted<const D*>::access(cD) <<endl;
198  cout << "Access(D const& as const D*) --->" << AccessCasted<const D*>::access(rcD) <<endl;
199  cout << "Access(const D* as D const&) --->" << AccessCasted<D const&>::access(pcD) <<endl;
200  cout << "Access(const D* as const D) --->" << AccessCasted<const D>::access(pcD) <<endl;
201  cout << "Access(D const& as D) --->" << AccessCasted<D>::access(rcD) <<endl; // it's OK to construct a new (non-const) object from const ref
202  const D cD1(cD); // likewise it's OK to construct from move-ref. Actually, we're not
203  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
204  // AccessCasted<D&>::access(rcD); // normal ref from const ref is not const correct
205  // AccessCasted<D*>::access(rcD); // likewise, regular pointer from const ref prohibited
206  // AccessCasted<D&>::access(pcD); // likewise, regular ref from pointer-to-const
207  // AccessCasted<D*>::access(pcD); // and regular pointer from pointer-to-const
208  // AccessCasted<D&&>::access(rcD); // ruled out already because moving a reference is invalid
209  // AccessCasted<D&&>::access(pcD); // ruled out already because moving a dereferenced pointer is invalid
210  // AccessCasted<D&>::access(move(cD)); // ruled out already because taking reference from moved value is invalid
211  // AccessCasted<D*>::access(move(cD)); // and same for taking pointer from a moved value.
212 
213  cout << "=== work cases: actual conversions ==="<<endl;
214  cout << "Access(B& as B&) --->" << AccessCasted<B&>::access(rB) <<endl;
215  cout << "Access(D& as B&) --->" << AccessCasted<B&>::access(rD) <<endl;
216  cout << "Access(B* as B*) --->" << AccessCasted<B*>::access(pB) <<endl;
217  cout << "Access(D* as B*) --->" << AccessCasted<B*>::access(pD) <<endl;
218  cout << "Access(D& as B*) --->" << AccessCasted<B*>::access(rD) <<endl;
219  cout << "Access(D* as B&) --->" << AccessCasted<B&>::access(pD) <<endl;
220  cout << "Access(B*& as B*&) --->" << AccessCasted<B*&>::access(rpB) <<endl;
221  cout << "Access(D*& as D*&) --->" << AccessCasted<D*&>::access(rpD) <<endl;
222  cout << "Access(D& as const B*) --->" << AccessCasted<const B*>::access(rD) <<endl;
223  cout << "Access(D* as B const&) --->" << AccessCasted<B const&>::access(pD) <<endl;
224  cout << "Access(D const& as const B*) --->" << AccessCasted<const B*>::access(rcD) <<endl;
225  cout << "Access(const D* as B const&) --->" << AccessCasted<B const&>::access(pcD) <<endl;
226  // AccessCasted<B*&>::access(rpD); // ruled out, since it would allow to sneak-in a non-D object into the D*
227  // AccessCasted<D&>::access(rB); // any down-casts are ruled out,
228  // AccessCasted<D*>::access(pB); // since neither B nor D has RTTI
229  // AccessCasted<D&>::access(pB); //
230  // AccessCasted<D*>::access(rB); //
231  // AccessCasted<E&>::access(rD); // we need RTTI on both ends to perform a safe dynamic downcast.
232  // AccessCasted<D*>::access((B*)pD); // dangerous, since we have no way to know for sure it's indeed a D object
233  // AccessCasted<E*>::access(pDE); // same here, since E has RTTI but D hasn't, we have no way to find out the real type
234 
235  VERIFY_ERROR (WRONG_TYPE, AccessCasted<F&>::access(rE)); // allowed by typing, but fails at runtime since it isn't an F-object
236  cout << "Access(E(F)& as F&) --->" << AccessCasted<F&>::access(rEF) <<endl;
237  cout << "Access(E(F)* as F*) --->" << AccessCasted<F*>::access(pEF) <<endl;
238  cout << "Access(E(F)* as F&) --->" << AccessCasted<F&>::access(pEF) <<endl;
239  cout << "Access(E(F)& as F*) --->" << AccessCasted<F*>::access(pEF) <<endl;
240  cout << "Access(F* as X*) --->" << AccessCasted<X*>::access(pF) <<endl; // upcast to the other mixin is OK
241  cout << "Access(X(F)* as X*) --->" << AccessCasted<X*>::access(pXF) <<endl; // (and note: address adjustment due to mixin layout)
242  cout << "Access(F* as B&) --->" << AccessCasted<B&>::access(pF) <<endl; // upcast to base
243  cout << "Access(F* as E&) --->" << AccessCasted<E&>::access(pF) <<endl; // upcast to parent (retaining the RTTI)
244  // AccessCasted<X*>::access(pEF); // cross-cast not supported (to complicated to implement)
245  // AccessCasted<F*>::access(pXF); // downcast not possible, since X does not provide RTTI
246 
247  int i = 2;
248  float fp = 3.1415;
249  cout << "Access(int as double) --->" << AccessCasted<double>::access(i) <<endl;
250  cout << "Access(float as long) --->" << AccessCasted<long>::access(fp) <<endl;
251  // AccessCasted<double&>::access(i); // would undermine the type system, thus ruled out
252  // AccessCasted<double const&>::access(i); // allowed, but warning: returning reference to temporary (and the warning is justified)
253 
254  CHECK (isSameObject (d, AccessCasted<B&>::access(d)));
255  CHECK (isSameObject (rD, AccessCasted<B&>::access(pD)));
257  CHECK (!isSameObject (d, AccessCasted<B>::access(rD)));
258 
259  CHECK (isSameObject (f, AccessCasted<F&>::access(rEF)));
260  CHECK (!isSameObject (f, AccessCasted<X&>::access(pF))); // note: address adjustment due to layout of mixin object X
261  }
262  };
263 
264 
266  LAUNCHER (AccessCasted_test, "unit common");
267 
268 
269 
270 }} // 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.
static if_can_use_dynamic_downcast< SRC &&, TAR >::type access(SRC &&elem)
Definition: run.hpp:49
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify a statement indeed raises an exception.
LAUNCHER(FormatCOUT_test,"unit common")
bool isSameObject(A const &a, B const &b)
compare plain object identity, bypassing any custom comparison operators.
Definition: util.hpp:337
std::vector< string > & Arg
Definition: run.hpp:54
#define LERR_(_NAME_)
Definition: error.hpp:58
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.
std::ostream & operator<<(std::ostream &os, _Fmt const &fmt)
send the formatted buffer directly to the output stream.
Helper for accessing a value, employing either a conversion or downcast, depending on the relation of...