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