Update contrib.
2 * Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * TUnique.cpp test file for UniqueInstance classes
20 #include "UniqueInstanceImpl.h"
24 using namespace UniqueInstance;
26 #define UNUSED_VAR(a) a = a
28 // we will manipulate TInts for simplicity
29 // some functions that will help us
32 TInt CompareVoids(void* pl, void* pr)
34 TInt l = *reinterpret_cast<TInt*>(pl);
35 TInt r = *reinterpret_cast<TInt*>(pr);
38 TInt CompareTInts(const TInt* l, const TInt* r) { return *r - *l; }
39 void* CopyVoidL(void* a, TInt)
41 return new(ELeave) TInt(*reinterpret_cast<TInt*>(a));
43 TInt* CopyTIntL(const TInt* a, TInt) { return new(ELeave) TInt(*a); }
44 void DeleteVoid(void* a)
46 delete reinterpret_cast<TInt*>(a);
48 void DeleteTInt(TInt* a) { delete a; }
50 CTUniqueStep* TestStep;
51 #define TESTPOINT(p) TestStep->testpoint(p,(TText8*)__FILE__,__LINE__)
52 #define TESTPRINT(p) TestStep->print(p,(TText8*)__FILE__,__LINE__)
63 TInt TestEmpty(RSkipList& a)
67 TESTPRINT(_L("RSkipList : unexpectedly has elements"));
72 TInt TestAddL(RSkipList& a, void* aElt, TInt aExpectedRefCount)
74 SElement* e = a.AddExisting(aElt);
77 if (CompareVoids(e->iObject,aElt))
79 TESTPRINT(_L("RSkipList : added element does not compare equal to returned value"));
82 if (e->iRefCount != aExpectedRefCount)
84 TESTPRINT(_L("RSkipList : unexpected reference count"));
89 TInt TestRemove(RSkipList& a, void* aElt)
91 if (CompareVoids(a.Remove(aElt), aElt))
93 TESTPRINT(_L("RSkipList : removed element does not compare equal to returned value"));
98 void Test(RSkipList& a)
102 void TestRSkipListL()
107 s.Open(CompareVoids, 16);
110 TInt* n500 = new(ELeave) TInt(-500);
111 TInt* n400 = new(ELeave) TInt(-400);
112 TInt* n300 = new(ELeave) TInt(-300);
113 TInt* n200 = new(ELeave) TInt(-200);
114 TInt* n190 = new(ELeave) TInt(-190);
115 TInt* n180 = new(ELeave) TInt(-180);
116 TInt* n170 = new(ELeave) TInt(-170);
117 TInt* n160 = new(ELeave) TInt(-160);
118 TInt* n150 = new(ELeave) TInt(-150);
119 TInt* n140 = new(ELeave) TInt(-140);
120 TInt* n130 = new(ELeave) TInt(-130);
121 TInt* n120 = new(ELeave) TInt(-120);
122 TInt* n110 = new(ELeave) TInt(-110);
123 TInt* n100 = new(ELeave) TInt(-100);
124 TInt* n87 = new(ELeave) TInt(-87);
125 TInt* n45 = new(ELeave) TInt(-45);
126 TInt* n3 = new(ELeave) TInt(-3);
127 TInt* p999 = new(ELeave) TInt(999);
128 TInt* p998 = new(ELeave) TInt(998);
129 TInt* p980 = new(ELeave) TInt(980);
130 TInt* p900 = new(ELeave) TInt(900);
131 TInt* p800 = new(ELeave) TInt(800);
132 TInt* p700 = new(ELeave) TInt(700);
133 TInt* p600 = new(ELeave) TInt(600);
134 TInt* p500 = new(ELeave) TInt(500);
135 TInt* p490 = new(ELeave) TInt(490);
136 TInt* p485 = new(ELeave) TInt(485);
137 TInt* p480 = new(ELeave) TInt(480);
138 TInt* p472 = new(ELeave) TInt(472);
139 TInt* p471 = new(ELeave) TInt(471);
140 TInt* p470 = new(ELeave) TInt(470);
141 TInt* p469 = new(ELeave) TInt(469);
142 TInt* p463 = new(ELeave) TInt(463);
143 TInt* p460 = new(ELeave) TInt(460);
144 TInt* p440 = new(ELeave) TInt(440);
145 TInt* p400 = new(ELeave) TInt(400);
146 TInt* p350 = new(ELeave) TInt(350);
147 TInt* p300 = new(ELeave) TInt(300);
148 TInt* p200 = new(ELeave) TInt(200);
149 TInt* p101 = new(ELeave) TInt(101);
150 TInt* p100 = new(ELeave) TInt(100);
151 TInt* p99 = new(ELeave) TInt(99);
152 TInt* p45 = new(ELeave) TInt(45);
153 TInt* p2 = new(ELeave) TInt(2);
155 // add everything a first time
156 TestAddL(s, p999, 1);
158 TestAddL(s, n190, 1);
160 TestAddL(s, n180, 1);
162 TestAddL(s, n160, 1);
164 TestAddL(s, p485, 1);
166 TestAddL(s, n140, 1);
168 TestAddL(s, n200, 1);
170 TestAddL(s, p470, 1);
172 TestAddL(s, n500, 1);
174 TestAddL(s, p980, 1);
176 TestAddL(s, n150, 1);
178 TestAddL(s, n170, 1);
180 TestAddL(s, p490, 1);
182 TestAddL(s, p500, 1);
184 TestAddL(s, p600, 1);
186 TestAddL(s, p440, 1);
188 TestAddL(s, p998, 1);
190 TestAddL(s, p700, 1);
192 TestAddL(s, p463, 1);
194 TestAddL(s, p460, 1);
196 TestAddL(s, p400, 1);
198 TestAddL(s, p800, 1);
200 TestAddL(s, n130, 1);
202 TestAddL(s, n120, 1);
204 TestAddL(s, p471, 1);
206 TestAddL(s, p472, 1);
208 TestAddL(s, p480, 1);
210 TestAddL(s, n400, 1);
212 TestAddL(s, n300, 1);
214 TestAddL(s, p300, 1);
216 TestAddL(s, n110, 1);
218 TestAddL(s, p350, 1);
220 TestAddL(s, p101, 1);
222 TestAddL(s, p200, 1);
224 TestAddL(s, p469, 1);
228 TestAddL(s, p100, 1);
232 TestAddL(s, p900, 1);
234 TestAddL(s, n100, 1);
245 // remove a few things
267 // add everything a second time
268 TestAddL(s, p440, 2);
269 TestAddL(s, p480, 2);
270 TestAddL(s, n180, 2);
272 TestAddL(s, n130, 2);
273 TestAddL(s, p470, 2);
274 TestAddL(s, n400, 1);
276 TestAddL(s, n190, 2);
277 TestAddL(s, p300, 1);
278 TestAddL(s, n120, 2);
279 TestAddL(s, n150, 2);
280 TestAddL(s, p463, 2);
281 TestAddL(s, n170, 2);
282 TestAddL(s, p469, 2);
283 TestAddL(s, p471, 2);
284 TestAddL(s, p490, 2);
285 TestAddL(s, p200, 1);
287 TestAddL(s, p980, 2);
288 TestAddL(s, p101, 2);
289 TestAddL(s, p600, 1);
290 TestAddL(s, n500, 1);
291 TestAddL(s, n110, 2);
292 TestAddL(s, p460, 2);
293 TestAddL(s, p485, 2);
294 TestAddL(s, n160, 2);
295 TestAddL(s, n140, 2);
296 TestAddL(s, p100, 1);
297 TestAddL(s, p472, 2);
298 TestAddL(s, p350, 2);
299 TestAddL(s, p400, 1);
300 TestAddL(s, p999, 2);
301 TestAddL(s, n200, 1);
302 TestAddL(s, p500, 1);
303 TestAddL(s, p998, 2);
305 TestAddL(s, n100, 1);
307 TestAddL(s, p800, 1);
308 TestAddL(s, p900, 1);
309 TestAddL(s, p700, 1);
310 TestAddL(s, n300, 1);
364 TestAddL(s, p999, 1);
365 TestAddL(s, n200, 1);
367 TestAddL(s, n110, 1);
368 TestAddL(s, p460, 1);
369 TestAddL(s, p980, 1);
370 TestAddL(s, n500, 1);
371 TestAddL(s, p700, 1);
372 TestAddL(s, n300, 1);
374 TestAddL(s, p463, 1);
375 TestAddL(s, p472, 1);
376 TestAddL(s, n100, 1);
379 TestAddL(s, p469, 1);
380 TestAddL(s, n120, 1);
381 TestAddL(s, p470, 1);
382 TestAddL(s, p400, 1);
383 TestAddL(s, n190, 1);
384 TestAddL(s, p998, 1);
385 TestAddL(s, p490, 1);
386 TestAddL(s, n180, 1);
387 TestAddL(s, p600, 1);
388 TestAddL(s, n160, 1);
389 TestAddL(s, p480, 1);
390 TestAddL(s, p350, 1);
391 TestAddL(s, p200, 1);
392 TestAddL(s, n130, 1);
393 TestAddL(s, p485, 1);
394 TestAddL(s, p500, 1);
395 TestAddL(s, p471, 1);
396 TestAddL(s, p440, 1);
397 TestAddL(s, p800, 1);
399 TestAddL(s, p101, 1);
400 TestAddL(s, n140, 1);
401 TestAddL(s, p100, 1);
402 TestAddL(s, n400, 1);
403 TestAddL(s, n170, 1);
405 TestAddL(s, p300, 1);
406 TestAddL(s, n150, 1);
407 TestAddL(s, p900, 1);
510 ///////////////////////
512 // CRepositoryImpl //
514 ///////////////////////
517 TInt TestObject(SElement* e, TInt val)
521 TESTPRINT(_L("CRepositoryImpl : object undefined"));
524 if (*reinterpret_cast<TInt*>(e->iObject) != val)
526 TESTPRINT(_L("CRepositoryImpl : object has wrong value"));
531 void TestCRepositoryImplL()
535 CRepositoryImpl* rep = new(ELeave) CRepositoryImpl(CompareVoids, DeleteVoid,
536 CopyVoidL, sizeof(TInt));
538 TInt* n47 = new(ELeave) TInt(-47);
539 TInt* z = new(ELeave) TInt(0);
540 TInt* z2 = new(ELeave) TInt(0);
541 TInt* p8765 = new(ELeave) TInt(8765);
543 SElement* z0ui = rep->IncOrCopyL(z);
545 SElement* zui = rep->InsertOrIncL(z);
546 SElement* n47ui = rep->InsertOrIncL(n47);
547 SElement* p8765ui = rep->InsertOrIncL(p8765);
548 SElement* z2ui = rep->InsertOrIncL(z2);
550 z = new(ELeave) TInt(0);
551 SElement* z3ui = rep->IncOrCopyL(z);
554 // test equal objects are nullified
555 TESTPOINT(zui->iObject == z2ui->iObject);
556 TESTPOINT(zui->iObject == z0ui->iObject);
557 TESTPOINT(zui->iObject == z3ui->iObject);
559 rep->DeleteOrDec(z0ui);
560 rep->DeleteOrDec(z3ui);
563 TestObject(n47ui, -47);
564 TestObject(p8765ui, 8765);
566 rep->DeleteOrDec(n47ui);
569 TestObject(p8765ui, 8765);
571 z = reinterpret_cast<TInt*>(rep->DetatchOrCopyL(zui));
572 z2 = reinterpret_cast<TInt*>(rep->DetatchOrCopyL(z2ui));
573 rep->DeleteOrDec(p8765ui);
575 // test that copyable objects are not aliased
577 // test that a valid copy is returned
590 ///////////////////////////////////
592 // CUniqueInstanceRepository<> //
594 // RUniqueInstance<> //
596 ///////////////////////////////////
599 TInt TestNull(const RUniqueInstance<TInt>& a)
603 TESTPRINT(_L("RUniqueInstance : null object has value"));
608 TInt TestValue(const RUniqueInstance<TInt>& a, TInt val)
612 TESTPRINT(_L("RUniqueInstance : unexpected null object"));
615 if (*a.Peek() != val)
617 TESTPRINT(_L("RUniqueInstance : object has wrong value"));
622 TInt TestRaw(TInt* ob, TInt val)
626 TESTPRINT(_L("RUniqueInstance : object unexpectedly does not own"));
631 TESTPRINT(_L("RUniqueInstance : object owns incorrect value"));
636 void TestRUniqueInstanceL()
640 CUniqueInstanceRepository<TInt>* intRep =
641 CUniqueInstanceRepository<TInt>::NewL(CompareTInts, DeleteTInt, CopyTIntL, 2);
643 RUniqueInstance<TInt> a(*intRep);
644 RUniqueInstance<TInt> b(*intRep);
645 RUniqueInstance<TInt> c(*intRep);
647 a.TakeL(new(ELeave) TInt(45));
648 b.TakeL(new(ELeave) TInt(-6));
649 c.TakeL(new(ELeave) TInt(45));
651 // test that equal elements are unified
652 TESTPOINT(a.Peek() == c.Peek());
683 c.TakeL(new(ELeave) TInt(87));
698 CUniqueInstanceRepository<TInt>* intRep2 =
699 CUniqueInstanceRepository<TInt>::NewL(CompareTInts, DeleteTInt, CopyTIntL, 4);
701 RUniqueInstance<TInt> d(*intRep2);
702 RUniqueInstance<TInt> e(*intRep2);
703 RUniqueInstance<TInt> f(*intRep2);
705 a.TakeL(new(ELeave) TInt(123));
706 b.TakeL(new(ELeave) TInt(-445));
707 c.TakeL(new(ELeave) TInt(123));
708 d.TakeL(new(ELeave) TInt(-7));
709 e.TakeL(new(ELeave) TInt(18));
729 e.TakeL(new(ELeave) TInt(18));
731 // test that equal objects are unified
732 TESTPOINT(a.Peek() == e.Peek());
734 d.TakeL(new(ELeave) TInt(-445));
736 // test that equal objects are unified
737 TESTPOINT(b.Peek() == d.Peek());
739 a.TakeL(new(ELeave) TInt(-445));
741 // test that objects from different repositories are not unified
742 TESTPOINT(a.Peek() != b.Peek());
746 // test that destroyed object peeks null
747 TESTPOINT(a.Peek() == 0);
769 TVerdict CTUniqueStep::doTestStepL()
771 SetTestStepResult(EPass);
773 TESTPRINT(_L("TUnique - Unique instance repository tests"));
774 TESTPRINT(_L(" @SYMTestCaseID:SYSLIB-FORM-LEGACY-UNIQUE-0001 Unique Instance Tests: "));
777 TestCRepositoryImplL();
778 TestRUniqueInstanceL();
780 return TestStepResult();