/////////////////////////////////////////////////////////////////////////////// // Unit Test for Loki // // Copyright Terje Slettebų and Pavel Vozenilek 2002. // // Permission to use, copy, modify, and distribute this software for any // purpose is hereby granted without fee, provided that this copyright and // permissions notice appear in all copies and derivatives. // // This software is provided "as is" without express or implied warranty. // /////////////////////////////////////////////////////////////////////////////// // $Header: #ifndef SMARTPTRTEST_H #define SMARTPTRTEST_H #include "UnitTest.h" #include #include #include #if 0 // throw assert on failed test #define LOKI_assert(x) assert(x) #else #define LOKI_assert(x) #endif using namespace Loki; // friend injection // see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28597 class TestClass; typedef SmartPtr Class; bool Compare( const Class& a, const Class& b ) { return true; } void friend_injection() { std::vector vec; std::sort( vec.begin(), vec.end(), Compare ); std::nth_element( vec.begin(), vec.begin(), vec.end(), Compare ); std::search( vec.begin(), vec.end(), vec.begin(), vec.end(), Compare ); Class a, b; Compare( a, b ); } /////////////////////////////////////////////////////////////////////////////// // SmartPtrTest /////////////////////////////////////////////////////////////////////////////// class TestClass { public: TestClass() : references(1) { ++instances; } ~TestClass() { --instances; } void AddRef() { ++references; } void Release() { --references; if (references <= 0) delete this; } TestClass* Clone() { return new TestClass(*this); } public: static int instances; int references; }; int TestClass::instances = 0; class SmartPtrTest : public Test { public: SmartPtrTest() : Test("SmartPtr.h") {} virtual void execute(TestResult &result) { printName(result); using namespace Loki; { SmartPtr p = new TestClass; } bool test1=TestClass::instances == 0; { p0 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p1 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p2 p(new TestClass); p2 pp(p); } LOKI_assert(TestClass::instances==0); { p3 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p4 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p5 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p6 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p7 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p8 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p9 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p10 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p11 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p12 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p13 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p14 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p15 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p16 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p17 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p18 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p19 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p20 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p21 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p22 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p23 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p24 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p25 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p26 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p27 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p28 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p29 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p30 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p31 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p40 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p41 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p42 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p43 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p44 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p45 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p46 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p47 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p48 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p49 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p50 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p51 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p52 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p53 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p54 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p55 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p56 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p57 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p58 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p59 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p60 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p61 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p62 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p63 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p64 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p65 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p66 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p67 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p68 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p69 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p70 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p71 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p72 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p73 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p74 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p75 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p76 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p77 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p78 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p79 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p80 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p81 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p82 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p83 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p84 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p85 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p86 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p87 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p88 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p89 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p90 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p91 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p92 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p93 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p94 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p95 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p96 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p97 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p98 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p99 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p100 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p101 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p102 p(new TestClass); } LOKI_assert(TestClass::instances==0); { p103 p(new TestClass); } bool test2=TestClass::instances==0; bool r=test1 && test2; testAssert("SmartPtr",r,result); std::cout << '\n'; } private: typedef SmartPtr p0; typedef SmartPtr p1; typedef SmartPtr::RefCountedMT, DisallowConversion, AssertCheck, DefaultSPStorage> p2; typedef SmartPtr p3; typedef SmartPtr p4; typedef SmartPtr p5; typedef SmartPtr p6; typedef SmartPtr p7; typedef SmartPtr p8; typedef SmartPtr p9; typedef SmartPtr::RefCountedMT, AllowConversion, AssertCheck, DefaultSPStorage> p10; typedef SmartPtr p11; typedef SmartPtr p12; typedef SmartPtr p13; typedef SmartPtr p14; typedef SmartPtr p15; typedef SmartPtr p16; typedef SmartPtr p17; typedef SmartPtr::RefCountedMT, DisallowConversion, AssertCheckStrict, DefaultSPStorage> p18; typedef SmartPtr p19; typedef SmartPtr p20; typedef SmartPtr p21; typedef SmartPtr p22; typedef SmartPtr p23; typedef SmartPtr p24; typedef SmartPtr p25; typedef SmartPtr::RefCountedMT, AllowConversion, AssertCheckStrict, DefaultSPStorage> p26; typedef SmartPtr p27; typedef SmartPtr p28; typedef SmartPtr p29; typedef SmartPtr p30; typedef SmartPtr p31; typedef SmartPtr p40; typedef SmartPtr p41; typedef SmartPtr::RefCountedMT, AllowConversion, RejectNullStatic, DefaultSPStorage> p42; typedef SmartPtr p43; typedef SmartPtr p44; typedef SmartPtr p45; typedef SmartPtr p46; typedef SmartPtr p47; typedef SmartPtr p48; typedef SmartPtr p49; typedef SmartPtr::RefCountedMT, DisallowConversion, RejectNullStatic, DefaultSPStorage> p50; typedef SmartPtr p51; typedef SmartPtr p52; typedef SmartPtr p53; typedef SmartPtr p54; typedef SmartPtr p55; typedef SmartPtr p56; typedef SmartPtr p57; typedef SmartPtr::RefCountedMT, AllowConversion, RejectNull, DefaultSPStorage> p58; typedef SmartPtr p59; typedef SmartPtr p60; typedef SmartPtr p61; typedef SmartPtr p62; typedef SmartPtr p63; typedef SmartPtr p64; typedef SmartPtr p65; typedef SmartPtr::RefCountedMT, DisallowConversion, RejectNull, DefaultSPStorage> p66; typedef SmartPtr p67; typedef SmartPtr p68; typedef SmartPtr p69; typedef SmartPtr p70; typedef SmartPtr p71; typedef SmartPtr p72; typedef SmartPtr p73; typedef SmartPtr::RefCountedMT, AllowConversion, RejectNullStrict, DefaultSPStorage> p74; typedef SmartPtr p75; typedef SmartPtr p76; typedef SmartPtr p77; typedef SmartPtr p78; typedef SmartPtr p79; typedef SmartPtr p80; typedef SmartPtr p81; typedef SmartPtr::RefCountedMT, DisallowConversion, RejectNullStrict, DefaultSPStorage> p82; typedef SmartPtr p83; typedef SmartPtr p84; typedef SmartPtr p85; typedef SmartPtr p86; typedef SmartPtr p87; typedef SmartPtr p88; typedef SmartPtr p89; typedef SmartPtr::RefCountedMT, AllowConversion, NoCheck, DefaultSPStorage> p90; typedef SmartPtr p91; typedef SmartPtr p92; typedef SmartPtr p93; typedef SmartPtr p94; typedef SmartPtr p95; typedef SmartPtr p96; typedef SmartPtr p97; typedef SmartPtr::RefCountedMT, DisallowConversion, NoCheck, DefaultSPStorage> p98; typedef SmartPtr p99; typedef SmartPtr p100; typedef SmartPtr p101; typedef SmartPtr p102; typedef SmartPtr p103; } smartPtrTest; #ifndef SMARTPTR_CPP # define SMARTPTR_CPP # include "../../src/SmartPtr.cpp" #endif #endif