1 /*
2  * XTL - the eXternalization Template Library (sample code)
3  * by Jose' Orlando Pereira, Universidade do Minho
4  *
5  * jop@di.uminho.pt - http://gsd.di.uminho.pt/~jop
6  *
7  * $Id: alltests.h,v 1.2 2005/04/14 15:28:36 keithsnively Exp $
8  */
9 
10 
11 #ifndef __XTL_TESTS
12 #define __XTL_TESTS
13 
14 #include <list>
15 #include <map>
16 #include <string>
17 #include <utility>
18 
19 class numbers {
20  private:
21  	bool aBool;
22 	char aChar;  unsigned char anUChar;
23 	char aChar2; short aShort; unsigned short anUShort;
24 	char aChar3; int anInt; unsigned int anUInt;
25 	char aChar4; long aLong; unsigned long anULong;
26 	char aChar5; longlong aLongLong; unsignedlonglong anULongLong;
27 	char aChar6; float aFloat; double aDouble;
28 
29  public:
numbers()30  	numbers():
31 		aBool(false), aChar(0), anUChar(0), aChar2(0), aShort(0),
32 		anUShort(0), aChar3(0), anInt(0), anUInt(0), aChar4(0),
33 		aLong(0), anULong(0), aChar5(0), aLongLong(0),
34 		anULongLong(0), aChar6(0), aFloat(0), aDouble(0) {}
35 
init()36 	void init() {
37  		aBool=true;
38 		aChar=-1;   anUChar=2;
39 		aChar2=-3;  aShort=-4; anUShort=5;
40 		aChar3=-6;  anInt=-7;  anUInt=8;
41 		aChar4=-9;  aLong=-10; anULong=11;
42 		aChar5=-12; aLongLong=-13; anULongLong=(unsignedlonglong(12345678ul) << 32) | 9012ul;
43 		aChar6=-15; aFloat=(float)13.14; aDouble=15.16;
44 	}
45 
46 	template <class Stream>
composite(Stream & stream)47 	inline void composite(Stream& stream) {
48 		stream.simple(aBool).simple(aChar).simple(anUChar)
49 			.simple(aChar2).simple(aShort).simple(anUShort)
50 			.simple(aChar3).simple(anInt).simple(anUInt)
51 			.simple(aChar4).simple(aLong).simple(anULong)
52 			.simple(aChar5).simple(aLongLong).simple(anULongLong)
53 			.simple(aChar6).simple(aFloat).simple(aDouble);
54 	}
55 
56 	bool operator==(const numbers& other) const {
57  		return aBool==other.aBool &&
58 			aChar==other.aChar &&
59 			anUChar==other.anUChar &&
60 			aChar2==other.aChar2 &&
61 			aShort==other.aShort &&
62 			anUShort==other.anUShort &&
63 			aChar3==other.aChar3 &&
64 			anInt==other.anInt &&
65 			anUInt==other.anUInt &&
66 			aChar4==other.aChar4 &&
67 			aLong==other.aLong &&
68 			anULong==other.anULong &&
69 			aChar5==other.aChar5 &&
70 			aLongLong==other.aLongLong &&
71 			anULongLong==other.anULongLong &&
72 			aChar6==other.aChar6 &&
73 			aFloat==other.aFloat &&
74 			aDouble==other.aDouble;
75 	}
76 };
77 
78 class strings {
79  private:
80  	char* ptr;
81 	char sptr[100];
82 	std::string cpp;
83 
84  public:
strings()85  	strings(): ptr((char*)0) {}
~strings()86  	~strings() {delete [] ptr;}
87 
init()88  	void init() {
89 		ptr=new char[20];std::strcpy(ptr, "XTL Test String 1");
90 		std::strcpy(sptr, "XTL Test String 2");
91 		cpp="XTL Teste String 3";
92 	}
93 
94 	template <class Stream>
composite(Stream & stream)95 	inline void composite(Stream& stream) {
96 		stream.cstring(ptr).cstring(sptr, 100).simple(cpp);
97 	}
98 
99 	bool operator==(const strings& other) const {
100 		return !std::strcmp(ptr, other.ptr) &&
101 			!std::strcmp(sptr, other.sptr) &&
102 			cpp==other.cpp;
103 	}
104 };
105 
106 class arrays {
107  private:
108  	int fixed[10];
109 
110 	int* variable;
111 	int size;
112 
113  public:
arrays()114  	arrays(): variable((int*)0),size(0) {}
~arrays()115 	~arrays() {delete [] variable;}
116 
init()117 	void init() {
118 		for(int i=0;i<10;i++)
119 			fixed[i]=i*2;
120 
121 		variable=new int[size=10];
122 		for(int i=0;i<size;i++)
123 			variable[i]=i*3;
124 	}
125 
126 	template <class Stream>
composite(Stream & stream)127 	inline void composite(Stream& stream) {
128 		stream.vector(fixed, 10);
129 		stream.array(variable, size);
130 	}
131 
132 	bool operator==(const arrays& other) const {
133 		for(int i=0;i<10;i++)
134 			if (fixed[i]!=other.fixed[i])
135 				return false;
136 
137 		if (size!=other.size)
138 			return false;
139 		for(int i=0;i<size;i++)
140 			if (variable[i]!=other.variable[i])
141 				return false;
142 
143 		return true;
144 	}
145 };
146 
147 class pointers {
148  private:
149  	int* refint;
150 	int* optint;
151 
152  public:
pointers()153  	pointers(): refint((int*)0), optint((int*)0) {}
~pointers()154 	~pointers() {delete refint; delete optint;}
155 
init()156 	void init() {
157 		refint=new int;
158 		*refint=1;
159 	}
160 
161 	template <class Stream>
composite(Stream & stream)162 	inline void composite(Stream& stream) {
163 		stream.reference(refint).pointer(optint);
164 	}
165 
166 	bool operator==(const pointers& other) const {
167 		return *refint==*other.refint &&
168 			optint==other.optint;
169 	}
170 };
171 
172 template <class T>
173 class templates {
174  private:
175  	T value;
176 
177  public:
templates()178  	templates(): value(0) {}
~templates()179  	~templates() {}
180 
init()181 	void init() {
182 		value=10;
183 	}
184 
185 	template <class Stream>
composite(Stream & stream)186 	inline void composite(Stream& stream) {
187 		stream.simple(value);
188 	}
189 
190 	bool operator==(const templates& other) const {
191 		return value==other.value;
192 	}
193 };
194 
195 class containers {
196  private:
197 	std::list<int> lint;
198 	std::map<int,int> mint;
199 
200  public:
init()201 	void init() {
202 		for(int i=0;i<10;i++)
203 			lint.insert(lint.end(),i);
204 		for(int i=0;i<10;i++)
205 			mint.insert(std::pair<int const,int>(i,i*2));
206 	}
207 
208 	template <class Stream>
composite(Stream & stream)209 	inline void composite(Stream& stream) {
210 #ifdef XTL_CONFIG_SIMPLE_CONTAINERS
211 		stream.simple(lint).simple(mint);
212 #else
213 		stream.container(lint);
214 #endif
215 	}
216 
217 	bool operator==(const containers& other) const {
218 		return true; //FIXME
219 	}
220 };
221 
222 #ifdef XTL_CONFIG_CHOICE_MACROS
223 class unions {
224  private:
225 	int discr;
226 	union {
227 		int ival;
228 		float fval;
229 	} val;
230 
231  public:
unions()232 	unions(): discr(0) {val.ival=0;}
233 
init()234 	void init() {
235 		discr=1;
236 		val.fval=1.1f;
237 	}
238 
239 	template <class Stream>
composite(Stream & stream)240 	inline void composite(Stream& stream) {
241 		stream.choices(discr, val.ival, val.fval);
242 	}
243 
244 	bool operator==(const unions& other) const {
245 		return discr==other.discr &&
246 			(discr==0?val.ival==other.val.ival:val.fval==other.val.fval);
247 	}
248 };
249 
250 class base {
251  private:
252  	int i;
253 
254  public:
base()255  	base():i(0) {}
256  	virtual ~base();
257 
258 	virtual void init();
259 
260 	template <class Stream>
composite(Stream & stream)261 	inline void composite(Stream& stream) {
262 		stream.simple(i);
263 	}
264 
265 	bool operator==(const base& other) const {
266 		return i==other.i;
267 	}
268 };
269 
270 class derived: public base {
271  private:
272  	float b;
273 
274  public:
derived()275  	derived(): b(0) {}
276 	~derived();
277 
278 	virtual void init();
279 
280 	template <class Stream>
composite(Stream & stream)281 	inline void composite(Stream& stream) {
282 		base::composite(stream);
283 		stream.simple(b);
284 	}
285 
286 	bool operator==(const derived& other) const {
287 		return base::operator==(other) && b==other.b;
288 	}
289 };
290 
291 class objects {
292  private:
293  	base* bptr1;
294  	base* bptr2;
295 
296  public:
objects()297  	objects(): bptr1((base*)0), bptr2((base*)0) {}
~objects()298  	~objects() {delete bptr1; delete bptr2;}
299 
init()300 	void init() {
301 		bptr1=new base;
302 		bptr1->init();
303 		bptr2=new derived;
304 		bptr2->init();
305 	}
306 
307 	template <class Stream>
composite(Stream & stream)308 	inline void composite(Stream& stream) {
309 		stream.object(bptr1, (base*)0, (derived*)0)
310 			.object(bptr2, (base*)0, (derived*)0);
311 	}
312 
313 	bool operator==(const objects& other) const {
314 		return true;
315 		derived* oder;
316 		try {
317 			oder=dynamic_cast<derived*>(other.bptr2);
catch(...)318 		} catch(...) {
319 			return false;
320 		}
321 		return *bptr1==*other.bptr1 && *bptr2==*oder;
322 	}
323 };
324 #endif
325 
326 class bytes {
327  private:
328  	char fixed[10];
329 
330 	char* variable;
331 	int size;
332 
333  public:
bytes()334  	bytes(): variable((char*)0),size(0) {}
~bytes()335 	~bytes() {delete [] variable;}
336 
init()337 	void init() {
338 		for(int i=0;i<10;i++)
339 			fixed[i]=i*2+40;
340 
341 		variable=new char[size=10];
342 		for(int i=0;i<size;i++)
343 			variable[i]=i*3+50;
344 	}
345 
346 	template <class Stream>
composite(Stream & stream)347 	inline void composite(Stream& stream) {
348 		stream.opaque(fixed, 10).bytes(variable, size);
349 	}
350 
351 	bool operator==(const bytes& other) const {
352 		for(int i=0;i<10;i++)
353 			if (fixed[i]!=other.fixed[i])
354 				return false;
355 
356 		if (size!=other.size)
357 			return false;
358 		for(int i=0;i<size;i++)
359 			if (variable[i]!=other.variable[i])
360 				return false;
361 
362 		return true;
363 	}
364 };
365 
366 class all_tests {
367  private:
368  	numbers n;
369 	strings s;
370 	arrays a;
371 	pointers p;
372 	templates<int> t;
373 	containers c;
374 #ifdef XTL_CONFIG_CHOICE_MACROS
375 	unions u;
376 	objects o;
377 #endif
378 	bytes b;
379 
380  public:
init()381  	void init() {
382 		n.init(); s.init(); a.init(); p.init(); t.init();
383 		c.init();
384 #ifdef XTL_CONFIG_CHOICE_MACROS
385 		u.init(); o.init();
386 #endif
387 		b.init();
388 	}
389 
390 	template <class Stream>
composite(Stream & stream)391 	inline void composite(Stream& stream) {
392 		stream.simple(n);
393 		stream.simple(s);
394 		stream.simple(a);
395 		stream.simple(p);
396 		stream.simple(t);
397 		stream.simple(c);
398 #ifdef XTL_CONFIG_CHOICE_MACROS
399 		stream.simple(u);
400 		stream.simple(o);
401 #endif
402 		stream.simple(b);
403 	}
404 
405 	bool operator==(const all_tests& other) const {
406 		return n==other.n &&
407 			s==other.s &&
408 			a==other.a &&
409 			p==other.p &&
410 			t==other.t &&
411 			c==other.c &&
412 #ifdef XTL_CONFIG_CHOICE_MACROS
413 			u==other.u &&
414 			o==other.o &&
415 #endif
416 			b==other.b &&
417 			true;
418 	}
419 };
420 
421 #if defined(IMPL_ALLTESTS) && defined(XTL_CONFIG_CHOICE_MACROS)
~base()422 base::~base() {}
423 
init()424 void base::init() {
425 	i=2;
426 }
427 
~derived()428 derived::~derived() {}
429 
init()430 void derived::init() {
431 	base::init();
432 	b=3;
433 }
434 #endif
435 
436 #endif
437