Move reverse manipulation code into ValueDescSet action
[synfig.git] / ETL / test / smart_ptr.cpp
1 /*! ========================================================================
2 ** Extended Template and Library Test Suite
3 ** Smart Pointer 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 //#define DEBUGPOINT()  fprintf(stderr,__FILE__":%d: DEBUGPOINT\n",__LINE__)
23 #define DEBUGPOINT()
24
25 /* === H E A D E R S ======================================================= */
26
27 #include <ETL/smart_ptr>
28 #include <list>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string>
32 #include <map>
33
34 /* === M A C R O S ========================================================= */
35
36 #define NUMBER_OF_OBJECTS       40000
37 using namespace std;
38
39
40 /* === C L A S S E S ======================================================= */
41
42 struct my_test_obj
43 {
44         static int instance_count;
45         int my_id;
46         my_test_obj(int my_id=0):my_id(my_id)
47         {
48                 instance_count++;
49         }
50
51         virtual ~my_test_obj()
52         {
53                 if(instance_count==0)
54                         printf("Error, instance count is going past zero!\n");
55                 instance_count--;
56         }
57
58         bool operator<(const my_test_obj &rhs)const
59         {
60                 return my_id<rhs.my_id;
61         }
62 };
63
64 struct my_other_test_obj : public my_test_obj
65 {
66         static int instance_count;
67         my_other_test_obj(int my_id=0):my_test_obj(my_id)
68         {
69                 instance_count++;
70         }
71         virtual ~my_other_test_obj()
72         {
73                 if(instance_count==0)
74                         printf("Error, instance count is going past zero!\n");
75                 instance_count--;
76         }
77 };
78
79 int my_test_obj::instance_count=0;
80 int my_other_test_obj::instance_count=0;
81
82 typedef etl::smart_ptr<my_test_obj> obj_smart_ptr;
83 typedef etl::smart_ptr<my_other_test_obj> other_obj_smart_ptr;
84 typedef list< obj_smart_ptr > obj_list;
85 typedef list< other_obj_smart_ptr > other_obj_list;
86
87 int smart_ptr_basic_test(void)
88 {
89         printf("smart_ptr: Size of a smart_ptr: %u\n",(unsigned int)sizeof(obj_smart_ptr));
90         printf("smart_ptr: Size of a reference_counter: %u\n",(unsigned int)sizeof(etl::reference_counter));
91
92
93         printf("smart_ptr: Basic test: ");
94         my_test_obj::instance_count=0;
95
96         {
97                 etl::smart_ptr<my_test_obj> obj_smart_ptr(new my_test_obj(rand()));
98         }
99
100         if(my_test_obj::instance_count!=0)
101         {
102                 printf("FAILED!\n");
103                 printf(__FILE__":%d: on create/destroy, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
104                 return 1;
105         }
106
107         {
108                 DEBUGPOINT();
109                 map<string,etl::smart_ptr<my_test_obj> > my_map;
110                 DEBUGPOINT();
111                 //etl::smart_ptr<my_test_obj> obj_smart_ptr(new my_test_obj(rand()));
112                 etl::smart_ptr<my_test_obj> temp;
113                 temp.spawn();
114                 DEBUGPOINT();
115                 temp.reset();
116                 DEBUGPOINT();
117                 my_map["bleh"]=temp;
118                 DEBUGPOINT();
119         }
120
121         if(my_test_obj::instance_count!=0)
122         {
123                 printf("FAILED!\n");
124                 printf(__FILE__":%d: on create/destroy, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
125                 return 1;
126         }
127
128         etl::smart_ptr<my_test_obj> obj_smart_ptr(new my_test_obj(rand()));
129
130         if(obj_smart_ptr != obj_smart_ptr.constant())
131         {
132                 printf("FAILED!\n");
133                 printf(__FILE__":%d: on call to smart_ptr<>::constant().\n",__LINE__);
134                 return 1;
135         }
136
137         printf("PASSED\n");
138
139         return 0;
140 }
141
142 int smart_ptr_general_use_test(void)
143 {
144         printf("smart_ptr: General-use test: ");
145         my_test_obj::instance_count=0;
146
147         obj_list my_list, my_other_list;
148         int i;
149
150         for(i=0;i<NUMBER_OF_OBJECTS;i++)
151                 my_list.push_back( obj_smart_ptr(new my_test_obj(rand())) );
152
153         my_other_list=my_list;
154         if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
155         {
156                 printf("FAILED!\n");
157                 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
158                 return 1;
159         }
160
161         my_list.sort();
162         if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
163         {
164                 printf("FAILED!\n");
165                 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
166                 return 1;
167         }
168
169         my_list.clear();
170         if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
171         {
172                 printf("FAILED!\n");
173                 printf(__FILE__":%d: On copy's clear, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
174                 return 1;
175         }
176
177         my_other_list.clear();
178         if(my_test_obj::instance_count)
179         {
180                 printf("FAILED!\n");
181                 printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
182                 return 1;
183         }
184
185         printf("PASSED\n");
186
187         return 0;
188 }
189
190 int smart_ptr_inheritance_test(void)
191 {
192         printf("smart_ptr: Inheritance test: ");
193         my_test_obj::instance_count=0;
194         my_other_test_obj::instance_count=0;
195
196         other_obj_list my_other_list;
197         int i;
198
199         for(i=0;i<NUMBER_OF_OBJECTS;i++)
200                 my_other_list.push_back( other_obj_smart_ptr(new my_other_test_obj(rand())) );
201
202         obj_list my_list(my_other_list.begin(),my_other_list.end());
203         if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
204         {
205                 printf("FAILED!\n");
206                 printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
207                 return 1;
208         }
209
210         for(i=0;i<NUMBER_OF_OBJECTS;i++)
211                 my_list.push_back( other_obj_smart_ptr(new my_other_test_obj(rand())) );
212         if(my_other_test_obj::instance_count!=NUMBER_OF_OBJECTS*2 ||
213            my_test_obj::instance_count!=my_other_test_obj::instance_count)
214         {
215                 printf("FAILED!\n");
216                 printf(__FILE__":%d: On inherited copy, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS*2);
217                 return 1;
218         }
219
220         my_list.sort();
221         my_other_list.sort();
222         if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS*2)
223         {
224                 printf("FAILED!\n");
225                 printf(__FILE__":%d: On sort, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS*2);
226                 return 1;
227         }
228
229         my_list.clear();
230         if(my_test_obj::instance_count!=NUMBER_OF_OBJECTS)
231         {
232                 printf("FAILED!\n");
233                 printf(__FILE__":%d: On clear, instance count=%d, should be %d.\n",__LINE__,my_test_obj::instance_count,NUMBER_OF_OBJECTS);
234                 return 1;
235         }
236
237         my_other_list.clear();
238         if(my_test_obj::instance_count)
239         {
240                 printf("FAILED!\n");
241                 printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n",__LINE__,my_test_obj::instance_count);
242                 return 1;
243         }
244
245         printf("PASSED\n");
246
247         return 0;
248 }
249
250 void test_func(etl::smart_ptr<my_test_obj> smart_ptr __attribute__ ((unused)))
251 {
252 }
253
254 int loose_smart_ptr_test(void)
255 {
256         printf("smart_ptr: loose_smart_ptr test: ");
257         my_test_obj::instance_count=0;
258
259         etl::loose_smart_ptr<my_test_obj> obj_smart_ptr_loose;
260         etl::smart_ptr<my_test_obj> obj_smart_ptr2;
261
262         {
263                 etl::smart_ptr<my_test_obj> obj_smart_ptr(new my_test_obj(rand()));
264                 if(my_test_obj::instance_count!=1)
265                 {
266                         printf("FAILED!\n");
267                         printf(__FILE__":%d: on smart_ptr assignment from new object, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
268                         return 1;
269                 }
270
271                 obj_smart_ptr_loose=obj_smart_ptr;
272                 if(obj_smart_ptr!=obj_smart_ptr_loose)
273                 {
274                         printf("FAILED!\n");
275                         printf(__FILE__":%d: on loose_smart_ptr assignment\n",__LINE__);
276                         return 1;
277                 }
278
279                 obj_smart_ptr2=obj_smart_ptr_loose;
280                 if(my_test_obj::instance_count!=1)
281                 {
282                         printf("FAILED!\n");
283                         printf(__FILE__":%d: on smart_ptr assignment from loose_smart_ptr, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
284                         return 1;
285                 }
286
287                 test_func(obj_smart_ptr_loose);
288                 if(my_test_obj::instance_count!=1)
289                 {
290                         printf("FAILED!\n");
291                         printf(__FILE__":%d: on smart_ptr assignment from loose_smart_ptr, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
292                         return 1;
293                 }
294         }
295
296         if(my_test_obj::instance_count!=1)
297         {
298                 printf("FAILED!\n");
299                 printf(__FILE__":%d: on create/destroy, instance count=%d, should be 1.\n",__LINE__,my_test_obj::instance_count);
300                 return 1;
301         }
302
303         printf("PASSED\n");
304         return 0;
305 }
306
307 /* === E N T R Y P O I N T ================================================= */
308
309 int main()
310 {
311         int error=0;
312
313         error+=smart_ptr_basic_test();
314         error+=smart_ptr_general_use_test();
315         error+=smart_ptr_inheritance_test();
316         error+=loose_smart_ptr_test();
317
318         return error;
319 }