xpcom/tests/TestAutoPtr.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     2 // vim:cindent:ts=4:et:sw=4:
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #include "nsAutoPtr.h"
     8 #include <stdio.h>
     9 #include "nscore.h"
    10 #include "mozilla/Attributes.h"
    12 class TestObjectBaseA {
    13     public:
    14         // Virtual dtor for deleting through base class pointer
    15         virtual ~TestObjectBaseA() { }
    16         int fooA;
    17 };
    19 class TestObjectBaseB {
    20     public:
    21         // Virtual dtor for deleting through base class pointer
    22         virtual ~TestObjectBaseB() { }
    23         int fooB;
    24 };
    26 class TestObject : public TestObjectBaseA, public TestObjectBaseB {
    27     public:
    28         TestObject()
    29         {
    30             printf("  Creating TestObject %p.\n",
    31                    static_cast<void*>(this));
    32         }
    34         // Virtual dtor for deleting through base class pointer
    35         virtual ~TestObject()
    36         {
    37             printf("  Destroying TestObject %p.\n",
    38                    static_cast<void*>(this));
    39         }
    40 };
    42 class TestRefObjectBaseA {
    43     public:
    44         int fooA;
    45         // Must return |nsrefcnt| to keep |nsDerivedSafe| happy.
    46         virtual nsrefcnt AddRef() = 0;
    47         virtual nsrefcnt Release() = 0;
    48 };
    50 class TestRefObjectBaseB {
    51     public:
    52         int fooB;
    53         virtual nsrefcnt AddRef() = 0;
    54         virtual nsrefcnt Release() = 0;
    55 };
    57 class TestRefObject MOZ_FINAL : public TestRefObjectBaseA, public TestRefObjectBaseB {
    58     public:
    59         TestRefObject()
    60             : mRefCount(0)
    61         {
    62             printf("  Creating TestRefObject %p.\n",
    63                    static_cast<void*>(this));
    64         }
    66         ~TestRefObject()
    67         {
    68             printf("  Destroying TestRefObject %p.\n",
    69                    static_cast<void*>(this));
    70         }
    72         nsrefcnt AddRef()
    73         {
    74             ++mRefCount;
    75             printf("  AddRef to %d on TestRefObject %p.\n",
    76                    mRefCount, static_cast<void*>(this));
    77             return mRefCount;
    78         }
    80         nsrefcnt Release()
    81         {
    82             --mRefCount;
    83             printf("  Release to %d on TestRefObject %p.\n",
    84                    mRefCount, static_cast<void*>(this));
    85             if (mRefCount == 0) {
    86                 delete const_cast<TestRefObject*>(this);
    87                 return 0;
    88             }
    89             return mRefCount;
    90         }
    92     protected:
    93         uint32_t mRefCount;
    95 };
    97 static void CreateTestObject(TestObject **aResult)
    98 {
    99     *aResult = new TestObject();
   100 }
   102 static void CreateTestRefObject(TestRefObject **aResult)
   103 {
   104     (*aResult = new TestRefObject())->AddRef();
   105 }
   107 static void DoSomethingWithTestObject(TestObject *aIn)
   108 {
   109     printf("  Doing something with |TestObject| %p.\n",
   110            static_cast<void*>(aIn));
   111 }
   113 static void DoSomethingWithConstTestObject(const TestObject *aIn)
   114 {
   115     printf("  Doing something with |const TestObject| %p.\n",
   116            static_cast<const void*>(aIn));
   117 }
   119 static void DoSomethingWithTestRefObject(TestRefObject *aIn)
   120 {
   121     printf("  Doing something with |TestRefObject| %p.\n",
   122            static_cast<void*>(aIn));
   123 }
   125 static void DoSomethingWithConstTestRefObject(const TestRefObject *aIn)
   126 {
   127     printf("  Doing something with |const TestRefObject| %p.\n",
   128            static_cast<const void*>(aIn));
   129 }
   131 static void DoSomethingWithTestObjectBaseB(TestObjectBaseB *aIn)
   132 {
   133     printf("  Doing something with |TestObjectBaseB| %p.\n",
   134            static_cast<void*>(aIn));
   135 }
   137 static void DoSomethingWithConstTestObjectBaseB(const TestObjectBaseB *aIn)
   138 {
   139     printf("  Doing something with |const TestObjectBaseB| %p.\n",
   140            static_cast<const void*>(aIn));
   141 }
   143 static void DoSomethingWithTestRefObjectBaseB(TestRefObjectBaseB *aIn)
   144 {
   145     printf("  Doing something with |TestRefObjectBaseB| %p.\n",
   146            static_cast<void*>(aIn));
   147 }
   149 static void DoSomethingWithConstTestRefObjectBaseB(const TestRefObjectBaseB *aIn)
   150 {
   151     printf("  Doing something with |const TestRefObjectBaseB| %p.\n",
   152            static_cast<const void*>(aIn));
   153 }
   155 int main()
   156 {
   157     {
   158         printf("Should create one |TestObject|:\n");
   159         nsAutoPtr<TestObject> pobj( new TestObject() );
   160         printf("Should destroy one |TestObject|:\n");
   161     }
   163     {
   164         printf("Should create one |TestObject|:\n");
   165         nsAutoPtr<TestObject> pobj( new TestObject() );
   166         printf("Should create one |TestObject| and then destroy one:\n");
   167         pobj = new TestObject();
   168         printf("Should destroy one |TestObject|:\n");
   169     }
   171     {
   172         printf("Should create 3 |TestObject|s:\n");
   173         nsAutoArrayPtr<TestObject> pobj( new TestObject[3] );
   174         printf("Should create 5 |TestObject|s and then destroy 3:\n");
   175         pobj = new TestObject[5];
   176         printf("Should destroy 5 |TestObject|s:\n");
   177     }
   179     {
   180         printf("Should create and AddRef one |TestRefObject|:\n");
   181         nsRefPtr<TestRefObject> pobj( new TestRefObject() );
   182         printf("Should Release and destroy one |TestRefObject|:\n");
   183     }
   185     {
   186         printf("Should create and AddRef one |TestRefObject|:\n");
   187         nsRefPtr<TestRefObject> pobj( new TestRefObject() );
   188         printf("Should create and AddRef one |TestRefObject| and then Release and destroy one:\n");
   189         pobj = new TestRefObject();
   190         printf("Should Release and destroy one |TestRefObject|:\n");
   191     }
   193     {
   194         printf("Should create and AddRef one |TestRefObject|:\n");
   195         nsRefPtr<TestRefObject> p1( new TestRefObject() );
   196         printf("Should AddRef one |TestRefObject|:\n");
   197         nsRefPtr<TestRefObject> p2( p1 );
   198         printf("Should Release twice and destroy one |TestRefObject|:\n");
   199     }
   201     printf("\nTesting equality (with all const-ness combinations):\n");
   203     {
   204         nsRefPtr<TestRefObject> p1( new TestRefObject() );
   205         nsRefPtr<TestRefObject> p2( p1 );
   206         printf("equality %s.\n",
   207                ((p1 == p2) && !(p1 != p2)) ? "OK" : "broken");
   208     }
   210     {
   211         const nsRefPtr<TestRefObject> p1( new TestRefObject() );
   212         nsRefPtr<TestRefObject> p2( p1 );
   213         printf("equality %s.\n",
   214                ((p1 == p2) && !(p1 != p2)) ? "OK" : "broken");
   215     }
   217     {
   218         nsRefPtr<TestRefObject> p1( new TestRefObject() );
   219         const nsRefPtr<TestRefObject> p2( p1 );
   220         printf("equality %s.\n",
   221                ((p1 == p2) && !(p1 != p2)) ? "OK" : "broken");
   222     }
   224     {
   225         const nsRefPtr<TestRefObject> p1( new TestRefObject() );
   226         const nsRefPtr<TestRefObject> p2( p1 );
   227         printf("equality %s.\n",
   228                ((p1 == p2) && !(p1 != p2)) ? "OK" : "broken");
   229     }
   231     {
   232         nsRefPtr<TestRefObject> p1( new TestRefObject() );
   233         TestRefObject * p2 = p1;
   234         printf("equality %s.\n",
   235                ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
   236     }
   238     {
   239         const nsRefPtr<TestRefObject> p1( new TestRefObject() );
   240         TestRefObject * p2 = p1;
   241         printf("equality %s.\n",
   242                ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
   243     }
   245 #if 0 /* MSVC++ 6.0 can't be coaxed to accept this */
   246     {
   247         nsRefPtr<TestRefObject> p1( new TestRefObject() );
   248         TestRefObject * const p2 = p1;
   249         printf("equality %s.\n",
   250                ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
   251     }
   253     {
   254         const nsRefPtr<TestRefObject> p1( new TestRefObject() );
   255         TestRefObject * const p2 = p1;
   256         printf("equality %s.\n",
   257                ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
   258     }
   259 #endif /* Things that MSVC++ 6.0 can't be coaxed to accept */
   261     {
   262         nsRefPtr<TestRefObject> p1( new TestRefObject() );
   263         const TestRefObject * p2 = p1;
   264         printf("equality %s.\n",
   265                ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
   266     }
   268     {
   269         const nsRefPtr<TestRefObject> p1( new TestRefObject() );
   270         const TestRefObject * p2 = p1;
   271         printf("equality %s.\n",
   272                ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
   273     }
   275     {
   276         nsRefPtr<TestRefObject> p1( new TestRefObject() );
   277         const TestRefObject * const p2 = p1;
   278         printf("equality %s.\n",
   279                ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
   280     }
   282     {
   283         const nsRefPtr<TestRefObject> p1( new TestRefObject() );
   284         const TestRefObject * const p2 = p1;
   285         printf("equality %s.\n",
   286                ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
   287     }
   289     printf("\nTesting getter_Transfers and getter_AddRefs.\n");
   291     {
   292         nsAutoPtr<TestObject> ptr;
   293         printf("Should create one |TestObject|:\n");
   294         CreateTestObject(getter_Transfers(ptr));
   295         printf("Should destroy one |TestObject|:\n");
   296     }
   298     {
   299         nsRefPtr<TestRefObject> ptr;
   300         printf("Should create and AddRef one |TestRefObject|:\n");
   301         CreateTestRefObject(getter_AddRefs(ptr));
   302         printf("Should Release and destroy one |TestRefObject|:\n");
   303     }
   305     printf("\nTesting casts and equality tests.\n");
   307     if ((void*)(TestObject*)0x1000 ==
   308         (void*)(TestObjectBaseB*)(TestObject*)0x1000)
   309         printf("\n\nAll these tests are meaningless!\n\n\n");
   311     {
   312         nsAutoPtr<TestObject> p1(new TestObject());
   313         TestObjectBaseB *p2 = p1;
   314         printf("equality %s.\n",
   315                ((static_cast<void*>(p1) != static_cast<void*>(p2)) &&
   316                 (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
   317                ? "OK" : "broken");
   318     }
   320     {
   321         TestObject *p1 = new TestObject();
   322         nsAutoPtr<TestObjectBaseB> p2(p1);
   323         printf("equality %s.\n",
   324                ((static_cast<void*>(p1) != static_cast<void*>(p2)) &&
   325                 (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
   326                ? "OK" : "broken");
   327     }
   329     {
   330         nsRefPtr<TestRefObject> p1 = new TestRefObject();
   331         // nsCOMPtr requires a |get| for something like this as well
   332         nsRefPtr<TestRefObjectBaseB> p2 = p1.get();
   333         printf("equality %s.\n",
   334                ((static_cast<void*>(p1) != static_cast<void*>(p2)) &&
   335                 (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
   336                ? "OK" : "broken");
   337     }
   339     {
   340         nsRefPtr<TestRefObject> p1 = new TestRefObject();
   341         TestRefObjectBaseB *p2 = p1;
   342         printf("equality %s.\n",
   343                ((static_cast<void*>(p1) != static_cast<void*>(p2)) &&
   344                 (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
   345                ? "OK" : "broken");
   346     }
   348     {
   349         TestRefObject *p1 = new TestRefObject();
   350         nsRefPtr<TestRefObjectBaseB> p2 = p1;
   351         printf("equality %s.\n",
   352                ((static_cast<void*>(p1) != static_cast<void*>(p2)) &&
   353                 (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
   354                ? "OK" : "broken");
   355     }
   357     printf("\nTesting |forget()|.\n");
   359     {
   360         printf("Should create one |TestObject|:\n");
   361         nsAutoPtr<TestObject> pobj( new TestObject() );
   362         printf("Should do nothing:\n");
   363         nsAutoPtr<TestObject> pobj2( pobj.forget() );
   364         printf("Should destroy one |TestObject|:\n");
   365     }
   367     {
   368         printf("Should create 3 |TestObject|s:\n");
   369         nsAutoArrayPtr<TestObject> pobj( new TestObject[3] );
   370         printf("Should do nothing:\n");
   371         nsAutoArrayPtr<TestObject> pobj2( pobj.forget() );
   372         printf("Should destroy 3 |TestObject|s:\n");
   373     }
   375     {
   376         printf("Should create one |TestRefObject|:\n");
   377         nsRefPtr<TestRefObject> pobj( new TestRefObject() );
   378         printf("Should do nothing:\n");
   379         nsRefPtr<TestRefObject> pobj2( pobj.forget() );
   380         printf("Should destroy one |TestRefObject|:\n");
   381     }
   384     printf("\nTesting construction.\n");
   386     {
   387         printf("Should create one |TestObject|:\n");
   388         nsAutoPtr<TestObject> pobj(new TestObject());
   389         printf("Should destroy one |TestObject|:\n");
   390     }
   392     {
   393         printf("Should create 3 |TestObject|s:\n");
   394         nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
   395         printf("Should destroy 3 |TestObject|s:\n");
   396     }
   398     {
   399         printf("Should create and AddRef one |TestRefObject|:\n");
   400         nsRefPtr<TestRefObject> pobj = new TestRefObject();
   401         printf("Should Release and destroy one |TestRefObject|:\n");
   402     }
   404     printf("\nTesting calling of functions (including array access and casts).\n");
   406     {
   407         printf("Should create one |TestObject|:\n");
   408         nsAutoPtr<TestObject> pobj(new TestObject());
   409         printf("Should do something with one |TestObject|:\n");
   410         DoSomethingWithTestObject(pobj);
   411         printf("Should do something with one |TestObject|:\n");
   412         DoSomethingWithConstTestObject(pobj);
   413         printf("Should destroy one |TestObject|:\n");
   414     }
   416     {
   417         printf("Should create 3 |TestObject|s:\n");
   418         nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
   419         printf("Should do something with one |TestObject|:\n");
   420         DoSomethingWithTestObject(&pobj[2]);
   421         printf("Should do something with one |TestObject|:\n");
   422         DoSomethingWithConstTestObject(&pobj[1]);
   423         printf("Should do something with one |TestObject|:\n");
   424         DoSomethingWithTestObject(pobj + 2);
   425         printf("Should do something with one |TestObject|:\n");
   426         DoSomethingWithConstTestObject(pobj + 1);
   427         printf("Should destroy 3 |TestObject|s:\n");
   428     }
   430     {
   431         printf("Should create and AddRef one |TestRefObject|:\n");
   432         nsRefPtr<TestRefObject> pobj = new TestRefObject();
   433         printf("Should do something with one |TestRefObject|:\n");
   434         DoSomethingWithTestRefObject(pobj);
   435         printf("Should do something with one |TestRefObject|:\n");
   436         DoSomethingWithConstTestRefObject(pobj);
   437         printf("Should Release and destroy one |TestRefObject|:\n");
   438     }
   440     {
   441         printf("Should create one |TestObject|:\n");
   442         nsAutoPtr<TestObject> pobj(new TestObject());
   443         printf("Should do something with one |TestObject|:\n");
   444         DoSomethingWithTestObjectBaseB(pobj);
   445         printf("Should do something with one |TestObject|:\n");
   446         DoSomethingWithConstTestObjectBaseB(pobj);
   447         printf("Should destroy one |TestObject|:\n");
   448     }
   450     {
   451         printf("Should create 3 |TestObject|s:\n");
   452         nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
   453         printf("Should do something with one |TestObject|:\n");
   454         DoSomethingWithTestObjectBaseB(&pobj[2]);
   455         printf("Should do something with one |TestObject|:\n");
   456         DoSomethingWithConstTestObjectBaseB(&pobj[1]);
   457         printf("Should do something with one |TestObject|:\n");
   458         DoSomethingWithTestObjectBaseB(pobj + 2);
   459         printf("Should do something with one |TestObject|:\n");
   460         DoSomethingWithConstTestObjectBaseB(pobj + 1);
   461         printf("Should destroy 3 |TestObject|s:\n");
   462     }
   464     {
   465         printf("Should create and AddRef one |TestRefObject|:\n");
   466         nsRefPtr<TestRefObject> pobj = new TestRefObject();
   467         printf("Should do something with one |TestRefObject|:\n");
   468         DoSomethingWithTestRefObjectBaseB(pobj);
   469         printf("Should do something with one |TestRefObject|:\n");
   470         DoSomethingWithConstTestRefObjectBaseB(pobj);
   471         printf("Should Release and destroy one |TestRefObject|:\n");
   472     }
   474     {
   475         printf("Should create one |TestObject|:\n");
   476         const nsAutoPtr<TestObject> pobj(new TestObject());
   477         printf("Should do something with one |TestObject|:\n");
   478         DoSomethingWithTestObject(pobj);
   479         printf("Should do something with one |TestObject|:\n");
   480         DoSomethingWithConstTestObject(pobj);
   481         printf("Should destroy one |TestObject|:\n");
   482     }
   484     {
   485         printf("Should create 3 |TestObject|s:\n");
   486         const nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
   487         printf("Should do something with one |TestObject|:\n");
   488         DoSomethingWithTestObject(&pobj[2]);
   489         printf("Should do something with one |TestObject|:\n");
   490         DoSomethingWithConstTestObject(&pobj[1]);
   491         printf("Should do something with one |TestObject|:\n");
   492         DoSomethingWithTestObject(pobj + 2);
   493         printf("Should do something with one |TestObject|:\n");
   494         DoSomethingWithConstTestObject(pobj + 1);
   495         printf("Should destroy 3 |TestObject|s:\n");
   496     }
   498     {
   499         printf("Should create and AddRef one |TestRefObject|:\n");
   500         const nsRefPtr<TestRefObject> pobj = new TestRefObject();
   501         printf("Should do something with one |TestRefObject|:\n");
   502         DoSomethingWithTestRefObject(pobj);
   503         printf("Should do something with one |TestRefObject|:\n");
   504         DoSomethingWithConstTestRefObject(pobj);
   505         printf("Should Release and destroy one |TestRefObject|:\n");
   506     }
   508     {
   509         printf("Should create one |TestObject|:\n");
   510         const nsAutoPtr<TestObject> pobj(new TestObject());
   511         printf("Should do something with one |TestObject|:\n");
   512         DoSomethingWithTestObjectBaseB(pobj);
   513         printf("Should do something with one |TestObject|:\n");
   514         DoSomethingWithConstTestObjectBaseB(pobj);
   515         printf("Should destroy one |TestObject|:\n");
   516     }
   518     {
   519         printf("Should create 3 |TestObject|s:\n");
   520         const nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
   521         printf("Should do something with one |TestObject|:\n");
   522         DoSomethingWithTestObjectBaseB(&pobj[2]);
   523         printf("Should do something with one |TestObject|:\n");
   524         DoSomethingWithConstTestObjectBaseB(&pobj[1]);
   525         printf("Should do something with one |TestObject|:\n");
   526         DoSomethingWithTestObjectBaseB(pobj + 2);
   527         printf("Should do something with one |TestObject|:\n");
   528         DoSomethingWithConstTestObjectBaseB(pobj + 1);
   529         printf("Should destroy 3 |TestObject|s:\n");
   530     }
   532     {
   533         printf("Should create and AddRef one |TestRefObject|:\n");
   534         const nsRefPtr<TestRefObject> pobj = new TestRefObject();
   535         printf("Should do something with one |TestRefObject|:\n");
   536         DoSomethingWithTestRefObjectBaseB(pobj);
   537         printf("Should do something with one |TestRefObject|:\n");
   538         DoSomethingWithConstTestRefObjectBaseB(pobj);
   539         printf("Should Release and destroy one |TestRefObject|:\n");
   540     }
   542     return 0;
   543 }

mercurial