|
1 // Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of the License "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // e32test\buffer\int_arr.cpp |
|
15 // |
|
16 // |
|
17 |
|
18 #include <e32test.h> |
|
19 #include <e32math.h> |
|
20 |
|
21 #define NUM_TESTS 500 |
|
22 |
|
23 GLREF_D RTest test; |
|
24 GLREF_C TInt Random(); |
|
25 |
|
26 struct SInt |
|
27 { |
|
28 SInt(TInt a) {i=a;} |
|
29 operator TInt() {return i;} |
|
30 |
|
31 TInt i; |
|
32 }; |
|
33 |
|
34 LOCAL_C TInt IntAppendAndAccessTest(TInt aCount, TInt aNumTests, TInt aRange) |
|
35 { |
|
36 TInt n; |
|
37 for (n=0; n<aNumTests; n++) |
|
38 { |
|
39 RArray<TInt> a; |
|
40 TInt *pA=(TInt*)User::Alloc(aCount*sizeof(TInt)); |
|
41 if (!pA) |
|
42 { |
|
43 a.Close(); |
|
44 return -65535; |
|
45 } |
|
46 TInt i; |
|
47 for (i=0; i<aCount; i++) |
|
48 { |
|
49 TInt x=Random()&aRange; |
|
50 a.Append(x); |
|
51 pA[i]=x; |
|
52 } |
|
53 if (a.Count()!=aCount) |
|
54 { |
|
55 a.Close(); |
|
56 return -1; |
|
57 } |
|
58 for (i=0; i<aCount; i++) |
|
59 { |
|
60 if (a[i]!=pA[i]) |
|
61 { |
|
62 a.Close(); |
|
63 return -2; |
|
64 } |
|
65 TInt x=Random()&aRange; |
|
66 a[i]=x; |
|
67 pA[i]=x; |
|
68 } |
|
69 if (a.Count()!=aCount) |
|
70 { |
|
71 a.Close(); |
|
72 return -3; |
|
73 } |
|
74 for (i=0; i<aCount; i++) |
|
75 { |
|
76 if (a[i]!=pA[i]) |
|
77 { |
|
78 a.Close(); |
|
79 return -4; |
|
80 } |
|
81 } |
|
82 delete pA; |
|
83 a.Close(); |
|
84 } |
|
85 return KErrNone; |
|
86 } |
|
87 |
|
88 LOCAL_C TInt IntRemoveTest() |
|
89 { |
|
90 TInt m=32; |
|
91 TInt n=m*m+1; |
|
92 RArray<TInt> a; |
|
93 TInt i; |
|
94 for (i=0; i<n; i++) |
|
95 { |
|
96 a.Append(i); |
|
97 } |
|
98 TInt p=2; |
|
99 for (i=2; i<=m; i++) |
|
100 { |
|
101 TInt j; |
|
102 for (j=0; j<(2*i-2); j++) |
|
103 { |
|
104 a.Remove(p); |
|
105 } |
|
106 p++; |
|
107 } |
|
108 if (a.Count()!=m+1) |
|
109 { |
|
110 a.Close(); |
|
111 return -1; |
|
112 } |
|
113 for (i=0; i<m; i++) |
|
114 { |
|
115 if (a[i]!=i*i) |
|
116 { |
|
117 a.Close(); |
|
118 return -2; |
|
119 } |
|
120 } |
|
121 a.Close(); |
|
122 return KErrNone; |
|
123 } |
|
124 |
|
125 LOCAL_C TInt IntFindTest(TInt aCount, TInt aNumTests, TInt aRange) |
|
126 { |
|
127 TInt n; |
|
128 for (n=0; n<aNumTests; n++) |
|
129 { |
|
130 RArray<TInt> a; |
|
131 TInt *pA=(TInt*)User::Alloc(aCount*sizeof(TInt)); |
|
132 if (!pA) |
|
133 { |
|
134 a.Close(); |
|
135 return -65535; |
|
136 } |
|
137 TInt i; |
|
138 for (i=0; i<aCount; i++) |
|
139 { |
|
140 TInt x=Random()&aRange; |
|
141 a.Append(x); |
|
142 pA[i]=x; |
|
143 } |
|
144 if (a.Count()!=aCount) |
|
145 { |
|
146 a.Close(); |
|
147 return -1; |
|
148 } |
|
149 for (i=0; i<aCount; i++) |
|
150 { |
|
151 TInt r=a.Find(pA[i]); |
|
152 if (r<0 || pA[r]!=pA[i] || r>i) |
|
153 { |
|
154 a.Close(); |
|
155 return -2; |
|
156 } |
|
157 TInt x=Random()&aRange; |
|
158 r=a.Find(x); |
|
159 if (r<0) |
|
160 { |
|
161 TInt j; |
|
162 for (j=0; j<aCount; j++) |
|
163 { |
|
164 if (pA[j]==x) |
|
165 { |
|
166 a.Close(); |
|
167 return -3; |
|
168 } |
|
169 } |
|
170 } |
|
171 else if (pA[r]!=x) |
|
172 { |
|
173 a.Close(); |
|
174 return -4; |
|
175 } |
|
176 else |
|
177 { |
|
178 TInt j; |
|
179 for (j=0; j<r; j++) |
|
180 { |
|
181 if (pA[j]==x) |
|
182 { |
|
183 a.Close(); |
|
184 return -5; |
|
185 } |
|
186 } |
|
187 } |
|
188 } |
|
189 delete pA; |
|
190 a.Close(); |
|
191 } |
|
192 return KErrNone; |
|
193 } |
|
194 |
|
195 LOCAL_C TInt IntFindInOrderTest(TInt aCount, TInt aNumTests, TInt aRange) |
|
196 // require aRange*aCount<2^32 |
|
197 { |
|
198 TInt n; |
|
199 for (n=0; n<aNumTests; n++) |
|
200 { |
|
201 RArray<TInt> a; |
|
202 TInt *pA=(TInt*)User::Alloc(aCount*sizeof(TInt)); |
|
203 if (!pA) |
|
204 { |
|
205 a.Close(); |
|
206 return -65535; |
|
207 } |
|
208 TInt i=0; |
|
209 TInt y=-256; |
|
210 for(i=0; i<aCount; i++) |
|
211 { |
|
212 TInt x=Random()&aRange; // this is always >=0 |
|
213 a.Append(y); |
|
214 pA[i]=y; |
|
215 y+=x; |
|
216 } |
|
217 if (a.Count()!=aCount) |
|
218 { |
|
219 a.Close(); |
|
220 return -1; |
|
221 } |
|
222 for (i=0; i<aCount; i++) |
|
223 { |
|
224 TInt r=a.FindInOrder(pA[i]); |
|
225 if (r<0 || pA[r]!=pA[i]) |
|
226 { |
|
227 a.Close(); |
|
228 return -2; |
|
229 } |
|
230 TInt x=Random()&aRange; |
|
231 r=a.FindInOrder(x); |
|
232 if (r<0) |
|
233 { |
|
234 TInt j; |
|
235 for (j=0; j<aCount; j++) |
|
236 { |
|
237 if (pA[j]==x) |
|
238 { |
|
239 a.Close(); |
|
240 return -3; |
|
241 } |
|
242 } |
|
243 } |
|
244 else if (pA[r]!=x) |
|
245 { |
|
246 a.Close(); |
|
247 return -4; |
|
248 } |
|
249 } |
|
250 delete pA; |
|
251 a.Close(); |
|
252 } |
|
253 return KErrNone; |
|
254 } |
|
255 |
|
256 LOCAL_C TInt IntFindInOrderTest2() |
|
257 { |
|
258 TInt i; |
|
259 RArray<TInt> sq; |
|
260 for (i=0; i<1024; i++) |
|
261 { |
|
262 TInt j=i*i; |
|
263 sq.Append(j); |
|
264 } |
|
265 for (i=0; i<10000; i++) |
|
266 { |
|
267 TInt x=Random()&1023; |
|
268 TInt y=x*x; |
|
269 TInt r=sq.FindInOrder(y); |
|
270 if (r!=x) |
|
271 { |
|
272 sq.Close(); |
|
273 return -1; |
|
274 } |
|
275 } |
|
276 sq.Close(); |
|
277 return 0; |
|
278 } |
|
279 |
|
280 LOCAL_C TInt IntInsertInOrderTest(TInt aCount, TInt aNumTests, TInt aRange) |
|
281 { |
|
282 TInt n; |
|
283 for (n=0; n<aNumTests; n++) |
|
284 { |
|
285 RArray<TInt> a; |
|
286 RArray<TInt> b; |
|
287 RArray<TInt> c; |
|
288 TInt i; |
|
289 TInt cc=0; |
|
290 for (i=0; i<aCount; i++) |
|
291 { |
|
292 TInt x=Random()&aRange; |
|
293 a.Append(x); |
|
294 b.InsertInOrderAllowRepeats(x); |
|
295 TInt r=c.InsertInOrder(x); |
|
296 if (r==KErrNone) |
|
297 cc++; |
|
298 } |
|
299 if (a.Count()!=aCount) |
|
300 { |
|
301 a.Close(); |
|
302 b.Close(); |
|
303 c.Close(); |
|
304 return -1; |
|
305 } |
|
306 if (b.Count()!=aCount) |
|
307 { |
|
308 a.Close(); |
|
309 b.Close(); |
|
310 c.Close(); |
|
311 return -2; |
|
312 } |
|
313 for (i=0; i<aCount-1; i++) |
|
314 { |
|
315 if (b[i]>b[i+1]) |
|
316 { |
|
317 a.Close(); |
|
318 b.Close(); |
|
319 c.Close(); |
|
320 return -3; |
|
321 } |
|
322 } |
|
323 for (i=0; i<aCount; i++) |
|
324 { |
|
325 if (a.Find(b[i])<0) |
|
326 { |
|
327 a.Close(); |
|
328 b.Close(); |
|
329 c.Close(); |
|
330 return -4; |
|
331 } |
|
332 if (b.Find(a[i])<0) |
|
333 { |
|
334 a.Close(); |
|
335 b.Close(); |
|
336 c.Close(); |
|
337 return -5; |
|
338 } |
|
339 if (c.Find(a[i])<0) |
|
340 { |
|
341 a.Close(); |
|
342 b.Close(); |
|
343 c.Close(); |
|
344 return -6; |
|
345 } |
|
346 } |
|
347 if (c.Count()!=cc) |
|
348 { |
|
349 a.Close(); |
|
350 b.Close(); |
|
351 c.Close(); |
|
352 return -7; |
|
353 } |
|
354 for (i=0; i<c.Count()-1; i++) |
|
355 { |
|
356 if (c[i]>=c[i+1]) |
|
357 { |
|
358 a.Close(); |
|
359 b.Close(); |
|
360 c.Close(); |
|
361 return -8; |
|
362 } |
|
363 if (a.Find(c[i])<0) |
|
364 { |
|
365 a.Close(); |
|
366 b.Close(); |
|
367 c.Close(); |
|
368 return -9; |
|
369 } |
|
370 } |
|
371 a.Close(); |
|
372 b.Close(); |
|
373 c.Close(); |
|
374 } |
|
375 return KErrNone; |
|
376 } |
|
377 |
|
378 LOCAL_C TInt IntInsertInOrderTest2() |
|
379 { |
|
380 TInt i; |
|
381 RArray<TInt> sq; |
|
382 for (i=0; i<1024; i++) |
|
383 { |
|
384 TInt j=i*i; |
|
385 sq.InsertInOrder(j); |
|
386 sq.InsertInOrder(-j); |
|
387 } |
|
388 if (sq.Count()!=2047) |
|
389 { |
|
390 sq.Close(); |
|
391 return -1; |
|
392 } |
|
393 for (i=0; i<2047; i++) |
|
394 { |
|
395 TInt y=0; |
|
396 if (i<1023) |
|
397 y=-(1023-i)*(1023-i); |
|
398 else if (i==1023) |
|
399 y=0; |
|
400 else if (i>1023) |
|
401 y=(i-1023)*(i-1023); |
|
402 if (sq[i]!=y) |
|
403 { |
|
404 sq.Close(); |
|
405 return -2; |
|
406 } |
|
407 } |
|
408 sq.Close(); |
|
409 return 0; |
|
410 } |
|
411 |
|
412 LOCAL_C TInt IntSortTest(TInt aCount, TInt aNumTests, TInt aRange) |
|
413 { |
|
414 TInt n; |
|
415 for (n=0; n<aNumTests; n++) |
|
416 { |
|
417 RArray<TInt> a; |
|
418 RArray<TInt> b; |
|
419 TInt i; |
|
420 for (i=0; i<aCount; i++) |
|
421 { |
|
422 TInt x=Random()&aRange; |
|
423 a.Append(x); |
|
424 b.InsertInOrderAllowRepeats(x); |
|
425 } |
|
426 a.Sort(); |
|
427 if (a.Count()!=aCount) |
|
428 { |
|
429 a.Close(); |
|
430 b.Close(); |
|
431 return -1; |
|
432 } |
|
433 if (b.Count()!=aCount) |
|
434 { |
|
435 a.Close(); |
|
436 b.Close(); |
|
437 return -2; |
|
438 } |
|
439 for (i=0; i<aCount; i++) |
|
440 { |
|
441 if (a[i]!=b[i]) |
|
442 { |
|
443 a.Close(); |
|
444 b.Close(); |
|
445 return -3; |
|
446 } |
|
447 } |
|
448 a.Close(); |
|
449 b.Close(); |
|
450 } |
|
451 return KErrNone; |
|
452 } |
|
453 |
|
454 LOCAL_C TInt SIntAppendAndAccessTest(TInt aCount, TInt aNumTests, TInt aRange) |
|
455 { |
|
456 TInt n; |
|
457 for (n=0; n<aNumTests; n++) |
|
458 { |
|
459 RArray<SInt> a; |
|
460 TInt *pA=(TInt*)User::Alloc(aCount*sizeof(TInt)); |
|
461 if (!pA) |
|
462 { |
|
463 a.Close(); |
|
464 return -65535; |
|
465 } |
|
466 TInt i; |
|
467 for (i=0; i<aCount; i++) |
|
468 { |
|
469 TInt x=Random()&aRange; |
|
470 a.Append(x); |
|
471 pA[i]=x; |
|
472 } |
|
473 if (a.Count()!=aCount) |
|
474 { |
|
475 a.Close(); |
|
476 return -1; |
|
477 } |
|
478 for (i=0; i<aCount; i++) |
|
479 { |
|
480 if (a[i]!=pA[i]) |
|
481 { |
|
482 a.Close(); |
|
483 return -2; |
|
484 } |
|
485 TInt x=Random()&aRange; |
|
486 a[i]=x; |
|
487 pA[i]=x; |
|
488 } |
|
489 if (a.Count()!=aCount) |
|
490 { |
|
491 a.Close(); |
|
492 return -3; |
|
493 } |
|
494 for (i=0; i<aCount; i++) |
|
495 { |
|
496 if (a[i]!=pA[i]) |
|
497 { |
|
498 a.Close(); |
|
499 return -4; |
|
500 } |
|
501 } |
|
502 delete pA; |
|
503 a.Close(); |
|
504 } |
|
505 return KErrNone; |
|
506 } |
|
507 |
|
508 LOCAL_C TInt SIntRemoveTest() |
|
509 { |
|
510 TInt m=32; |
|
511 TInt n=m*m+1; |
|
512 RArray<SInt> a; |
|
513 TInt i; |
|
514 for (i=0; i<n; i++) |
|
515 { |
|
516 a.Append(i); |
|
517 } |
|
518 TInt p=2; |
|
519 for (i=2; i<=m; i++) |
|
520 { |
|
521 TInt j; |
|
522 for (j=0; j<(2*i-2); j++) |
|
523 { |
|
524 a.Remove(p); |
|
525 } |
|
526 p++; |
|
527 } |
|
528 if (a.Count()!=m+1) |
|
529 { |
|
530 a.Close(); |
|
531 return -1; |
|
532 } |
|
533 for (i=0; i<m; i++) |
|
534 { |
|
535 if (a[i]!=i*i) |
|
536 { |
|
537 a.Close(); |
|
538 return -2; |
|
539 } |
|
540 } |
|
541 a.Close(); |
|
542 return KErrNone; |
|
543 } |
|
544 |
|
545 LOCAL_C TInt SIntFindTest(TInt aCount, TInt aNumTests, TInt aRange) |
|
546 { |
|
547 TInt n; |
|
548 for (n=0; n<aNumTests; n++) |
|
549 { |
|
550 RArray<SInt> a; |
|
551 TInt *pA=(TInt*)User::Alloc(aCount*sizeof(TInt)); |
|
552 if (!pA) |
|
553 { |
|
554 a.Close(); |
|
555 return -65535; |
|
556 } |
|
557 TInt i; |
|
558 for (i=0; i<aCount; i++) |
|
559 { |
|
560 TInt x=Random()&aRange; |
|
561 a.Append(x); |
|
562 pA[i]=x; |
|
563 } |
|
564 if (a.Count()!=aCount) |
|
565 { |
|
566 a.Close(); |
|
567 return -1; |
|
568 } |
|
569 for (i=0; i<aCount; i++) |
|
570 { |
|
571 TInt r=a.Find(pA[i]); |
|
572 if (r<0 || pA[r]!=pA[i] || r>i) |
|
573 { |
|
574 a.Close(); |
|
575 return -2; |
|
576 } |
|
577 TInt x=Random()&aRange; |
|
578 r=a.Find(x); |
|
579 if (r<0) |
|
580 { |
|
581 TInt j; |
|
582 for (j=0; j<aCount; j++) |
|
583 { |
|
584 if (pA[j]==x) |
|
585 { |
|
586 a.Close(); |
|
587 return -3; |
|
588 } |
|
589 } |
|
590 } |
|
591 else if (pA[r]!=x) |
|
592 { |
|
593 a.Close(); |
|
594 return -4; |
|
595 } |
|
596 else |
|
597 { |
|
598 TInt j; |
|
599 for (j=0; j<r; j++) |
|
600 { |
|
601 if (pA[j]==x) |
|
602 { |
|
603 a.Close(); |
|
604 return -5; |
|
605 } |
|
606 } |
|
607 } |
|
608 } |
|
609 delete pA; |
|
610 a.Close(); |
|
611 } |
|
612 return KErrNone; |
|
613 } |
|
614 |
|
615 LOCAL_C TInt SIntFindInOrderTest(TInt aCount, TInt aNumTests, TInt aRange) |
|
616 // require aRange*aCount<2^32 |
|
617 { |
|
618 TInt n; |
|
619 for (n=0; n<aNumTests; n++) |
|
620 { |
|
621 RArray<SInt> a; |
|
622 TInt *pA=(TInt*)User::Alloc(aCount*sizeof(TInt)); |
|
623 if (!pA) |
|
624 { |
|
625 a.Close(); |
|
626 return -65535; |
|
627 } |
|
628 TInt i=0; |
|
629 TInt y=-256; |
|
630 for(i=0; i<aCount; i++) |
|
631 { |
|
632 TInt x=Random()&aRange; // this is always >=0 |
|
633 a.Append(y); |
|
634 pA[i]=y; |
|
635 y+=x; |
|
636 } |
|
637 if (a.Count()!=aCount) |
|
638 { |
|
639 a.Close(); |
|
640 return -1; |
|
641 } |
|
642 for (i=0; i<aCount; i++) |
|
643 { |
|
644 TInt r=a.FindInSignedKeyOrder(pA[i]); |
|
645 if (r<0 || pA[r]!=pA[i]) |
|
646 { |
|
647 a.Close(); |
|
648 return -2; |
|
649 } |
|
650 TInt x=Random()&aRange; |
|
651 r=a.FindInSignedKeyOrder(x); |
|
652 if (r<0) |
|
653 { |
|
654 TInt j; |
|
655 for (j=0; j<aCount; j++) |
|
656 { |
|
657 if (pA[j]==x) |
|
658 { |
|
659 a.Close(); |
|
660 return -3; |
|
661 } |
|
662 } |
|
663 } |
|
664 else if (pA[r]!=x) |
|
665 { |
|
666 a.Close(); |
|
667 return -4; |
|
668 } |
|
669 } |
|
670 delete pA; |
|
671 a.Close(); |
|
672 } |
|
673 return KErrNone; |
|
674 } |
|
675 |
|
676 LOCAL_C TInt SIntFindInOrderTest2() |
|
677 { |
|
678 TInt i; |
|
679 RArray<SInt> sq; |
|
680 for (i=0; i<1024; i++) |
|
681 { |
|
682 TInt j=i*i; |
|
683 sq.Append(j); |
|
684 } |
|
685 for (i=0; i<10000; i++) |
|
686 { |
|
687 TInt x=Random()&1023; |
|
688 TInt y=x*x; |
|
689 TInt r=sq.FindInSignedKeyOrder(y); |
|
690 if (r!=x) |
|
691 { |
|
692 sq.Close(); |
|
693 return -1; |
|
694 } |
|
695 } |
|
696 sq.Close(); |
|
697 return 0; |
|
698 } |
|
699 |
|
700 LOCAL_C TInt SIntInsertInOrderTest(TInt aCount, TInt aNumTests, TInt aRange) |
|
701 { |
|
702 TInt n; |
|
703 for (n=0; n<aNumTests; n++) |
|
704 { |
|
705 RArray<SInt> a; |
|
706 RArray<SInt> b; |
|
707 RArray<SInt> c; |
|
708 TInt i; |
|
709 TInt cc=0; |
|
710 for (i=0; i<aCount; i++) |
|
711 { |
|
712 TInt x=Random()&aRange; |
|
713 a.Append(x); |
|
714 b.InsertInSignedKeyOrderAllowRepeats(x); |
|
715 TInt r=c.InsertInSignedKeyOrder(x); |
|
716 if (r==KErrNone) |
|
717 cc++; |
|
718 } |
|
719 if (a.Count()!=aCount) |
|
720 { |
|
721 a.Close(); |
|
722 b.Close(); |
|
723 c.Close(); |
|
724 return -1; |
|
725 } |
|
726 if (b.Count()!=aCount) |
|
727 { |
|
728 a.Close(); |
|
729 b.Close(); |
|
730 c.Close(); |
|
731 return -2; |
|
732 } |
|
733 for (i=0; i<aCount-1; i++) |
|
734 { |
|
735 if (b[i]>b[i+1]) |
|
736 { |
|
737 a.Close(); |
|
738 b.Close(); |
|
739 c.Close(); |
|
740 return -3; |
|
741 } |
|
742 } |
|
743 for (i=0; i<aCount; i++) |
|
744 { |
|
745 if (a.Find(b[i])<0) |
|
746 { |
|
747 a.Close(); |
|
748 b.Close(); |
|
749 c.Close(); |
|
750 return -4; |
|
751 } |
|
752 if (b.Find(a[i])<0) |
|
753 { |
|
754 a.Close(); |
|
755 b.Close(); |
|
756 c.Close(); |
|
757 return -5; |
|
758 } |
|
759 if (c.Find(a[i])<0) |
|
760 { |
|
761 a.Close(); |
|
762 b.Close(); |
|
763 c.Close(); |
|
764 return -6; |
|
765 } |
|
766 } |
|
767 if (c.Count()!=cc) |
|
768 { |
|
769 a.Close(); |
|
770 b.Close(); |
|
771 c.Close(); |
|
772 return -7; |
|
773 } |
|
774 for (i=0; i<c.Count()-1; i++) |
|
775 { |
|
776 if (c[i]>=c[i+1]) |
|
777 { |
|
778 a.Close(); |
|
779 b.Close(); |
|
780 c.Close(); |
|
781 return -8; |
|
782 } |
|
783 if (a.Find(c[i])<0) |
|
784 { |
|
785 a.Close(); |
|
786 b.Close(); |
|
787 c.Close(); |
|
788 return -9; |
|
789 } |
|
790 } |
|
791 a.Close(); |
|
792 b.Close(); |
|
793 c.Close(); |
|
794 } |
|
795 return KErrNone; |
|
796 } |
|
797 |
|
798 LOCAL_C TInt SIntInsertInOrderTest2() |
|
799 { |
|
800 TInt i; |
|
801 RArray<SInt> sq; |
|
802 for (i=0; i<1024; i++) |
|
803 { |
|
804 TInt j=i*i; |
|
805 sq.InsertInSignedKeyOrder(j); |
|
806 sq.InsertInSignedKeyOrder(-j); |
|
807 } |
|
808 if (sq.Count()!=2047) |
|
809 { |
|
810 sq.Close(); |
|
811 return -1; |
|
812 } |
|
813 for (i=0; i<2047; i++) |
|
814 { |
|
815 TInt y=0; |
|
816 if (i<1023) |
|
817 y=-(1023-i)*(1023-i); |
|
818 else if (i==1023) |
|
819 y=0; |
|
820 else if (i>1023) |
|
821 y=(i-1023)*(i-1023); |
|
822 if (sq[i]!=y) |
|
823 { |
|
824 sq.Close(); |
|
825 return -2; |
|
826 } |
|
827 } |
|
828 sq.Close(); |
|
829 return 0; |
|
830 } |
|
831 |
|
832 LOCAL_C TInt SIntSortTest(TInt aCount, TInt aNumTests, TInt aRange) |
|
833 { |
|
834 TInt n; |
|
835 for (n=0; n<aNumTests; n++) |
|
836 { |
|
837 RArray<SInt> a; |
|
838 RArray<SInt> b; |
|
839 TInt i; |
|
840 for (i=0; i<aCount; i++) |
|
841 { |
|
842 TInt x=Random()&aRange; |
|
843 a.Append(x); |
|
844 b.InsertInSignedKeyOrderAllowRepeats(x); |
|
845 } |
|
846 a.SortSigned(); |
|
847 if (a.Count()!=aCount) |
|
848 { |
|
849 a.Close(); |
|
850 b.Close(); |
|
851 return -1; |
|
852 } |
|
853 if (b.Count()!=aCount) |
|
854 { |
|
855 a.Close(); |
|
856 b.Close(); |
|
857 return -2; |
|
858 } |
|
859 for (i=0; i<aCount; i++) |
|
860 { |
|
861 if (a[i]!=b[i]) |
|
862 { |
|
863 a.Close(); |
|
864 b.Close(); |
|
865 return -3; |
|
866 } |
|
867 } |
|
868 a.Close(); |
|
869 b.Close(); |
|
870 } |
|
871 return KErrNone; |
|
872 } |
|
873 |
|
874 TInt IntSpecificFindTests1() |
|
875 { |
|
876 RArray<TInt> a; |
|
877 TInt i; |
|
878 TInt v = 0; |
|
879 TInt first, last, any; |
|
880 for (i=0; i<101; ++i) |
|
881 { |
|
882 if (v*v<i) |
|
883 ++v; |
|
884 TInt r = a.InsertInOrderAllowRepeats(v); |
|
885 test(r==KErrNone); |
|
886 } |
|
887 test (a.Count()==101); |
|
888 for (v=0; v<=10; ++v) |
|
889 { |
|
890 first = a.SpecificFindInOrder(v, EArrayFindMode_First); |
|
891 last = a.SpecificFindInOrder(v, EArrayFindMode_Last); |
|
892 any = a.SpecificFindInOrder(v, EArrayFindMode_Any); |
|
893 test(first>=0 && first<a.Count()); |
|
894 test(last>=0 && last<=a.Count()); |
|
895 test(any>=first && any<last); |
|
896 if (v==0) |
|
897 { |
|
898 test(first==0 && last==1); |
|
899 } |
|
900 else if (v==1) |
|
901 { |
|
902 test(first==1 && last==2); |
|
903 } |
|
904 else |
|
905 { |
|
906 TInt expf = (v-1)*(v-1)+1; |
|
907 TInt expl = v*v+1; |
|
908 test(expf==first && expl==last); |
|
909 } |
|
910 TInt n = last - first; |
|
911 TInt expected = v ? 2*v-1 : 1; |
|
912 test(n == expected); |
|
913 } |
|
914 first = a.SpecificFindInOrder(11, EArrayFindMode_First); |
|
915 test(first == KErrNotFound); |
|
916 last = a.SpecificFindInOrder(11, EArrayFindMode_Last); |
|
917 test(last == KErrNotFound); |
|
918 any = a.SpecificFindInOrder(11, EArrayFindMode_Any); |
|
919 test(any == KErrNotFound); |
|
920 a.Close(); |
|
921 return KErrNone; |
|
922 } |
|
923 |
|
924 TInt IntSpecificFindTests2(TInt aCount, TInt aNumTests, TInt aRange) |
|
925 { |
|
926 TInt n; |
|
927 TInt nmiss = 0; |
|
928 TInt nrpt = 0; |
|
929 TInt ntot = 0; |
|
930 for (n=0; n<aNumTests; n++) |
|
931 { |
|
932 RArray<TInt> a; |
|
933 RArray<TInt> b; |
|
934 TInt i; |
|
935 for (i=0; i<aCount; i++) |
|
936 { |
|
937 TInt x=Random()&aRange; |
|
938 x-=(aRange>>1); |
|
939 a.Append(x); |
|
940 b.InsertInOrderAllowRepeats(x); |
|
941 } |
|
942 a.Sort(); |
|
943 test(a.Count()==aCount); |
|
944 test(b.Count()==aCount); |
|
945 for (i=0; i<aCount; i++) |
|
946 test(a[i]==b[i]); |
|
947 for (i=-(aRange>>1); i<=(aRange>>1); ++i) |
|
948 { |
|
949 TInt first = a.SpecificFindInOrder(i, EArrayFindMode_First); |
|
950 TInt last = a.SpecificFindInOrder(i, EArrayFindMode_Last); |
|
951 TInt any = a.SpecificFindInOrder(i, EArrayFindMode_Any); |
|
952 TInt fi, li, ai; |
|
953 TInt first2 = a.SpecificFindInOrder(i, fi, EArrayFindMode_First); |
|
954 TInt last2 = a.SpecificFindInOrder(i, li, EArrayFindMode_Last); |
|
955 TInt any2 = a.SpecificFindInOrder(i, ai, EArrayFindMode_Any); |
|
956 ++ntot; |
|
957 if (first < 0) |
|
958 { |
|
959 test(first == KErrNotFound); |
|
960 test(first == last); |
|
961 test(first == any); |
|
962 test(first == first2); |
|
963 test(first == last2); |
|
964 test(first == any2); |
|
965 test(fi == li); |
|
966 test(fi == ai); |
|
967 test(li==aCount || a[li]>i); |
|
968 test(li==0 || a[li-1]<i); |
|
969 ++nmiss; |
|
970 } |
|
971 else |
|
972 { |
|
973 test(first2 == KErrNone); |
|
974 test(last2 == KErrNone); |
|
975 test(any2 == KErrNone); |
|
976 test(first == fi); |
|
977 test(last == li); |
|
978 test(any == ai); |
|
979 test(a[fi] == i); |
|
980 test(a[li-1] == i); |
|
981 test(li==aCount || a[li]>i); |
|
982 test(ai>=fi && ai<li); |
|
983 test(a[ai] == i); |
|
984 if (li-fi > 1) |
|
985 ++nrpt; |
|
986 } |
|
987 } |
|
988 a.Close(); |
|
989 b.Close(); |
|
990 } |
|
991 test.Printf(_L("ntot=%d nmiss=%d nrpt=%d\n"), ntot, nmiss, nrpt); |
|
992 return KErrNone; |
|
993 } |
|
994 |
|
995 TInt SIntSpecificFindTests2(TInt aCount, TInt aNumTests, TInt aRange) |
|
996 { |
|
997 TInt n; |
|
998 TInt nmiss = 0; |
|
999 TInt nrpt = 0; |
|
1000 TInt ntot = 0; |
|
1001 for (n=0; n<aNumTests; n++) |
|
1002 { |
|
1003 RArray<SInt> a; |
|
1004 RArray<SInt> b; |
|
1005 TInt i; |
|
1006 for (i=0; i<aCount; i++) |
|
1007 { |
|
1008 TInt x=Random()&aRange; |
|
1009 x-=(aRange>>1); |
|
1010 a.Append(x); |
|
1011 b.InsertInSignedKeyOrderAllowRepeats(x); |
|
1012 } |
|
1013 a.SortSigned(); |
|
1014 test(a.Count()==aCount); |
|
1015 test(b.Count()==aCount); |
|
1016 for (i=0; i<aCount; i++) |
|
1017 test(a[i]==b[i]); |
|
1018 for (i=-(aRange>>1); i<=(aRange>>1); ++i) |
|
1019 { |
|
1020 TInt first = a.SpecificFindInSignedKeyOrder(i, EArrayFindMode_First); |
|
1021 TInt last = a.SpecificFindInSignedKeyOrder(i, EArrayFindMode_Last); |
|
1022 TInt any = a.SpecificFindInSignedKeyOrder(i, EArrayFindMode_Any); |
|
1023 TInt fi, li, ai; |
|
1024 TInt first2 = a.SpecificFindInSignedKeyOrder(i, fi, EArrayFindMode_First); |
|
1025 TInt last2 = a.SpecificFindInSignedKeyOrder(i, li, EArrayFindMode_Last); |
|
1026 TInt any2 = a.SpecificFindInSignedKeyOrder(i, ai, EArrayFindMode_Any); |
|
1027 ++ntot; |
|
1028 if (first < 0) |
|
1029 { |
|
1030 test(first == KErrNotFound); |
|
1031 test(first == last); |
|
1032 test(first == any); |
|
1033 test(first == first2); |
|
1034 test(first == last2); |
|
1035 test(first == any2); |
|
1036 test(fi == li); |
|
1037 test(fi == ai); |
|
1038 test(li==aCount || a[li]>i); |
|
1039 test(li==0 || a[li-1]<i); |
|
1040 ++nmiss; |
|
1041 } |
|
1042 else |
|
1043 { |
|
1044 test(first2 == KErrNone); |
|
1045 test(last2 == KErrNone); |
|
1046 test(any2 == KErrNone); |
|
1047 test(first == fi); |
|
1048 test(last == li); |
|
1049 test(any == ai); |
|
1050 test(a[fi] == i); |
|
1051 test(a[li-1] == i); |
|
1052 test(li==aCount || a[li]>i); |
|
1053 test(ai>=fi && ai<li); |
|
1054 test(a[ai] == i); |
|
1055 if (li-fi > 1) |
|
1056 ++nrpt; |
|
1057 } |
|
1058 } |
|
1059 a.Close(); |
|
1060 b.Close(); |
|
1061 } |
|
1062 test.Printf(_L("ntot=%d nmiss=%d nrpt=%d\n"), ntot, nmiss, nrpt); |
|
1063 return KErrNone; |
|
1064 } |
|
1065 |
|
1066 GLDEF_C void DoIntArrayTests() |
|
1067 { |
|
1068 test.Start(_L("Signed Integer Arrays...")); |
|
1069 |
|
1070 test.Next(_L("AppendAndAccess tests...")); |
|
1071 test.Next(_L("Count 10 Range 15")); |
|
1072 test(IntAppendAndAccessTest(10,NUM_TESTS,15)==KErrNone); |
|
1073 test.Next(_L("Count 20 Range 255")); |
|
1074 test(IntAppendAndAccessTest(20,NUM_TESTS,255)==KErrNone); |
|
1075 test.Next(_L("Count 100 Range all")); |
|
1076 test(IntAppendAndAccessTest(100,NUM_TESTS,-1)==KErrNone); |
|
1077 |
|
1078 test.Next(_L("Remove tests...")); |
|
1079 test(IntRemoveTest()==KErrNone); |
|
1080 |
|
1081 test.Next(_L("Find tests...")); |
|
1082 test.Next(_L("Count 10 Range 15")); |
|
1083 test(IntFindTest(10,NUM_TESTS,15)==KErrNone); |
|
1084 test.Next(_L("Count 20 Range 255")); |
|
1085 test(IntFindTest(20,NUM_TESTS,255)==KErrNone); |
|
1086 test.Next(_L("Count 100 Range all")); |
|
1087 test(IntFindTest(100,NUM_TESTS,-1)==KErrNone); |
|
1088 |
|
1089 test.Next(_L("FindInOrder tests...")); |
|
1090 test.Next(_L("Count 10 Range 15")); |
|
1091 test(IntFindInOrderTest(10,NUM_TESTS,15)==KErrNone); |
|
1092 test.Next(_L("Count 20 Range 255")); |
|
1093 test(IntFindInOrderTest(20,NUM_TESTS,255)==KErrNone); |
|
1094 test.Next(_L("Count 100 Range 4095")); |
|
1095 test(IntFindInOrderTest(100,NUM_TESTS,4095)==KErrNone); |
|
1096 test.Next(_L("Squares")); |
|
1097 test(IntFindInOrderTest2()==KErrNone); |
|
1098 |
|
1099 test.Next(_L("InsertInOrder tests...")); |
|
1100 test.Next(_L("Count 10 Range 15")); |
|
1101 test(IntInsertInOrderTest(10,NUM_TESTS,15)==KErrNone); |
|
1102 test.Next(_L("Count 20 Range 255")); |
|
1103 test(IntInsertInOrderTest(20,NUM_TESTS,255)==KErrNone); |
|
1104 test.Next(_L("Count 100 Range all")); |
|
1105 test(IntInsertInOrderTest(100,NUM_TESTS,-1)==KErrNone); |
|
1106 test.Next(_L("Squares")); |
|
1107 test(IntInsertInOrderTest2()==KErrNone); |
|
1108 |
|
1109 test.Next(_L("Sort tests...")); |
|
1110 test.Next(_L("Count 10 Range 15")); |
|
1111 test(IntSortTest(10,NUM_TESTS,15)==KErrNone); |
|
1112 test.Next(_L("Count 20 Range 255")); |
|
1113 test(IntSortTest(20,NUM_TESTS,255)==KErrNone); |
|
1114 test.Next(_L("Count 100 Range all")); |
|
1115 test(IntSortTest(100,NUM_TESTS,-1)==KErrNone); |
|
1116 |
|
1117 test.End(); |
|
1118 |
|
1119 test.Start(_L("4 byte RArrays...")); |
|
1120 |
|
1121 test.Next(_L("AppendAndAccess tests...")); |
|
1122 test.Next(_L("Count 10 Range 15")); |
|
1123 test(SIntAppendAndAccessTest(10,NUM_TESTS,15)==KErrNone); |
|
1124 test.Next(_L("Count 20 Range 255")); |
|
1125 test(SIntAppendAndAccessTest(20,NUM_TESTS,255)==KErrNone); |
|
1126 test.Next(_L("Count 100 Range all")); |
|
1127 test(SIntAppendAndAccessTest(100,NUM_TESTS,-1)==KErrNone); |
|
1128 |
|
1129 test.Next(_L("Remove tests...")); |
|
1130 test(SIntRemoveTest()==KErrNone); |
|
1131 |
|
1132 test.Next(_L("Find tests...")); |
|
1133 test.Next(_L("Count 10 Range 15")); |
|
1134 test(SIntFindTest(10,NUM_TESTS,15)==KErrNone); |
|
1135 test.Next(_L("Count 20 Range 255")); |
|
1136 test(SIntFindTest(20,NUM_TESTS,255)==KErrNone); |
|
1137 test.Next(_L("Count 100 Range all")); |
|
1138 test(SIntFindTest(100,NUM_TESTS,-1)==KErrNone); |
|
1139 |
|
1140 test.Next(_L("FindInOrder tests...")); |
|
1141 test.Next(_L("Count 10 Range 15")); |
|
1142 test(SIntFindInOrderTest(10,NUM_TESTS,15)==KErrNone); |
|
1143 test.Next(_L("Count 20 Range 255")); |
|
1144 test(SIntFindInOrderTest(20,NUM_TESTS,255)==KErrNone); |
|
1145 test.Next(_L("Count 100 Range 4095")); |
|
1146 test(SIntFindInOrderTest(100,NUM_TESTS,4095)==KErrNone); |
|
1147 test.Next(_L("Squares")); |
|
1148 test(SIntFindInOrderTest2()==KErrNone); |
|
1149 |
|
1150 test.Next(_L("InsertInOrder tests...")); |
|
1151 test.Next(_L("Count 10 Range 15")); |
|
1152 test(SIntInsertInOrderTest(10,NUM_TESTS,15)==KErrNone); |
|
1153 test.Next(_L("Count 20 Range 255")); |
|
1154 test(SIntInsertInOrderTest(20,NUM_TESTS,255)==KErrNone); |
|
1155 test.Next(_L("Count 100 Range all")); |
|
1156 test(SIntInsertInOrderTest(100,NUM_TESTS,-1)==KErrNone); |
|
1157 test.Next(_L("Squares")); |
|
1158 test(SIntInsertInOrderTest2()==KErrNone); |
|
1159 |
|
1160 test.Next(_L("Sort tests...")); |
|
1161 test.Next(_L("Count 10 Range 15")); |
|
1162 test(SIntSortTest(10,NUM_TESTS,15)==KErrNone); |
|
1163 test.Next(_L("Count 20 Range 255")); |
|
1164 test(SIntSortTest(20,NUM_TESTS,255)==KErrNone); |
|
1165 test.Next(_L("Count 100 Range all")); |
|
1166 test(SIntSortTest(100,NUM_TESTS,-1)==KErrNone); |
|
1167 |
|
1168 test.Next(_L("IntSpecificFindTests...")); |
|
1169 test(IntSpecificFindTests1()==KErrNone); |
|
1170 test(IntSpecificFindTests2(100, 10, 15)==KErrNone); |
|
1171 test(IntSpecificFindTests2(100, 10, 127)==KErrNone); |
|
1172 |
|
1173 test.Next(_L("SIntSpecificFindTests...")); |
|
1174 test(SIntSpecificFindTests2(100, 10, 15)==KErrNone); |
|
1175 test(SIntSpecificFindTests2(100, 10, 127)==KErrNone); |
|
1176 test.End(); |
|
1177 } |