xpcom/tests/TestRefPtr.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include <assert.h>
michael@0 7 #include <stdio.h>
michael@0 8 #include "nsCOMPtr.h"
michael@0 9 #include "nsAutoPtr.h"
michael@0 10 #include "nsISupports.h"
michael@0 11
michael@0 12 #define NS_FOO_IID \
michael@0 13 { 0x6f7652e0, 0xee43, 0x11d1, \
michael@0 14 { 0x9c, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
michael@0 15
michael@0 16 class Foo : public nsISupports
michael@0 17 {
michael@0 18 public:
michael@0 19 NS_DECLARE_STATIC_IID_ACCESSOR(NS_FOO_IID)
michael@0 20
michael@0 21 public:
michael@0 22 Foo();
michael@0 23 // virtual dtor because Bar uses our Release()
michael@0 24 virtual ~Foo();
michael@0 25
michael@0 26 NS_IMETHOD_(MozExternalRefCountType) AddRef();
michael@0 27 NS_IMETHOD_(MozExternalRefCountType) Release();
michael@0 28 NS_IMETHOD QueryInterface( const nsIID&, void** );
michael@0 29
michael@0 30 static void print_totals();
michael@0 31
michael@0 32 private:
michael@0 33 unsigned int refcount_;
michael@0 34
michael@0 35 static unsigned int total_constructions_;
michael@0 36 static unsigned int total_destructions_;
michael@0 37 };
michael@0 38
michael@0 39 NS_DEFINE_STATIC_IID_ACCESSOR(Foo, NS_FOO_IID)
michael@0 40
michael@0 41 class Bar;
michael@0 42
michael@0 43 // some types I'll need
michael@0 44 typedef unsigned long NS_RESULT;
michael@0 45
michael@0 46 // some functions I'll need (and define below)
michael@0 47 nsresult CreateFoo( void** );
michael@0 48 nsresult CreateBar( void** result );
michael@0 49 void AnFooPtrPtrContext( Foo** );
michael@0 50 void AnISupportsPtrPtrContext( nsISupports** );
michael@0 51 void AVoidPtrPtrContext( void** );
michael@0 52 void set_a_Foo( nsRefPtr<Foo>* result );
michael@0 53 nsRefPtr<Foo> return_a_Foo();
michael@0 54
michael@0 55
michael@0 56
michael@0 57
michael@0 58 unsigned int Foo::total_constructions_;
michael@0 59 unsigned int Foo::total_destructions_;
michael@0 60
michael@0 61 class test_message
michael@0 62 {
michael@0 63 public:
michael@0 64 test_message()
michael@0 65 {
michael@0 66 printf("BEGIN unit tests for |nsRefPtr|, compiled " __DATE__ "\n");
michael@0 67 }
michael@0 68
michael@0 69 ~test_message()
michael@0 70 {
michael@0 71 Foo::print_totals();
michael@0 72 printf("END unit tests for |nsRefPtr|.\n");
michael@0 73 }
michael@0 74 };
michael@0 75
michael@0 76 test_message gTestMessage;
michael@0 77
michael@0 78
michael@0 79 /*
michael@0 80 ...
michael@0 81 */
michael@0 82
michael@0 83 void
michael@0 84 Foo::print_totals()
michael@0 85 {
michael@0 86 printf("total constructions/destructions --> %d/%d\n",
michael@0 87 total_constructions_, total_destructions_);
michael@0 88 }
michael@0 89
michael@0 90 Foo::Foo()
michael@0 91 : refcount_(0)
michael@0 92 {
michael@0 93 ++total_constructions_;
michael@0 94 printf(" new Foo@%p [#%d]\n",
michael@0 95 static_cast<void*>(this), total_constructions_);
michael@0 96 }
michael@0 97
michael@0 98 Foo::~Foo()
michael@0 99 {
michael@0 100 ++total_destructions_;
michael@0 101 printf("Foo@%p::~Foo() [#%d]\n",
michael@0 102 static_cast<void*>(this), total_destructions_);
michael@0 103 }
michael@0 104
michael@0 105 MozExternalRefCountType
michael@0 106 Foo::AddRef()
michael@0 107 {
michael@0 108 ++refcount_;
michael@0 109 printf("Foo@%p::AddRef(), refcount --> %d\n",
michael@0 110 static_cast<void*>(this), refcount_);
michael@0 111 return refcount_;
michael@0 112 }
michael@0 113
michael@0 114 MozExternalRefCountType
michael@0 115 Foo::Release()
michael@0 116 {
michael@0 117 int newcount = --refcount_;
michael@0 118 if ( newcount == 0 )
michael@0 119 printf(">>");
michael@0 120
michael@0 121 printf("Foo@%p::Release(), refcount --> %d\n",
michael@0 122 static_cast<void*>(this), refcount_);
michael@0 123
michael@0 124 if ( newcount == 0 )
michael@0 125 {
michael@0 126 printf(" delete Foo@%p\n", static_cast<void*>(this));
michael@0 127 printf("<<Foo@%p::Release()\n", static_cast<void*>(this));
michael@0 128 delete this;
michael@0 129 }
michael@0 130
michael@0 131 return newcount;
michael@0 132 }
michael@0 133
michael@0 134 nsresult
michael@0 135 Foo::QueryInterface( const nsIID& aIID, void** aResult )
michael@0 136 {
michael@0 137 printf("Foo@%p::QueryInterface()\n", static_cast<void*>(this));
michael@0 138 nsISupports* rawPtr = 0;
michael@0 139 nsresult status = NS_OK;
michael@0 140
michael@0 141 if ( aIID.Equals(NS_GET_IID(Foo)) )
michael@0 142 rawPtr = this;
michael@0 143 else
michael@0 144 {
michael@0 145 nsID iid_of_ISupports = NS_ISUPPORTS_IID;
michael@0 146 if ( aIID.Equals(iid_of_ISupports) )
michael@0 147 rawPtr = static_cast<nsISupports*>(this);
michael@0 148 else
michael@0 149 status = NS_ERROR_NO_INTERFACE;
michael@0 150 }
michael@0 151
michael@0 152 NS_IF_ADDREF(rawPtr);
michael@0 153 *aResult = rawPtr;
michael@0 154
michael@0 155 return status;
michael@0 156 }
michael@0 157
michael@0 158 nsresult
michael@0 159 CreateFoo( void** result )
michael@0 160 // a typical factory function (that calls AddRef)
michael@0 161 {
michael@0 162 printf(">>CreateFoo() --> ");
michael@0 163 Foo* foop = new Foo;
michael@0 164 printf("Foo@%p\n", static_cast<void*>(foop));
michael@0 165
michael@0 166 foop->AddRef();
michael@0 167 *result = foop;
michael@0 168
michael@0 169 printf("<<CreateFoo()\n");
michael@0 170 return NS_OK;
michael@0 171 }
michael@0 172
michael@0 173 void
michael@0 174 set_a_Foo( nsRefPtr<Foo>* result )
michael@0 175 {
michael@0 176 printf(">>set_a_Foo()\n");
michael@0 177 assert(result);
michael@0 178
michael@0 179 nsRefPtr<Foo> foop( do_QueryObject(new Foo) );
michael@0 180 *result = foop;
michael@0 181 printf("<<set_a_Foo()\n");
michael@0 182 }
michael@0 183
michael@0 184 nsRefPtr<Foo>
michael@0 185 return_a_Foo()
michael@0 186 {
michael@0 187 printf(">>return_a_Foo()\n");
michael@0 188 nsRefPtr<Foo> foop( do_QueryObject(new Foo) );
michael@0 189 printf("<<return_a_Foo()\n");
michael@0 190 return foop;
michael@0 191 }
michael@0 192
michael@0 193
michael@0 194
michael@0 195
michael@0 196 #define NS_BAR_IID \
michael@0 197 { 0x6f7652e1, 0xee43, 0x11d1, \
michael@0 198 { 0x9c, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
michael@0 199
michael@0 200 class Bar : public Foo
michael@0 201 {
michael@0 202 public:
michael@0 203 NS_DECLARE_STATIC_IID_ACCESSOR(NS_BAR_IID)
michael@0 204
michael@0 205 public:
michael@0 206 Bar();
michael@0 207 virtual ~Bar();
michael@0 208
michael@0 209 NS_IMETHOD QueryInterface( const nsIID&, void** );
michael@0 210 };
michael@0 211
michael@0 212 NS_DEFINE_STATIC_IID_ACCESSOR(Bar, NS_BAR_IID)
michael@0 213
michael@0 214 Bar::Bar()
michael@0 215 {
michael@0 216 printf(" new Bar@%p\n", static_cast<void*>(this));
michael@0 217 }
michael@0 218
michael@0 219 Bar::~Bar()
michael@0 220 {
michael@0 221 printf("Bar@%p::~Bar()\n", static_cast<void*>(this));
michael@0 222 }
michael@0 223
michael@0 224 nsresult
michael@0 225 Bar::QueryInterface( const nsID& aIID, void** aResult )
michael@0 226 {
michael@0 227 printf("Bar@%p::QueryInterface()\n", static_cast<void*>(this));
michael@0 228 nsISupports* rawPtr = 0;
michael@0 229 nsresult status = NS_OK;
michael@0 230
michael@0 231 if ( aIID.Equals(NS_GET_IID(Bar)) )
michael@0 232 rawPtr = this;
michael@0 233 else if ( aIID.Equals(NS_GET_IID(Foo)) )
michael@0 234 rawPtr = static_cast<Foo*>(this);
michael@0 235 else
michael@0 236 {
michael@0 237 nsID iid_of_ISupports = NS_ISUPPORTS_IID;
michael@0 238 if ( aIID.Equals(iid_of_ISupports) )
michael@0 239 rawPtr = static_cast<nsISupports*>(this);
michael@0 240 else
michael@0 241 status = NS_ERROR_NO_INTERFACE;
michael@0 242 }
michael@0 243
michael@0 244 NS_IF_ADDREF(rawPtr);
michael@0 245 *aResult = rawPtr;
michael@0 246
michael@0 247 return status;
michael@0 248 }
michael@0 249
michael@0 250
michael@0 251
michael@0 252 nsresult
michael@0 253 CreateBar( void** result )
michael@0 254 // a typical factory function (that calls AddRef)
michael@0 255 {
michael@0 256 printf(">>CreateBar() --> ");
michael@0 257 Bar* barp = new Bar;
michael@0 258 printf("Bar@%p\n", static_cast<void*>(barp));
michael@0 259
michael@0 260 barp->AddRef();
michael@0 261 *result = barp;
michael@0 262
michael@0 263 printf("<<CreateBar()\n");
michael@0 264 return NS_OK;
michael@0 265 }
michael@0 266
michael@0 267 void
michael@0 268 AnFooPtrPtrContext( Foo** )
michael@0 269 {
michael@0 270 }
michael@0 271
michael@0 272 void
michael@0 273 AVoidPtrPtrContext( void** )
michael@0 274 {
michael@0 275 }
michael@0 276
michael@0 277 void
michael@0 278 AnISupportsPtrPtrContext( nsISupports** )
michael@0 279 {
michael@0 280 }
michael@0 281
michael@0 282 nsresult
michael@0 283 TestBloat_Raw_Unsafe()
michael@0 284 {
michael@0 285 Bar* barP = 0;
michael@0 286 nsresult result = CreateBar(reinterpret_cast<void**>(&barP));
michael@0 287
michael@0 288 if ( barP )
michael@0 289 {
michael@0 290 Foo* fooP = 0;
michael@0 291 if ( NS_SUCCEEDED( result = barP->QueryInterface(NS_GET_IID(Foo), reinterpret_cast<void**>(&fooP)) ) )
michael@0 292 {
michael@0 293 fooP->print_totals();
michael@0 294 NS_RELEASE(fooP);
michael@0 295 }
michael@0 296
michael@0 297 NS_RELEASE(barP);
michael@0 298 }
michael@0 299
michael@0 300 return result;
michael@0 301 }
michael@0 302
michael@0 303
michael@0 304 static
michael@0 305 nsresult
michael@0 306 TestBloat_Smart()
michael@0 307 {
michael@0 308 nsRefPtr<Bar> barP;
michael@0 309 nsresult result = CreateBar( getter_AddRefs(barP) );
michael@0 310
michael@0 311 nsRefPtr<Foo> fooP( do_QueryObject(barP, &result) );
michael@0 312
michael@0 313 if ( fooP )
michael@0 314 fooP->print_totals();
michael@0 315
michael@0 316 return result;
michael@0 317 }
michael@0 318
michael@0 319
michael@0 320
michael@0 321
michael@0 322 nsRefPtr<Foo> gFoop;
michael@0 323
michael@0 324 int
michael@0 325 main()
michael@0 326 {
michael@0 327 printf(">>main()\n");
michael@0 328
michael@0 329 printf("sizeof(nsRefPtr<Foo>) --> %u\n", unsigned(sizeof(nsRefPtr<Foo>)));
michael@0 330
michael@0 331 TestBloat_Raw_Unsafe();
michael@0 332 TestBloat_Smart();
michael@0 333
michael@0 334
michael@0 335 {
michael@0 336 printf("\n### Test 1: will a |nsCOMPtr| call |AddRef| on a pointer assigned into it?\n");
michael@0 337 nsRefPtr<Foo> foop( do_QueryObject(new Foo) );
michael@0 338
michael@0 339 printf("\n### Test 2: will a |nsCOMPtr| |Release| its old pointer when a new one is assigned in?\n");
michael@0 340 foop = do_QueryObject(new Foo);
michael@0 341
michael@0 342 // [Shouldn't compile] Is it a compile time error to try to |AddRef| by hand?
michael@0 343 //foop->AddRef();
michael@0 344
michael@0 345 // [Shouldn't compile] Is it a compile time error to try to |Release| be hand?
michael@0 346 //foop->Release();
michael@0 347
michael@0 348 // [Shouldn't compile] Is it a compile time error to try to |delete| an |nsCOMPtr|?
michael@0 349 //delete foop;
michael@0 350
michael@0 351 printf("\n### Test 3: can you |AddRef| if you must?\n");
michael@0 352 static_cast<Foo*>(foop)->AddRef();
michael@0 353
michael@0 354 printf("\n### Test 4: can you |Release| if you must?\n");
michael@0 355 static_cast<Foo*>(foop)->Release();
michael@0 356
michael@0 357 printf("\n### Test 5: will a |nsCOMPtr| |Release| when it goes out of scope?\n");
michael@0 358 }
michael@0 359
michael@0 360 {
michael@0 361 printf("\n### Test 6: will a |nsCOMPtr| call the correct destructor?\n");
michael@0 362 nsRefPtr<Foo> foop( do_QueryObject(new Bar) );
michael@0 363 }
michael@0 364
michael@0 365 {
michael@0 366 printf("\n### Test 7: can you compare one |nsCOMPtr| with another [!=]?\n");
michael@0 367
michael@0 368 nsRefPtr<Foo> foo1p( do_QueryObject(new Foo) );
michael@0 369
michael@0 370 // [Shouldn't compile] Is it a compile time error to omit |getter_[doesnt_]AddRef[s]|?
michael@0 371 //AnFooPtrPtrContext(&foo1p);
michael@0 372
michael@0 373 // [Shouldn't compile] Is it a compile time error to omit |getter_[doesnt_]AddRef[s]|?
michael@0 374 //AVoidPtrPtrContext(&foo1p);
michael@0 375
michael@0 376 nsRefPtr<Foo> foo2p( do_QueryObject(new Foo) );
michael@0 377
michael@0 378 if ( foo1p != foo2p )
michael@0 379 printf("foo1p != foo2p\n");
michael@0 380 else
michael@0 381 printf("foo1p == foo2p\n");
michael@0 382
michael@0 383 printf("\n### Test 7.5: can you compare a |nsCOMPtr| with NULL, 0, nullptr [!=]?\n");
michael@0 384 if ( foo1p != 0 )
michael@0 385 printf("foo1p != 0\n");
michael@0 386 if ( 0 != foo1p )
michael@0 387 printf("0 != foo1p\n");
michael@0 388 if ( foo1p == 0 )
michael@0 389 printf("foo1p == 0\n");
michael@0 390 if ( 0 == foo1p )
michael@0 391 printf("0 == foo1p\n");
michael@0 392
michael@0 393
michael@0 394 Foo* raw_foo2p = foo2p.get();
michael@0 395
michael@0 396 printf("\n### Test 8: can you compare a |nsCOMPtr| with a raw interface pointer [!=]?\n");
michael@0 397 if ( foo1p.get() != raw_foo2p )
michael@0 398 printf("foo1p != raw_foo2p\n");
michael@0 399 else
michael@0 400 printf("foo1p == raw_foo2p\n");
michael@0 401
michael@0 402
michael@0 403 printf("\n### Test 9: can you assign one |nsCOMPtr| into another?\n");
michael@0 404 foo1p = foo2p;
michael@0 405
michael@0 406 printf("\n### Test 10: can you compare one |nsCOMPtr| with another [==]?\n");
michael@0 407 if ( foo1p == foo2p )
michael@0 408 printf("foo1p == foo2p\n");
michael@0 409 else
michael@0 410 printf("foo1p != foo2p\n");
michael@0 411
michael@0 412 printf("\n### Test 11: can you compare a |nsCOMPtr| with a raw interface pointer [==]?\n");
michael@0 413 if ( raw_foo2p == foo2p.get() )
michael@0 414 printf("raw_foo2p == foo2p\n");
michael@0 415 else
michael@0 416 printf("raw_foo2p != foo2p\n");
michael@0 417
michael@0 418 #if 1
michael@0 419 printf("\n### Test 11.5: can you compare a |nsCOMPtr| with a raw interface pointer [==]?\n");
michael@0 420 if ( nsRefPtr<Foo>( raw_foo2p ) == foo2p )
michael@0 421 printf("raw_foo2p == foo2p\n");
michael@0 422 else
michael@0 423 printf("raw_foo2p != foo2p\n");
michael@0 424 #endif
michael@0 425
michael@0 426 printf("\n### Test 12: bare pointer test?\n");
michael@0 427 if ( foo1p )
michael@0 428 printf("foo1p is not NULL\n");
michael@0 429 else
michael@0 430 printf("foo1p is NULL\n");
michael@0 431
michael@0 432 printf("\n### Test 13: numeric pointer test?\n");
michael@0 433 if ( foo1p == 0 )
michael@0 434 printf("foo1p is NULL\n");
michael@0 435 else
michael@0 436 printf("foo1p is not NULL\n");
michael@0 437
michael@0 438 #if 0
michael@0 439 if ( foo1p == 1 )
michael@0 440 printf("foo1p allowed compare with in\n");
michael@0 441 #endif
michael@0 442
michael@0 443 printf("\n### Test 14: how about when two |nsCOMPtr|s referring to the same object go out of scope?\n");
michael@0 444 }
michael@0 445
michael@0 446 {
michael@0 447 printf("\n### Test 15,16 ...setup...\n");
michael@0 448 Foo* raw_foo1p = new Foo;
michael@0 449 raw_foo1p->AddRef();
michael@0 450
michael@0 451 Foo* raw_foo2p = new Foo;
michael@0 452 raw_foo2p->AddRef();
michael@0 453
michael@0 454 printf("\n### Test 15: what if I don't want to |AddRef| when I construct?\n");
michael@0 455 nsRefPtr<Foo> foo1p( dont_AddRef(raw_foo1p) );
michael@0 456 //nsRefPtr<Foo> foo1p = dont_AddRef(raw_foo1p);
michael@0 457
michael@0 458 printf("\n### Test 16: what if I don't want to |AddRef| when I assign in?\n");
michael@0 459 nsRefPtr<Foo> foo2p;
michael@0 460 foo2p = dont_AddRef(raw_foo2p);
michael@0 461 }
michael@0 462
michael@0 463
michael@0 464
michael@0 465
michael@0 466
michael@0 467
michael@0 468
michael@0 469 {
michael@0 470 printf("\n### setup for Test 17\n");
michael@0 471 nsRefPtr<Foo> foop;
michael@0 472 printf("### Test 17: basic parameter behavior?\n");
michael@0 473 CreateFoo( nsRefPtrGetterAddRefs<Foo>(foop) );
michael@0 474 }
michael@0 475 printf("### End Test 17\n");
michael@0 476
michael@0 477
michael@0 478 {
michael@0 479 printf("\n### setup for Test 18\n");
michael@0 480 nsRefPtr<Foo> foop;
michael@0 481 printf("### Test 18: basic parameter behavior, using the short form?\n");
michael@0 482 CreateFoo( getter_AddRefs(foop) );
michael@0 483 }
michael@0 484 printf("### End Test 18\n");
michael@0 485
michael@0 486
michael@0 487 {
michael@0 488 printf("\n### setup for Test 19, 20\n");
michael@0 489 nsRefPtr<Foo> foop;
michael@0 490 printf("### Test 19: reference parameter behavior?\n");
michael@0 491 set_a_Foo(address_of(foop));
michael@0 492
michael@0 493 printf("### Test 20: return value behavior?\n");
michael@0 494 foop = return_a_Foo();
michael@0 495 }
michael@0 496 printf("### End Test 19, 20\n");
michael@0 497
michael@0 498 {
michael@0 499 printf("\n### setup for Test 21\n");
michael@0 500 nsRefPtr<Foo> fooP;
michael@0 501
michael@0 502 printf("### Test 21: is |QueryInterface| called on assigning in a raw pointer?\n");
michael@0 503 fooP = do_QueryObject(new Foo);
michael@0 504 }
michael@0 505 printf("### End Test 21\n");
michael@0 506
michael@0 507 {
michael@0 508 printf("\n### setup for Test 22\n");
michael@0 509 nsRefPtr<Foo> fooP;
michael@0 510 fooP = do_QueryObject(new Foo);
michael@0 511
michael@0 512 nsRefPtr<Foo> foo2P;
michael@0 513
michael@0 514 printf("### Test 22: is |QueryInterface| _not_ called when assigning in a smart-pointer of the same type?\n");
michael@0 515 foo2P = fooP;
michael@0 516 }
michael@0 517 printf("### End Test 22\n");
michael@0 518
michael@0 519 {
michael@0 520 printf("\n### setup for Test 23\n");
michael@0 521 nsRefPtr<Bar> barP( do_QueryObject(new Bar) );
michael@0 522
michael@0 523 printf("### Test 23: is |QueryInterface| called when assigning in a smart-pointer of a different type?\n");
michael@0 524
michael@0 525 nsRefPtr<Foo> fooP( do_QueryObject(barP) );
michael@0 526 if ( fooP )
michael@0 527 printf("an Bar* is an Foo*\n");
michael@0 528 }
michael@0 529 printf("### End Test 23\n");
michael@0 530
michael@0 531
michael@0 532 {
michael@0 533 printf("\n### setup for Test 24\n");
michael@0 534 nsRefPtr<Foo> fooP( do_QueryObject(new Foo) );
michael@0 535
michael@0 536 printf("### Test 24: does |forget| avoid an AddRef/Release when assigning to another nsCOMPtr?\n");
michael@0 537 nsRefPtr<Foo> fooP2( fooP.forget() );
michael@0 538 }
michael@0 539 printf("### End Test 24\n");
michael@0 540
michael@0 541 {
michael@0 542 nsRefPtr<Foo> fooP;
michael@0 543
michael@0 544 AnFooPtrPtrContext( getter_AddRefs(fooP) );
michael@0 545 AVoidPtrPtrContext( getter_AddRefs(fooP) );
michael@0 546 }
michael@0 547
michael@0 548
michael@0 549 printf("\n### Test 25: will a static |nsCOMPtr| |Release| before program termination?\n");
michael@0 550 gFoop = do_QueryObject(new Foo);
michael@0 551
michael@0 552 printf("<<main()\n");
michael@0 553 return 0;
michael@0 554 }
michael@0 555

mercurial