xpcom/tests/TestAutoPtr.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/TestAutoPtr.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,543 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +// vim:cindent:ts=4:et:sw=4:
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "nsAutoPtr.h"
    1.11 +#include <stdio.h>
    1.12 +#include "nscore.h"
    1.13 +#include "mozilla/Attributes.h"
    1.14 +
    1.15 +class TestObjectBaseA {
    1.16 +    public:
    1.17 +        // Virtual dtor for deleting through base class pointer
    1.18 +        virtual ~TestObjectBaseA() { }
    1.19 +        int fooA;
    1.20 +};
    1.21 +
    1.22 +class TestObjectBaseB {
    1.23 +    public:
    1.24 +        // Virtual dtor for deleting through base class pointer
    1.25 +        virtual ~TestObjectBaseB() { }
    1.26 +        int fooB;
    1.27 +};
    1.28 +
    1.29 +class TestObject : public TestObjectBaseA, public TestObjectBaseB {
    1.30 +    public:
    1.31 +        TestObject()
    1.32 +        {
    1.33 +            printf("  Creating TestObject %p.\n",
    1.34 +                   static_cast<void*>(this));
    1.35 +        }
    1.36 +
    1.37 +        // Virtual dtor for deleting through base class pointer
    1.38 +        virtual ~TestObject()
    1.39 +        {
    1.40 +            printf("  Destroying TestObject %p.\n",
    1.41 +                   static_cast<void*>(this));
    1.42 +        }
    1.43 +};
    1.44 +
    1.45 +class TestRefObjectBaseA {
    1.46 +    public:
    1.47 +        int fooA;
    1.48 +        // Must return |nsrefcnt| to keep |nsDerivedSafe| happy.
    1.49 +        virtual nsrefcnt AddRef() = 0;
    1.50 +        virtual nsrefcnt Release() = 0;
    1.51 +};
    1.52 +
    1.53 +class TestRefObjectBaseB {
    1.54 +    public:
    1.55 +        int fooB;
    1.56 +        virtual nsrefcnt AddRef() = 0;
    1.57 +        virtual nsrefcnt Release() = 0;
    1.58 +};
    1.59 +
    1.60 +class TestRefObject MOZ_FINAL : public TestRefObjectBaseA, public TestRefObjectBaseB {
    1.61 +    public:
    1.62 +        TestRefObject()
    1.63 +            : mRefCount(0)
    1.64 +        {
    1.65 +            printf("  Creating TestRefObject %p.\n",
    1.66 +                   static_cast<void*>(this));
    1.67 +        }
    1.68 +
    1.69 +        ~TestRefObject()
    1.70 +        {
    1.71 +            printf("  Destroying TestRefObject %p.\n",
    1.72 +                   static_cast<void*>(this));
    1.73 +        }
    1.74 +
    1.75 +        nsrefcnt AddRef()
    1.76 +        {
    1.77 +            ++mRefCount;
    1.78 +            printf("  AddRef to %d on TestRefObject %p.\n",
    1.79 +                   mRefCount, static_cast<void*>(this));
    1.80 +            return mRefCount;
    1.81 +        }
    1.82 +
    1.83 +        nsrefcnt Release()
    1.84 +        {
    1.85 +            --mRefCount;
    1.86 +            printf("  Release to %d on TestRefObject %p.\n",
    1.87 +                   mRefCount, static_cast<void*>(this));
    1.88 +            if (mRefCount == 0) {
    1.89 +                delete const_cast<TestRefObject*>(this);
    1.90 +                return 0;
    1.91 +            }
    1.92 +            return mRefCount;
    1.93 +        }
    1.94 +
    1.95 +    protected:
    1.96 +        uint32_t mRefCount;
    1.97 +
    1.98 +};
    1.99 +
   1.100 +static void CreateTestObject(TestObject **aResult)
   1.101 +{
   1.102 +    *aResult = new TestObject();
   1.103 +}
   1.104 +
   1.105 +static void CreateTestRefObject(TestRefObject **aResult)
   1.106 +{
   1.107 +    (*aResult = new TestRefObject())->AddRef();
   1.108 +}
   1.109 +
   1.110 +static void DoSomethingWithTestObject(TestObject *aIn)
   1.111 +{
   1.112 +    printf("  Doing something with |TestObject| %p.\n",
   1.113 +           static_cast<void*>(aIn));
   1.114 +}
   1.115 +
   1.116 +static void DoSomethingWithConstTestObject(const TestObject *aIn)
   1.117 +{
   1.118 +    printf("  Doing something with |const TestObject| %p.\n",
   1.119 +           static_cast<const void*>(aIn));
   1.120 +}
   1.121 +
   1.122 +static void DoSomethingWithTestRefObject(TestRefObject *aIn)
   1.123 +{
   1.124 +    printf("  Doing something with |TestRefObject| %p.\n",
   1.125 +           static_cast<void*>(aIn));
   1.126 +}
   1.127 +
   1.128 +static void DoSomethingWithConstTestRefObject(const TestRefObject *aIn)
   1.129 +{
   1.130 +    printf("  Doing something with |const TestRefObject| %p.\n",
   1.131 +           static_cast<const void*>(aIn));
   1.132 +}
   1.133 +
   1.134 +static void DoSomethingWithTestObjectBaseB(TestObjectBaseB *aIn)
   1.135 +{
   1.136 +    printf("  Doing something with |TestObjectBaseB| %p.\n",
   1.137 +           static_cast<void*>(aIn));
   1.138 +}
   1.139 +
   1.140 +static void DoSomethingWithConstTestObjectBaseB(const TestObjectBaseB *aIn)
   1.141 +{
   1.142 +    printf("  Doing something with |const TestObjectBaseB| %p.\n",
   1.143 +           static_cast<const void*>(aIn));
   1.144 +}
   1.145 +
   1.146 +static void DoSomethingWithTestRefObjectBaseB(TestRefObjectBaseB *aIn)
   1.147 +{
   1.148 +    printf("  Doing something with |TestRefObjectBaseB| %p.\n",
   1.149 +           static_cast<void*>(aIn));
   1.150 +}
   1.151 +
   1.152 +static void DoSomethingWithConstTestRefObjectBaseB(const TestRefObjectBaseB *aIn)
   1.153 +{
   1.154 +    printf("  Doing something with |const TestRefObjectBaseB| %p.\n",
   1.155 +           static_cast<const void*>(aIn));
   1.156 +}
   1.157 +
   1.158 +int main()
   1.159 +{
   1.160 +    {
   1.161 +        printf("Should create one |TestObject|:\n");
   1.162 +        nsAutoPtr<TestObject> pobj( new TestObject() );
   1.163 +        printf("Should destroy one |TestObject|:\n");
   1.164 +    }
   1.165 +
   1.166 +    {
   1.167 +        printf("Should create one |TestObject|:\n");
   1.168 +        nsAutoPtr<TestObject> pobj( new TestObject() );
   1.169 +        printf("Should create one |TestObject| and then destroy one:\n");
   1.170 +        pobj = new TestObject();
   1.171 +        printf("Should destroy one |TestObject|:\n");
   1.172 +    }
   1.173 +
   1.174 +    {
   1.175 +        printf("Should create 3 |TestObject|s:\n");
   1.176 +        nsAutoArrayPtr<TestObject> pobj( new TestObject[3] );
   1.177 +        printf("Should create 5 |TestObject|s and then destroy 3:\n");
   1.178 +        pobj = new TestObject[5];
   1.179 +        printf("Should destroy 5 |TestObject|s:\n");
   1.180 +    }
   1.181 +
   1.182 +    {
   1.183 +        printf("Should create and AddRef one |TestRefObject|:\n");
   1.184 +        nsRefPtr<TestRefObject> pobj( new TestRefObject() );
   1.185 +        printf("Should Release and destroy one |TestRefObject|:\n");
   1.186 +    }
   1.187 +
   1.188 +    {
   1.189 +        printf("Should create and AddRef one |TestRefObject|:\n");
   1.190 +        nsRefPtr<TestRefObject> pobj( new TestRefObject() );
   1.191 +        printf("Should create and AddRef one |TestRefObject| and then Release and destroy one:\n");
   1.192 +        pobj = new TestRefObject();
   1.193 +        printf("Should Release and destroy one |TestRefObject|:\n");
   1.194 +    }
   1.195 +
   1.196 +    {
   1.197 +        printf("Should create and AddRef one |TestRefObject|:\n");
   1.198 +        nsRefPtr<TestRefObject> p1( new TestRefObject() );
   1.199 +        printf("Should AddRef one |TestRefObject|:\n");
   1.200 +        nsRefPtr<TestRefObject> p2( p1 );
   1.201 +        printf("Should Release twice and destroy one |TestRefObject|:\n");
   1.202 +    }
   1.203 +
   1.204 +    printf("\nTesting equality (with all const-ness combinations):\n");
   1.205 +
   1.206 +    {
   1.207 +        nsRefPtr<TestRefObject> p1( new TestRefObject() );
   1.208 +        nsRefPtr<TestRefObject> p2( p1 );
   1.209 +        printf("equality %s.\n",
   1.210 +               ((p1 == p2) && !(p1 != p2)) ? "OK" : "broken");
   1.211 +    }
   1.212 +
   1.213 +    {
   1.214 +        const nsRefPtr<TestRefObject> p1( new TestRefObject() );
   1.215 +        nsRefPtr<TestRefObject> p2( p1 );
   1.216 +        printf("equality %s.\n",
   1.217 +               ((p1 == p2) && !(p1 != p2)) ? "OK" : "broken");
   1.218 +    }
   1.219 +
   1.220 +    {
   1.221 +        nsRefPtr<TestRefObject> p1( new TestRefObject() );
   1.222 +        const nsRefPtr<TestRefObject> p2( p1 );
   1.223 +        printf("equality %s.\n",
   1.224 +               ((p1 == p2) && !(p1 != p2)) ? "OK" : "broken");
   1.225 +    }
   1.226 +
   1.227 +    {
   1.228 +        const nsRefPtr<TestRefObject> p1( new TestRefObject() );
   1.229 +        const nsRefPtr<TestRefObject> p2( p1 );
   1.230 +        printf("equality %s.\n",
   1.231 +               ((p1 == p2) && !(p1 != p2)) ? "OK" : "broken");
   1.232 +    }
   1.233 +
   1.234 +    {
   1.235 +        nsRefPtr<TestRefObject> p1( new TestRefObject() );
   1.236 +        TestRefObject * p2 = p1;
   1.237 +        printf("equality %s.\n",
   1.238 +               ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
   1.239 +    }
   1.240 +
   1.241 +    {
   1.242 +        const nsRefPtr<TestRefObject> p1( new TestRefObject() );
   1.243 +        TestRefObject * p2 = p1;
   1.244 +        printf("equality %s.\n",
   1.245 +               ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
   1.246 +    }
   1.247 +
   1.248 +#if 0 /* MSVC++ 6.0 can't be coaxed to accept this */
   1.249 +    {
   1.250 +        nsRefPtr<TestRefObject> p1( new TestRefObject() );
   1.251 +        TestRefObject * const p2 = p1;
   1.252 +        printf("equality %s.\n",
   1.253 +               ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
   1.254 +    }
   1.255 +
   1.256 +    {
   1.257 +        const nsRefPtr<TestRefObject> p1( new TestRefObject() );
   1.258 +        TestRefObject * const p2 = p1;
   1.259 +        printf("equality %s.\n",
   1.260 +               ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
   1.261 +    }
   1.262 +#endif /* Things that MSVC++ 6.0 can't be coaxed to accept */
   1.263 +
   1.264 +    {
   1.265 +        nsRefPtr<TestRefObject> p1( new TestRefObject() );
   1.266 +        const TestRefObject * p2 = p1;
   1.267 +        printf("equality %s.\n",
   1.268 +               ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
   1.269 +    }
   1.270 +
   1.271 +    {
   1.272 +        const nsRefPtr<TestRefObject> p1( new TestRefObject() );
   1.273 +        const TestRefObject * p2 = p1;
   1.274 +        printf("equality %s.\n",
   1.275 +               ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
   1.276 +    }
   1.277 +
   1.278 +    {
   1.279 +        nsRefPtr<TestRefObject> p1( new TestRefObject() );
   1.280 +        const TestRefObject * const p2 = p1;
   1.281 +        printf("equality %s.\n",
   1.282 +               ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
   1.283 +    }
   1.284 +
   1.285 +    {
   1.286 +        const nsRefPtr<TestRefObject> p1( new TestRefObject() );
   1.287 +        const TestRefObject * const p2 = p1;
   1.288 +        printf("equality %s.\n",
   1.289 +               ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
   1.290 +    }
   1.291 +
   1.292 +    printf("\nTesting getter_Transfers and getter_AddRefs.\n");
   1.293 +
   1.294 +    {
   1.295 +        nsAutoPtr<TestObject> ptr;
   1.296 +        printf("Should create one |TestObject|:\n");
   1.297 +        CreateTestObject(getter_Transfers(ptr));
   1.298 +        printf("Should destroy one |TestObject|:\n");
   1.299 +    }
   1.300 +
   1.301 +    {
   1.302 +        nsRefPtr<TestRefObject> ptr;
   1.303 +        printf("Should create and AddRef one |TestRefObject|:\n");
   1.304 +        CreateTestRefObject(getter_AddRefs(ptr));
   1.305 +        printf("Should Release and destroy one |TestRefObject|:\n");
   1.306 +    }
   1.307 +
   1.308 +    printf("\nTesting casts and equality tests.\n");
   1.309 +
   1.310 +    if ((void*)(TestObject*)0x1000 ==
   1.311 +        (void*)(TestObjectBaseB*)(TestObject*)0x1000)
   1.312 +        printf("\n\nAll these tests are meaningless!\n\n\n");
   1.313 +
   1.314 +    {
   1.315 +        nsAutoPtr<TestObject> p1(new TestObject());
   1.316 +        TestObjectBaseB *p2 = p1;
   1.317 +        printf("equality %s.\n",
   1.318 +               ((static_cast<void*>(p1) != static_cast<void*>(p2)) &&
   1.319 +                (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
   1.320 +               ? "OK" : "broken");
   1.321 +    }
   1.322 +
   1.323 +    {
   1.324 +        TestObject *p1 = new TestObject();
   1.325 +        nsAutoPtr<TestObjectBaseB> p2(p1);
   1.326 +        printf("equality %s.\n",
   1.327 +               ((static_cast<void*>(p1) != static_cast<void*>(p2)) &&
   1.328 +                (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
   1.329 +               ? "OK" : "broken");
   1.330 +    }
   1.331 +
   1.332 +    {
   1.333 +        nsRefPtr<TestRefObject> p1 = new TestRefObject();
   1.334 +        // nsCOMPtr requires a |get| for something like this as well
   1.335 +        nsRefPtr<TestRefObjectBaseB> p2 = p1.get();
   1.336 +        printf("equality %s.\n",
   1.337 +               ((static_cast<void*>(p1) != static_cast<void*>(p2)) &&
   1.338 +                (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
   1.339 +               ? "OK" : "broken");
   1.340 +    }
   1.341 +
   1.342 +    {
   1.343 +        nsRefPtr<TestRefObject> p1 = new TestRefObject();
   1.344 +        TestRefObjectBaseB *p2 = p1;
   1.345 +        printf("equality %s.\n",
   1.346 +               ((static_cast<void*>(p1) != static_cast<void*>(p2)) &&
   1.347 +                (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
   1.348 +               ? "OK" : "broken");
   1.349 +    }
   1.350 +
   1.351 +    {
   1.352 +        TestRefObject *p1 = new TestRefObject();
   1.353 +        nsRefPtr<TestRefObjectBaseB> p2 = p1;
   1.354 +        printf("equality %s.\n",
   1.355 +               ((static_cast<void*>(p1) != static_cast<void*>(p2)) &&
   1.356 +                (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
   1.357 +               ? "OK" : "broken");
   1.358 +    }
   1.359 +
   1.360 +    printf("\nTesting |forget()|.\n");
   1.361 +
   1.362 +    {
   1.363 +        printf("Should create one |TestObject|:\n");
   1.364 +        nsAutoPtr<TestObject> pobj( new TestObject() );
   1.365 +        printf("Should do nothing:\n");
   1.366 +        nsAutoPtr<TestObject> pobj2( pobj.forget() );
   1.367 +        printf("Should destroy one |TestObject|:\n");
   1.368 +    }
   1.369 +
   1.370 +    {
   1.371 +        printf("Should create 3 |TestObject|s:\n");
   1.372 +        nsAutoArrayPtr<TestObject> pobj( new TestObject[3] );
   1.373 +        printf("Should do nothing:\n");
   1.374 +        nsAutoArrayPtr<TestObject> pobj2( pobj.forget() );
   1.375 +        printf("Should destroy 3 |TestObject|s:\n");
   1.376 +    }
   1.377 +
   1.378 +    {
   1.379 +        printf("Should create one |TestRefObject|:\n");
   1.380 +        nsRefPtr<TestRefObject> pobj( new TestRefObject() );
   1.381 +        printf("Should do nothing:\n");
   1.382 +        nsRefPtr<TestRefObject> pobj2( pobj.forget() );
   1.383 +        printf("Should destroy one |TestRefObject|:\n");
   1.384 +    }
   1.385 +
   1.386 +
   1.387 +    printf("\nTesting construction.\n");
   1.388 +
   1.389 +    {
   1.390 +        printf("Should create one |TestObject|:\n");
   1.391 +        nsAutoPtr<TestObject> pobj(new TestObject());
   1.392 +        printf("Should destroy one |TestObject|:\n");
   1.393 +    }
   1.394 +
   1.395 +    {
   1.396 +        printf("Should create 3 |TestObject|s:\n");
   1.397 +        nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
   1.398 +        printf("Should destroy 3 |TestObject|s:\n");
   1.399 +    }
   1.400 +
   1.401 +    {
   1.402 +        printf("Should create and AddRef one |TestRefObject|:\n");
   1.403 +        nsRefPtr<TestRefObject> pobj = new TestRefObject();
   1.404 +        printf("Should Release and destroy one |TestRefObject|:\n");
   1.405 +    }
   1.406 +
   1.407 +    printf("\nTesting calling of functions (including array access and casts).\n");
   1.408 +
   1.409 +    {
   1.410 +        printf("Should create one |TestObject|:\n");
   1.411 +        nsAutoPtr<TestObject> pobj(new TestObject());
   1.412 +        printf("Should do something with one |TestObject|:\n");
   1.413 +        DoSomethingWithTestObject(pobj);
   1.414 +        printf("Should do something with one |TestObject|:\n");
   1.415 +        DoSomethingWithConstTestObject(pobj);
   1.416 +        printf("Should destroy one |TestObject|:\n");
   1.417 +    }
   1.418 +
   1.419 +    {
   1.420 +        printf("Should create 3 |TestObject|s:\n");
   1.421 +        nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
   1.422 +        printf("Should do something with one |TestObject|:\n");
   1.423 +        DoSomethingWithTestObject(&pobj[2]);
   1.424 +        printf("Should do something with one |TestObject|:\n");
   1.425 +        DoSomethingWithConstTestObject(&pobj[1]);
   1.426 +        printf("Should do something with one |TestObject|:\n");
   1.427 +        DoSomethingWithTestObject(pobj + 2);
   1.428 +        printf("Should do something with one |TestObject|:\n");
   1.429 +        DoSomethingWithConstTestObject(pobj + 1);
   1.430 +        printf("Should destroy 3 |TestObject|s:\n");
   1.431 +    }
   1.432 +
   1.433 +    {
   1.434 +        printf("Should create and AddRef one |TestRefObject|:\n");
   1.435 +        nsRefPtr<TestRefObject> pobj = new TestRefObject();
   1.436 +        printf("Should do something with one |TestRefObject|:\n");
   1.437 +        DoSomethingWithTestRefObject(pobj);
   1.438 +        printf("Should do something with one |TestRefObject|:\n");
   1.439 +        DoSomethingWithConstTestRefObject(pobj);
   1.440 +        printf("Should Release and destroy one |TestRefObject|:\n");
   1.441 +    }
   1.442 +
   1.443 +    {
   1.444 +        printf("Should create one |TestObject|:\n");
   1.445 +        nsAutoPtr<TestObject> pobj(new TestObject());
   1.446 +        printf("Should do something with one |TestObject|:\n");
   1.447 +        DoSomethingWithTestObjectBaseB(pobj);
   1.448 +        printf("Should do something with one |TestObject|:\n");
   1.449 +        DoSomethingWithConstTestObjectBaseB(pobj);
   1.450 +        printf("Should destroy one |TestObject|:\n");
   1.451 +    }
   1.452 +
   1.453 +    {
   1.454 +        printf("Should create 3 |TestObject|s:\n");
   1.455 +        nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
   1.456 +        printf("Should do something with one |TestObject|:\n");
   1.457 +        DoSomethingWithTestObjectBaseB(&pobj[2]);
   1.458 +        printf("Should do something with one |TestObject|:\n");
   1.459 +        DoSomethingWithConstTestObjectBaseB(&pobj[1]);
   1.460 +        printf("Should do something with one |TestObject|:\n");
   1.461 +        DoSomethingWithTestObjectBaseB(pobj + 2);
   1.462 +        printf("Should do something with one |TestObject|:\n");
   1.463 +        DoSomethingWithConstTestObjectBaseB(pobj + 1);
   1.464 +        printf("Should destroy 3 |TestObject|s:\n");
   1.465 +    }
   1.466 +
   1.467 +    {
   1.468 +        printf("Should create and AddRef one |TestRefObject|:\n");
   1.469 +        nsRefPtr<TestRefObject> pobj = new TestRefObject();
   1.470 +        printf("Should do something with one |TestRefObject|:\n");
   1.471 +        DoSomethingWithTestRefObjectBaseB(pobj);
   1.472 +        printf("Should do something with one |TestRefObject|:\n");
   1.473 +        DoSomethingWithConstTestRefObjectBaseB(pobj);
   1.474 +        printf("Should Release and destroy one |TestRefObject|:\n");
   1.475 +    }
   1.476 +
   1.477 +    {
   1.478 +        printf("Should create one |TestObject|:\n");
   1.479 +        const nsAutoPtr<TestObject> pobj(new TestObject());
   1.480 +        printf("Should do something with one |TestObject|:\n");
   1.481 +        DoSomethingWithTestObject(pobj);
   1.482 +        printf("Should do something with one |TestObject|:\n");
   1.483 +        DoSomethingWithConstTestObject(pobj);
   1.484 +        printf("Should destroy one |TestObject|:\n");
   1.485 +    }
   1.486 +
   1.487 +    {
   1.488 +        printf("Should create 3 |TestObject|s:\n");
   1.489 +        const nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
   1.490 +        printf("Should do something with one |TestObject|:\n");
   1.491 +        DoSomethingWithTestObject(&pobj[2]);
   1.492 +        printf("Should do something with one |TestObject|:\n");
   1.493 +        DoSomethingWithConstTestObject(&pobj[1]);
   1.494 +        printf("Should do something with one |TestObject|:\n");
   1.495 +        DoSomethingWithTestObject(pobj + 2);
   1.496 +        printf("Should do something with one |TestObject|:\n");
   1.497 +        DoSomethingWithConstTestObject(pobj + 1);
   1.498 +        printf("Should destroy 3 |TestObject|s:\n");
   1.499 +    }
   1.500 +
   1.501 +    {
   1.502 +        printf("Should create and AddRef one |TestRefObject|:\n");
   1.503 +        const nsRefPtr<TestRefObject> pobj = new TestRefObject();
   1.504 +        printf("Should do something with one |TestRefObject|:\n");
   1.505 +        DoSomethingWithTestRefObject(pobj);
   1.506 +        printf("Should do something with one |TestRefObject|:\n");
   1.507 +        DoSomethingWithConstTestRefObject(pobj);
   1.508 +        printf("Should Release and destroy one |TestRefObject|:\n");
   1.509 +    }
   1.510 +
   1.511 +    {
   1.512 +        printf("Should create one |TestObject|:\n");
   1.513 +        const nsAutoPtr<TestObject> pobj(new TestObject());
   1.514 +        printf("Should do something with one |TestObject|:\n");
   1.515 +        DoSomethingWithTestObjectBaseB(pobj);
   1.516 +        printf("Should do something with one |TestObject|:\n");
   1.517 +        DoSomethingWithConstTestObjectBaseB(pobj);
   1.518 +        printf("Should destroy one |TestObject|:\n");
   1.519 +    }
   1.520 +
   1.521 +    {
   1.522 +        printf("Should create 3 |TestObject|s:\n");
   1.523 +        const nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
   1.524 +        printf("Should do something with one |TestObject|:\n");
   1.525 +        DoSomethingWithTestObjectBaseB(&pobj[2]);
   1.526 +        printf("Should do something with one |TestObject|:\n");
   1.527 +        DoSomethingWithConstTestObjectBaseB(&pobj[1]);
   1.528 +        printf("Should do something with one |TestObject|:\n");
   1.529 +        DoSomethingWithTestObjectBaseB(pobj + 2);
   1.530 +        printf("Should do something with one |TestObject|:\n");
   1.531 +        DoSomethingWithConstTestObjectBaseB(pobj + 1);
   1.532 +        printf("Should destroy 3 |TestObject|s:\n");
   1.533 +    }
   1.534 +
   1.535 +    {
   1.536 +        printf("Should create and AddRef one |TestRefObject|:\n");
   1.537 +        const nsRefPtr<TestRefObject> pobj = new TestRefObject();
   1.538 +        printf("Should do something with one |TestRefObject|:\n");
   1.539 +        DoSomethingWithTestRefObjectBaseB(pobj);
   1.540 +        printf("Should do something with one |TestRefObject|:\n");
   1.541 +        DoSomethingWithConstTestRefObjectBaseB(pobj);
   1.542 +        printf("Should Release and destroy one |TestRefObject|:\n");
   1.543 +    }
   1.544 +
   1.545 +    return 0;
   1.546 +}

mercurial