Update contrib.
1 // Copyright (c) 1996-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\buffer\t_func.cpp
16 // Comprehensive test for the ARM coded Mem, Des and integer divide routines.
17 // Check memory for a large variation of buffer sizes and alignments,
18 // check all the optimizations made in the copying/filling code.
20 // Mem::Fill, Mem::Copy, Mem::Move, Mem::Swap, Mem::Compare
22 // - Create blocks, fill some data into one block, copy data across block of varying
23 // lengths, alignments and check the copy is as expected.
24 // - Create blocks, fill some data into the block, move data from one block to other
25 // block and check it is as expected.
26 // - Create blocks, fill some data and check the data is filled as expected.
27 // - Create blocks, fill some data in two blocks, swap the blocks check the data
28 // is swapped as expected.
29 // - Create blocks, fill some data into the blocks, compare the data at different specified
30 // offsets, compare the return value, check it is as expected.
31 // - Check the conversion from specified integer numbers in different number systems to
32 // character representation is as expected. Check for both upper and lower case results.
33 // - Initialize variables with signed, unsigned, positive, negative integer values, check the
34 // integer division routines are as expected.
35 // - Check the integer modulo operation results are as expected.
36 // Platforms/Drives/Compatibility:
38 // Assumptions/Requirement/Pre-requisites:
39 // Failures and causes:
40 // Base Port information:
45 #include <e32std_private.h>
47 #include <e32base_private.h>
52 RTest test(_L("T_FUNC"));
56 void PrintInfo(TText8* aBuf1,TText8* aBuf2)
59 test.Printf(_L("source before target\n"));
61 test.Printf(_L("source and target identical\n"));
63 test.Printf(_L("target before source\n"));
66 void testFailed(const TDesC& anError)
68 test.Printf(_L("Test %S failed\n"),&anError);
81 test.Next(_L("Mem::Copy"));
83 // Test various copying lengths and alignments and src before/after trg
91 for(ii=24;ii<496;ii+=19)
93 for(jj=24;jj<496;jj+=18)
95 length=Min(496-jj,496-ii);
97 Mem::Fill(&bufa[0],512,a55);
98 Mem::Fill(&bufb[0],512,aAA);
100 addr=Mem::Copy(&bufa[ii],&bufb[jj],length);
102 if(addr!=(&bufa[ii]+length))
104 PrintInfo(bufb,bufa);
105 test.Printf(_L("Mem::Copy returned incorrect address for %d bytes\n"),length);
111 for(kk=0;kk<512;kk++)
113 if(kk<ii && bufa[kk]!=0x55)
115 PrintInfo(bufb,bufa);
116 test.Printf(_L("Mem::Copy failed copying %d bytes from bufb[%d] to bufa[%d] at byte %d\n"),length,jj,ii,kk);
121 if(kk>=ii && kk<(ii+length) && bufa[kk]!=0xaa)
123 PrintInfo(bufb,bufa);
124 test.Printf(_L("Mem::Copy failed copying %d bytes from bufb[%d] to bufa[%d] at byte %d\n"),length,jj,ii,kk);
129 if(kk>=(ii+length) && bufa[kk]!=0x55)
131 PrintInfo(bufb,bufa);
132 test.Printf(_L("Mem::Copy failed copying %d bytes from bufb[%d] to bufa[%d] at byte %d\n"),length,jj,ii,kk);
139 Mem::Fill(&bufa[0],512,a55);
140 Mem::Fill(&bufb[0],512,aAA);
142 length=Min(496-jj,496-ii);
144 addr=Mem::Copy(&bufb[ii],&bufa[jj],length);
146 if(addr!=(&bufb[ii]+length))
148 PrintInfo(bufa,bufb);
149 test.Printf(_L("Mem::Copy returned incorrect address for %d bytes\n"),length);
155 for(kk=0;kk<512;kk++)
157 if(kk<ii && bufb[kk]!=0xaa)
159 PrintInfo(bufa,bufb);
160 test.Printf(_L("Mem::Copy failed copying %d bytes from bufa[%d] to bufb[%d] at byte %d\n"),length,jj,ii,kk);
165 if(kk>=ii && kk<(ii+length) && bufb[kk]!=0x55)
167 PrintInfo(bufa,bufb);
168 test.Printf(_L("Mem::Copy failed copying %d bytes from bufa[%d] to bufb[%d] at byte %d\n"),length,jj,ii,kk);
173 if(kk>=(ii+length) && bufb[kk]!=0xaa)
175 PrintInfo(bufa,bufb);
176 test.Printf(_L("Mem::Copy failed copying %d bytes from bufa[%d] to bufb[%d] at byte %d\n"),length,jj,ii,kk);
187 // Assert panic test, debug ARM build only. See DEF118984.
189 #if defined(_DEBUG) && defined(__CPU_ARM)
190 const TInt KMemMoveBufferSize=0x20;
191 const TInt KMemMoveBadLength=3;
196 EMemMoveSourceNotAligned=1,
197 EMemMoveTargetNotAligned=2,
198 EMemMoveLengthNotMultipleOf4=3,
201 LOCAL_C TInt MemMoveClient(TAny* aPtr)
203 TMemMoveTest aMode = *(TMemMoveTest *)&aPtr;
204 TText8 srcbuf[KMemMoveBufferSize];
205 TText8 trgbuf[KMemMoveBufferSize];
206 TText8 *src=srcbuf, *trg=trgbuf;
207 TInt length=KMemMoveBufferSize;
213 case EMemMoveSourceNotAligned:
216 case EMemMoveTargetNotAligned:
219 case EMemMoveLengthNotMultipleOf4:
220 length=KMemMoveBadLength;
223 Mem::Move(trg,src,length);
235 test.Next(_L("Mem::Move()"));
237 // Test various copying lengths and alignments and src before/after trg
245 for(ii=0;ii<512;ii+=24)
247 for(jj=0;jj<512;jj+=24)
249 length=Min(512-jj,512-ii);
251 Mem::Fill(&bufa[0],512,a55);
252 Mem::Fill(&bufb[0],512,aAA);
254 addr=Mem::Move(&bufa[ii],&bufb[jj],length);
256 if(addr!=(&bufa[ii]+length))
258 PrintInfo(bufb,bufa);
259 test.Printf(_L("Mem::Copy returned incorrect address for %d bytes\n"),length);
265 for(kk=0;kk<512;kk++)
267 if(kk<ii && bufa[kk]!=0x55)
269 PrintInfo(bufb,bufa);
270 test.Printf(_L("Mem::Move failed copying %d bytes from bufb[%d] to bufa[%d] at byte %d\n"),length,jj,ii,kk);
275 if(kk>=ii && kk<(ii+length) && bufa[kk]!=0xaa)
277 PrintInfo(bufb,bufa);
278 test.Printf(_L("Mem::Move failed copying %d bytes from bufb[%d] to bufa[%d] at byte %d\n"),length,jj,ii,kk);
283 if(kk>=(ii+length) && bufa[kk]!=0x55)
285 PrintInfo(bufb,bufa);
286 test.Printf(_L("Mem::Move failed copying %d bytes from bufb[%d] to bufa[%d] at byte %d\n"),length,jj,ii,kk);
293 Mem::Fill(&bufa[0],512,a55);
294 Mem::Fill(&bufb[0],512,aAA);
296 length=Min(512-jj,512-ii);
298 addr=Mem::Move(&bufb[ii],&bufa[jj],length);
300 if(addr!=(&bufb[ii]+length))
302 PrintInfo(bufa,bufb);
303 test.Printf(_L("Mem::Copy returned incorrect address for %d bytes\n"),length);
309 for(kk=0;kk<512;kk++)
311 if(kk<ii && bufb[kk]!=0xaa)
313 PrintInfo(bufa,bufb);
314 test.Printf(_L("Mem::Move failed copying %d bytes from bufa[%d] to bufb[%d] at byte %d\n"),length,jj,ii,kk);
319 if(kk>=ii && kk<(ii+length) && bufb[kk]!=0x55)
321 PrintInfo(bufa,bufb);
322 test.Printf(_L("Mem::Move failed copying %d bytes from bufa[%d] to bufb[%d] at byte %d\n"),length,jj,ii,kk);
327 if(kk>=(ii+length) && bufb[kk]!=0xaa)
329 PrintInfo(bufa,bufb);
330 test.Printf(_L("Mem::Move failed copying %d bytes from bufa[%d] to bufb[%d] at byte %d\n"),length,jj,ii,kk);
338 #if defined(_DEBUG) && defined(__CPU_ARM)
340 // Test wordmove asserts. Source and target addresses are word aligned,
341 // and length is a multiple of the word size.
342 // Test asserts (debug build only)
344 RThread clientThread;
345 TRequestStatus status(KRequestPending);
347 User::SetJustInTime(EFalse);
348 test.Next(_L("Mem::Move() - wordmove() valid call"));
349 test(clientThread.Create(_L("MemMovePanic - Valid"),MemMoveClient,KDefaultStackSize,0x2000,0x2000,(TAny*)EMemMoveValid)==KErrNone);
350 clientThread.Logon(status);
351 clientThread.Resume();
352 User::WaitForRequest(status);
353 test(clientThread.ExitType()==EExitKill);
354 test(clientThread.ExitReason()==KErrNone);
355 clientThread.Close();
356 status=KRequestPending;
357 test.Next(_L("Mem::Move() - wordmove() source alignment"));
358 test(clientThread.Create(_L("MemMovePanic - SrcAlign"),MemMoveClient,KDefaultStackSize,0x2000,0x2000,(TAny*)EMemMoveSourceNotAligned)==KErrNone);
359 clientThread.Logon(status);
360 clientThread.Resume();
361 User::WaitForRequest(status);
362 test(clientThread.ExitType()==EExitPanic);
363 test(clientThread.ExitReason()==EWordMoveSourceNotAligned);
364 clientThread.Close();
365 status=KRequestPending;
366 test.Next(_L("Mem::Move() - wordmove() target alignment"));
367 test(clientThread.Create(_L("MemMovePanic - TrgAlign"),MemMoveClient,KDefaultStackSize,0x2000,0x2000,(TAny*)EMemMoveTargetNotAligned)==KErrNone);
368 clientThread.Logon(status);
369 clientThread.Resume();
370 User::WaitForRequest(status);
371 test(clientThread.ExitType()==EExitPanic);
372 test(clientThread.ExitReason()==EWordMoveTargetNotAligned);
373 clientThread.Close();
374 status=KRequestPending;
375 test.Next(_L("Mem::Move() - wordmove() length word multiple"));
376 test(clientThread.Create(_L("MemMovePanic - LengthMultiple"),MemMoveClient,KDefaultStackSize,0x2000,0x2000,(TAny*)EMemMoveLengthNotMultipleOf4)==KErrNone);
377 clientThread.Logon(status);
378 clientThread.Resume();
379 User::WaitForRequest(status);
380 test(clientThread.ExitType()==EExitPanic);
381 test(clientThread.ExitReason()==EWordMoveLengthNotMultipleOf4);
382 clientThread.Close();
383 User::SetJustInTime(ETrue);
392 TInt ii,jj,kk,pos,length;
394 test.Next(_L("Mem::Fill()"));
399 for(ii=0;ii<512-32;ii++)
403 Mem::Fill(&bufa[0],512,aAA);
408 Mem::Fill(&bufa[pos],length,a55);
410 for(kk=0;kk<512;kk++)
412 if(kk<(pos) && bufa[kk]!=0xaa)
414 test.Printf(_L("Mem::Fill failed filling %d bytes to bufa[%d] at byte %d (1)\n"),length,pos,kk);
419 if(kk>=(pos) && kk<(pos+length) && bufa[kk]!=0x55)
421 test.Printf(_L("Mem::Fill failed filling %d bytes to bufa[%d] at byte %d (2)\n"),length,pos,kk);
426 if(kk>=(pos+length) && bufa[kk]!=0xaa)
428 test.Printf(_L("Mem::Fill failed filling %d bytes to bufa[%d] at byte %d (3)\n"),length,pos,kk);
441 test.Next(_L("Mem::Swap()"));
452 for(ii=24;ii<496;ii+=5)
454 for(jj=24;jj<496;jj+=3)
456 length=Min(496-jj,496-ii);
458 Mem::Fill(&bufa[0],512,a55);
459 Mem::Fill(&bufb[0],512,aAA);
461 Mem::Swap(&bufa[ii],&bufb[jj],length);
463 for(kk=0;kk<512;kk++)
465 if(kk<ii && bufa[kk]!=0x55)
467 test.Printf(_L("Mem::Swap failed. bufa[%d] contains wrong byte. Swap length=%d, ii=%d, jj=%d\n"),kk,length,ii,jj);
471 if(kk>=ii && kk<(ii+length) && bufa[kk]!=0xaa)
473 test.Printf(_L("Mem::Swap failed. bufa[%d] contains wrong byte. Swap length=%d, ii=%d, jj=%d\n"),kk,length,ii,jj);
477 if(kk>=(ii+length) && bufa[kk]!=0x55)
479 test.Printf(_L("Mem::Swap failed. bufa[%d] contains wrong byte. Swap length=%d, ii=%d, jj=%d\n"),kk,length,ii,jj);
483 if(kk<jj && bufb[kk]!=0xaa)
485 test.Printf(_L("Mem::Swap failed. bufb[%d] contains wrong byte. Swap length=%d, ii=%d, jj=%d\n"),kk,length,ii,jj);
489 if(kk>=jj && kk<(jj+length) && bufb[kk]!=0x55)
491 test.Printf(_L("Mem::Swap failed. bufb[%d] contains wrong byte. Swap length=%d, ii=%d, jj=%d\n"),kk,length,ii,jj);
495 if(kk>=(jj+length) && bufb[kk]!=0xaa)
497 test.Printf(_L("Mem::Swap failed. bufb[%d] contains wrong byte. Swap length=%d, ii=%d, jj=%d\n"),kk,length,ii,jj);
506 void testMemCompare()
512 test.Next(_L("Mem::Compare()"));
520 TInt posi,posj,offi,offj,leni,lenj;
522 for(ii=0;ii<512;ii+=2)
524 for(jj=0;jj<512;jj+=3)
526 Mem::Fill(&bufa[0],512,a55);
527 Mem::Fill(&bufb[0],512,a55);
529 posi=(511+ii)/2; // get a position somewhere in the middle
541 // Make sure that outside the compare range is different
543 Mem::Fill(&bufa[ii+leni],4,a11);
544 Mem::Fill(&bufb[jj+lenj],4,a99);
546 kk=Mem::Compare(&bufa[ii],leni,&bufb[jj],lenj);
548 if(offi==offj) // Wrong byte is at same offset
550 if(ii==jj) // Same lengths being compared, so should compare ok
554 test.Printf(_L("%d returned when offi=%d, offj=%d, ii=%d, jj=%d\n"),kk,offi,offj,ii,jj);
555 test.Printf(_L("Should return zero\n"));
556 test.Printf(_L("bufa=%d,bufb=%d, leftl=%d, rightl=%d\n"),&bufa[ii],&bufb[jj],leni,lenj);
561 else // Different lengths, so should return difference of lengths
565 test.Printf(_L("%d returned when offi=%d, offj=%d, ii=%d, jj=%d\n"),kk,offi,offj,ii,jj);
566 test.Printf(_L("Should return difference of the lengths\n"));
567 test.Printf(_L("bufa=%d,bufb=%d, leftl=%d, rightl=%d\n"),&bufa[ii],&bufb[jj],leni,lenj);
573 if(offi!=offj) // Wrong byte at different offset
575 if(offi<offj && kk!=0x12-0x55)
577 test.Printf(_L("%d returned when offi=%d, offj=%d, ii=%d, jj=%d\n"),kk,offi,offj,ii,jj);
578 test.Printf(_L("Should return difference of the bytes\n"));
579 test.Printf(_L("bufa=%d,bufb=%d, leftl=%d, rightl=%d\n"),&bufa[ii],&bufb[jj],leni,lenj);
583 if(offj<offi && kk!=0x55-0x12)
585 test.Printf(_L("%d returned when offi=%d, offj=%d, ii=%d, jj=%d\n"),kk,offi,offj,ii,jj);
586 test.Printf(_L("Should return difference of the bytes\n"));
587 test.Printf(_L("bufa=%d,bufb=%d, leftl=%d, rightl=%d\n"),&bufa[ii],&bufb[jj],leni,lenj);
601 test.Next(_L("Des::Num(EHex)"));
602 aBuf.Num(0x1b34a678,EHex);
603 if(aBuf!=_L("1b34a678"))
604 testFailed(_L("Des::Num(0x1b34a678,EHex)"));
607 aBuf.Num(0x1234,EHex);
609 testFailed(_L("Des::Num(0x1234,EHex)"));
612 test.Next(_L("Des::Num(EDecimal)"));
613 aBuf.Num(7462521,EDecimal);
614 if(aBuf!=_L("7462521"))
615 testFailed(_L("Des::Num(7462521,EDecimal)"));
618 aBuf.Num(1234,EDecimal);
620 testFailed(_L("Des::Num(1234,EDecimal)"));
623 test.Next(_L("Des::Num(EOctal)"));
625 aBuf.Num(03521,EOctal);
627 testFailed(_L("Des::Num(03521,EOctal)"));
630 aBuf.Num(0706321,EOctal);
631 if(aBuf!=_L("706321"))
632 testFailed(_L("Des::Num(0706321,EOctal)"));
635 test.Next(_L("Des::Num(EBinary)"));
636 aBuf.Num(0x92074625,EBinary);
637 if(aBuf!=_L("10010010000001110100011000100101"))
639 testFailed(_L("Des::Num(0x92074625,EBinary)"));
643 aBuf.Num(0x4625,EBinary);
644 if(aBuf!=_L("100011000100101"))
645 testFailed(_L("Des::Num(0x4625,EBinary)"));
654 test.Next(_L("Des::NumUC(EHex)"));
655 aBuf.NumUC(0x1b3ca678,EHex);
656 if(aBuf!=_L("1B3CA678"))
657 testFailed(_L("Des::NumUC(0x1b3ca678,EHex)"));
660 aBuf.NumUC(0x89abcdef,EHex);
661 if(aBuf!=_L("89ABCDEF"))
662 testFailed(_L("Des::NumUC(0x89abcdef,EHex)"));
665 test.Next(_L("Des::NumUC(EDecimal)"));
666 aBuf.NumUC(7462521,EDecimal);
667 if(aBuf!=_L("7462521"))
668 testFailed(_L("Des::NumUC(7462521,EDecimal)"));
671 aBuf.NumUC(1234,EDecimal);
673 testFailed(_L("Des::NumUC(1234,EDecimal)"));
676 test.Next(_L("Des::NumUC(EOctal)"));
678 aBuf.NumUC(03521,EOctal);
680 testFailed(_L("Des::NumUC(03521,EOctal)"));
683 aBuf.NumUC(0706321,EOctal);
684 if(aBuf!=_L("706321"))
685 testFailed(_L("Des::NumUC(0706321,EOctal)"));
688 test.Next(_L("Des::NumUC(EBinary)"));
689 aBuf.NumUC(0x92074625,EBinary);
690 if(aBuf!=_L("10010010000001110100011000100101"))
692 testFailed(_L("Des::NumUC(0x92074625,EBinary)"));
696 aBuf.NumUC(0x4625,EBinary);
697 if(aBuf!=_L("100011000100101"))
698 testFailed(_L("Des::NumUC(0x4625,EBinary)"));
702 void testDivTen(TInt aInc)
704 // Always pass aInc as zero. It's just there to stop the compiler
705 // optimising the a=a/10 statements out for you. They must be
706 // worked out by the operating system at runtime.
710 TUint a=68417814+aInc; // some random unsigned number
711 TInt b=-48910759+aInc; // some random signed negative number
712 TInt c=2147483647+aInc; // maximum positive number
713 TUint d=3147484647u+aInc; // high positive unsigned number
715 TUint ar=68417814/10;
716 TInt br=-48910759/10;
717 TInt cr=2147483647/10;
718 TUint dr=3147484647u/10;
725 test.Next(_L("Integer divide by 10"));
729 test.Printf(_L("68417814/10 gives %u\n"),a);
734 test.Printf(_L("-48910759/10 gives %d\n"),b);
739 test.Printf(_L("2147483647/10 gives %d\n"),c);
744 test.Printf(_L("3147484647/10 gives %u\n"),d);
749 void testDivSeven(TInt aInc)
751 // Always pass aInc as zero. It's just there to stop the compiler
752 // optimising the a=a/7 statements out for you. They must be
753 // worked out by the operating system at runtime.
757 TUint a=68417814+aInc; // some random unsigned number
758 TInt b=-48910759+aInc; // some random signed negative number
759 TInt c=2147483647+aInc; // maximum positive number
760 TUint d=3147484647u+aInc; // high positive unsigned number
764 TInt cr=2147483647/7;
765 TUint dr=3147484647u/7;
772 test.Next(_L("Integer divide by 7"));
776 test.Printf(_L("68417814/7 gives %u\n"),a);
781 test.Printf(_L("-48910759/7 gives %d\n"),b);
786 test.Printf(_L("2147483647/7 gives %d\n"),c);
791 test.Printf(_L("3147484647/7 gives %u\n"),d);
796 void testDivFive(TInt aInc)
798 // Always pass aInc as zero. It's just there to stop the compiler
799 // optimising the a=a/5 statements out for you. They must be
800 // worked out by the operating system at runtime.
804 TUint a=68417814+aInc; // some random unsigned number
805 TInt b=-48910759+aInc; // some random signed negative number
806 TInt c=2147483647+aInc; // maximum positive number
807 TUint d=3147484647u+aInc; // high positive unsigned number
811 TInt cr=2147483647/5;
812 TUint dr=3147484647u/5;
819 test.Next(_L("Integer divide by 5"));
823 test.Printf(_L("68417814/5 gives %u\n"),a);
828 test.Printf(_L("-48910759/5 gives %d\n"),b);
833 test.Printf(_L("2147483647/5 gives %d\n"),c);
838 test.Printf(_L("3147484647/5 gives %u\n"),d);
843 void testDivSixteen(TInt aInc)
845 // Always pass aInc as zero. It's just there to stop the compiler
846 // optimising the a=a/16 statements out for you. They must be
847 // worked out by the operating system at runtime.
851 TUint a=68417814+aInc; // some random unsigned number
852 TInt b=-48910759+aInc; // some random signed negative number
853 TInt c=2147483647+aInc; // maximum positive number
854 TUint d=3147484647u+aInc; // high positive unsigned number
856 TUint ar=68417814/16;
857 TInt br=-48910759/16;
858 TInt cr=2147483647/16;
859 TUint dr=3147484647u/16;
866 test.Next(_L("Integer divide by 16"));
870 test.Printf(_L("68417814/16 gives %u\n"),a);
875 test.Printf(_L("-48910759/16 gives %d\n"),b);
880 test.Printf(_L("2147483647/16 gives %d\n"),c);
885 test.Printf(_L("3147484647/16 gives %u\n"),d);
890 void testModulo(TInt aInc)
893 test.Next(_L("Integer modulo"));
901 TInt jj=(kk*73)+aInc+ii;
905 test.Printf(_L("%d mod %d gives %d\n"),jj,kk,jj%kk);
914 // Benchmark for Mem functions
921 test.Start(_L("T_FUNC"));
936 test(failed==KErrNone);