|
1 /* |
|
2 * Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
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". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * TUnique.cpp test file for UniqueInstance classes |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include "UniqueInstanceImpl.h" |
|
21 #include <e32test.h> |
|
22 |
|
23 using namespace UniqueInstance; |
|
24 |
|
25 #define UNUSED_VAR(a) a = a |
|
26 |
|
27 // we will manipulate TInts for simplicity |
|
28 // some functions that will help us |
|
29 namespace |
|
30 { |
|
31 TInt CompareVoids(void* pl, void* pr) |
|
32 { |
|
33 TInt l = *reinterpret_cast<TInt*>(pl); |
|
34 TInt r = *reinterpret_cast<TInt*>(pr); |
|
35 return r - l; |
|
36 } |
|
37 TInt CompareTInts(const TInt* l, const TInt* r) { return *r - *l; } |
|
38 void* CopyVoidL(void* a, TInt) |
|
39 { |
|
40 return new(ELeave) TInt(*reinterpret_cast<TInt*>(a)); |
|
41 } |
|
42 TInt* CopyTIntL(const TInt* a, TInt) { return new(ELeave) TInt(*a); } |
|
43 void DeleteVoid(void* a) |
|
44 { |
|
45 delete reinterpret_cast<TInt*>(a); |
|
46 } |
|
47 void DeleteTInt(TInt* a) { delete a; } |
|
48 |
|
49 CTrapCleanup* TrapCleanup; |
|
50 RTest test(_L("TUnique - Unique instance repository tests")); |
|
51 } |
|
52 |
|
53 ///////////////// |
|
54 // // |
|
55 // RSkipList // |
|
56 // // |
|
57 ///////////////// |
|
58 |
|
59 namespace |
|
60 { |
|
61 TInt TestEmpty(RSkipList& a) |
|
62 { |
|
63 if (!a.IsEmpty()) |
|
64 { |
|
65 test.Printf(_L("RSkipList : unexpectedly has elements")); |
|
66 return 1; |
|
67 } |
|
68 return 0; |
|
69 } |
|
70 TInt TestAddL(RSkipList& a, void* aElt, TInt aExpectedRefCount) |
|
71 { |
|
72 SElement* e = a.AddExisting(aElt); |
|
73 if (!e) |
|
74 e = a.AddNewL(aElt); |
|
75 if (CompareVoids(e->iObject,aElt)) |
|
76 { |
|
77 test.Printf(_L("RSkipList : added element does not compare equal to returned value")); |
|
78 return 1; |
|
79 } |
|
80 if (e->iRefCount != aExpectedRefCount) |
|
81 { |
|
82 test.Printf(_L("RSkipList : unexpected reference count")); |
|
83 return 1; |
|
84 } |
|
85 return 0; |
|
86 } |
|
87 TInt TestRemove(RSkipList& a, void* aElt) |
|
88 { |
|
89 if (CompareVoids(a.Remove(aElt), aElt)) |
|
90 { |
|
91 test.Printf(_L("RSkipList : removed element does not compare equal to returned value")); |
|
92 return 1; |
|
93 } |
|
94 return 0; |
|
95 } |
|
96 void Test(RSkipList& a) |
|
97 { |
|
98 a.Test(); |
|
99 } |
|
100 void TestRSkipListL() |
|
101 { |
|
102 __UHEAP_MARK; |
|
103 |
|
104 RSkipList s; |
|
105 s.Open(CompareVoids, 16); |
|
106 Test(s); |
|
107 |
|
108 TInt* n500 = new(ELeave) TInt(-500); |
|
109 TInt* n400 = new(ELeave) TInt(-400); |
|
110 TInt* n300 = new(ELeave) TInt(-300); |
|
111 TInt* n200 = new(ELeave) TInt(-200); |
|
112 TInt* n190 = new(ELeave) TInt(-190); |
|
113 TInt* n180 = new(ELeave) TInt(-180); |
|
114 TInt* n170 = new(ELeave) TInt(-170); |
|
115 TInt* n160 = new(ELeave) TInt(-160); |
|
116 TInt* n150 = new(ELeave) TInt(-150); |
|
117 TInt* n140 = new(ELeave) TInt(-140); |
|
118 TInt* n130 = new(ELeave) TInt(-130); |
|
119 TInt* n120 = new(ELeave) TInt(-120); |
|
120 TInt* n110 = new(ELeave) TInt(-110); |
|
121 TInt* n100 = new(ELeave) TInt(-100); |
|
122 TInt* n87 = new(ELeave) TInt(-87); |
|
123 TInt* n45 = new(ELeave) TInt(-45); |
|
124 TInt* n3 = new(ELeave) TInt(-3); |
|
125 TInt* p999 = new(ELeave) TInt(999); |
|
126 TInt* p998 = new(ELeave) TInt(998); |
|
127 TInt* p980 = new(ELeave) TInt(980); |
|
128 TInt* p900 = new(ELeave) TInt(900); |
|
129 TInt* p800 = new(ELeave) TInt(800); |
|
130 TInt* p700 = new(ELeave) TInt(700); |
|
131 TInt* p600 = new(ELeave) TInt(600); |
|
132 TInt* p500 = new(ELeave) TInt(500); |
|
133 TInt* p490 = new(ELeave) TInt(490); |
|
134 TInt* p485 = new(ELeave) TInt(485); |
|
135 TInt* p480 = new(ELeave) TInt(480); |
|
136 TInt* p472 = new(ELeave) TInt(472); |
|
137 TInt* p471 = new(ELeave) TInt(471); |
|
138 TInt* p470 = new(ELeave) TInt(470); |
|
139 TInt* p469 = new(ELeave) TInt(469); |
|
140 TInt* p463 = new(ELeave) TInt(463); |
|
141 TInt* p460 = new(ELeave) TInt(460); |
|
142 TInt* p440 = new(ELeave) TInt(440); |
|
143 TInt* p400 = new(ELeave) TInt(400); |
|
144 TInt* p350 = new(ELeave) TInt(350); |
|
145 TInt* p300 = new(ELeave) TInt(300); |
|
146 TInt* p200 = new(ELeave) TInt(200); |
|
147 TInt* p101 = new(ELeave) TInt(101); |
|
148 TInt* p100 = new(ELeave) TInt(100); |
|
149 TInt* p99 = new(ELeave) TInt(99); |
|
150 TInt* p45 = new(ELeave) TInt(45); |
|
151 TInt* p2 = new(ELeave) TInt(2); |
|
152 |
|
153 // add everything a first time |
|
154 TestAddL(s, p999, 1); |
|
155 Test(s); |
|
156 TestAddL(s, n190, 1); |
|
157 Test(s); |
|
158 TestAddL(s, n180, 1); |
|
159 Test(s); |
|
160 TestAddL(s, n160, 1); |
|
161 Test(s); |
|
162 TestAddL(s, p485, 1); |
|
163 Test(s); |
|
164 TestAddL(s, n140, 1); |
|
165 Test(s); |
|
166 TestAddL(s, n200, 1); |
|
167 Test(s); |
|
168 TestAddL(s, p470, 1); |
|
169 Test(s); |
|
170 TestAddL(s, n500, 1); |
|
171 Test(s); |
|
172 TestAddL(s, p980, 1); |
|
173 Test(s); |
|
174 TestAddL(s, n150, 1); |
|
175 Test(s); |
|
176 TestAddL(s, n170, 1); |
|
177 Test(s); |
|
178 TestAddL(s, p490, 1); |
|
179 Test(s); |
|
180 TestAddL(s, p500, 1); |
|
181 Test(s); |
|
182 TestAddL(s, p600, 1); |
|
183 Test(s); |
|
184 TestAddL(s, p440, 1); |
|
185 Test(s); |
|
186 TestAddL(s, p998, 1); |
|
187 Test(s); |
|
188 TestAddL(s, p700, 1); |
|
189 Test(s); |
|
190 TestAddL(s, p463, 1); |
|
191 Test(s); |
|
192 TestAddL(s, p460, 1); |
|
193 Test(s); |
|
194 TestAddL(s, p400, 1); |
|
195 Test(s); |
|
196 TestAddL(s, p800, 1); |
|
197 Test(s); |
|
198 TestAddL(s, n130, 1); |
|
199 Test(s); |
|
200 TestAddL(s, n120, 1); |
|
201 Test(s); |
|
202 TestAddL(s, p471, 1); |
|
203 Test(s); |
|
204 TestAddL(s, p472, 1); |
|
205 Test(s); |
|
206 TestAddL(s, p480, 1); |
|
207 Test(s); |
|
208 TestAddL(s, n400, 1); |
|
209 Test(s); |
|
210 TestAddL(s, n300, 1); |
|
211 Test(s); |
|
212 TestAddL(s, p300, 1); |
|
213 Test(s); |
|
214 TestAddL(s, n110, 1); |
|
215 Test(s); |
|
216 TestAddL(s, p350, 1); |
|
217 Test(s); |
|
218 TestAddL(s, p101, 1); |
|
219 Test(s); |
|
220 TestAddL(s, p200, 1); |
|
221 Test(s); |
|
222 TestAddL(s, p469, 1); |
|
223 Test(s); |
|
224 TestAddL(s, p99, 1); |
|
225 Test(s); |
|
226 TestAddL(s, p100, 1); |
|
227 Test(s); |
|
228 TestAddL(s, p45, 1); |
|
229 Test(s); |
|
230 TestAddL(s, p900, 1); |
|
231 Test(s); |
|
232 TestAddL(s, n100, 1); |
|
233 Test(s); |
|
234 TestAddL(s, n87, 1); |
|
235 Test(s); |
|
236 TestAddL(s, n45, 1); |
|
237 Test(s); |
|
238 TestAddL(s, n3, 1); |
|
239 Test(s); |
|
240 TestAddL(s, p2, 1); |
|
241 Test(s); |
|
242 |
|
243 // remove a few things |
|
244 TestRemove(s, p500); |
|
245 Test(s); |
|
246 TestRemove(s, p900); |
|
247 Test(s); |
|
248 TestRemove(s, n100); |
|
249 Test(s); |
|
250 TestRemove(s, n200); |
|
251 Test(s); |
|
252 TestRemove(s, p800); |
|
253 Test(s); |
|
254 TestRemove(s, p100); |
|
255 Test(s); |
|
256 TestRemove(s, n400); |
|
257 TestRemove(s, p700); |
|
258 TestRemove(s, p200); |
|
259 TestRemove(s, p600); |
|
260 TestRemove(s, n300); |
|
261 TestRemove(s, p300); |
|
262 TestRemove(s, p400); |
|
263 TestRemove(s, n500); |
|
264 |
|
265 // add everything a second time |
|
266 TestAddL(s, p440, 2); |
|
267 TestAddL(s, p480, 2); |
|
268 TestAddL(s, n180, 2); |
|
269 TestAddL(s, n3, 2); |
|
270 TestAddL(s, n130, 2); |
|
271 TestAddL(s, p470, 2); |
|
272 TestAddL(s, n400, 1); |
|
273 TestAddL(s, n45, 2); |
|
274 TestAddL(s, n190, 2); |
|
275 TestAddL(s, p300, 1); |
|
276 TestAddL(s, n120, 2); |
|
277 TestAddL(s, n150, 2); |
|
278 TestAddL(s, p463, 2); |
|
279 TestAddL(s, n170, 2); |
|
280 TestAddL(s, p469, 2); |
|
281 TestAddL(s, p471, 2); |
|
282 TestAddL(s, p490, 2); |
|
283 TestAddL(s, p200, 1); |
|
284 TestAddL(s, p99, 2); |
|
285 TestAddL(s, p980, 2); |
|
286 TestAddL(s, p101, 2); |
|
287 TestAddL(s, p600, 1); |
|
288 TestAddL(s, n500, 1); |
|
289 TestAddL(s, n110, 2); |
|
290 TestAddL(s, p460, 2); |
|
291 TestAddL(s, p485, 2); |
|
292 TestAddL(s, n160, 2); |
|
293 TestAddL(s, n140, 2); |
|
294 TestAddL(s, p100, 1); |
|
295 TestAddL(s, p472, 2); |
|
296 TestAddL(s, p350, 2); |
|
297 TestAddL(s, p400, 1); |
|
298 TestAddL(s, p999, 2); |
|
299 TestAddL(s, n200, 1); |
|
300 TestAddL(s, p500, 1); |
|
301 TestAddL(s, p998, 2); |
|
302 TestAddL(s, n87, 2); |
|
303 TestAddL(s, n100, 1); |
|
304 TestAddL(s, p45, 2); |
|
305 TestAddL(s, p800, 1); |
|
306 TestAddL(s, p900, 1); |
|
307 TestAddL(s, p700, 1); |
|
308 TestAddL(s, n300, 1); |
|
309 TestAddL(s, p2, 2); |
|
310 |
|
311 Test(s); |
|
312 |
|
313 // remove everything |
|
314 TestRemove(s, p500); |
|
315 TestRemove(s, p900); |
|
316 TestRemove(s, p980); |
|
317 TestRemove(s, p471); |
|
318 TestRemove(s, p472); |
|
319 TestRemove(s, p480); |
|
320 TestRemove(s, p45); |
|
321 TestRemove(s, n160); |
|
322 TestRemove(s, n100); |
|
323 TestRemove(s, n87); |
|
324 TestRemove(s, p469); |
|
325 TestRemove(s, p490); |
|
326 TestRemove(s, n170); |
|
327 TestRemove(s, n200); |
|
328 TestRemove(s, p460); |
|
329 TestRemove(s, p800); |
|
330 TestRemove(s, n120); |
|
331 TestRemove(s, p485); |
|
332 TestRemove(s, p100); |
|
333 TestRemove(s, p99); |
|
334 TestRemove(s, p101); |
|
335 TestRemove(s, n110); |
|
336 TestRemove(s, n400); |
|
337 TestRemove(s, p463); |
|
338 TestRemove(s, n45); |
|
339 TestRemove(s, n3); |
|
340 TestRemove(s, n190); |
|
341 TestRemove(s, p700); |
|
342 TestRemove(s, p999); |
|
343 TestRemove(s, p200); |
|
344 TestRemove(s, p2); |
|
345 TestRemove(s, n180); |
|
346 TestRemove(s, n150); |
|
347 TestRemove(s, p600); |
|
348 TestRemove(s, n300); |
|
349 TestRemove(s, p300); |
|
350 TestRemove(s, p350); |
|
351 TestRemove(s, p400); |
|
352 TestRemove(s, p440); |
|
353 TestRemove(s, p470); |
|
354 TestRemove(s, p998); |
|
355 TestRemove(s, n140); |
|
356 TestRemove(s, n130); |
|
357 TestRemove(s, n500); |
|
358 |
|
359 Test(s); |
|
360 |
|
361 // add everything |
|
362 TestAddL(s, p999, 1); |
|
363 TestAddL(s, n200, 1); |
|
364 TestAddL(s, n3, 1); |
|
365 TestAddL(s, n110, 1); |
|
366 TestAddL(s, p460, 1); |
|
367 TestAddL(s, p980, 1); |
|
368 TestAddL(s, n500, 1); |
|
369 TestAddL(s, p700, 1); |
|
370 TestAddL(s, n300, 1); |
|
371 TestAddL(s, p2, 1); |
|
372 TestAddL(s, p463, 1); |
|
373 TestAddL(s, p472, 1); |
|
374 TestAddL(s, n100, 1); |
|
375 TestAddL(s, p45, 1); |
|
376 TestAddL(s, n45, 1); |
|
377 TestAddL(s, p469, 1); |
|
378 TestAddL(s, n120, 1); |
|
379 TestAddL(s, p470, 1); |
|
380 TestAddL(s, p400, 1); |
|
381 TestAddL(s, n190, 1); |
|
382 TestAddL(s, p998, 1); |
|
383 TestAddL(s, p490, 1); |
|
384 TestAddL(s, n180, 1); |
|
385 TestAddL(s, p600, 1); |
|
386 TestAddL(s, n160, 1); |
|
387 TestAddL(s, p480, 1); |
|
388 TestAddL(s, p350, 1); |
|
389 TestAddL(s, p200, 1); |
|
390 TestAddL(s, n130, 1); |
|
391 TestAddL(s, p485, 1); |
|
392 TestAddL(s, p500, 1); |
|
393 TestAddL(s, p471, 1); |
|
394 TestAddL(s, p440, 1); |
|
395 TestAddL(s, p800, 1); |
|
396 TestAddL(s, n87, 1); |
|
397 TestAddL(s, p101, 1); |
|
398 TestAddL(s, n140, 1); |
|
399 TestAddL(s, p100, 1); |
|
400 TestAddL(s, n400, 1); |
|
401 TestAddL(s, n170, 1); |
|
402 TestAddL(s, p99, 1); |
|
403 TestAddL(s, p300, 1); |
|
404 TestAddL(s, n150, 1); |
|
405 TestAddL(s, p900, 1); |
|
406 |
|
407 Test(s); |
|
408 |
|
409 // remove everything |
|
410 TestRemove(s, n120); |
|
411 TestRemove(s, p485); |
|
412 TestRemove(s, p100); |
|
413 TestRemove(s, n500); |
|
414 TestRemove(s, n400); |
|
415 TestRemove(s, p463); |
|
416 TestRemove(s, n150); |
|
417 TestRemove(s, p440); |
|
418 TestRemove(s, n170); |
|
419 TestRemove(s, n3); |
|
420 TestRemove(s, p500); |
|
421 TestRemove(s, p900); |
|
422 TestRemove(s, p980); |
|
423 TestRemove(s, p471); |
|
424 TestRemove(s, p600); |
|
425 TestRemove(s, n300); |
|
426 TestRemove(s, p300); |
|
427 TestRemove(s, p350); |
|
428 TestRemove(s, p700); |
|
429 TestRemove(s, n45); |
|
430 TestRemove(s, n200); |
|
431 TestRemove(s, p2); |
|
432 TestRemove(s, p400); |
|
433 TestRemove(s, n110); |
|
434 TestRemove(s, n190); |
|
435 TestRemove(s, p999); |
|
436 TestRemove(s, p200); |
|
437 TestRemove(s, p99); |
|
438 TestRemove(s, p470); |
|
439 TestRemove(s, p800); |
|
440 TestRemove(s, n180); |
|
441 TestRemove(s, p460); |
|
442 TestRemove(s, p998); |
|
443 TestRemove(s, n140); |
|
444 TestRemove(s, p472); |
|
445 TestRemove(s, p101); |
|
446 TestRemove(s, p480); |
|
447 TestRemove(s, n130); |
|
448 TestRemove(s, p45); |
|
449 TestRemove(s, n160); |
|
450 TestRemove(s, n100); |
|
451 TestRemove(s, n87); |
|
452 TestRemove(s, p469); |
|
453 TestRemove(s, p490); |
|
454 |
|
455 TestEmpty(s); |
|
456 |
|
457 delete n500; |
|
458 delete n400; |
|
459 delete n300; |
|
460 delete n200; |
|
461 delete n190; |
|
462 delete n180; |
|
463 delete n170; |
|
464 delete n160; |
|
465 delete n150; |
|
466 delete n140; |
|
467 delete n130; |
|
468 delete n120; |
|
469 delete n110; |
|
470 delete n100; |
|
471 delete n87; |
|
472 delete n45; |
|
473 delete n3; |
|
474 delete p2; |
|
475 delete p45; |
|
476 delete p99; |
|
477 delete p100; |
|
478 delete p101; |
|
479 delete p200; |
|
480 delete p300; |
|
481 delete p350; |
|
482 delete p400; |
|
483 delete p440; |
|
484 delete p460; |
|
485 delete p463; |
|
486 delete p469; |
|
487 delete p470; |
|
488 delete p471; |
|
489 delete p472; |
|
490 delete p480; |
|
491 delete p485; |
|
492 delete p490; |
|
493 delete p500; |
|
494 delete p600; |
|
495 delete p700; |
|
496 delete p800; |
|
497 delete p900; |
|
498 delete p980; |
|
499 delete p998; |
|
500 delete p999; |
|
501 |
|
502 s.Close(); |
|
503 |
|
504 __UHEAP_MARKENDC(0); |
|
505 } |
|
506 } |
|
507 |
|
508 /////////////////////// |
|
509 // // |
|
510 // CRepositoryImpl // |
|
511 // // |
|
512 /////////////////////// |
|
513 namespace |
|
514 { |
|
515 TInt TestObject(SElement* e, TInt val) |
|
516 { |
|
517 if (!e->iObject) |
|
518 { |
|
519 test.Printf(_L("CRepositoryImpl : object undefined")); |
|
520 return 1; |
|
521 } |
|
522 if (*reinterpret_cast<TInt*>(e->iObject) != val) |
|
523 { |
|
524 test.Printf(_L("CRepositoryImpl : object has wrong value")); |
|
525 return 1; |
|
526 } |
|
527 return 0; |
|
528 } |
|
529 void TestCRepositoryImplL() |
|
530 { |
|
531 __UHEAP_MARK; |
|
532 |
|
533 CRepositoryImpl* rep = new(ELeave) CRepositoryImpl(CompareVoids, DeleteVoid, |
|
534 CopyVoidL, sizeof(TInt)); |
|
535 rep->ConstructL(1); |
|
536 TInt* n47 = new(ELeave) TInt(-47); |
|
537 TInt* z = new(ELeave) TInt(0); |
|
538 TInt* z2 = new(ELeave) TInt(0); |
|
539 TInt* p8765 = new(ELeave) TInt(8765); |
|
540 |
|
541 SElement* z0ui = rep->IncOrCopyL(z); |
|
542 |
|
543 SElement* zui = rep->InsertOrIncL(z); |
|
544 SElement* n47ui = rep->InsertOrIncL(n47); |
|
545 SElement* p8765ui = rep->InsertOrIncL(p8765); |
|
546 SElement* z2ui = rep->InsertOrIncL(z2); |
|
547 |
|
548 z = new(ELeave) TInt(0); |
|
549 SElement* z3ui = rep->IncOrCopyL(z); |
|
550 delete z; |
|
551 |
|
552 // test equal objects are nullified |
|
553 test(zui->iObject == z2ui->iObject); |
|
554 test(zui->iObject == z0ui->iObject); |
|
555 test(zui->iObject == z3ui->iObject); |
|
556 |
|
557 rep->DeleteOrDec(z0ui); |
|
558 rep->DeleteOrDec(z3ui); |
|
559 |
|
560 TestObject(zui, 0); |
|
561 TestObject(n47ui, -47); |
|
562 TestObject(p8765ui, 8765); |
|
563 |
|
564 rep->DeleteOrDec(n47ui); |
|
565 |
|
566 TestObject(zui, 0); |
|
567 TestObject(p8765ui, 8765); |
|
568 |
|
569 z = reinterpret_cast<TInt*>(rep->DetatchOrCopyL(zui)); |
|
570 z2 = reinterpret_cast<TInt*>(rep->DetatchOrCopyL(z2ui)); |
|
571 rep->DeleteOrDec(p8765ui); |
|
572 |
|
573 // test that copyable objects are not aliased |
|
574 test(z != z2); |
|
575 // test that a valid copy is returned |
|
576 test(*z == 0); |
|
577 test(*z2 == 0); |
|
578 |
|
579 delete z; |
|
580 delete z2; |
|
581 |
|
582 delete rep; |
|
583 |
|
584 __UHEAP_MARKENDC(0); |
|
585 } |
|
586 } |
|
587 |
|
588 /////////////////////////////////// |
|
589 // // |
|
590 // CUniqueInstanceRepository<> // |
|
591 // and // |
|
592 // RUniqueInstance<> // |
|
593 // // |
|
594 /////////////////////////////////// |
|
595 namespace |
|
596 { |
|
597 TInt TestNull(const RUniqueInstance<TInt>& a) |
|
598 { |
|
599 if (a.Peek()) |
|
600 { |
|
601 test.Printf(_L("RUniqueInstance : null object has value")); |
|
602 return 1; |
|
603 } |
|
604 return 0; |
|
605 } |
|
606 TInt TestValue(const RUniqueInstance<TInt>& a, TInt val) |
|
607 { |
|
608 if (!a.Peek()) |
|
609 { |
|
610 test.Printf(_L("RUniqueInstance : unexpected null object")); |
|
611 return 1; |
|
612 } |
|
613 if (*a.Peek() != val) |
|
614 { |
|
615 test.Printf(_L("RUniqueInstance : object has wrong value")); |
|
616 return 1; |
|
617 } |
|
618 return 0; |
|
619 } |
|
620 TInt TestRaw(TInt* ob, TInt val) |
|
621 { |
|
622 if (!ob) |
|
623 { |
|
624 test.Printf(_L("RUniqueInstance : object unexpectedly does not own")); |
|
625 return 1; |
|
626 } |
|
627 if (*ob != val) |
|
628 { |
|
629 test.Printf(_L("RUniqueInstance : object owns incorrect value")); |
|
630 return 1; |
|
631 } |
|
632 return 0; |
|
633 } |
|
634 void TestRUniqueInstanceL() |
|
635 { |
|
636 __UHEAP_MARK; |
|
637 |
|
638 CUniqueInstanceRepository<TInt>* intRep = |
|
639 CUniqueInstanceRepository<TInt>::NewL(CompareTInts, DeleteTInt, CopyTIntL, 2); |
|
640 |
|
641 RUniqueInstance<TInt> a(*intRep); |
|
642 RUniqueInstance<TInt> b(*intRep); |
|
643 RUniqueInstance<TInt> c(*intRep); |
|
644 |
|
645 a.TakeL(new(ELeave) TInt(45)); |
|
646 b.TakeL(new(ELeave) TInt(-6)); |
|
647 c.TakeL(new(ELeave) TInt(45)); |
|
648 |
|
649 // test that equal elements are unified |
|
650 test(a.Peek() == c.Peek()); |
|
651 |
|
652 TestValue(a, 45); |
|
653 TestValue(b, -6); |
|
654 TestValue(c, 45); |
|
655 |
|
656 a.CopyTo(c); |
|
657 |
|
658 TestValue(a, 45); |
|
659 TestValue(b, -6); |
|
660 TestValue(c, 45); |
|
661 |
|
662 b.CopyTo(b); |
|
663 b.MoveTo(b); |
|
664 |
|
665 TestValue(a, 45); |
|
666 TestValue(b, -6); |
|
667 TestValue(c, 45); |
|
668 |
|
669 b.MoveTo(c); |
|
670 |
|
671 TestValue(a, 45); |
|
672 TestNull(b); |
|
673 TestValue(c, -6); |
|
674 |
|
675 b.MoveTo(c); |
|
676 |
|
677 TestValue(a, 45); |
|
678 TestNull(b); |
|
679 TestNull(c); |
|
680 |
|
681 c.TakeL(new(ELeave) TInt(87)); |
|
682 c.CopyTo(b); |
|
683 TInt* r = c.DropL(); |
|
684 |
|
685 TestRaw(r, 87); |
|
686 |
|
687 delete r; |
|
688 r = b.DropL(); |
|
689 |
|
690 TestRaw(r, 87); |
|
691 |
|
692 delete r; |
|
693 |
|
694 a.Close(); |
|
695 |
|
696 CUniqueInstanceRepository<TInt>* intRep2 = |
|
697 CUniqueInstanceRepository<TInt>::NewL(CompareTInts, DeleteTInt, CopyTIntL, 4); |
|
698 |
|
699 RUniqueInstance<TInt> d(*intRep2); |
|
700 RUniqueInstance<TInt> e(*intRep2); |
|
701 RUniqueInstance<TInt> f(*intRep2); |
|
702 |
|
703 a.TakeL(new(ELeave) TInt(123)); |
|
704 b.TakeL(new(ELeave) TInt(-445)); |
|
705 c.TakeL(new(ELeave) TInt(123)); |
|
706 d.TakeL(new(ELeave) TInt(-7)); |
|
707 e.TakeL(new(ELeave) TInt(18)); |
|
708 { |
|
709 TInt n0 = -999; |
|
710 TInt n1 = 1; |
|
711 TInt n2 = 999; |
|
712 f.TakeCopyL(&n0); |
|
713 TestValue(f, -999); |
|
714 f.TakeCopyL(&n1); |
|
715 TestValue(f, 1); |
|
716 f.TakeCopyL(&n2); |
|
717 TestValue(f, 999); |
|
718 f.Close(); |
|
719 } |
|
720 |
|
721 a.MoveTo(d); |
|
722 TestNull(a); |
|
723 TestValue(d, 123); |
|
724 c.CopyTo(d); |
|
725 TestValue(d, 123); |
|
726 e.MoveTo(a); |
|
727 e.TakeL(new(ELeave) TInt(18)); |
|
728 |
|
729 // test that equal objects are unified |
|
730 test(a.Peek() == e.Peek()); |
|
731 |
|
732 d.TakeL(new(ELeave) TInt(-445)); |
|
733 |
|
734 // test that equal objects are unified |
|
735 test(b.Peek() == d.Peek()); |
|
736 |
|
737 a.TakeL(new(ELeave) TInt(-445)); |
|
738 |
|
739 // test that objects from different repositories are not unified |
|
740 test(a.Peek() != b.Peek()); |
|
741 |
|
742 a.Close(); |
|
743 |
|
744 // test that destroyed object peeks null |
|
745 test(a.Peek() == 0); |
|
746 |
|
747 b.MoveTo(c); |
|
748 b.Close(); |
|
749 c.Close(); |
|
750 d.Close(); |
|
751 e.Close(); |
|
752 |
|
753 delete intRep2; |
|
754 delete intRep; |
|
755 |
|
756 __UHEAP_MARKENDC(0); |
|
757 } |
|
758 } |
|
759 |
|
760 |
|
761 //////////// |
|
762 // // |
|
763 // Main // |
|
764 // // |
|
765 //////////// |
|
766 |
|
767 void RunTests() |
|
768 { |
|
769 __UHEAP_MARK; |
|
770 test.Title(); |
|
771 test.Start(_L(" @SYMTestCaseID:SYSLIB-FORM-LEGACY-UNIQUE-0001 Unique Instance Tests: ")); |
|
772 |
|
773 TestRSkipListL(); |
|
774 TestCRepositoryImplL(); |
|
775 TestRUniqueInstanceL(); |
|
776 |
|
777 test.End(); |
|
778 test.Close(); |
|
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 } |