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 TestBloat_Raw_Unsafe()
328 nsresult result
= CreateIBar(REINTERPRET_CAST(void**, &barP
));
333 if ( NS_SUCCEEDED( result
= barP
->QueryInterface(NS_GET_IID(IFoo
), REINTERPRET_CAST(void**, &fooP
)) ) )
335 fooP
->print_totals();
351 nsresult result
= CreateIBar( getter_AddRefs(barP
) );
353 nsCOMPtr
<IFoo
> fooP( do_QueryInterface(barP
, &result
) );
356 fooP
->print_totals();
364 nsCOMPtr
<IFoo
> gFoop
;
369 printf(">>main()\n");
371 printf("sizeof(nsCOMPtr<IFoo>) --> %u\n", unsigned(sizeof(nsCOMPtr
<IFoo
>)));
373 TestBloat_Raw_Unsafe();
378 printf("\n### Test 1: will a |nsCOMPtr| call |AddRef| on a pointer assigned into it?\n");
379 nsCOMPtr
<IFoo
> foop( do_QueryInterface(new IFoo
) );
381 printf("\n### Test 2: will a |nsCOMPtr| |Release| its old pointer when a new one is assigned in?\n");
382 foop
= do_QueryInterface(new IFoo
);
384 // [Shouldn't compile] Is it a compile time error to try to |AddRef| by hand?
387 // [Shouldn't compile] Is it a compile time error to try to |Release| be hand?
390 // [Shouldn't compile] Is it a compile time error to try to |delete| an |nsCOMPtr|?
393 printf("\n### Test 3: can you |AddRef| if you must?\n");
394 STATIC_CAST(IFoo
*, foop
)->AddRef();
396 printf("\n### Test 4: can you |Release| if you must?\n");
397 STATIC_CAST(IFoo
*, foop
)->Release();
399 printf("\n### Test 5: will a |nsCOMPtr| |Release| when it goes out of scope?\n");
403 printf("\n### Test 6: will a |nsCOMPtr| call the correct destructor?\n");
404 nsCOMPtr
<IFoo
> foop( do_QueryInterface(new IBar
) );
408 printf("\n### Test 7: can you compare one |nsCOMPtr| with another [!=]?\n");
410 nsCOMPtr
<IFoo
> foo1p( do_QueryInterface(new IFoo
) );
412 // [Shouldn't compile] Is it a compile time error to omit |getter_[doesnt_]AddRef[s]|?
413 //AnIFooPtrPtrContext(&foo1p);
415 // [Shouldn't compile] Is it a compile time error to omit |getter_[doesnt_]AddRef[s]|?
416 //AVoidPtrPtrContext(&foo1p);
418 nsCOMPtr
<IFoo
> foo2p( do_QueryInterface(new IFoo
) );
420 if ( foo1p
!= foo2p
)
421 printf("foo1p != foo2p\n");
423 printf("foo1p == foo2p\n");
425 printf("\n### Test 7.5: can you compare a |nsCOMPtr| with NULL, 0, nsnull [!=]?\n");
427 printf("foo1p != 0\n");
429 printf("0 != foo1p\n");
431 printf("foo1p == 0\n");
433 printf("0 == foo1p\n");
436 IFoo
* raw_foo2p
= foo2p
.get();
438 printf("\n### Test 8: can you compare a |nsCOMPtr| with a raw interface pointer [!=]?\n");
439 if ( foo1p
.get() != raw_foo2p
)
440 printf("foo1p != raw_foo2p\n");
442 printf("foo1p == raw_foo2p\n");
445 printf("\n### Test 9: can you assign one |nsCOMPtr| into another?\n");
448 printf("\n### Test 10: can you compare one |nsCOMPtr| with another [==]?\n");
449 if ( foo1p
== foo2p
)
450 printf("foo1p == foo2p\n");
452 printf("foo1p != foo2p\n");
454 printf("\n### Test 11: can you compare a |nsCOMPtr| with a raw interface pointer [==]?\n");
455 if ( raw_foo2p
== foo2p
.get() )
456 printf("raw_foo2p == foo2p\n");
458 printf("raw_foo2p != foo2p\n");
461 printf("\n### Test 11.5: can you compare a |nsCOMPtr| with a raw interface pointer [==]?\n");
462 if ( nsCOMPtr
<IFoo
>( raw_foo2p
) == foo2p
)
463 printf("raw_foo2p == foo2p\n");
465 printf("raw_foo2p != foo2p\n");
468 printf("\n### Test 12: bare pointer test?\n");
470 printf("foo1p is not NULL\n");
472 printf("foo1p is NULL\n");
474 printf("\n### Test 13: numeric pointer test?\n");
476 printf("foo1p is NULL\n");
478 printf("foo1p is not NULL\n");
482 printf("foo1p allowed compare with in\n");
485 printf("\n### Test 14: how about when two |nsCOMPtr|s referring to the same object go out of scope?\n");
489 printf("\n### Test 15,16 ...setup...\n");
490 IFoo
* raw_foo1p
= new IFoo
;
493 IFoo
* raw_foo2p
= new IFoo
;
496 printf("\n### Test 15: what if I don't want to |AddRef| when I construct?\n");
497 nsCOMPtr
<IFoo
> foo1p( dont_AddRef(raw_foo1p
) );
498 //nsCOMPtr<IFoo> foo1p = dont_AddRef(raw_foo1p);
500 printf("\n### Test 16: what if I don't want to |AddRef| when I assign in?\n");
501 nsCOMPtr
<IFoo
> foo2p
;
502 foo2p
= dont_AddRef(raw_foo2p
);
512 printf("\n### setup for Test 17\n");
514 printf("### Test 17: basic parameter behavior?\n");
515 CreateIFoo( nsGetterAddRefs
<IFoo
>(foop
) );
517 printf("### End Test 17\n");
521 printf("\n### setup for Test 18\n");
523 printf("### Test 18: basic parameter behavior, using the short form?\n");
524 CreateIFoo( getter_AddRefs(foop
) );
526 printf("### End Test 18\n");
530 printf("\n### setup for Test 19, 20\n");
532 printf("### Test 19: reference parameter behavior?\n");
533 set_a_IFoo(address_of(foop
));
535 printf("### Test 20: return value behavior?\n");
536 foop
= return_a_IFoo();
538 printf("### End Test 19, 20\n");
541 printf("\n### setup for Test 21\n");
544 printf("### Test 21: is |QueryInterface| called on assigning in a raw pointer?\n");
545 fooP
= do_QueryInterface(new IFoo
);
547 printf("### End Test 21\n");
550 printf("\n### setup for Test 22\n");
552 fooP
= do_QueryInterface(new IFoo
);
554 nsCOMPtr
<IFoo
> foo2P
;
556 printf("### Test 22: is |QueryInterface| _not_ called when assigning in a smart-pointer of the same type?\n");
559 printf("### End Test 22\n");
562 printf("\n### setup for Test 23\n");
563 nsCOMPtr
<IBar
> barP( do_QueryInterface(new IBar
) );
565 printf("### Test 23: is |QueryInterface| called when assigning in a smart-pointer of a different type?\n");
567 nsCOMPtr
<IFoo
> fooP( do_QueryInterface(barP
) );
569 printf("an IBar* is an IFoo*\n");
571 printf("### End Test 23\n");
575 printf("\n### setup for Test 24\n");
576 nsCOMPtr
<IFoo
> fooP( do_QueryInterface(new IFoo
) );
578 printf("### Test 24: does |forget| avoid an AddRef/Release when assigning to another nsCOMPtr?\n");
579 nsCOMPtr
<IFoo
> fooP2( fooP
.forget() );
581 printf("### End Test 24\n");
586 AnIFooPtrPtrContext( getter_AddRefs(fooP
) );
587 AVoidPtrPtrContext( getter_AddRefs(fooP
) );
588 AnISupportsPtrPtrContext( getter_AddRefs(fooP
) );
593 nsCOMPtr
<nsISupports
> supportsP
;
595 AVoidPtrPtrContext( getter_AddRefs(supportsP
) );
596 AnISupportsPtrPtrContext( getter_AddRefs(supportsP
) );
600 printf("\n### Test 25: will a static |nsCOMPtr| |Release| before program termination?\n");
601 gFoop
= do_QueryInterface(new IFoo
);
603 printf("<<main()\n");