Release 0.61.08
[synfig.git] / ETL / tags / 0.61.06 / test / handle.cpp
1 /*! ========================================================================
2 ** Extended Template and Library Test Suite
3 ** Handle Template Class Test
4 ** $Id$
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 **
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
12 **
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ** General Public License for more details.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #include <ETL/handle>
25 #include <list>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string>
29 #include <map>
30 #include <vector>
31
32 /* === M A C R O S ========================================================= */
33
34 #define NUMBER_OF_OBJECTS       40000
35 using namespace std;
36
37 /* === C L A S S E S ======================================================= */
38
39 struct my_test_obj : public etl::rshared_object
40 {
41         static int instance_count;
42         int my_id;
43         my_test_obj(int my_id=0):my_id(my_id)
44         {
45                 instance_count++;
46         }
47
48         virtual ~my_test_obj()
49         {
50                 if(instance_count==0)
51                         printf("Error, instance count is going past zero!\n");
52                 instance_count--;
53         }
54
55         bool operator<(const my_test_obj &rhs)const
56         {
57                 return my_id<rhs.my_id;
58         }
59 };
60
61 struct my_other_test_obj : public my_test_obj
62 {
63         static int instance_count;
64         my_other_test_obj(int my_id=0):my_test_obj(my_id)
65         {
66                 instance_count++;
67         }
68         virtual ~my_other_test_obj()
69         {
70                 if(instance_count==0)
71                         printf("Error, instance count is going past zero!\n");
72                 instance_count--;
73         }
74 };
75
76 int my_test_obj::instance_count=0;
77 int my_other_test_obj::instance_count=0;
78
79 typedef etl::handle<my_test_obj> obj_handle;
80 typedef etl::rhandle<my_test_obj> robj_handle;
81 typedef etl::handle<my_other_test_obj> other_obj_handle;
82 typedef list< obj_handle > obj_list;
83 typedef list< other_obj_handle > other_obj_list;
84 typedef list< robj_handle > robj_list;
85
86 int handle_basic_test()
87 {
88         printf("handle: Size of a handle: %u\n",(unsigned int)sizeof(etl::handle<int>));
89         printf("handle: Size of a loose_handle: %u\n",(unsigned int)sizeof(etl::loose_handle<int>));
90         printf("handle: Size of a rhandle: %u\n",(unsigned int)sizeof(etl::rhandle<int>));
91         printf("handle: Size of a shared_object: %u\n",(unsigned int)sizeof(etl::shared_object));
92         printf("handle: Size of a rshared_object: %u\n",(unsigned int)sizeof(etl::rshared_object));
93
94         printf("handle: Basic test: ");
95         my_test_obj::instance_count=0;
96
97         {
98                 etl::handle<my_test_obj> obj_handle(new my_test_obj(rand()));
99         }
100
101         if(my_test_obj::instance_count!=0)
102         {
103                 printf("FAILED!\n");
104                 printf(__FILE__":%d: on create/distroy, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
105                 return 1;
106         }
107
108         {
109                 map<string,etl::handle<my_test_obj> > my_map;
110                 etl::handle<my_test_obj> obj_handle(new my_test_obj(rand()));
111                 my_map["bleh"]=obj_handle;
112         }
113
114         if(my_test_obj::instance_count!=0)
115         {
116                 printf("FAILED!\n");
117                 printf(__FILE__":%d: on create/distroy, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
118                 return 1;
119         }
120
121         etl::handle<my_test_obj> obj_handle(new my_test_obj(rand()));
122
123         if(obj_handle != obj_handle.constant())
124         {
125                 printf("FAILED!\n");
126                 printf(__FILE__":%d: on call to handle<>::constant().\n",__LINE__);
127                 return 1;
128         }
129
130         printf("PASSED\n");
131
132         return 0;
133 }
134
135 int handle_general_use_test(void)
136 {
137         printf("handle: General-use test: ");
138         my_test_obj::instance_count=0;
139
140         obj_list my_list, my_other_list;
141         int i;
142
143         for(i=0;i<NUMBER_OF_OBJECTS;i++)
144                 my_list.push_back( obj_handle(new my_test_obj(rand())) );
145
146         my_other_list=my_list;
147         if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
148         {
149                 printf("FAILED!\n");
150                 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
151                 return 1;
152         }
153
154         my_list.sort();
155         if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
156         {
157                 printf("FAILED!\n");
158                 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
159                 return 1;
160         }
161
162         my_list.clear();
163         if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
164         {
165                 printf("FAILED!\n");
166                 printf(__FILE__":%d: On copy's clear, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
167                 return 1;
168         }
169
170         my_other_list.clear();
171         if(my_test_obj::instance_count)
172         {
173                 printf("FAILED!\n");
174                 printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
175                 return 1;
176         }
177
178         printf("PASSED\n");
179
180         return 0;
181 }
182
183         struct ListItem
184         {
185                 robj_handle obj;
186                 int bleh;
187                 int blah;
188                 ListItem(robj_handle obj,int bleh=1, int blah=2):
189                         obj(obj),bleh(bleh),blah(blah) { }
190         };
191
192 int rhandle_general_use_test(void)
193 {
194
195
196         printf("rhandle: General-use test: ");
197         my_test_obj::instance_count=0;
198
199         robj_list my_list;
200         int i;
201
202         robj_handle obj=        new my_test_obj(rand());
203         for(i=0;i<NUMBER_OF_OBJECTS;i++)
204                 my_list.push_back(obj);
205
206         obj_list my_other_list(my_list.begin(),my_list.end());
207
208
209
210         if(obj.count()!=NUMBER_OF_OBJECTS*2+1)
211         {
212                 printf("FAILED!\n");
213                 printf(__FILE__":%d: On copy, handle count=%d, should be %d.\n",__LINE__,obj.count(),NUMBER_OF_OBJECTS*2+1);
214                 return 1;
215         }
216
217         if(obj.rcount()!=NUMBER_OF_OBJECTS+1)
218         {
219                 printf("FAILED!\n");
220                 printf(__FILE__":%d: On copy, rhandle count=%d, should be %d.\n",__LINE__,obj.rcount(),NUMBER_OF_OBJECTS+1);
221                 return 1;
222         }
223
224         my_list.sort();
225         if(obj.rcount()!=NUMBER_OF_OBJECTS+1)
226         {
227                 printf("FAILED!\n");
228                 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,obj.rcount(),NUMBER_OF_OBJECTS+1);
229                 return 1;
230         }
231
232         {robj_handle bleh(obj);}
233
234         if(obj.rcount()!=NUMBER_OF_OBJECTS+1)
235         {
236                 printf("FAILED!\n");
237                 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,obj.rcount(),NUMBER_OF_OBJECTS+1);
238                 return 1;
239         }
240
241         my_other_list.clear();
242
243         if(obj.rcount()!=obj.count())
244         {
245                 printf("FAILED!\n");
246                 printf(__FILE__":%d: On copy's clear, handle count (%d) != rhandle count (%d)\n",__LINE__,obj.count(),obj.rcount());
247                 return 1;
248         }
249
250         if(obj.rcount()!=NUMBER_OF_OBJECTS+1)
251         {
252                 printf("FAILED!\n");
253                 printf(__FILE__":%d: On copy's clear, instance count=%d, should be %d.\n",__LINE__,obj.rcount(),NUMBER_OF_OBJECTS+1);
254                 return 1;
255         }
256
257         robj_handle new_obj=    new my_test_obj(rand());
258
259         int replacements=obj.replace(new_obj);
260
261         if(replacements!=NUMBER_OF_OBJECTS+1)
262         {
263                 printf("FAILED!\n");
264                 printf(__FILE__":%d: Only managed to replace %d, should have replaced %d\n",__LINE__,replacements,NUMBER_OF_OBJECTS+1);
265                 return 1;
266         }
267
268         if(obj!=new_obj)
269         {
270                 printf("FAILED!\n");
271                 printf(__FILE__":%d: On replace, handles should be equal.\n",__LINE__);
272                 return 1;
273         }
274
275         {
276                 robj_handle bleh(obj);
277                 robj_handle blah(obj.get());
278         }
279
280
281         my_list.clear();
282         obj.detach();
283         new_obj.detach();
284
285         if(my_test_obj::instance_count)
286         {
287                 printf("FAILED!\n");
288                 printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
289                 return 1;
290         }
291
292
293
294         std::vector<ListItem> my_item_list;
295         for(i=0;i<NUMBER_OF_OBJECTS;i++)
296                 my_item_list.push_back(ListItem(new my_test_obj(rand()),3,4));
297
298
299         for(i=0;i<100;i++)
300         {
301                 int src,dest;
302                 src=rand()%NUMBER_OF_OBJECTS;
303                 dest=rand()%NUMBER_OF_OBJECTS;
304                 ListItem tmp(my_item_list[src]);
305                 assert(tmp.obj.rcount()>=2);
306                 my_item_list.erase(my_item_list.begin()+src);
307                 assert(tmp.obj.rcount()>=1);
308                 my_item_list.insert(my_item_list.begin()+dest,tmp);
309                 assert(tmp.obj.rcount()>=2);
310         }
311
312         my_item_list.clear();
313
314         if(my_test_obj::instance_count)
315         {
316                 printf("FAILED!\n");
317                 printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
318                 return 1;
319         }
320
321         printf("PASSED\n");
322
323         return 0;
324 }
325
326 int handle_inheritance_test(void)
327 {
328         printf("handle: Inheritance test: ");
329         my_test_obj::instance_count=0;
330         my_other_test_obj::instance_count=0;
331
332         other_obj_list my_other_list;
333         int i;
334
335         for(i=0;i<NUMBER_OF_OBJECTS;i++)
336                 my_other_list.push_back( other_obj_handle(new my_other_test_obj(rand())) );
337
338         obj_list my_list(my_other_list.begin(),my_other_list.end());
339         if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
340         {
341                 printf("FAILED!\n");
342                 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
343                 return 1;
344         }
345
346         for(i=0;i<NUMBER_OF_OBJECTS;i++)
347                 my_list.push_back( other_obj_handle(new my_other_test_obj(rand())) );
348         if(my_other_test_obj::instance_count!=NUMBER_OF_OBJECTS*2 ||
349            my_test_obj::instance_count!=my_other_test_obj::instance_count)
350         {
351                 printf("FAILED!\n");
352                 printf(__FILE__":%d: On inherited copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS*2);
353                 return 1;
354         }
355
356         my_list.sort();
357         my_other_list.sort();
358         if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS*2)
359         {
360                 printf("FAILED!\n");
361                 printf(__FILE__":%d: On sort, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS*2);
362                 return 1;
363         }
364
365         my_list.clear();
366         if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
367         {
368                 printf("FAILED!\n");
369                 printf(__FILE__":%d: On clear, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
370                 return 1;
371         }
372
373         my_other_list.clear();
374         if(my_test_obj::instance_count)
375         {
376                 printf("FAILED!\n");
377                 printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
378                 return 1;
379         }
380
381         printf("PASSED\n");
382
383         return 0;
384 }
385
386 void test_func(etl::handle<my_test_obj> handle)
387 {
388         if(handle) { int i=handle.count(); i++; }
389 }
390
391 int loose_handle_test(void)
392 {
393         printf("handle: loose_handle test: ");
394         my_test_obj::instance_count=0;
395
396         etl::loose_handle<my_test_obj> obj_handle_loose;
397         etl::handle<my_test_obj> obj_handle2;
398
399         {
400                 etl::handle<my_test_obj> obj_handle(new my_test_obj(rand()));
401                 if(my_test_obj::instance_count!=1)
402                 {
403                         printf("FAILED!\n");
404                         printf(__FILE__":%d: on handle assignment from new object, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
405                         return 1;
406                 }
407
408                 obj_handle_loose=obj_handle;
409                 if(obj_handle!=obj_handle_loose)
410                 {
411                         printf("FAILED!\n");
412                         printf(__FILE__":%d: on loose_handle assignment\n",__LINE__);
413                         return 1;
414                 }
415
416                 obj_handle2=obj_handle_loose;
417                 if(my_test_obj::instance_count!=1)
418                 {
419                         printf("FAILED!\n");
420                         printf(__FILE__":%d: on handle assignment from loose_handle, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
421                         return 1;
422                 }
423
424                 test_func(obj_handle_loose);
425                 if(my_test_obj::instance_count!=1)
426                 {
427                         printf("FAILED!\n");
428                         printf(__FILE__":%d: on handle assignment from loose_handle, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
429                         return 1;
430                 }
431         }
432
433         if(my_test_obj::instance_count!=1)
434         {
435                 printf("FAILED!\n");
436                 printf(__FILE__":%d: on create/destroy, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
437                 return 1;
438         }
439
440         printf("PASSED\n");
441         return 0;
442 }
443
444 int handle_cast_test()
445 {
446         printf("handle: casting test: ");
447
448         etl::handle<my_test_obj> obj;
449         etl::handle<my_other_test_obj> other_obj;
450         etl::loose_handle<my_other_test_obj> loose_obj;
451
452         other_obj.spawn();
453         loose_obj=other_obj;
454
455         obj=etl::handle<my_test_obj>::cast_dynamic(loose_obj);
456
457         if(obj!=other_obj)
458         {
459                 printf("FAILED!\n");
460                 printf(__FILE__":%d: on handle assignment from loose_handle.\n",__LINE__);
461                 return 1;
462         }
463
464         printf("PASSED\n");
465         return 0;
466 }
467
468 /* === E N T R Y P O I N T ================================================= */
469
470 int main()
471 {
472         int error=0;
473
474         error+=handle_basic_test();
475         error+=handle_cast_test();
476         error+=handle_general_use_test();
477         error+=handle_inheritance_test();
478         error+=loose_handle_test();
479         error+=rhandle_general_use_test();
480
481         return error;
482 }