Back to index

lightning-sunbird  0.9+nobinonly
TestCOMPtr.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <assert.h>
00040 #include <stdio.h>
00041 #include "nsCOMPtr.h"
00042 #include "nsISupports.h"
00043 
00044 #ifdef HAVE_CPP_NEW_CASTS
00045   #define STATIC_CAST(T,x)  static_cast<T>(x)
00046   #define REINTERPRET_CAST(T,x) reinterpret_cast<T>(x)
00047 #else
00048   #define STATIC_CAST(T,x)  ((T)(x))
00049   #define REINTERPRET_CAST(T,x) ((T)(x))
00050 #endif
00051 
00052 
00053 #define NS_IFOO_IID \
00054 { 0x6f7652e0,  0xee43, 0x11d1, \
00055  { 0x9c, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
00056 
00057 class IFoo : public nsISupports
00058   {
00059               public:
00060                      NS_DEFINE_STATIC_IID_ACCESSOR(NS_IFOO_IID)
00061 
00062               public:
00063       IFoo();
00064       // virtual dtor because IBar uses our Release()
00065       virtual ~IFoo();
00066 
00067       NS_IMETHOD_(nsrefcnt) AddRef();
00068       NS_IMETHOD_(nsrefcnt) Release();
00069       NS_IMETHOD QueryInterface( const nsIID&, void** );
00070 
00071       static void print_totals();
00072 
00073     private:
00074       unsigned int refcount_;
00075 
00076       static unsigned int total_constructions_;
00077       static unsigned int total_destructions_;
00078   };
00079 
00080 class IBar;
00081 
00082   // some types I'll need
00083 typedef unsigned long NS_RESULT;
00084 
00085   // some functions I'll need (and define below)
00086           nsresult  CreateIFoo( void** );
00087           nsresult  CreateIBar( void** result );
00088               void  AnIFooPtrPtrContext( IFoo** );
00089               void   AnISupportsPtrPtrContext( nsISupports** );
00090               void  AVoidPtrPtrContext( void** );
00091               void  set_a_IFoo( nsCOMPtr<IFoo>* result );
00092 nsCOMPtr<IFoo>  return_a_IFoo();
00093 
00094 
00095 
00096 
00097 unsigned int IFoo::total_constructions_;
00098 unsigned int IFoo::total_destructions_;
00099 
00100 class test_message
00101   {
00102     public:
00103       test_message()
00104         {
00105           printf("BEGIN unit tests for |nsCOMPtr|, compiled " __DATE__ "\n");
00106         }
00107 
00108      ~test_message()
00109         {
00110           IFoo::print_totals();
00111           printf("END unit tests for |nsCOMPtr|.\n");
00112         }
00113   };
00114 
00115 test_message gTestMessage;
00116 
00117 
00118   /*
00119     ...
00120   */
00121 
00122 void
00123 IFoo::print_totals()
00124   {
00125     printf("total constructions/destructions --> %d/%d\n", 
00126            total_constructions_, total_destructions_);
00127   }
00128 
00129 IFoo::IFoo()
00130     : refcount_(0)
00131   {
00132     ++total_constructions_;
00133     printf("  new IFoo@%p [#%d]\n",
00134            STATIC_CAST(void*, this), total_constructions_);
00135   }
00136 
00137 IFoo::~IFoo()
00138   {
00139     ++total_destructions_;
00140     printf("IFoo@%p::~IFoo() [#%d]\n",
00141            STATIC_CAST(void*, this), total_destructions_);
00142   }
00143 
00144 nsrefcnt
00145 IFoo::AddRef()
00146   {
00147     ++refcount_;
00148     printf("IFoo@%p::AddRef(), refcount --> %d\n", 
00149            STATIC_CAST(void*, this), refcount_);
00150     return refcount_;
00151   }
00152 
00153 nsrefcnt
00154 IFoo::Release()
00155   {
00156     int wrap_message = (refcount_ == 1);
00157     if ( wrap_message )
00158       printf(">>");
00159       
00160     --refcount_;
00161     printf("IFoo@%p::Release(), refcount --> %d\n",
00162            STATIC_CAST(void*, this), refcount_);
00163 
00164     if ( !refcount_ )
00165       {
00166         printf("  delete IFoo@%p\n", STATIC_CAST(void*, this));
00167         delete this;
00168       }
00169 
00170     if ( wrap_message )
00171       printf("<<IFoo@%p::Release()\n", STATIC_CAST(void*, this));
00172 
00173     return refcount_;
00174   }
00175 
00176 nsresult
00177 IFoo::QueryInterface( const nsIID& aIID, void** aResult )
00178        {
00179     printf("IFoo@%p::QueryInterface()\n", STATIC_CAST(void*, this));
00180               nsISupports* rawPtr = 0;
00181               nsresult status = NS_OK;
00182 
00183               if ( aIID.Equals(GetIID()) )
00184                      rawPtr = this;
00185               else
00186                      {
00187                             nsID iid_of_ISupports = NS_ISUPPORTS_IID;
00188                             if ( aIID.Equals(iid_of_ISupports) )
00189                                    rawPtr = STATIC_CAST(nsISupports*, this);
00190                             else
00191                                    status = NS_ERROR_NO_INTERFACE;
00192                      }
00193 
00194               NS_IF_ADDREF(rawPtr);
00195               *aResult = rawPtr;
00196 
00197               return status;
00198        }
00199 
00200 nsresult
00201 CreateIFoo( void** result )
00202     // a typical factory function (that calls AddRef)
00203   {
00204     printf(">>CreateIFoo() --> ");
00205     IFoo* foop = new IFoo;
00206     printf("IFoo@%p\n", STATIC_CAST(void*, foop));
00207 
00208     foop->AddRef();
00209     *result = foop;
00210 
00211     printf("<<CreateIFoo()\n");
00212     return 0;
00213   }
00214 
00215 void
00216 set_a_IFoo( nsCOMPtr<IFoo>* result )
00217   {
00218     printf(">>set_a_IFoo()\n");
00219     assert(result);
00220 
00221     nsCOMPtr<IFoo> foop( do_QueryInterface(new IFoo) );
00222     *result = foop;
00223     printf("<<set_a_IFoo()\n");
00224   }
00225 
00226 nsCOMPtr<IFoo>
00227 return_a_IFoo()
00228   {
00229     printf(">>return_a_IFoo()\n");
00230     nsCOMPtr<IFoo> foop( do_QueryInterface(new IFoo) );
00231     printf("<<return_a_IFoo()\n");
00232     return foop;
00233   }
00234 
00235 
00236 
00237 
00238 #define NS_IBAR_IID \
00239 { 0x6f7652e1,  0xee43, 0x11d1, \
00240  { 0x9c, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
00241 
00242 class IBar : public IFoo
00243   {
00244        public:
00245               NS_DEFINE_STATIC_IID_ACCESSOR(NS_IBAR_IID)
00246 
00247     public:
00248       IBar();
00249       virtual ~IBar();
00250 
00251       NS_IMETHOD QueryInterface( const nsIID&, void** );
00252   };
00253 
00254 IBar::IBar()
00255   {
00256     printf("  new IBar@%p\n", STATIC_CAST(void*, this));
00257   }
00258 
00259 IBar::~IBar()
00260   {
00261     printf("IBar@%p::~IBar()\n", STATIC_CAST(void*, this));
00262   }
00263 
00264 nsresult
00265 IBar::QueryInterface( const nsID& aIID, void** aResult )
00266        {
00267     printf("IBar@%p::QueryInterface()\n", STATIC_CAST(void*, this));
00268               nsISupports* rawPtr = 0;
00269               nsresult status = NS_OK;
00270 
00271               if ( aIID.Equals(GetIID()) )
00272                      rawPtr = this;
00273               else if ( aIID.Equals(NS_GET_IID(IFoo)) )
00274                      rawPtr = STATIC_CAST(IFoo*, this);
00275               else
00276                      {
00277                             nsID iid_of_ISupports = NS_ISUPPORTS_IID;
00278                             if ( aIID.Equals(iid_of_ISupports) )
00279                                    rawPtr = STATIC_CAST(nsISupports*, this);
00280                             else
00281                                    status = NS_ERROR_NO_INTERFACE;
00282                      }
00283 
00284               NS_IF_ADDREF(rawPtr);
00285               *aResult = rawPtr;
00286 
00287               return status;
00288        }
00289 
00290 
00291 
00292 nsresult
00293 CreateIBar( void** result )
00294     // a typical factory function (that calls AddRef)
00295   {
00296     printf(">>CreateIBar() --> ");
00297     IBar* barp = new IBar;
00298     printf("IBar@%p\n", STATIC_CAST(void*, barp));
00299 
00300     barp->AddRef();
00301     *result = barp;
00302 
00303     printf("<<CreateIBar()\n");
00304     return 0;
00305   }
00306 
00307 void
00308 AnIFooPtrPtrContext( IFoo** )
00309   {
00310   }
00311 
00312 void
00313 AVoidPtrPtrContext( void** )
00314   {
00315   }
00316 
00317 void
00318 AnISupportsPtrPtrContext( nsISupports** )
00319        {
00320        }
00321 
00322 
00323 // Optimism
00324 #define TEST_EXCEPTIONS 1
00325 
00326 // HAVE_CPP_EXCEPTIONS is defined automagically on unix
00327 #if defined(XP_UNIX) || defined(XP_BEOS) || defined(XP_OS2)
00328 #if !defined(HAVE_CPP_EXCEPTIONS)
00329 #undef TEST_EXCEPTIONS
00330 #endif
00331 #endif
00332 
00333 #ifdef TEST_EXCEPTIONS
00334 static
00335 nsresult
00336 TestBloat_Raw()
00337        {
00338               IBar* barP = 0;
00339               nsresult result = CreateIBar(REINTERPRET_CAST(void**, &barP));
00340 
00341               if ( barP )
00342                      {
00343                             try
00344                                    {
00345                                           IFoo* fooP = 0;
00346                                           if ( NS_SUCCEEDED( result = barP->QueryInterface(NS_GET_IID(IFoo), REINTERPRET_CAST(void**, &fooP)) ) )
00347                                                  {
00348                                                         try
00349                                                                {
00350                                                                       fooP->print_totals();
00351                                                                }
00352                                                         catch( ... )
00353                                                                {
00354                                                                       NS_RELEASE(fooP);
00355                                                                       throw;
00356                                                                }
00357 
00358                                                         NS_RELEASE(fooP);
00359                                                  }
00360                                    }
00361                             catch( ... )
00362                                    {
00363                                           NS_RELEASE(barP);
00364                                           throw;
00365                                    }
00366 
00367                             NS_RELEASE(barP);
00368                      }
00369 
00370               return result;
00371        }
00372 #endif // TEST_EXCEPTIONS
00373 
00374 static
00375 nsresult
00376 TestBloat_Raw_Unsafe()
00377        {
00378               IBar* barP = 0;
00379               nsresult result = CreateIBar(REINTERPRET_CAST(void**, &barP));
00380 
00381               if ( barP )
00382                      {
00383                             IFoo* fooP = 0;
00384                             if ( NS_SUCCEEDED( result = barP->QueryInterface(NS_GET_IID(IFoo), REINTERPRET_CAST(void**, &fooP)) ) )
00385                                    {
00386                                           fooP->print_totals();
00387                                           NS_RELEASE(fooP);
00388                                    }
00389 
00390                             NS_RELEASE(barP);
00391                      }
00392 
00393               return result;
00394        }
00395 
00396 
00397 static
00398 nsresult
00399 TestBloat_Smart()
00400        {
00401               nsCOMPtr<IBar> barP;
00402               nsresult result = CreateIBar( getter_AddRefs(barP) );
00403 
00404               nsCOMPtr<IFoo> fooP( do_QueryInterface(barP, &result) );
00405 
00406               if ( fooP )
00407                      fooP->print_totals();
00408 
00409               return result;
00410        }
00411 
00412 
00413 
00414 
00415 nsCOMPtr<IFoo> gFoop;
00416 
00417 int
00418 main()
00419   {
00420     printf(">>main()\n");
00421 
00422               printf("sizeof(nsCOMPtr<IFoo>) --> %d\n", sizeof(nsCOMPtr<IFoo>));
00423 
00424 #ifdef TEST_EXCEPTIONS
00425               TestBloat_Raw();
00426 #endif // TEST_EXCEPTIONS
00427               TestBloat_Raw_Unsafe();
00428               TestBloat_Smart();
00429 
00430 
00431     {
00432       printf("\n### Test  1: will a |nsCOMPtr| call |AddRef| on a pointer assigned into it?\n");
00433       nsCOMPtr<IFoo> foop( do_QueryInterface(new IFoo) );
00434 
00435       printf("\n### Test  2: will a |nsCOMPtr| |Release| its old pointer when a new one is assigned in?\n");
00436       foop = do_QueryInterface(new IFoo);
00437 
00438         // [Shouldn't compile] Is it a compile time error to try to |AddRef| by hand?
00439       //foop->AddRef();
00440 
00441         // [Shouldn't compile] Is it a compile time error to try to |Release| be hand?
00442       //foop->Release();
00443 
00444                             // [Shouldn't compile] Is it a compile time error to try to |delete| an |nsCOMPtr|?
00445                      //delete foop;
00446 
00447       printf("\n### Test  3: can you |AddRef| if you must?\n");
00448       STATIC_CAST(IFoo*, foop)->AddRef();
00449 
00450       printf("\n### Test  4: can you |Release| if you must?\n");
00451       STATIC_CAST(IFoo*, foop)->Release();
00452 
00453       printf("\n### Test  5: will a |nsCOMPtr| |Release| when it goes out of scope?\n");
00454     }
00455 
00456     {
00457       printf("\n### Test  6: will a |nsCOMPtr| call the correct destructor?\n");
00458       nsCOMPtr<IFoo> foop( do_QueryInterface(new IBar) );
00459     }
00460 
00461     {
00462       printf("\n### Test  7: can you compare one |nsCOMPtr| with another [!=]?\n");
00463 
00464       nsCOMPtr<IFoo> foo1p( do_QueryInterface(new IFoo) );
00465 
00466         // [Shouldn't compile] Is it a compile time error to omit |getter_[doesnt_]AddRef[s]|?
00467       //AnIFooPtrPtrContext(&foo1p);
00468 
00469         // [Shouldn't compile] Is it a compile time error to omit |getter_[doesnt_]AddRef[s]|?
00470       //AVoidPtrPtrContext(&foo1p);
00471 
00472       nsCOMPtr<IFoo> foo2p( do_QueryInterface(new IFoo) );
00473 
00474       if ( foo1p != foo2p )
00475         printf("foo1p != foo2p\n");
00476       else
00477         printf("foo1p == foo2p\n");
00478 
00479       printf("\n### Test  7.5: can you compare a |nsCOMPtr| with NULL, 0, nsnull [!=]?\n");
00480       if ( foo1p != 0 )
00481        printf("foo1p != 0\n");
00482       if ( 0 != foo1p )
00483        printf("0 != foo1p\n");
00484       if ( foo1p == 0 )
00485        printf("foo1p == 0\n");
00486       if ( 0 == foo1p )
00487        printf("0 == foo1p\n");
00488                      
00489 
00490       IFoo* raw_foo2p = foo2p.get();
00491 
00492       printf("\n### Test  8: can you compare a |nsCOMPtr| with a raw interface pointer [!=]?\n");
00493       if ( foo1p.get() != raw_foo2p )
00494         printf("foo1p != raw_foo2p\n");
00495       else
00496         printf("foo1p == raw_foo2p\n");
00497 
00498 
00499       printf("\n### Test  9: can you assign one |nsCOMPtr| into another?\n");
00500       foo1p = foo2p;
00501 
00502       printf("\n### Test 10: can you compare one |nsCOMPtr| with another [==]?\n");
00503       if ( foo1p == foo2p )
00504         printf("foo1p == foo2p\n");
00505       else
00506         printf("foo1p != foo2p\n");
00507 
00508       printf("\n### Test 11: can you compare a |nsCOMPtr| with a raw interface pointer [==]?\n");
00509       if ( raw_foo2p == foo2p.get() )
00510         printf("raw_foo2p == foo2p\n");
00511       else
00512         printf("raw_foo2p != foo2p\n");
00513 
00514 #if 1
00515       printf("\n### Test 11.5: can you compare a |nsCOMPtr| with a raw interface pointer [==]?\n");
00516       if ( nsCOMPtr<IFoo>( raw_foo2p ) == foo2p )
00517         printf("raw_foo2p == foo2p\n");
00518       else
00519         printf("raw_foo2p != foo2p\n");
00520 #endif
00521 
00522       printf("\n### Test 12: bare pointer test?\n");
00523       if ( foo1p )
00524         printf("foo1p is not NULL\n");
00525       else
00526         printf("foo1p is NULL\n");
00527 
00528       printf("\n### Test 13: numeric pointer test?\n");
00529       if ( foo1p == 0 )
00530         printf("foo1p is NULL\n");
00531       else
00532         printf("foo1p is not NULL\n");
00533 
00534 #if 0
00535                      if ( foo1p == 1 )
00536                             printf("foo1p allowed compare with in\n");
00537 #endif
00538 
00539       printf("\n### Test 14: how about when two |nsCOMPtr|s referring to the same object go out of scope?\n");
00540     }
00541 
00542     {
00543       printf("\n### Test 15,16 ...setup...\n");
00544       IFoo* raw_foo1p = new IFoo;
00545       raw_foo1p->AddRef();
00546 
00547       IFoo* raw_foo2p = new IFoo;
00548       raw_foo2p->AddRef();
00549 
00550       printf("\n### Test 15: what if I don't want to |AddRef| when I construct?\n");
00551       nsCOMPtr<IFoo> foo1p( dont_AddRef(raw_foo1p) );
00552       //nsCOMPtr<IFoo> foo1p = dont_AddRef(raw_foo1p);
00553 
00554       printf("\n### Test 16: what if I don't want to |AddRef| when I assign in?\n");
00555       nsCOMPtr<IFoo> foo2p;
00556       foo2p = dont_AddRef(raw_foo2p);
00557     }
00558 
00559 
00560 
00561 
00562 
00563 
00564 
00565     {
00566        printf("\n### setup for Test 17\n");
00567       nsCOMPtr<IFoo> foop;
00568       printf("### Test 17: basic parameter behavior?\n");
00569       CreateIFoo( nsGetterAddRefs<IFoo>(foop) );
00570     }
00571     printf("### End Test 17\n");
00572 
00573 
00574     {
00575        printf("\n### setup for Test 18\n");
00576       nsCOMPtr<IFoo> foop;
00577       printf("### Test 18: basic parameter behavior, using the short form?\n");
00578       CreateIFoo( getter_AddRefs(foop) );
00579     }
00580     printf("### End Test 18\n");
00581 
00582 
00583     {
00584        printf("\n### setup for Test 19, 20\n");
00585       nsCOMPtr<IFoo> foop;
00586       printf("### Test 19: reference parameter behavior?\n");
00587       set_a_IFoo(address_of(foop));
00588 
00589       printf("### Test 20: return value behavior?\n");
00590       foop = return_a_IFoo();
00591     }
00592     printf("### End Test 19, 20\n");
00593 
00594               {
00595        printf("\n### setup for Test 21\n");
00596                      nsCOMPtr<IFoo> fooP;
00597 
00598                      printf("### Test 21: is |QueryInterface| called on assigning in a raw pointer?\n");
00599                      fooP = do_QueryInterface(new IFoo);
00600               }
00601     printf("### End Test 21\n");
00602 
00603               {
00604        printf("\n### setup for Test 22\n");
00605                      nsCOMPtr<IFoo> fooP;
00606                      fooP = do_QueryInterface(new IFoo);
00607 
00608                      nsCOMPtr<IFoo> foo2P;
00609 
00610                      printf("### Test 22: is |QueryInterface| _not_ called when assigning in a smart-pointer of the same type?\n");
00611                      foo2P = fooP;
00612               }
00613     printf("### End Test 22\n");
00614 
00615               {
00616        printf("\n### setup for Test 23\n");
00617                      nsCOMPtr<IBar> barP( do_QueryInterface(new IBar) );
00618 
00619                      printf("### Test 23: is |QueryInterface| called when assigning in a smart-pointer of a different type?\n");
00620 
00621                      nsCOMPtr<IFoo> fooP( do_QueryInterface(barP) );
00622                      if ( fooP )
00623                             printf("an IBar* is an IFoo*\n");
00624               }
00625     printf("### End Test 23\n");
00626 
00627 
00628               {
00629                      nsCOMPtr<IFoo> fooP;
00630 
00631                      AnIFooPtrPtrContext( getter_AddRefs(fooP) );
00632                      AVoidPtrPtrContext( getter_AddRefs(fooP) );
00633                      AnISupportsPtrPtrContext( getter_AddRefs(fooP) );
00634               }
00635 
00636 
00637               {
00638                      nsCOMPtr<nsISupports> supportsP;
00639 
00640                      AVoidPtrPtrContext( getter_AddRefs(supportsP) );
00641                      AnISupportsPtrPtrContext( getter_AddRefs(supportsP) );
00642               }
00643 
00644 
00645     printf("\n### Test 24: will a static |nsCOMPtr| |Release| before program termination?\n");
00646     gFoop = do_QueryInterface(new IFoo);
00647     
00648     printf("<<main()\n");
00649     return 0;
00650   }
00651