/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
// vim:cindent:ts=4:et:sw=4:
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is TestCOMPtrEq.cpp.
*
* The Initial Developer of the Original Code is
* L. David Baron.
* Portions created by the Initial Developer are Copyright (C) 2001
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* L. David Baron <dbaron@dbaron.org> (original author)
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsAutoPtr.h"
#include <stdio.h>
#include "nscore.h"
class TestObjectBaseA {
public:
// Virtual dtor for deleting through base class pointer
virtual ~TestObjectBaseA() { };
int fooA;
};
class TestObjectBaseB {
public:
// Virtual dtor for deleting through base class pointer
virtual ~TestObjectBaseB() { };
int fooB;
};
public:
{
printf(" Creating TestObject %p.\n",
NS_STATIC_CAST(void*, this));
}
// Virtual dtor for deleting through base class pointer
virtual ~TestObject()
{
printf(" Destroying TestObject %p.\n",
NS_STATIC_CAST(void*, this));
}
};
class TestRefObjectBaseA {
public:
int fooA;
// Must return |nsrefcnt| to keep |nsDerivedSafe| happy.
};
class TestRefObjectBaseB {
public:
int fooB;
};
public:
: mRefCount(0)
{
printf(" Creating TestRefObject %p.\n",
NS_STATIC_CAST(void*, this));
}
{
printf(" Destroying TestRefObject %p.\n",
NS_STATIC_CAST(void*, this));
}
{
++mRefCount;
printf(" AddRef to %d on TestRefObject %p.\n",
mRefCount, NS_STATIC_CAST(void*, this));
return mRefCount;
}
{
--mRefCount;
printf(" Release to %d on TestRefObject %p.\n",
mRefCount, NS_STATIC_CAST(void*, this));
if (mRefCount == 0) {
delete NS_CONST_CAST(TestRefObject*, this);
return 0;
}
return mRefCount;
}
protected:
};
{
*aResult = new TestObject();
}
{
}
{
printf(" Doing something with |TestObject| %p.\n",
NS_STATIC_CAST(void*, aIn));
}
{
printf(" Doing something with |const TestObject| %p.\n",
NS_STATIC_CAST(const void*, aIn));
}
{
printf(" Doing something with |TestRefObject| %p.\n",
NS_STATIC_CAST(void*, aIn));
}
{
printf(" Doing something with |const TestRefObject| %p.\n",
NS_STATIC_CAST(const void*, aIn));
}
{
printf(" Doing something with |TestObjectBaseB| %p.\n",
NS_STATIC_CAST(void*, aIn));
}
{
printf(" Doing something with |const TestObjectBaseB| %p.\n",
NS_STATIC_CAST(const void*, aIn));
}
{
printf(" Doing something with |TestRefObjectBaseB| %p.\n",
NS_STATIC_CAST(void*, aIn));
}
{
printf(" Doing something with |const TestRefObjectBaseB| %p.\n",
NS_STATIC_CAST(const void*, aIn));
}
int main()
{
{
printf("Should create one |TestObject|:\n");
printf("Should destroy one |TestObject|:\n");
}
{
printf("Should create one |TestObject|:\n");
printf("Should create one |TestObject| and then destroy one:\n");
pobj = new TestObject();
printf("Should destroy one |TestObject|:\n");
}
{
printf("Should create 3 |TestObject|s:\n");
printf("Should create 5 |TestObject|s and then destroy 3:\n");
printf("Should destroy 5 |TestObject|s:\n");
}
{
printf("Should create and AddRef one |TestRefObject|:\n");
printf("Should Release and destroy one |TestRefObject|:\n");
}
{
printf("Should create and AddRef one |TestRefObject|:\n");
printf("Should create and AddRef one |TestRefObject| and then Release and destroy one:\n");
pobj = new TestRefObject();
printf("Should Release and destroy one |TestRefObject|:\n");
}
{
printf("Should create and AddRef one |TestRefObject|:\n");
printf("Should AddRef one |TestRefObject|:\n");
printf("Should Release twice and destroy one |TestRefObject|:\n");
}
printf("\nTesting equality (with all const-ness combinations):\n");
{
printf("equality %s.\n",
}
{
printf("equality %s.\n",
}
{
printf("equality %s.\n",
}
{
printf("equality %s.\n",
}
{
printf("equality %s.\n",
}
{
printf("equality %s.\n",
}
#if 0 /* MSVC++ 6.0 can't be coaxed to accept this */
{
printf("equality %s.\n",
}
{
printf("equality %s.\n",
}
#endif /* Things that MSVC++ 6.0 can't be coaxed to accept */
{
printf("equality %s.\n",
}
{
printf("equality %s.\n",
}
{
printf("equality %s.\n",
}
{
printf("equality %s.\n",
}
printf("\nTesting getter_Transfers and getter_AddRefs.\n");
{
printf("Should create one |TestObject|:\n");
printf("Should destroy one |TestObject|:\n");
}
{
printf("Should create and AddRef one |TestRefObject|:\n");
printf("Should Release and destroy one |TestRefObject|:\n");
}
printf("\nTesting casts and equality tests.\n");
if ((void*)(TestObject*)0x1000 ==
printf("\n\nAll these tests are meaningless!\n\n\n");
{
printf("equality %s.\n",
? "OK" : "broken");
}
{
printf("equality %s.\n",
? "OK" : "broken");
}
{
// nsCOMPtr requires a |get| for something like this as well
printf("equality %s.\n",
? "OK" : "broken");
}
{
printf("equality %s.\n",
? "OK" : "broken");
}
{
printf("equality %s.\n",
? "OK" : "broken");
}
printf("\nTesting |forget()|.\n");
{
printf("Should create one |TestObject|:\n");
printf("Should do nothing:\n");
printf("Should destroy one |TestObject|:\n");
}
{
printf("Should create 3 |TestObject|s:\n");
printf("Should do nothing:\n");
printf("Should destroy 3 |TestObject|s:\n");
}
printf("\nTesting construction.\n");
{
printf("Should create one |TestObject|:\n");
printf("Should destroy one |TestObject|:\n");
}
{
printf("Should create 3 |TestObject|s:\n");
printf("Should destroy 3 |TestObject|s:\n");
}
{
printf("Should create and AddRef one |TestRefObject|:\n");
printf("Should Release and destroy one |TestRefObject|:\n");
}
printf("\nTesting calling of functions (including array access and casts).\n");
{
printf("Should create one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should destroy one |TestObject|:\n");
}
{
printf("Should create 3 |TestObject|s:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should destroy 3 |TestObject|s:\n");
}
{
printf("Should create and AddRef one |TestRefObject|:\n");
printf("Should do something with one |TestRefObject|:\n");
printf("Should do something with one |TestRefObject|:\n");
printf("Should Release and destroy one |TestRefObject|:\n");
}
{
printf("Should create one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should destroy one |TestObject|:\n");
}
{
printf("Should create 3 |TestObject|s:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should destroy 3 |TestObject|s:\n");
}
{
printf("Should create and AddRef one |TestRefObject|:\n");
printf("Should do something with one |TestRefObject|:\n");
printf("Should do something with one |TestRefObject|:\n");
printf("Should Release and destroy one |TestRefObject|:\n");
}
{
printf("Should create one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should destroy one |TestObject|:\n");
}
{
printf("Should create 3 |TestObject|s:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should destroy 3 |TestObject|s:\n");
}
{
printf("Should create and AddRef one |TestRefObject|:\n");
printf("Should do something with one |TestRefObject|:\n");
printf("Should do something with one |TestRefObject|:\n");
printf("Should Release and destroy one |TestRefObject|:\n");
}
{
printf("Should create one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should destroy one |TestObject|:\n");
}
{
printf("Should create 3 |TestObject|s:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should do something with one |TestObject|:\n");
printf("Should destroy 3 |TestObject|s:\n");
}
{
printf("Should create and AddRef one |TestRefObject|:\n");
printf("Should do something with one |TestRefObject|:\n");
printf("Should do something with one |TestRefObject|:\n");
printf("Should Release and destroy one |TestRefObject|:\n");
}
return 0;
}