textrendering/textformatting/test/src/TUnique.cpp
changeset 53 11e2bb0d14ba
parent 0 1fb32624e06b
child 55 336bee5c2d35
equal deleted inserted replaced
46:6124ff6478cc 53:11e2bb0d14ba
     1 /*
     1 /*
     2 * Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    17 */
    17 */
    18 
    18 
    19 
    19 
    20 #include "UniqueInstanceImpl.h"
    20 #include "UniqueInstanceImpl.h"
    21 #include <e32test.h>
    21 #include <e32test.h>
       
    22 #include "tunique.h"
    22 
    23 
    23 using namespace UniqueInstance;
    24 using namespace UniqueInstance;
    24 
    25 
    25 #define UNUSED_VAR(a) a = a
    26 #define UNUSED_VAR(a) a = a
    26 
    27 
    44 	{
    45 	{
    45 	delete reinterpret_cast<TInt*>(a);
    46 	delete reinterpret_cast<TInt*>(a);
    46 	}
    47 	}
    47 void DeleteTInt(TInt* a) { delete a; }
    48 void DeleteTInt(TInt* a) { delete a; }
    48 
    49 
    49 CTrapCleanup* TrapCleanup;
    50 CTUniqueStep* TestStep;
    50 RTest test(_L("TUnique - Unique instance repository tests"));
    51 #define TESTPOINT(p) TestStep->testpoint(p,(TText8*)__FILE__,__LINE__)
       
    52 #define TESTPRINT(p) TestStep->print(p,(TText8*)__FILE__,__LINE__)
    51 }
    53 }
    52 
    54 
    53 /////////////////
    55 /////////////////
    54 //			   //
    56 //			   //
    55 //	RSkipList  //
    57 //	RSkipList  //
    60 {
    62 {
    61 TInt TestEmpty(RSkipList& a)
    63 TInt TestEmpty(RSkipList& a)
    62 	{
    64 	{
    63 	if (!a.IsEmpty())
    65 	if (!a.IsEmpty())
    64 		{
    66 		{
    65 		test.Printf(_L("RSkipList : unexpectedly has elements"));
    67 		TESTPRINT(_L("RSkipList : unexpectedly has elements"));
    66 		return 1;
    68 		return 1;
    67 		}
    69 		}
    68 	return 0;
    70 	return 0;
    69 	}
    71 	}
    70 TInt TestAddL(RSkipList& a, void* aElt, TInt aExpectedRefCount)
    72 TInt TestAddL(RSkipList& a, void* aElt, TInt aExpectedRefCount)
    72 	SElement* e = a.AddExisting(aElt);
    74 	SElement* e = a.AddExisting(aElt);
    73 	if (!e)
    75 	if (!e)
    74 		e = a.AddNewL(aElt);
    76 		e = a.AddNewL(aElt);
    75 	if (CompareVoids(e->iObject,aElt))
    77 	if (CompareVoids(e->iObject,aElt))
    76 		{
    78 		{
    77 		test.Printf(_L("RSkipList : added element does not compare equal to returned value"));
    79         TESTPRINT(_L("RSkipList : added element does not compare equal to returned value"));
    78 		return 1;
    80 		return 1;
    79 		}
    81 		}
    80 	if (e->iRefCount != aExpectedRefCount)
    82 	if (e->iRefCount != aExpectedRefCount)
    81 		{
    83 		{
    82 		test.Printf(_L("RSkipList : unexpected reference count"));
    84         TESTPRINT(_L("RSkipList : unexpected reference count"));
    83 		return 1;
    85 		return 1;
    84 		}
    86 		}
    85 	return 0;
    87 	return 0;
    86 	}
    88 	}
    87 TInt TestRemove(RSkipList& a, void* aElt)
    89 TInt TestRemove(RSkipList& a, void* aElt)
    88 	{
    90 	{
    89 	if (CompareVoids(a.Remove(aElt), aElt))
    91 	if (CompareVoids(a.Remove(aElt), aElt))
    90 		{
    92 		{
    91 		test.Printf(_L("RSkipList : removed element does not compare equal to returned value"));
    93         TESTPRINT(_L("RSkipList : removed element does not compare equal to returned value"));
    92 		return 1;
    94 		return 1;
    93 		}
    95 		}
    94 	return 0;
    96 	return 0;
    95 	}
    97 	}
    96 void Test(RSkipList& a)
    98 void Test(RSkipList& a)
   514 {
   516 {
   515 TInt TestObject(SElement* e, TInt val)
   517 TInt TestObject(SElement* e, TInt val)
   516 	{
   518 	{
   517 	if (!e->iObject)
   519 	if (!e->iObject)
   518 		{
   520 		{
   519 		test.Printf(_L("CRepositoryImpl : object undefined"));
   521         TESTPRINT(_L("CRepositoryImpl : object undefined"));
   520 		return 1;
   522 		return 1;
   521 		}
   523 		}
   522 	if (*reinterpret_cast<TInt*>(e->iObject) != val)
   524 	if (*reinterpret_cast<TInt*>(e->iObject) != val)
   523 		{
   525 		{
   524 		test.Printf(_L("CRepositoryImpl : object has wrong value"));
   526         TESTPRINT(_L("CRepositoryImpl : object has wrong value"));
   525 		return 1;
   527 		return 1;
   526 		}
   528 		}
   527 	return 0;
   529 	return 0;
   528 	}
   530 	}
   529 void TestCRepositoryImplL()
   531 void TestCRepositoryImplL()
   548 	z = new(ELeave) TInt(0);
   550 	z = new(ELeave) TInt(0);
   549 	SElement* z3ui		= rep->IncOrCopyL(z);
   551 	SElement* z3ui		= rep->IncOrCopyL(z);
   550 	delete z;
   552 	delete z;
   551 
   553 
   552 	// test equal objects are nullified
   554 	// test equal objects are nullified
   553 	test(zui->iObject == z2ui->iObject);
   555 	TESTPOINT(zui->iObject == z2ui->iObject);
   554 	test(zui->iObject == z0ui->iObject);
   556 	TESTPOINT(zui->iObject == z0ui->iObject);
   555 	test(zui->iObject == z3ui->iObject);
   557 	TESTPOINT(zui->iObject == z3ui->iObject);
   556 
   558 
   557 	rep->DeleteOrDec(z0ui);
   559 	rep->DeleteOrDec(z0ui);
   558 	rep->DeleteOrDec(z3ui);
   560 	rep->DeleteOrDec(z3ui);
   559 
   561 
   560 	TestObject(zui, 0);
   562 	TestObject(zui, 0);
   569 	z = reinterpret_cast<TInt*>(rep->DetatchOrCopyL(zui));
   571 	z = reinterpret_cast<TInt*>(rep->DetatchOrCopyL(zui));
   570 	z2 = reinterpret_cast<TInt*>(rep->DetatchOrCopyL(z2ui));
   572 	z2 = reinterpret_cast<TInt*>(rep->DetatchOrCopyL(z2ui));
   571 	rep->DeleteOrDec(p8765ui);
   573 	rep->DeleteOrDec(p8765ui);
   572 
   574 
   573 	// test that copyable objects are not aliased
   575 	// test that copyable objects are not aliased
   574 	test(z != z2);
   576 	TESTPOINT(z != z2);
   575 	// test that a valid copy is returned
   577 	// test that a valid copy is returned
   576 	test(*z == 0);
   578 	TESTPOINT(*z == 0);
   577 	test(*z2 == 0);
   579 	TESTPOINT(*z2 == 0);
   578 
   580 
   579 	delete z;
   581 	delete z;
   580 	delete z2;
   582 	delete z2;
   581 
   583 
   582 	delete rep;
   584 	delete rep;
   596 {
   598 {
   597 TInt TestNull(const RUniqueInstance<TInt>& a)
   599 TInt TestNull(const RUniqueInstance<TInt>& a)
   598 	{
   600 	{
   599 	if (a.Peek())
   601 	if (a.Peek())
   600 		{
   602 		{
   601 		test.Printf(_L("RUniqueInstance : null object has value"));
   603         TESTPRINT(_L("RUniqueInstance : null object has value"));
   602 		return 1;
   604 		return 1;
   603 		}
   605 		}
   604 	return 0;
   606 	return 0;
   605 	}
   607 	}
   606 TInt TestValue(const RUniqueInstance<TInt>& a, TInt val)
   608 TInt TestValue(const RUniqueInstance<TInt>& a, TInt val)
   607 	{
   609 	{
   608 	if (!a.Peek())
   610 	if (!a.Peek())
   609 		{
   611 		{
   610 		test.Printf(_L("RUniqueInstance : unexpected null object"));
   612         TESTPRINT(_L("RUniqueInstance : unexpected null object"));
   611 		return 1;
   613 		return 1;
   612 		}
   614 		}
   613 	if (*a.Peek() != val)
   615 	if (*a.Peek() != val)
   614 		{
   616 		{
   615 		test.Printf(_L("RUniqueInstance : object has wrong value"));
   617         TESTPRINT(_L("RUniqueInstance : object has wrong value"));
   616 		return 1;
   618 		return 1;
   617 		}
   619 		}
   618 	return 0;
   620 	return 0;
   619 	}
   621 	}
   620 TInt TestRaw(TInt* ob, TInt val)
   622 TInt TestRaw(TInt* ob, TInt val)
   621 	{
   623 	{
   622 	if (!ob)
   624 	if (!ob)
   623 		{
   625 		{
   624 		test.Printf(_L("RUniqueInstance : object unexpectedly does not own"));
   626         TESTPRINT(_L("RUniqueInstance : object unexpectedly does not own"));
   625 		return 1;
   627 		return 1;
   626 		}
   628 		}
   627 	if (*ob != val)
   629 	if (*ob != val)
   628 		{
   630 		{
   629 		test.Printf(_L("RUniqueInstance : object owns incorrect value"));
   631         TESTPRINT(_L("RUniqueInstance : object owns incorrect value"));
   630 		return 1;
   632 		return 1;
   631 		}
   633 		}
   632 	return 0;
   634 	return 0;
   633 	}
   635 	}
   634 void TestRUniqueInstanceL()
   636 void TestRUniqueInstanceL()
   645 	a.TakeL(new(ELeave) TInt(45));
   647 	a.TakeL(new(ELeave) TInt(45));
   646 	b.TakeL(new(ELeave) TInt(-6));
   648 	b.TakeL(new(ELeave) TInt(-6));
   647 	c.TakeL(new(ELeave) TInt(45));
   649 	c.TakeL(new(ELeave) TInt(45));
   648 
   650 
   649 	// test that equal elements are unified
   651 	// test that equal elements are unified
   650 	test(a.Peek() == c.Peek());
   652 	TESTPOINT(a.Peek() == c.Peek());
   651 
   653 
   652 	TestValue(a, 45);
   654 	TestValue(a, 45);
   653 	TestValue(b, -6);
   655 	TestValue(b, -6);
   654 	TestValue(c, 45);
   656 	TestValue(c, 45);
   655 
   657 
   725 	TestValue(d, 123);
   727 	TestValue(d, 123);
   726 	e.MoveTo(a);
   728 	e.MoveTo(a);
   727 	e.TakeL(new(ELeave) TInt(18));
   729 	e.TakeL(new(ELeave) TInt(18));
   728 
   730 
   729 	// test that equal objects are unified
   731 	// test that equal objects are unified
   730 	test(a.Peek() == e.Peek());
   732 	TESTPOINT(a.Peek() == e.Peek());
   731 
   733 
   732 	d.TakeL(new(ELeave) TInt(-445));
   734 	d.TakeL(new(ELeave) TInt(-445));
   733 
   735 
   734 	// test that equal objects are unified
   736 	// test that equal objects are unified
   735 	test(b.Peek() == d.Peek());
   737 	TESTPOINT(b.Peek() == d.Peek());
   736 
   738 
   737 	a.TakeL(new(ELeave) TInt(-445));
   739 	a.TakeL(new(ELeave) TInt(-445));
   738 
   740 
   739 	// test that objects from different repositories are not unified
   741 	// test that objects from different repositories are not unified
   740 	test(a.Peek() != b.Peek());
   742 	TESTPOINT(a.Peek() != b.Peek());
   741 
   743 
   742 	a.Close();
   744 	a.Close();
   743 
   745 
   744 	// test that destroyed object peeks null
   746 	// test that destroyed object peeks null
   745 	test(a.Peek() == 0);
   747 	TESTPOINT(a.Peek() == 0);
   746 
   748 
   747 	b.MoveTo(c);
   749 	b.MoveTo(c);
   748 	b.Close();
   750 	b.Close();
   749 	c.Close();
   751 	c.Close();
   750 	d.Close();
   752 	d.Close();
   762 //		  //
   764 //		  //
   763 //  Main  //
   765 //  Main  //
   764 //		  //
   766 //		  //
   765 ////////////
   767 ////////////
   766 
   768 
   767 void RunTests()
   769 TVerdict CTUniqueStep::doTestStepL()
   768 	{
   770 	{
   769 	__UHEAP_MARK;
   771     SetTestStepResult(EPass);
   770 	test.Title();
   772     TestStep = this;
   771 	test.Start(_L(" @SYMTestCaseID:SYSLIB-FORM-LEGACY-UNIQUE-0001 Unique Instance Tests: "));
   773     TESTPRINT(_L("TUnique - Unique instance repository tests"));
       
   774 	TESTPRINT(_L(" @SYMTestCaseID:SYSLIB-FORM-LEGACY-UNIQUE-0001 Unique Instance Tests: "));
   772 
   775 
   773 	TestRSkipListL();
   776 	TestRSkipListL();
   774 	TestCRepositoryImplL();
   777 	TestCRepositoryImplL();
   775 	TestRUniqueInstanceL();
   778 	TestRUniqueInstanceL();
   776 
   779 
   777 	test.End();
   780 	return TestStepResult();
   778 	test.Close();
   781 	}
   779 
       
   780 	__UHEAP_MARKENDC(0);
       
   781 	}
       
   782 
       
   783 TInt E32Main()
       
   784 	{
       
   785 	TrapCleanup = CTrapCleanup::New();
       
   786 	TRAPD(err, RunTests());
       
   787     test(err == KErrNone);
       
   788 	delete TrapCleanup;
       
   789 	return 0;
       
   790 	}