VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/tests/TestAutoPtr.cpp@ 62264

Last change on this file since 62264 was 62264, checked in by vboxsync, 9 years ago

xpcom: finally remove VBOX_GCC_Wno-delete-non-virtual-dtor as this warning is useful and disabling it would hide potential leaks

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.5 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2// vim:cindent:ts=4:et:sw=4:
3/* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is TestCOMPtrEq.cpp.
17 *
18 * The Initial Developer of the Original Code is
19 * L. David Baron.
20 * Portions created by the Initial Developer are Copyright (C) 2001
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 * L. David Baron <[email protected]> (original author)
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40#include "nsAutoPtr.h"
41#include <stdio.h>
42#include "nscore.h"
43
44class TestObjectBaseA {
45 public:
46 // Virtual dtor for deleting through base class pointer
47 virtual ~TestObjectBaseA() { };
48 int fooA;
49};
50
51class TestObjectBaseB {
52 public:
53 // Virtual dtor for deleting through base class pointer
54 virtual ~TestObjectBaseB() { };
55 int fooB;
56};
57
58class TestObject : public TestObjectBaseA, public TestObjectBaseB {
59 public:
60 TestObject()
61 {
62 printf(" Creating TestObject %p.\n",
63 NS_STATIC_CAST(void*, this));
64 }
65
66 // Virtual dtor for deleting through base class pointer
67 virtual ~TestObject()
68 {
69 printf(" Destroying TestObject %p.\n",
70 NS_STATIC_CAST(void*, this));
71 }
72};
73
74class TestRefObjectBaseA {
75 public:
76 int fooA;
77 // Must return |nsrefcnt| to keep |nsDerivedSafe| happy.
78 virtual nsrefcnt AddRef() = 0;
79 virtual nsrefcnt Release() = 0;
80};
81
82class TestRefObjectBaseB {
83 public:
84 virtual ~TestRefObjectBaseB() {}
85 int fooB;
86 virtual nsrefcnt AddRef() = 0;
87 virtual nsrefcnt Release() = 0;
88};
89
90class TestRefObject : public TestRefObjectBaseA, public TestRefObjectBaseB {
91 public:
92 TestRefObject()
93 : mRefCount(0)
94 {
95 printf(" Creating TestRefObject %p.\n",
96 NS_STATIC_CAST(void*, this));
97 }
98
99 ~TestRefObject()
100 {
101 printf(" Destroying TestRefObject %p.\n",
102 NS_STATIC_CAST(void*, this));
103 }
104
105 nsrefcnt AddRef()
106 {
107 ++mRefCount;
108 printf(" AddRef to %d on TestRefObject %p.\n",
109 mRefCount, NS_STATIC_CAST(void*, this));
110 return mRefCount;
111 }
112
113 nsrefcnt Release()
114 {
115 --mRefCount;
116 printf(" Release to %d on TestRefObject %p.\n",
117 mRefCount, NS_STATIC_CAST(void*, this));
118 if (mRefCount == 0) {
119 delete NS_CONST_CAST(TestRefObject*, this);
120 return 0;
121 }
122 return mRefCount;
123 }
124
125 protected:
126 PRUint32 mRefCount;
127
128};
129
130static void CreateTestObject(TestObject **aResult)
131{
132 *aResult = new TestObject();
133}
134
135static void CreateTestRefObject(TestRefObject **aResult)
136{
137 (*aResult = new TestRefObject())->AddRef();
138}
139
140static void DoSomethingWithTestObject(TestObject *aIn)
141{
142 printf(" Doing something with |TestObject| %p.\n",
143 NS_STATIC_CAST(void*, aIn));
144}
145
146static void DoSomethingWithConstTestObject(const TestObject *aIn)
147{
148 printf(" Doing something with |const TestObject| %p.\n",
149 NS_STATIC_CAST(const void*, aIn));
150}
151
152static void DoSomethingWithTestRefObject(TestRefObject *aIn)
153{
154 printf(" Doing something with |TestRefObject| %p.\n",
155 NS_STATIC_CAST(void*, aIn));
156}
157
158static void DoSomethingWithConstTestRefObject(const TestRefObject *aIn)
159{
160 printf(" Doing something with |const TestRefObject| %p.\n",
161 NS_STATIC_CAST(const void*, aIn));
162}
163
164static void DoSomethingWithTestObjectBaseB(TestObjectBaseB *aIn)
165{
166 printf(" Doing something with |TestObjectBaseB| %p.\n",
167 NS_STATIC_CAST(void*, aIn));
168}
169
170static void DoSomethingWithConstTestObjectBaseB(const TestObjectBaseB *aIn)
171{
172 printf(" Doing something with |const TestObjectBaseB| %p.\n",
173 NS_STATIC_CAST(const void*, aIn));
174}
175
176static void DoSomethingWithTestRefObjectBaseB(TestRefObjectBaseB *aIn)
177{
178 printf(" Doing something with |TestRefObjectBaseB| %p.\n",
179 NS_STATIC_CAST(void*, aIn));
180}
181
182static void DoSomethingWithConstTestRefObjectBaseB(const TestRefObjectBaseB *aIn)
183{
184 printf(" Doing something with |const TestRefObjectBaseB| %p.\n",
185 NS_STATIC_CAST(const void*, aIn));
186}
187
188int main()
189{
190 {
191 printf("Should create one |TestObject|:\n");
192 nsAutoPtr<TestObject> pobj( new TestObject() );
193 printf("Should destroy one |TestObject|:\n");
194 }
195
196 {
197 printf("Should create one |TestObject|:\n");
198 nsAutoPtr<TestObject> pobj( new TestObject() );
199 printf("Should create one |TestObject| and then destroy one:\n");
200 pobj = new TestObject();
201 printf("Should destroy one |TestObject|:\n");
202 }
203
204 {
205 printf("Should create 3 |TestObject|s:\n");
206 nsAutoArrayPtr<TestObject> pobj( new TestObject[3] );
207 printf("Should create 5 |TestObject|s and then destroy 3:\n");
208 pobj = new TestObject[5];
209 printf("Should destroy 5 |TestObject|s:\n");
210 }
211
212 {
213 printf("Should create and AddRef one |TestRefObject|:\n");
214 nsRefPtr<TestRefObject> pobj( new TestRefObject() );
215 printf("Should Release and destroy one |TestRefObject|:\n");
216 }
217
218 {
219 printf("Should create and AddRef one |TestRefObject|:\n");
220 nsRefPtr<TestRefObject> pobj( new TestRefObject() );
221 printf("Should create and AddRef one |TestRefObject| and then Release and destroy one:\n");
222 pobj = new TestRefObject();
223 printf("Should Release and destroy one |TestRefObject|:\n");
224 }
225
226 {
227 printf("Should create and AddRef one |TestRefObject|:\n");
228 nsRefPtr<TestRefObject> p1( new TestRefObject() );
229 printf("Should AddRef one |TestRefObject|:\n");
230 nsRefPtr<TestRefObject> p2( p1 );
231 printf("Should Release twice and destroy one |TestRefObject|:\n");
232 }
233
234 printf("\nTesting equality (with all const-ness combinations):\n");
235
236 {
237 nsRefPtr<TestRefObject> p1( new TestRefObject() );
238 nsRefPtr<TestRefObject> p2( p1 );
239 printf("equality %s.\n",
240 ((p1 == p2) && !(p1 != p2)) ? "OK" : "broken");
241 }
242
243 {
244 const nsRefPtr<TestRefObject> p1( new TestRefObject() );
245 nsRefPtr<TestRefObject> p2( p1 );
246 printf("equality %s.\n",
247 ((p1 == p2) && !(p1 != p2)) ? "OK" : "broken");
248 }
249
250 {
251 nsRefPtr<TestRefObject> p1( new TestRefObject() );
252 const nsRefPtr<TestRefObject> p2( p1 );
253 printf("equality %s.\n",
254 ((p1 == p2) && !(p1 != p2)) ? "OK" : "broken");
255 }
256
257 {
258 const nsRefPtr<TestRefObject> p1( new TestRefObject() );
259 const nsRefPtr<TestRefObject> p2( p1 );
260 printf("equality %s.\n",
261 ((p1 == p2) && !(p1 != p2)) ? "OK" : "broken");
262 }
263
264 {
265 nsRefPtr<TestRefObject> p1( new TestRefObject() );
266 TestRefObject * p2 = p1;
267 printf("equality %s.\n",
268 ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
269 }
270
271 {
272 const nsRefPtr<TestRefObject> p1( new TestRefObject() );
273 TestRefObject * p2 = p1;
274 printf("equality %s.\n",
275 ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
276 }
277
278#if 0 /* MSVC++ 6.0 can't be coaxed to accept this */
279 {
280 nsRefPtr<TestRefObject> p1( new TestRefObject() );
281 TestRefObject * const p2 = p1;
282 printf("equality %s.\n",
283 ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
284 }
285
286 {
287 const nsRefPtr<TestRefObject> p1( new TestRefObject() );
288 TestRefObject * const p2 = p1;
289 printf("equality %s.\n",
290 ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
291 }
292#endif /* Things that MSVC++ 6.0 can't be coaxed to accept */
293
294 {
295 nsRefPtr<TestRefObject> p1( new TestRefObject() );
296 const TestRefObject * p2 = p1;
297 printf("equality %s.\n",
298 ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
299 }
300
301 {
302 const nsRefPtr<TestRefObject> p1( new TestRefObject() );
303 const TestRefObject * p2 = p1;
304 printf("equality %s.\n",
305 ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
306 }
307
308 {
309 nsRefPtr<TestRefObject> p1( new TestRefObject() );
310 const TestRefObject * const p2 = p1;
311 printf("equality %s.\n",
312 ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
313 }
314
315 {
316 const nsRefPtr<TestRefObject> p1( new TestRefObject() );
317 const TestRefObject * const p2 = p1;
318 printf("equality %s.\n",
319 ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
320 }
321
322 printf("\nTesting getter_Transfers and getter_AddRefs.\n");
323
324 {
325 nsAutoPtr<TestObject> ptr;
326 printf("Should create one |TestObject|:\n");
327 CreateTestObject(getter_Transfers(ptr));
328 printf("Should destroy one |TestObject|:\n");
329 }
330
331 {
332 nsRefPtr<TestRefObject> ptr;
333 printf("Should create and AddRef one |TestRefObject|:\n");
334 CreateTestRefObject(getter_AddRefs(ptr));
335 printf("Should Release and destroy one |TestRefObject|:\n");
336 }
337
338 printf("\nTesting casts and equality tests.\n");
339
340 if ((void*)(TestObject*)0x1000 ==
341 (void*)(TestObjectBaseB*)(TestObject*)0x1000)
342 printf("\n\nAll these tests are meaningless!\n\n\n");
343
344 {
345 nsAutoPtr<TestObject> p1(new TestObject());
346 TestObjectBaseB *p2 = p1;
347 printf("equality %s.\n",
348 ((NS_STATIC_CAST(void*, p1) != NS_STATIC_CAST(void*, p2)) &&
349 (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
350 ? "OK" : "broken");
351 }
352
353 {
354 TestObject *p1 = new TestObject();
355 nsAutoPtr<TestObjectBaseB> p2(p1);
356 printf("equality %s.\n",
357 ((NS_STATIC_CAST(void*, p1) != NS_STATIC_CAST(void*, p2)) &&
358 (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
359 ? "OK" : "broken");
360 }
361
362 {
363 nsRefPtr<TestRefObject> p1 = new TestRefObject();
364 // nsCOMPtr requires a |get| for something like this as well
365 nsRefPtr<TestRefObjectBaseB> p2 = p1.get();
366 printf("equality %s.\n",
367 ((NS_STATIC_CAST(void*, p1) != NS_STATIC_CAST(void*, p2)) &&
368 (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
369 ? "OK" : "broken");
370 }
371
372 {
373 nsRefPtr<TestRefObject> p1 = new TestRefObject();
374 TestRefObjectBaseB *p2 = p1;
375 printf("equality %s.\n",
376 ((NS_STATIC_CAST(void*, p1) != NS_STATIC_CAST(void*, p2)) &&
377 (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
378 ? "OK" : "broken");
379 }
380
381 {
382 TestRefObject *p1 = new TestRefObject();
383 nsRefPtr<TestRefObjectBaseB> p2 = p1;
384 printf("equality %s.\n",
385 ((NS_STATIC_CAST(void*, p1) != NS_STATIC_CAST(void*, p2)) &&
386 (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
387 ? "OK" : "broken");
388 }
389
390 printf("\nTesting |forget()|.\n");
391
392 {
393 printf("Should create one |TestObject|:\n");
394 nsAutoPtr<TestObject> pobj( new TestObject() );
395 printf("Should do nothing:\n");
396 nsAutoPtr<TestObject> pobj2( pobj.forget() );
397 printf("Should destroy one |TestObject|:\n");
398 }
399
400 {
401 printf("Should create 3 |TestObject|s:\n");
402 nsAutoArrayPtr<TestObject> pobj( new TestObject[3] );
403 printf("Should do nothing:\n");
404 nsAutoArrayPtr<TestObject> pobj2( pobj.forget() );
405 printf("Should destroy 3 |TestObject|s:\n");
406 }
407
408 printf("\nTesting construction.\n");
409
410 {
411 printf("Should create one |TestObject|:\n");
412 nsAutoPtr<TestObject> pobj(new TestObject());
413 printf("Should destroy one |TestObject|:\n");
414 }
415
416 {
417 printf("Should create 3 |TestObject|s:\n");
418 nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
419 printf("Should destroy 3 |TestObject|s:\n");
420 }
421
422 {
423 printf("Should create and AddRef one |TestRefObject|:\n");
424 nsRefPtr<TestRefObject> pobj = new TestRefObject();
425 printf("Should Release and destroy one |TestRefObject|:\n");
426 }
427
428 printf("\nTesting calling of functions (including array access and casts).\n");
429
430 {
431 printf("Should create one |TestObject|:\n");
432 nsAutoPtr<TestObject> pobj(new TestObject());
433 printf("Should do something with one |TestObject|:\n");
434 DoSomethingWithTestObject(pobj);
435 printf("Should do something with one |TestObject|:\n");
436 DoSomethingWithConstTestObject(pobj);
437 printf("Should destroy one |TestObject|:\n");
438 }
439
440 {
441 printf("Should create 3 |TestObject|s:\n");
442 nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
443 printf("Should do something with one |TestObject|:\n");
444 DoSomethingWithTestObject(&pobj[2]);
445 printf("Should do something with one |TestObject|:\n");
446 DoSomethingWithConstTestObject(&pobj[1]);
447 printf("Should do something with one |TestObject|:\n");
448 DoSomethingWithTestObject(pobj + 2);
449 printf("Should do something with one |TestObject|:\n");
450 DoSomethingWithConstTestObject(pobj + 1);
451 printf("Should destroy 3 |TestObject|s:\n");
452 }
453
454 {
455 printf("Should create and AddRef one |TestRefObject|:\n");
456 nsRefPtr<TestRefObject> pobj = new TestRefObject();
457 printf("Should do something with one |TestRefObject|:\n");
458 DoSomethingWithTestRefObject(pobj);
459 printf("Should do something with one |TestRefObject|:\n");
460 DoSomethingWithConstTestRefObject(pobj);
461 printf("Should Release and destroy one |TestRefObject|:\n");
462 }
463
464 {
465 printf("Should create one |TestObject|:\n");
466 nsAutoPtr<TestObject> pobj(new TestObject());
467 printf("Should do something with one |TestObject|:\n");
468 DoSomethingWithTestObjectBaseB(pobj);
469 printf("Should do something with one |TestObject|:\n");
470 DoSomethingWithConstTestObjectBaseB(pobj);
471 printf("Should destroy one |TestObject|:\n");
472 }
473
474 {
475 printf("Should create 3 |TestObject|s:\n");
476 nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
477 printf("Should do something with one |TestObject|:\n");
478 DoSomethingWithTestObjectBaseB(&pobj[2]);
479 printf("Should do something with one |TestObject|:\n");
480 DoSomethingWithConstTestObjectBaseB(&pobj[1]);
481 printf("Should do something with one |TestObject|:\n");
482 DoSomethingWithTestObjectBaseB(pobj + 2);
483 printf("Should do something with one |TestObject|:\n");
484 DoSomethingWithConstTestObjectBaseB(pobj + 1);
485 printf("Should destroy 3 |TestObject|s:\n");
486 }
487
488 {
489 printf("Should create and AddRef one |TestRefObject|:\n");
490 nsRefPtr<TestRefObject> pobj = new TestRefObject();
491 printf("Should do something with one |TestRefObject|:\n");
492 DoSomethingWithTestRefObjectBaseB(pobj);
493 printf("Should do something with one |TestRefObject|:\n");
494 DoSomethingWithConstTestRefObjectBaseB(pobj);
495 printf("Should Release and destroy one |TestRefObject|:\n");
496 }
497
498 {
499 printf("Should create one |TestObject|:\n");
500 const nsAutoPtr<TestObject> pobj(new TestObject());
501 printf("Should do something with one |TestObject|:\n");
502 DoSomethingWithTestObject(pobj);
503 printf("Should do something with one |TestObject|:\n");
504 DoSomethingWithConstTestObject(pobj);
505 printf("Should destroy one |TestObject|:\n");
506 }
507
508 {
509 printf("Should create 3 |TestObject|s:\n");
510 const nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
511 printf("Should do something with one |TestObject|:\n");
512 DoSomethingWithTestObject(&pobj[2]);
513 printf("Should do something with one |TestObject|:\n");
514 DoSomethingWithConstTestObject(&pobj[1]);
515 printf("Should do something with one |TestObject|:\n");
516 DoSomethingWithTestObject(pobj + 2);
517 printf("Should do something with one |TestObject|:\n");
518 DoSomethingWithConstTestObject(pobj + 1);
519 printf("Should destroy 3 |TestObject|s:\n");
520 }
521
522 {
523 printf("Should create and AddRef one |TestRefObject|:\n");
524 const nsRefPtr<TestRefObject> pobj = new TestRefObject();
525 printf("Should do something with one |TestRefObject|:\n");
526 DoSomethingWithTestRefObject(pobj);
527 printf("Should do something with one |TestRefObject|:\n");
528 DoSomethingWithConstTestRefObject(pobj);
529 printf("Should Release and destroy one |TestRefObject|:\n");
530 }
531
532 {
533 printf("Should create one |TestObject|:\n");
534 const nsAutoPtr<TestObject> pobj(new TestObject());
535 printf("Should do something with one |TestObject|:\n");
536 DoSomethingWithTestObjectBaseB(pobj);
537 printf("Should do something with one |TestObject|:\n");
538 DoSomethingWithConstTestObjectBaseB(pobj);
539 printf("Should destroy one |TestObject|:\n");
540 }
541
542 {
543 printf("Should create 3 |TestObject|s:\n");
544 const nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
545 printf("Should do something with one |TestObject|:\n");
546 DoSomethingWithTestObjectBaseB(&pobj[2]);
547 printf("Should do something with one |TestObject|:\n");
548 DoSomethingWithConstTestObjectBaseB(&pobj[1]);
549 printf("Should do something with one |TestObject|:\n");
550 DoSomethingWithTestObjectBaseB(pobj + 2);
551 printf("Should do something with one |TestObject|:\n");
552 DoSomethingWithConstTestObjectBaseB(pobj + 1);
553 printf("Should destroy 3 |TestObject|s:\n");
554 }
555
556 {
557 printf("Should create and AddRef one |TestRefObject|:\n");
558 const nsRefPtr<TestRefObject> pobj = new TestRefObject();
559 printf("Should do something with one |TestRefObject|:\n");
560 DoSomethingWithTestRefObjectBaseB(pobj);
561 printf("Should do something with one |TestRefObject|:\n");
562 DoSomethingWithConstTestRefObjectBaseB(pobj);
563 printf("Should Release and destroy one |TestRefObject|:\n");
564 }
565
566 return 0;
567}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette