diff --git a/xpcom/tests/TestAutoPtr.cpp b/xpcom/tests/TestAutoPtr.cpp index 3f87d1bb6f83..7f90660a6f42 100644 --- a/xpcom/tests/TestAutoPtr.cpp +++ b/xpcom/tests/TestAutoPtr.cpp @@ -83,131 +83,159 @@ static void DoSomethingWithConstTestObjectBaseB(const TestObjectBaseB *aIn) static_cast(aIn)); } +void test_assignment() +{ + { + printf("Should create one |TestObject|:\n"); + nsAutoPtr pobj( new TestObject() ); + printf("Should destroy one |TestObject|:\n"); + } + + { + printf("Should create one |TestObject|:\n"); + nsAutoPtr pobj( new TestObject() ); + printf("Should create one |TestObject| and then destroy one:\n"); + pobj = new TestObject(); + printf("Should destroy one |TestObject|:\n"); + } +} + +void test_getter_Transfers() +{ + printf("\nTesting getter_Transfers.\n"); + + { + nsAutoPtr ptr; + printf("Should create one |TestObject|:\n"); + CreateTestObject(getter_Transfers(ptr)); + printf("Should destroy one |TestObject|:\n"); + } +} + +void test_casting() +{ + printf("\nTesting casts and equality tests.\n"); + + // This comparison is always false, as it should be. The extra parens + // suppress a -Wunreachable-code warning about printf being unreachable. + if (((void*)(TestObject*)0x1000) == + ((void*)(TestObjectBaseB*)(TestObject*)0x1000)) + printf("\n\nAll these tests are meaningless!\n\n\n"); + + { + nsAutoPtr p1(new TestObject()); + TestObjectBaseB *p2 = p1; + printf("equality %s.\n", + ((static_cast(p1) != static_cast(p2)) && + (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) + ? "OK" : "broken"); + } + + { + TestObject *p1 = new TestObject(); + nsAutoPtr p2(p1); + printf("equality %s.\n", + ((static_cast(p1) != static_cast(p2)) && + (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) + ? "OK" : "broken"); + } +} + +void test_forget() +{ + printf("\nTesting |forget()|.\n"); + + { + printf("Should create one |TestObject|:\n"); + nsAutoPtr pobj( new TestObject() ); + printf("Should do nothing:\n"); + nsAutoPtr pobj2( pobj.forget() ); + printf("Should destroy one |TestObject|:\n"); + } +} + +void test_construction() +{ + printf("\nTesting construction.\n"); + + { + printf("Should create one |TestObject|:\n"); + nsAutoPtr pobj(new TestObject()); + printf("Should destroy one |TestObject|:\n"); + } +} + +void test_implicit_conversion() +{ + printf("\nTesting calling of functions (including array access and casts).\n"); + + { + printf("Should create one |TestObject|:\n"); + nsAutoPtr pobj(new TestObject()); + printf("Should do something with one |TestObject|:\n"); + DoSomethingWithTestObject(pobj); + printf("Should do something with one |TestObject|:\n"); + DoSomethingWithConstTestObject(pobj); + printf("Should destroy one |TestObject|:\n"); + } + + { + printf("Should create one |TestObject|:\n"); + nsAutoPtr pobj(new TestObject()); + printf("Should do something with one |TestObject|:\n"); + DoSomethingWithTestObjectBaseB(pobj); + printf("Should do something with one |TestObject|:\n"); + DoSomethingWithConstTestObjectBaseB(pobj); + printf("Should destroy one |TestObject|:\n"); + } + + { + printf("Should create one |TestObject|:\n"); + const nsAutoPtr pobj(new TestObject()); + printf("Should do something with one |TestObject|:\n"); + DoSomethingWithTestObject(pobj); + printf("Should do something with one |TestObject|:\n"); + DoSomethingWithConstTestObject(pobj); + printf("Should destroy one |TestObject|:\n"); + } + + { + printf("Should create one |TestObject|:\n"); + const nsAutoPtr pobj(new TestObject()); + printf("Should do something with one |TestObject|:\n"); + DoSomethingWithTestObjectBaseB(pobj); + printf("Should do something with one |TestObject|:\n"); + DoSomethingWithConstTestObjectBaseB(pobj); + printf("Should destroy one |TestObject|:\n"); + } +} + +void test_arrow_operator() +{ + { + int test = 1; + void (TestObjectBaseA::*fPtr)( int, int*, int& ) = &TestObjectBaseA::MemberFunction; + void (TestObjectBaseA::*fVPtr)( int, int*, int& ) = &TestObjectBaseA::VirtualMemberFunction; + void (TestObjectBaseA::*fVCPtr)( int, int*, int& ) const = &TestObjectBaseA::VirtualConstMemberFunction; + printf("Should create one |TestObject|:\n"); + nsAutoPtr pobj(new TestObject()); + printf("Should do something with operator->*:\n"); + (pobj->*fPtr)(test, &test, test); + (pobj->*fVPtr)(test, &test, test); + (pobj->*fVCPtr)(test, &test, test); + printf("Should destroy one |TestObject|:\n"); + } +} + int main() { - { - printf("Should create one |TestObject|:\n"); - nsAutoPtr pobj( new TestObject() ); - printf("Should destroy one |TestObject|:\n"); - } + test_assignment(); + test_getter_Transfers(); + test_casting(); + test_forget(); + test_construction(); + test_implicit_conversion(); + test_arrow_operator(); //??? - { - printf("Should create one |TestObject|:\n"); - nsAutoPtr pobj( new TestObject() ); - printf("Should create one |TestObject| and then destroy one:\n"); - pobj = new TestObject(); - printf("Should destroy one |TestObject|:\n"); - } - - printf("\nTesting getter_Transfers.\n"); - - { - nsAutoPtr ptr; - printf("Should create one |TestObject|:\n"); - CreateTestObject(getter_Transfers(ptr)); - printf("Should destroy one |TestObject|:\n"); - } - - printf("\nTesting casts and equality tests.\n"); - - // This comparison is always false, as it should be. The extra parens - // suppress a -Wunreachable-code warning about printf being unreachable. - if (((void*)(TestObject*)0x1000) == - ((void*)(TestObjectBaseB*)(TestObject*)0x1000)) - printf("\n\nAll these tests are meaningless!\n\n\n"); - - { - nsAutoPtr p1(new TestObject()); - TestObjectBaseB *p2 = p1; - printf("equality %s.\n", - ((static_cast(p1) != static_cast(p2)) && - (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) - ? "OK" : "broken"); - } - - { - TestObject *p1 = new TestObject(); - nsAutoPtr p2(p1); - printf("equality %s.\n", - ((static_cast(p1) != static_cast(p2)) && - (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) - ? "OK" : "broken"); - } - - printf("\nTesting |forget()|.\n"); - - { - printf("Should create one |TestObject|:\n"); - nsAutoPtr pobj( new TestObject() ); - printf("Should do nothing:\n"); - nsAutoPtr pobj2( pobj.forget() ); - printf("Should destroy one |TestObject|:\n"); - } - - printf("\nTesting construction.\n"); - - { - printf("Should create one |TestObject|:\n"); - nsAutoPtr pobj(new TestObject()); - printf("Should destroy one |TestObject|:\n"); - } - - printf("\nTesting calling of functions (including array access and casts).\n"); - - { - printf("Should create one |TestObject|:\n"); - nsAutoPtr pobj(new TestObject()); - printf("Should do something with one |TestObject|:\n"); - DoSomethingWithTestObject(pobj); - printf("Should do something with one |TestObject|:\n"); - DoSomethingWithConstTestObject(pobj); - printf("Should destroy one |TestObject|:\n"); - } - - { - printf("Should create one |TestObject|:\n"); - nsAutoPtr pobj(new TestObject()); - printf("Should do something with one |TestObject|:\n"); - DoSomethingWithTestObjectBaseB(pobj); - printf("Should do something with one |TestObject|:\n"); - DoSomethingWithConstTestObjectBaseB(pobj); - printf("Should destroy one |TestObject|:\n"); - } - - { - printf("Should create one |TestObject|:\n"); - const nsAutoPtr pobj(new TestObject()); - printf("Should do something with one |TestObject|:\n"); - DoSomethingWithTestObject(pobj); - printf("Should do something with one |TestObject|:\n"); - DoSomethingWithConstTestObject(pobj); - printf("Should destroy one |TestObject|:\n"); - } - - { - printf("Should create one |TestObject|:\n"); - const nsAutoPtr pobj(new TestObject()); - printf("Should do something with one |TestObject|:\n"); - DoSomethingWithTestObjectBaseB(pobj); - printf("Should do something with one |TestObject|:\n"); - DoSomethingWithConstTestObjectBaseB(pobj); - printf("Should destroy one |TestObject|:\n"); - } - - - { - int test = 1; - void (TestObjectBaseA::*fPtr)( int, int*, int& ) = &TestObjectBaseA::MemberFunction; - void (TestObjectBaseA::*fVPtr)( int, int*, int& ) = &TestObjectBaseA::VirtualMemberFunction; - void (TestObjectBaseA::*fVCPtr)( int, int*, int& ) const = &TestObjectBaseA::VirtualConstMemberFunction; - printf("Should create one |TestObject|:\n"); - nsAutoPtr pobj(new TestObject()); - printf("Should do something with operator->*:\n"); - (pobj->*fPtr)(test, &test, test); - (pobj->*fVPtr)(test, &test, test); - (pobj->*fVCPtr)(test, &test, test); - printf("Should destroy one |TestObject|:\n"); - } - - return 0; + return 0; }