1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
42 #include "nsISupports.h"
44 #ifdef HAVE_CPP_NEW_CASTS
45 #define STATIC_CAST(T,x) static_cast<T>(x)
46 #define REINTERPRET_CAST(T,x) reinterpret_cast<T>(x)
48 #define STATIC_CAST(T,x) ((T)(x))
49 #define REINTERPRET_CAST(T,x) ((T)(x))
54 { 0x6f7652e0, 0xee43, 0x11d1, \
55 { 0x9c, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
57 class IFoo
: public nsISupports
60 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID
)
64 // virtual dtor because IBar uses our Release()
67 NS_IMETHOD_(nsrefcnt
) AddRef();
68 NS_IMETHOD_(nsrefcnt
) Release();
69 NS_IMETHOD
QueryInterface( const nsIID
&, void** );
71 static void print_totals();
74 unsigned int refcount_
;
76 static unsigned int total_constructions_
;
77 static unsigned int total_destructions_
;
80 NS_DEFINE_STATIC_IID_ACCESSOR(IFoo
, NS_IFOO_IID
)
84 // some types I'll need
85 typedef unsigned long NS_RESULT
;
87 // some functions I'll need (and define below)
88 nsresult
CreateIFoo( void** );
89 nsresult
CreateIBar( void** result
);
90 void AnIFooPtrPtrContext( IFoo
** );
91 void AnISupportsPtrPtrContext( nsISupports
** );
92 void AVoidPtrPtrContext( void** );
93 void set_a_IFoo( nsCOMPtr
<IFoo
>* result
);
94 nsCOMPtr
<IFoo
> return_a_IFoo();
99 unsigned int IFoo::total_constructions_
;
100 unsigned int IFoo::total_destructions_
;
107 printf("BEGIN unit tests for |nsCOMPtr|, compiled " __DATE__
"\n");
112 IFoo::print_totals();
113 printf("END unit tests for |nsCOMPtr|.\n");
117 test_message gTestMessage
;
127 printf("total constructions/destructions --> %d/%d\n",
128 total_constructions_
, total_destructions_
);
134 ++total_constructions_
;
135 printf(" new IFoo@%p [#%d]\n",
136 STATIC_CAST(void*, this), total_constructions_
);
141 ++total_destructions_
;
142 printf("IFoo@%p::~IFoo() [#%d]\n",
143 STATIC_CAST(void*, this), total_destructions_
);
150 printf("IFoo@%p::AddRef(), refcount --> %d\n",
151 STATIC_CAST(void*, this), refcount_
);
158 int newcount
= --refcount_
;
162 printf("IFoo@%p::Release(), refcount --> %d\n",
163 STATIC_CAST(void*, this), refcount_
);
167 printf(" delete IFoo@%p\n", STATIC_CAST(void*, this));
168 printf("<<IFoo@%p::Release()\n", STATIC_CAST(void*, this));
176 IFoo::QueryInterface( const nsIID
& aIID
, void** aResult
)
178 printf("IFoo@%p::QueryInterface()\n", STATIC_CAST(void*, this));
179 nsISupports
* rawPtr
= 0;
180 nsresult status
= NS_OK
;
182 if ( aIID
.Equals(GetIID()) )
186 nsID iid_of_ISupports
= NS_ISUPPORTS_IID
;
187 if ( aIID
.Equals(iid_of_ISupports
) )
188 rawPtr
= STATIC_CAST(nsISupports
*, this);
190 status
= NS_ERROR_NO_INTERFACE
;
193 NS_IF_ADDREF(rawPtr
);
200 CreateIFoo( void** result
)
201 // a typical factory function (that calls AddRef)
203 printf(">>CreateIFoo() --> ");
204 IFoo
* foop
= new IFoo
;
205 printf("IFoo@%p\n", STATIC_CAST(void*, foop
));
210 printf("<<CreateIFoo()\n");
215 set_a_IFoo( nsCOMPtr
<IFoo
>* result
)
217 printf(">>set_a_IFoo()\n");
220 nsCOMPtr
<IFoo
> foop( do_QueryInterface(new IFoo
) );
222 printf("<<set_a_IFoo()\n");
228 printf(">>return_a_IFoo()\n");
229 nsCOMPtr
<IFoo
> foop( do_QueryInterface(new IFoo
) );
230 printf("<<return_a_IFoo()\n");
237 #define NS_IBAR_IID \
238 { 0x6f7652e1, 0xee43, 0x11d1, \
239 { 0x9c, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
241 class IBar
: public IFoo
244 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IBAR_IID
)
250 NS_IMETHOD
QueryInterface( const nsIID
&, void** );
253 NS_DEFINE_STATIC_IID_ACCESSOR(IBar
, NS_IBAR_IID
)
257 printf(" new IBar@%p\n", STATIC_CAST(void*, this));
262 printf("IBar@%p::~IBar()\n", STATIC_CAST(void*, this));
266 IBar::QueryInterface( const nsID
& aIID
, void** aResult
)
268 printf("IBar@%p::QueryInterface()\n", STATIC_CAST(void*, this));
269 nsISupports
* rawPtr
= 0;
270 nsresult status
= NS_OK
;
272 if ( aIID
.Equals(GetIID()) )
274 else if ( aIID
.Equals(NS_GET_IID(IFoo
)) )
275 rawPtr
= STATIC_CAST(IFoo
*, this);
278 nsID iid_of_ISupports
= NS_ISUPPORTS_IID
;
279 if ( aIID
.Equals(iid_of_ISupports
) )
280 rawPtr
= STATIC_CAST(nsISupports
*, this);
282 status
= NS_ERROR_NO_INTERFACE
;
285 NS_IF_ADDREF(rawPtr
);
294 CreateIBar( void** result
)
295 // a typical factory function (that calls AddRef)
297 printf(">>CreateIBar() --> ");
298 IBar
* barp
= new IBar
;
299 printf("IBar@%p\n", STATIC_CAST(void*, barp
));
304 printf("<<CreateIBar()\n");
309 AnIFooPtrPtrContext( IFoo
** )
314 AVoidPtrPtrContext( void** )
319 AnISupportsPtrPtrContext( nsISupports
** )
325 #define TEST_EXCEPTIONS 1
327 // HAVE_CPP_EXCEPTIONS is defined automagically on unix
328 #if defined(XP_UNIX) || defined(XP_BEOS) || defined(XP_OS2)
329 #if !defined(HAVE_CPP_EXCEPTIONS)
330 #undef TEST_EXCEPTIONS
334 #ifdef TEST_EXCEPTIONS
340 nsresult result
= CreateIBar(REINTERPRET_CAST(void**, &barP
));
347 if ( NS_SUCCEEDED( result
= barP
->QueryInterface(NS_GET_IID(IFoo
), REINTERPRET_CAST(void**, &fooP
)) ) )
351 fooP
->print_totals();
373 #endif // TEST_EXCEPTIONS
377 TestBloat_Raw_Unsafe()
380 nsresult result
= CreateIBar(REINTERPRET_CAST(void**, &barP
));
385 if ( NS_SUCCEEDED( result
= barP
->QueryInterface(NS_GET_IID(IFoo
), REINTERPRET_CAST(void**, &fooP
)) ) )
387 fooP
->print_totals();
403 nsresult result
= CreateIBar( getter_AddRefs(barP
) );
405 nsCOMPtr
<IFoo
> fooP( do_QueryInterface(barP
, &result
) );
408 fooP
->print_totals();
416 nsCOMPtr
<IFoo
> gFoop
;
421 printf(">>main()\n");
423 printf("sizeof(nsCOMPtr<IFoo>) --> %d\n", sizeof(nsCOMPtr
<IFoo
>));
425 #ifdef TEST_EXCEPTIONS
427 #endif // TEST_EXCEPTIONS
428 TestBloat_Raw_Unsafe();
433 printf("\n### Test 1: will a |nsCOMPtr| call |AddRef| on a pointer assigned into it?\n");
434 nsCOMPtr
<IFoo
> foop( do_QueryInterface(new IFoo
) );
436 printf("\n### Test 2: will a |nsCOMPtr| |Release| its old pointer when a new one is assigned in?\n");
437 foop
= do_QueryInterface(new IFoo
);
439 // [Shouldn't compile] Is it a compile time error to try to |AddRef| by hand?
442 // [Shouldn't compile] Is it a compile time error to try to |Release| be hand?
445 // [Shouldn't compile] Is it a compile time error to try to |delete| an |nsCOMPtr|?
448 printf("\n### Test 3: can you |AddRef| if you must?\n");
449 STATIC_CAST(IFoo
*, foop
)->AddRef();
451 printf("\n### Test 4: can you |Release| if you must?\n");
452 STATIC_CAST(IFoo
*, foop
)->Release();
454 printf("\n### Test 5: will a |nsCOMPtr| |Release| when it goes out of scope?\n");
458 printf("\n### Test 6: will a |nsCOMPtr| call the correct destructor?\n");
459 nsCOMPtr
<IFoo
> foop( do_QueryInterface(new IBar
) );
463 printf("\n### Test 7: can you compare one |nsCOMPtr| with another [!=]?\n");
465 nsCOMPtr
<IFoo
> foo1p( do_QueryInterface(new IFoo
) );
467 // [Shouldn't compile] Is it a compile time error to omit |getter_[doesnt_]AddRef[s]|?
468 //AnIFooPtrPtrContext(&foo1p);
470 // [Shouldn't compile] Is it a compile time error to omit |getter_[doesnt_]AddRef[s]|?
471 //AVoidPtrPtrContext(&foo1p);
473 nsCOMPtr
<IFoo
> foo2p( do_QueryInterface(new IFoo
) );
475 if ( foo1p
!= foo2p
)
476 printf("foo1p != foo2p\n");
478 printf("foo1p == foo2p\n");
480 printf("\n### Test 7.5: can you compare a |nsCOMPtr| with NULL, 0, nsnull [!=]?\n");
482 printf("foo1p != 0\n");
484 printf("0 != foo1p\n");
486 printf("foo1p == 0\n");
488 printf("0 == foo1p\n");
491 IFoo
* raw_foo2p
= foo2p
.get();
493 printf("\n### Test 8: can you compare a |nsCOMPtr| with a raw interface pointer [!=]?\n");
494 if ( foo1p
.get() != raw_foo2p
)
495 printf("foo1p != raw_foo2p\n");
497 printf("foo1p == raw_foo2p\n");
500 printf("\n### Test 9: can you assign one |nsCOMPtr| into another?\n");
503 printf("\n### Test 10: can you compare one |nsCOMPtr| with another [==]?\n");
504 if ( foo1p
== foo2p
)
505 printf("foo1p == foo2p\n");
507 printf("foo1p != foo2p\n");
509 printf("\n### Test 11: can you compare a |nsCOMPtr| with a raw interface pointer [==]?\n");
510 if ( raw_foo2p
== foo2p
.get() )
511 printf("raw_foo2p == foo2p\n");
513 printf("raw_foo2p != foo2p\n");
516 printf("\n### Test 11.5: can you compare a |nsCOMPtr| with a raw interface pointer [==]?\n");
517 if ( nsCOMPtr
<IFoo
>( raw_foo2p
) == foo2p
)
518 printf("raw_foo2p == foo2p\n");
520 printf("raw_foo2p != foo2p\n");
523 printf("\n### Test 12: bare pointer test?\n");
525 printf("foo1p is not NULL\n");
527 printf("foo1p is NULL\n");
529 printf("\n### Test 13: numeric pointer test?\n");
531 printf("foo1p is NULL\n");
533 printf("foo1p is not NULL\n");
537 printf("foo1p allowed compare with in\n");
540 printf("\n### Test 14: how about when two |nsCOMPtr|s referring to the same object go out of scope?\n");
544 printf("\n### Test 15,16 ...setup...\n");
545 IFoo
* raw_foo1p
= new IFoo
;
548 IFoo
* raw_foo2p
= new IFoo
;
551 printf("\n### Test 15: what if I don't want to |AddRef| when I construct?\n");
552 nsCOMPtr
<IFoo
> foo1p( dont_AddRef(raw_foo1p
) );
553 //nsCOMPtr<IFoo> foo1p = dont_AddRef(raw_foo1p);
555 printf("\n### Test 16: what if I don't want to |AddRef| when I assign in?\n");
556 nsCOMPtr
<IFoo
> foo2p
;
557 foo2p
= dont_AddRef(raw_foo2p
);
567 printf("\n### setup for Test 17\n");
569 printf("### Test 17: basic parameter behavior?\n");
570 CreateIFoo( nsGetterAddRefs
<IFoo
>(foop
) );
572 printf("### End Test 17\n");
576 printf("\n### setup for Test 18\n");
578 printf("### Test 18: basic parameter behavior, using the short form?\n");
579 CreateIFoo( getter_AddRefs(foop
) );
581 printf("### End Test 18\n");
585 printf("\n### setup for Test 19, 20\n");
587 printf("### Test 19: reference parameter behavior?\n");
588 set_a_IFoo(address_of(foop
));
590 printf("### Test 20: return value behavior?\n");
591 foop
= return_a_IFoo();
593 printf("### End Test 19, 20\n");
596 printf("\n### setup for Test 21\n");
599 printf("### Test 21: is |QueryInterface| called on assigning in a raw pointer?\n");
600 fooP
= do_QueryInterface(new IFoo
);
602 printf("### End Test 21\n");
605 printf("\n### setup for Test 22\n");
607 fooP
= do_QueryInterface(new IFoo
);
609 nsCOMPtr
<IFoo
> foo2P
;
611 printf("### Test 22: is |QueryInterface| _not_ called when assigning in a smart-pointer of the same type?\n");
614 printf("### End Test 22\n");
617 printf("\n### setup for Test 23\n");
618 nsCOMPtr
<IBar
> barP( do_QueryInterface(new IBar
) );
620 printf("### Test 23: is |QueryInterface| called when assigning in a smart-pointer of a different type?\n");
622 nsCOMPtr
<IFoo
> fooP( do_QueryInterface(barP
) );
624 printf("an IBar* is an IFoo*\n");
626 printf("### End Test 23\n");
630 printf("\n### setup for Test 24\n");
631 nsCOMPtr
<IFoo
> fooP( do_QueryInterface(new IFoo
) );
633 printf("### Test 24: does |forget| avoid an AddRef/Release when assigning to another nsCOMPtr?\n");
634 nsCOMPtr
<IFoo
> fooP2( fooP
.forget() );
636 printf("### End Test 24\n");
641 AnIFooPtrPtrContext( getter_AddRefs(fooP
) );
642 AVoidPtrPtrContext( getter_AddRefs(fooP
) );
643 AnISupportsPtrPtrContext( getter_AddRefs(fooP
) );
648 nsCOMPtr
<nsISupports
> supportsP
;
650 AVoidPtrPtrContext( getter_AddRefs(supportsP
) );
651 AnISupportsPtrPtrContext( getter_AddRefs(supportsP
) );
655 printf("\n### Test 25: will a static |nsCOMPtr| |Release| before program termination?\n");
656 gFoop
= do_QueryInterface(new IFoo
);
658 printf("<<main()\n");