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
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/
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
16 * The Original Code is TestCOMPtrEq.cpp.
18 * The Initial Developer of the Original Code is
20 * Portions created by the Initial Developer are Copyright (C) 2001
21 * the Initial Developer. All Rights Reserved.
24 * L. David Baron <dbaron@dbaron.org> (original author)
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.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsAutoPtr.h"
44 class TestObjectBaseA
{
46 // Virtual dtor for deleting through base class pointer
47 virtual ~TestObjectBaseA() { };
51 class TestObjectBaseB
{
53 // Virtual dtor for deleting through base class pointer
54 virtual ~TestObjectBaseB() { };
58 class TestObject
: public TestObjectBaseA
, public TestObjectBaseB
{
62 printf(" Creating TestObject %p.\n",
63 static_cast<void*>(this));
66 // Virtual dtor for deleting through base class pointer
69 printf(" Destroying TestObject %p.\n",
70 static_cast<void*>(this));
74 class TestRefObjectBaseA
{
77 // Must return |nsrefcnt| to keep |nsDerivedSafe| happy.
78 virtual nsrefcnt
AddRef() = 0;
79 virtual nsrefcnt
Release() = 0;
82 class TestRefObjectBaseB
{
85 virtual nsrefcnt
AddRef() = 0;
86 virtual nsrefcnt
Release() = 0;
89 class TestRefObject
: public TestRefObjectBaseA
, public TestRefObjectBaseB
{
94 printf(" Creating TestRefObject %p.\n",
95 static_cast<void*>(this));
100 printf(" Destroying TestRefObject %p.\n",
101 static_cast<void*>(this));
107 printf(" AddRef to %d on TestRefObject %p.\n",
108 mRefCount
, static_cast<void*>(this));
115 printf(" Release to %d on TestRefObject %p.\n",
116 mRefCount
, static_cast<void*>(this));
117 if (mRefCount
== 0) {
118 delete const_cast<TestRefObject
*>(this);
129 static void CreateTestObject(TestObject
**aResult
)
131 *aResult
= new TestObject();
134 static void CreateTestRefObject(TestRefObject
**aResult
)
136 (*aResult
= new TestRefObject())->AddRef();
139 static void DoSomethingWithTestObject(TestObject
*aIn
)
141 printf(" Doing something with |TestObject| %p.\n",
142 static_cast<void*>(aIn
));
145 static void DoSomethingWithConstTestObject(const TestObject
*aIn
)
147 printf(" Doing something with |const TestObject| %p.\n",
148 static_cast<const void*>(aIn
));
151 static void DoSomethingWithTestRefObject(TestRefObject
*aIn
)
153 printf(" Doing something with |TestRefObject| %p.\n",
154 static_cast<void*>(aIn
));
157 static void DoSomethingWithConstTestRefObject(const TestRefObject
*aIn
)
159 printf(" Doing something with |const TestRefObject| %p.\n",
160 static_cast<const void*>(aIn
));
163 static void DoSomethingWithTestObjectBaseB(TestObjectBaseB
*aIn
)
165 printf(" Doing something with |TestObjectBaseB| %p.\n",
166 static_cast<void*>(aIn
));
169 static void DoSomethingWithConstTestObjectBaseB(const TestObjectBaseB
*aIn
)
171 printf(" Doing something with |const TestObjectBaseB| %p.\n",
172 static_cast<const void*>(aIn
));
175 static void DoSomethingWithTestRefObjectBaseB(TestRefObjectBaseB
*aIn
)
177 printf(" Doing something with |TestRefObjectBaseB| %p.\n",
178 static_cast<void*>(aIn
));
181 static void DoSomethingWithConstTestRefObjectBaseB(const TestRefObjectBaseB
*aIn
)
183 printf(" Doing something with |const TestRefObjectBaseB| %p.\n",
184 static_cast<const void*>(aIn
));
190 printf("Should create one |TestObject|:\n");
191 nsAutoPtr
<TestObject
> pobj( new TestObject() );
192 printf("Should destroy one |TestObject|:\n");
196 printf("Should create one |TestObject|:\n");
197 nsAutoPtr
<TestObject
> pobj( new TestObject() );
198 printf("Should create one |TestObject| and then destroy one:\n");
199 pobj
= new TestObject();
200 printf("Should destroy one |TestObject|:\n");
204 printf("Should create 3 |TestObject|s:\n");
205 nsAutoArrayPtr
<TestObject
> pobj( new TestObject
[3] );
206 printf("Should create 5 |TestObject|s and then destroy 3:\n");
207 pobj
= new TestObject
[5];
208 printf("Should destroy 5 |TestObject|s:\n");
212 printf("Should create and AddRef one |TestRefObject|:\n");
213 nsRefPtr
<TestRefObject
> pobj( new TestRefObject() );
214 printf("Should Release and destroy one |TestRefObject|:\n");
218 printf("Should create and AddRef one |TestRefObject|:\n");
219 nsRefPtr
<TestRefObject
> pobj( new TestRefObject() );
220 printf("Should create and AddRef one |TestRefObject| and then Release and destroy one:\n");
221 pobj
= new TestRefObject();
222 printf("Should Release and destroy one |TestRefObject|:\n");
226 printf("Should create and AddRef one |TestRefObject|:\n");
227 nsRefPtr
<TestRefObject
> p1( new TestRefObject() );
228 printf("Should AddRef one |TestRefObject|:\n");
229 nsRefPtr
<TestRefObject
> p2( p1
);
230 printf("Should Release twice and destroy one |TestRefObject|:\n");
233 printf("\nTesting equality (with all const-ness combinations):\n");
236 nsRefPtr
<TestRefObject
> p1( new TestRefObject() );
237 nsRefPtr
<TestRefObject
> p2( p1
);
238 printf("equality %s.\n",
239 ((p1
== p2
) && !(p1
!= p2
)) ? "OK" : "broken");
243 const nsRefPtr
<TestRefObject
> p1( new TestRefObject() );
244 nsRefPtr
<TestRefObject
> p2( p1
);
245 printf("equality %s.\n",
246 ((p1
== p2
) && !(p1
!= p2
)) ? "OK" : "broken");
250 nsRefPtr
<TestRefObject
> p1( new TestRefObject() );
251 const nsRefPtr
<TestRefObject
> p2( p1
);
252 printf("equality %s.\n",
253 ((p1
== p2
) && !(p1
!= p2
)) ? "OK" : "broken");
257 const nsRefPtr
<TestRefObject
> p1( new TestRefObject() );
258 const nsRefPtr
<TestRefObject
> p2( p1
);
259 printf("equality %s.\n",
260 ((p1
== p2
) && !(p1
!= p2
)) ? "OK" : "broken");
264 nsRefPtr
<TestRefObject
> p1( new TestRefObject() );
265 TestRefObject
* p2
= p1
;
266 printf("equality %s.\n",
267 ((p1
== p2
) && !(p1
!= p2
) && (p2
== p1
) && !(p2
!= p1
)) ? "OK" : "broken");
271 const nsRefPtr
<TestRefObject
> p1( new TestRefObject() );
272 TestRefObject
* p2
= p1
;
273 printf("equality %s.\n",
274 ((p1
== p2
) && !(p1
!= p2
) && (p2
== p1
) && !(p2
!= p1
)) ? "OK" : "broken");
277 #if 0 /* MSVC++ 6.0 can't be coaxed to accept this */
279 nsRefPtr
<TestRefObject
> p1( new TestRefObject() );
280 TestRefObject
* const p2
= p1
;
281 printf("equality %s.\n",
282 ((p1
== p2
) && !(p1
!= p2
) && (p2
== p1
) && !(p2
!= p1
)) ? "OK" : "broken");
286 const nsRefPtr
<TestRefObject
> p1( new TestRefObject() );
287 TestRefObject
* const p2
= p1
;
288 printf("equality %s.\n",
289 ((p1
== p2
) && !(p1
!= p2
) && (p2
== p1
) && !(p2
!= p1
)) ? "OK" : "broken");
291 #endif /* Things that MSVC++ 6.0 can't be coaxed to accept */
294 nsRefPtr
<TestRefObject
> p1( new TestRefObject() );
295 const TestRefObject
* p2
= p1
;
296 printf("equality %s.\n",
297 ((p1
== p2
) && !(p1
!= p2
) && (p2
== p1
) && !(p2
!= p1
)) ? "OK" : "broken");
301 const nsRefPtr
<TestRefObject
> p1( new TestRefObject() );
302 const TestRefObject
* p2
= p1
;
303 printf("equality %s.\n",
304 ((p1
== p2
) && !(p1
!= p2
) && (p2
== p1
) && !(p2
!= p1
)) ? "OK" : "broken");
308 nsRefPtr
<TestRefObject
> p1( new TestRefObject() );
309 const TestRefObject
* const p2
= p1
;
310 printf("equality %s.\n",
311 ((p1
== p2
) && !(p1
!= p2
) && (p2
== p1
) && !(p2
!= p1
)) ? "OK" : "broken");
315 const nsRefPtr
<TestRefObject
> p1( new TestRefObject() );
316 const TestRefObject
* const p2
= p1
;
317 printf("equality %s.\n",
318 ((p1
== p2
) && !(p1
!= p2
) && (p2
== p1
) && !(p2
!= p1
)) ? "OK" : "broken");
321 printf("\nTesting getter_Transfers and getter_AddRefs.\n");
324 nsAutoPtr
<TestObject
> ptr
;
325 printf("Should create one |TestObject|:\n");
326 CreateTestObject(getter_Transfers(ptr
));
327 printf("Should destroy one |TestObject|:\n");
331 nsRefPtr
<TestRefObject
> ptr
;
332 printf("Should create and AddRef one |TestRefObject|:\n");
333 CreateTestRefObject(getter_AddRefs(ptr
));
334 printf("Should Release and destroy one |TestRefObject|:\n");
337 printf("\nTesting casts and equality tests.\n");
339 if ((void*)(TestObject
*)0x1000 ==
340 (void*)(TestObjectBaseB
*)(TestObject
*)0x1000)
341 printf("\n\nAll these tests are meaningless!\n\n\n");
344 nsAutoPtr
<TestObject
> p1(new TestObject());
345 TestObjectBaseB
*p2
= p1
;
346 printf("equality %s.\n",
347 ((static_cast<void*>(p1
) != static_cast<void*>(p2
)) &&
348 (p1
== p2
) && !(p1
!= p2
) && (p2
== p1
) && !(p2
!= p1
))
353 TestObject
*p1
= new TestObject();
354 nsAutoPtr
<TestObjectBaseB
> p2(p1
);
355 printf("equality %s.\n",
356 ((static_cast<void*>(p1
) != static_cast<void*>(p2
)) &&
357 (p1
== p2
) && !(p1
!= p2
) && (p2
== p1
) && !(p2
!= p1
))
362 nsRefPtr
<TestRefObject
> p1
= new TestRefObject();
363 // nsCOMPtr requires a |get| for something like this as well
364 nsRefPtr
<TestRefObjectBaseB
> p2
= p1
.get();
365 printf("equality %s.\n",
366 ((static_cast<void*>(p1
) != static_cast<void*>(p2
)) &&
367 (p1
== p2
) && !(p1
!= p2
) && (p2
== p1
) && !(p2
!= p1
))
372 nsRefPtr
<TestRefObject
> p1
= new TestRefObject();
373 TestRefObjectBaseB
*p2
= p1
;
374 printf("equality %s.\n",
375 ((static_cast<void*>(p1
) != static_cast<void*>(p2
)) &&
376 (p1
== p2
) && !(p1
!= p2
) && (p2
== p1
) && !(p2
!= p1
))
381 TestRefObject
*p1
= new TestRefObject();
382 nsRefPtr
<TestRefObjectBaseB
> p2
= p1
;
383 printf("equality %s.\n",
384 ((static_cast<void*>(p1
) != static_cast<void*>(p2
)) &&
385 (p1
== p2
) && !(p1
!= p2
) && (p2
== p1
) && !(p2
!= p1
))
389 printf("\nTesting |forget()|.\n");
392 printf("Should create one |TestObject|:\n");
393 nsAutoPtr
<TestObject
> pobj( new TestObject() );
394 printf("Should do nothing:\n");
395 nsAutoPtr
<TestObject
> pobj2( pobj
.forget() );
396 printf("Should destroy one |TestObject|:\n");
400 printf("Should create 3 |TestObject|s:\n");
401 nsAutoArrayPtr
<TestObject
> pobj( new TestObject
[3] );
402 printf("Should do nothing:\n");
403 nsAutoArrayPtr
<TestObject
> pobj2( pobj
.forget() );
404 printf("Should destroy 3 |TestObject|s:\n");
408 printf("Should create one |TestRefObject|:\n");
409 nsRefPtr
<TestRefObject
> pobj( new TestRefObject() );
410 printf("Should do nothing:\n");
411 nsRefPtr
<TestRefObject
> pobj2( pobj
.forget() );
412 printf("Should destroy one |TestRefObject|:\n");
416 printf("\nTesting construction.\n");
419 printf("Should create one |TestObject|:\n");
420 nsAutoPtr
<TestObject
> pobj(new TestObject());
421 printf("Should destroy one |TestObject|:\n");
425 printf("Should create 3 |TestObject|s:\n");
426 nsAutoArrayPtr
<TestObject
> pobj(new TestObject
[3]);
427 printf("Should destroy 3 |TestObject|s:\n");
431 printf("Should create and AddRef one |TestRefObject|:\n");
432 nsRefPtr
<TestRefObject
> pobj
= new TestRefObject();
433 printf("Should Release and destroy one |TestRefObject|:\n");
436 printf("\nTesting calling of functions (including array access and casts).\n");
439 printf("Should create one |TestObject|:\n");
440 nsAutoPtr
<TestObject
> pobj(new TestObject());
441 printf("Should do something with one |TestObject|:\n");
442 DoSomethingWithTestObject(pobj
);
443 printf("Should do something with one |TestObject|:\n");
444 DoSomethingWithConstTestObject(pobj
);
445 printf("Should destroy one |TestObject|:\n");
449 printf("Should create 3 |TestObject|s:\n");
450 nsAutoArrayPtr
<TestObject
> pobj(new TestObject
[3]);
451 printf("Should do something with one |TestObject|:\n");
452 DoSomethingWithTestObject(&pobj
[2]);
453 printf("Should do something with one |TestObject|:\n");
454 DoSomethingWithConstTestObject(&pobj
[1]);
455 printf("Should do something with one |TestObject|:\n");
456 DoSomethingWithTestObject(pobj
+ 2);
457 printf("Should do something with one |TestObject|:\n");
458 DoSomethingWithConstTestObject(pobj
+ 1);
459 printf("Should destroy 3 |TestObject|s:\n");
463 printf("Should create and AddRef one |TestRefObject|:\n");
464 nsRefPtr
<TestRefObject
> pobj
= new TestRefObject();
465 printf("Should do something with one |TestRefObject|:\n");
466 DoSomethingWithTestRefObject(pobj
);
467 printf("Should do something with one |TestRefObject|:\n");
468 DoSomethingWithConstTestRefObject(pobj
);
469 printf("Should Release and destroy one |TestRefObject|:\n");
473 printf("Should create one |TestObject|:\n");
474 nsAutoPtr
<TestObject
> pobj(new TestObject());
475 printf("Should do something with one |TestObject|:\n");
476 DoSomethingWithTestObjectBaseB(pobj
);
477 printf("Should do something with one |TestObject|:\n");
478 DoSomethingWithConstTestObjectBaseB(pobj
);
479 printf("Should destroy one |TestObject|:\n");
483 printf("Should create 3 |TestObject|s:\n");
484 nsAutoArrayPtr
<TestObject
> pobj(new TestObject
[3]);
485 printf("Should do something with one |TestObject|:\n");
486 DoSomethingWithTestObjectBaseB(&pobj
[2]);
487 printf("Should do something with one |TestObject|:\n");
488 DoSomethingWithConstTestObjectBaseB(&pobj
[1]);
489 printf("Should do something with one |TestObject|:\n");
490 DoSomethingWithTestObjectBaseB(pobj
+ 2);
491 printf("Should do something with one |TestObject|:\n");
492 DoSomethingWithConstTestObjectBaseB(pobj
+ 1);
493 printf("Should destroy 3 |TestObject|s:\n");
497 printf("Should create and AddRef one |TestRefObject|:\n");
498 nsRefPtr
<TestRefObject
> pobj
= new TestRefObject();
499 printf("Should do something with one |TestRefObject|:\n");
500 DoSomethingWithTestRefObjectBaseB(pobj
);
501 printf("Should do something with one |TestRefObject|:\n");
502 DoSomethingWithConstTestRefObjectBaseB(pobj
);
503 printf("Should Release and destroy one |TestRefObject|:\n");
507 printf("Should create one |TestObject|:\n");
508 const nsAutoPtr
<TestObject
> pobj(new TestObject());
509 printf("Should do something with one |TestObject|:\n");
510 DoSomethingWithTestObject(pobj
);
511 printf("Should do something with one |TestObject|:\n");
512 DoSomethingWithConstTestObject(pobj
);
513 printf("Should destroy one |TestObject|:\n");
517 printf("Should create 3 |TestObject|s:\n");
518 const nsAutoArrayPtr
<TestObject
> pobj(new TestObject
[3]);
519 printf("Should do something with one |TestObject|:\n");
520 DoSomethingWithTestObject(&pobj
[2]);
521 printf("Should do something with one |TestObject|:\n");
522 DoSomethingWithConstTestObject(&pobj
[1]);
523 printf("Should do something with one |TestObject|:\n");
524 DoSomethingWithTestObject(pobj
+ 2);
525 printf("Should do something with one |TestObject|:\n");
526 DoSomethingWithConstTestObject(pobj
+ 1);
527 printf("Should destroy 3 |TestObject|s:\n");
531 printf("Should create and AddRef one |TestRefObject|:\n");
532 const nsRefPtr
<TestRefObject
> pobj
= new TestRefObject();
533 printf("Should do something with one |TestRefObject|:\n");
534 DoSomethingWithTestRefObject(pobj
);
535 printf("Should do something with one |TestRefObject|:\n");
536 DoSomethingWithConstTestRefObject(pobj
);
537 printf("Should Release and destroy one |TestRefObject|:\n");
541 printf("Should create one |TestObject|:\n");
542 const nsAutoPtr
<TestObject
> pobj(new TestObject());
543 printf("Should do something with one |TestObject|:\n");
544 DoSomethingWithTestObjectBaseB(pobj
);
545 printf("Should do something with one |TestObject|:\n");
546 DoSomethingWithConstTestObjectBaseB(pobj
);
547 printf("Should destroy one |TestObject|:\n");
551 printf("Should create 3 |TestObject|s:\n");
552 const nsAutoArrayPtr
<TestObject
> pobj(new TestObject
[3]);
553 printf("Should do something with one |TestObject|:\n");
554 DoSomethingWithTestObjectBaseB(&pobj
[2]);
555 printf("Should do something with one |TestObject|:\n");
556 DoSomethingWithConstTestObjectBaseB(&pobj
[1]);
557 printf("Should do something with one |TestObject|:\n");
558 DoSomethingWithTestObjectBaseB(pobj
+ 2);
559 printf("Should do something with one |TestObject|:\n");
560 DoSomethingWithConstTestObjectBaseB(pobj
+ 1);
561 printf("Should destroy 3 |TestObject|s:\n");
565 printf("Should create and AddRef one |TestRefObject|:\n");
566 const nsRefPtr
<TestRefObject
> pobj
= new TestRefObject();
567 printf("Should do something with one |TestRefObject|:\n");
568 DoSomethingWithTestRefObjectBaseB(pobj
);
569 printf("Should do something with one |TestRefObject|:\n");
570 DoSomethingWithConstTestRefObjectBaseB(pobj
);
571 printf("Should Release and destroy one |TestRefObject|:\n");