kerneltest/e32test/buffer/t_func.cpp
changeset 9 96e5fb8b040d
equal deleted inserted replaced
-1:000000000000 9:96e5fb8b040d
       
     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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32test\buffer\t_func.cpp
       
    15 // Overview:
       
    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.
       
    19 // API Information:
       
    20 // Mem::Fill, Mem::Copy, Mem::Move, Mem::Swap, Mem::Compare
       
    21 // Details:
       
    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:
       
    37 // All 
       
    38 // Assumptions/Requirement/Pre-requisites:
       
    39 // Failures and causes:
       
    40 // Base Port information:
       
    41 // 
       
    42 //
       
    43 
       
    44 #include <e32std.h>
       
    45 #include <e32std_private.h>
       
    46 #include <e32base.h>
       
    47 #include <e32base_private.h>
       
    48 #include <e32test.h>
       
    49 #include <e32svr.h>
       
    50 #include <e32panic.h>
       
    51 
       
    52 RTest test(_L("T_FUNC"));
       
    53 
       
    54 TInt failed;
       
    55 
       
    56 void PrintInfo(TText8* aBuf1,TText8* aBuf2)
       
    57     {
       
    58     if(aBuf1<aBuf2)
       
    59         test.Printf(_L("source before target\n"));
       
    60     else if(aBuf1==aBuf2)
       
    61         test.Printf(_L("source and target identical\n"));
       
    62     else
       
    63         test.Printf(_L("target before source\n"));      
       
    64     }
       
    65 
       
    66 void testFailed(const TDesC& anError)
       
    67     {
       
    68     test.Printf(_L("Test %S failed\n"),&anError);
       
    69     //test.Getch();
       
    70     failed=KErrGeneral;
       
    71     }
       
    72 
       
    73 void testMemCopy()
       
    74     {
       
    75 
       
    76     TText8 bufa[0x200];
       
    77     TText8 bufb[0x200];
       
    78 
       
    79     TInt ii,jj,kk;
       
    80 
       
    81     test.Next(_L("Mem::Copy"));
       
    82 //
       
    83 // Test various copying lengths and alignments and src before/after trg
       
    84 //
       
    85     TInt length;
       
    86     TChar a55(0x55);
       
    87     TChar aAA(0xaa);
       
    88 
       
    89     TUint8* addr;
       
    90 
       
    91     for(ii=24;ii<496;ii+=19)
       
    92         {
       
    93         for(jj=24;jj<496;jj+=18)
       
    94             {
       
    95             length=Min(496-jj,496-ii);
       
    96 
       
    97             Mem::Fill(&bufa[0],512,a55);
       
    98             Mem::Fill(&bufb[0],512,aAA);
       
    99 
       
   100             addr=Mem::Copy(&bufa[ii],&bufb[jj],length);
       
   101 
       
   102             if(addr!=(&bufa[ii]+length))
       
   103                 {
       
   104                 PrintInfo(bufb,bufa);
       
   105                 test.Printf(_L("Mem::Copy returned incorrect address for %d bytes\n"),length);
       
   106                 failed=KErrGeneral;
       
   107                 //test.Getch();
       
   108                 return;
       
   109                 }
       
   110 
       
   111             for(kk=0;kk<512;kk++)
       
   112                 {
       
   113                 if(kk<ii && bufa[kk]!=0x55)
       
   114                     {
       
   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);
       
   117                     failed=KErrGeneral;
       
   118                     //test.Getch();
       
   119                     return;
       
   120                     }
       
   121                 if(kk>=ii && kk<(ii+length) && bufa[kk]!=0xaa)
       
   122                     {
       
   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);
       
   125                     failed=KErrGeneral;
       
   126                     //test.Getch();
       
   127                     return;
       
   128                     }
       
   129                 if(kk>=(ii+length) && bufa[kk]!=0x55)
       
   130                     {
       
   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);
       
   133                     failed=KErrGeneral;
       
   134                     //test.Getch();
       
   135                     return;
       
   136                     }
       
   137                 }
       
   138 
       
   139             Mem::Fill(&bufa[0],512,a55);
       
   140             Mem::Fill(&bufb[0],512,aAA);
       
   141 
       
   142             length=Min(496-jj,496-ii);
       
   143           
       
   144             addr=Mem::Copy(&bufb[ii],&bufa[jj],length);
       
   145 
       
   146             if(addr!=(&bufb[ii]+length))
       
   147                 {
       
   148                 PrintInfo(bufa,bufb);
       
   149                 test.Printf(_L("Mem::Copy returned incorrect address for %d bytes\n"),length);
       
   150                 failed=KErrGeneral;
       
   151                 //test.Getch();
       
   152                 return;
       
   153                 }
       
   154 
       
   155             for(kk=0;kk<512;kk++)
       
   156                 {
       
   157                 if(kk<ii && bufb[kk]!=0xaa)
       
   158                     {
       
   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);
       
   161                     failed=KErrGeneral;
       
   162                     //test.Getch();
       
   163                     return;
       
   164                     }
       
   165                 if(kk>=ii && kk<(ii+length) && bufb[kk]!=0x55)
       
   166                     {
       
   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);
       
   169                     failed=KErrGeneral;
       
   170                     //test.Getch();
       
   171                     return;
       
   172                     }
       
   173                 if(kk>=(ii+length) && bufb[kk]!=0xaa)
       
   174                     {
       
   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);
       
   177                     failed=KErrGeneral;
       
   178                     //test.Getch();
       
   179                     return;
       
   180                     }
       
   181                 }
       
   182             };
       
   183         }
       
   184     }
       
   185 
       
   186 //
       
   187 // Assert panic test, debug ARM build only.   See DEF118984.
       
   188 //
       
   189 #if defined(_DEBUG) && defined(__CPU_ARM)
       
   190 const TInt KMemMoveBufferSize=0x20;
       
   191 const TInt KMemMoveBadLength=3;
       
   192 
       
   193 enum TMemMoveTest
       
   194 	{
       
   195 	EMemMoveValid=0,
       
   196 	EMemMoveSourceNotAligned=1,
       
   197 	EMemMoveTargetNotAligned=2,
       
   198 	EMemMoveLengthNotMultipleOf4=3,
       
   199 	};
       
   200 
       
   201 LOCAL_C TInt MemMoveClient(TAny* aPtr)
       
   202 	{
       
   203 	TMemMoveTest aMode = *(TMemMoveTest *)&aPtr;
       
   204 	TText8 srcbuf[KMemMoveBufferSize];
       
   205 	TText8 trgbuf[KMemMoveBufferSize];
       
   206 	TText8 *src=srcbuf, *trg=trgbuf;
       
   207 	TInt length=KMemMoveBufferSize;
       
   208 
       
   209 	switch (aMode)
       
   210 		{
       
   211 	case EMemMoveValid:
       
   212 		break;
       
   213 	case EMemMoveSourceNotAligned:
       
   214 		src=&srcbuf[1];
       
   215 		break;
       
   216 	case EMemMoveTargetNotAligned:
       
   217 		trg=&trgbuf[1];
       
   218 		break;
       
   219 	case EMemMoveLengthNotMultipleOf4:
       
   220 		length=KMemMoveBadLength;
       
   221 		break;
       
   222 		}
       
   223 	Mem::Move(trg,src,length);
       
   224 	return KErrNone;
       
   225 	}
       
   226 #endif //_DEBUG
       
   227 
       
   228 void testMemMove()
       
   229     {
       
   230     TText8 bufa[0x200];
       
   231     TText8 bufb[0x200];
       
   232 
       
   233     TInt ii,jj,kk;
       
   234 
       
   235     test.Next(_L("Mem::Move()"));
       
   236 //
       
   237 // Test various copying lengths and alignments and src before/after trg
       
   238 //
       
   239     TInt length;
       
   240     TChar a55(0x55);
       
   241     TChar aAA(0xaa);
       
   242 
       
   243     TUint8* addr;
       
   244 
       
   245     for(ii=0;ii<512;ii+=24)
       
   246         {
       
   247         for(jj=0;jj<512;jj+=24)
       
   248             {
       
   249             length=Min(512-jj,512-ii);
       
   250 
       
   251             Mem::Fill(&bufa[0],512,a55);
       
   252             Mem::Fill(&bufb[0],512,aAA);
       
   253 
       
   254             addr=Mem::Move(&bufa[ii],&bufb[jj],length);
       
   255 
       
   256             if(addr!=(&bufa[ii]+length))
       
   257                 {
       
   258                 PrintInfo(bufb,bufa);
       
   259                 test.Printf(_L("Mem::Copy returned incorrect address for %d bytes\n"),length);
       
   260                 failed=KErrGeneral;
       
   261                 //test.Getch();
       
   262                 return;
       
   263                 }
       
   264 
       
   265             for(kk=0;kk<512;kk++)
       
   266                 {
       
   267                 if(kk<ii && bufa[kk]!=0x55)
       
   268                     {
       
   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);
       
   271                     failed=KErrGeneral;
       
   272                     //test.Getch();
       
   273                     return;
       
   274                     }
       
   275                 if(kk>=ii && kk<(ii+length) && bufa[kk]!=0xaa)
       
   276                     {
       
   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);
       
   279                     failed=KErrGeneral;
       
   280                     //test.Getch();
       
   281                     return;
       
   282                     }
       
   283                 if(kk>=(ii+length) && bufa[kk]!=0x55)
       
   284                     {
       
   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);
       
   287                     failed=KErrGeneral;
       
   288                     //test.Getch();
       
   289                     return;
       
   290                     }
       
   291                 }
       
   292 
       
   293             Mem::Fill(&bufa[0],512,a55);
       
   294             Mem::Fill(&bufb[0],512,aAA);
       
   295 
       
   296             length=Min(512-jj,512-ii);
       
   297           
       
   298             addr=Mem::Move(&bufb[ii],&bufa[jj],length);
       
   299 
       
   300             if(addr!=(&bufb[ii]+length))
       
   301                 {
       
   302                 PrintInfo(bufa,bufb);
       
   303                 test.Printf(_L("Mem::Copy returned incorrect address for %d bytes\n"),length);
       
   304                 failed=KErrGeneral;
       
   305                 //test.Getch();
       
   306                 return;
       
   307                 }
       
   308 
       
   309             for(kk=0;kk<512;kk++)
       
   310                 {
       
   311                 if(kk<ii && bufb[kk]!=0xaa)
       
   312                     {
       
   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);
       
   315                     failed=KErrGeneral;
       
   316                     //test.Getch();
       
   317                     return;
       
   318                     }
       
   319                 if(kk>=ii && kk<(ii+length) && bufb[kk]!=0x55)
       
   320                     {
       
   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);
       
   323                     failed=KErrGeneral;
       
   324                     //test.Getch();
       
   325                     return;
       
   326                     }
       
   327                 if(kk>=(ii+length) && bufb[kk]!=0xaa)
       
   328                     {
       
   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);
       
   331                     failed=KErrGeneral;
       
   332                     //test.Getch();
       
   333                     return;
       
   334                     }
       
   335                 }
       
   336             };
       
   337         }
       
   338 #if defined(_DEBUG) && defined(__CPU_ARM)
       
   339     //
       
   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)
       
   343     //
       
   344     RThread clientThread;
       
   345     TRequestStatus status(KRequestPending);
       
   346 
       
   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);
       
   384 #endif //_DEBUG
       
   385     }
       
   386 
       
   387 void testMemFill()
       
   388     {
       
   389 
       
   390     TText8 bufa[0x200];
       
   391 
       
   392     TInt ii,jj,kk,pos,length;
       
   393 
       
   394     test.Next(_L("Mem::Fill()"));
       
   395 
       
   396     TChar a55(0x55);
       
   397     TChar aAA(0xaa);
       
   398 
       
   399     for(ii=0;ii<512-32;ii++)
       
   400         {
       
   401 		for(jj=0;jj<32;jj++)
       
   402 			{
       
   403 			Mem::Fill(&bufa[0],512,aAA);
       
   404 
       
   405 			pos=ii+jj;
       
   406 			length=512-32-ii;
       
   407 
       
   408 			Mem::Fill(&bufa[pos],length,a55);
       
   409 
       
   410 			for(kk=0;kk<512;kk++)
       
   411 				{
       
   412 				if(kk<(pos) && bufa[kk]!=0xaa)
       
   413 					{
       
   414 					test.Printf(_L("Mem::Fill failed filling %d bytes to bufa[%d] at byte %d (1)\n"),length,pos,kk);
       
   415 					failed=KErrGeneral;
       
   416 					//test.Getch();
       
   417 					return;
       
   418 					}
       
   419 				if(kk>=(pos) && kk<(pos+length) && bufa[kk]!=0x55)
       
   420 					{
       
   421 					test.Printf(_L("Mem::Fill failed filling %d bytes to bufa[%d] at byte %d (2)\n"),length,pos,kk);
       
   422 					failed=KErrGeneral;
       
   423 					//test.Getch();
       
   424 					return;
       
   425 					}
       
   426 				if(kk>=(pos+length) && bufa[kk]!=0xaa)
       
   427 					{
       
   428 					test.Printf(_L("Mem::Fill failed filling %d bytes to bufa[%d] at byte %d (3)\n"),length,pos,kk);
       
   429 					failed=KErrGeneral;
       
   430 					//test.Getch();
       
   431 					return;
       
   432 					}
       
   433 				}
       
   434 			}
       
   435 		}
       
   436 	}
       
   437 
       
   438 void testMemSwap()
       
   439     {
       
   440 
       
   441     test.Next(_L("Mem::Swap()"));
       
   442 
       
   443     TText8 bufa[0x200];
       
   444     TText8 bufb[0x200];
       
   445 
       
   446     TInt ii,jj,kk;
       
   447 
       
   448     TInt length;
       
   449     TChar a55(0x55);
       
   450     TChar aAA(0xaa);
       
   451 
       
   452     for(ii=24;ii<496;ii+=5)
       
   453         {
       
   454         for(jj=24;jj<496;jj+=3)
       
   455             {
       
   456             length=Min(496-jj,496-ii);
       
   457 
       
   458             Mem::Fill(&bufa[0],512,a55);
       
   459             Mem::Fill(&bufb[0],512,aAA);
       
   460 
       
   461             Mem::Swap(&bufa[ii],&bufb[jj],length);
       
   462 
       
   463             for(kk=0;kk<512;kk++)
       
   464                 {
       
   465                 if(kk<ii && bufa[kk]!=0x55)
       
   466                     {
       
   467                     test.Printf(_L("Mem::Swap failed. bufa[%d] contains wrong byte. Swap length=%d, ii=%d, jj=%d\n"),kk,length,ii,jj);
       
   468                     failed=KErrGeneral;
       
   469                     //test.Getch();
       
   470                     }
       
   471                 if(kk>=ii && kk<(ii+length) && bufa[kk]!=0xaa)
       
   472                     {
       
   473                     test.Printf(_L("Mem::Swap failed. bufa[%d] contains wrong byte. Swap length=%d, ii=%d, jj=%d\n"),kk,length,ii,jj);
       
   474                     failed=KErrGeneral;
       
   475                     //test.Getch();
       
   476                     }
       
   477                 if(kk>=(ii+length) && bufa[kk]!=0x55)
       
   478                     {
       
   479                     test.Printf(_L("Mem::Swap failed. bufa[%d] contains wrong byte. Swap length=%d, ii=%d, jj=%d\n"),kk,length,ii,jj);
       
   480                     failed=KErrGeneral;
       
   481                     //test.Getch();
       
   482                     }
       
   483                 if(kk<jj && bufb[kk]!=0xaa)
       
   484                     {
       
   485                     test.Printf(_L("Mem::Swap failed. bufb[%d] contains wrong byte. Swap length=%d, ii=%d, jj=%d\n"),kk,length,ii,jj);
       
   486                     failed=KErrGeneral;
       
   487                     //test.Getch();
       
   488                     }
       
   489                 if(kk>=jj && kk<(jj+length) && bufb[kk]!=0x55)
       
   490                     {
       
   491                     test.Printf(_L("Mem::Swap failed. bufb[%d] contains wrong byte. Swap length=%d, ii=%d, jj=%d\n"),kk,length,ii,jj);
       
   492                     failed=KErrGeneral;
       
   493                     //test.Getch();
       
   494                     }
       
   495                 if(kk>=(jj+length) && bufb[kk]!=0xaa)
       
   496                     {
       
   497                     test.Printf(_L("Mem::Swap failed. bufb[%d] contains wrong byte. Swap length=%d, ii=%d, jj=%d\n"),kk,length,ii,jj);
       
   498                     failed=KErrGeneral;
       
   499                     //test.Getch();
       
   500                     }
       
   501                 }
       
   502             }
       
   503         }
       
   504     }
       
   505 
       
   506 void testMemCompare()
       
   507     {
       
   508 
       
   509     TText8 bufa[516];
       
   510     TText8 bufb[516];
       
   511 
       
   512     test.Next(_L("Mem::Compare()"));
       
   513 
       
   514     TInt ii,jj,kk;
       
   515 
       
   516     TChar a55(0x55);
       
   517     TChar a11(0x11);
       
   518     TChar a99(0x99);
       
   519 
       
   520     TInt posi,posj,offi,offj,leni,lenj;
       
   521 
       
   522     for(ii=0;ii<512;ii+=2)
       
   523         {
       
   524         for(jj=0;jj<512;jj+=3)
       
   525             {
       
   526             Mem::Fill(&bufa[0],512,a55);
       
   527             Mem::Fill(&bufb[0],512,a55);
       
   528 
       
   529             posi=(511+ii)/2; // get a position somewhere in the middle
       
   530             posj=(511+jj)/2;
       
   531 
       
   532             bufa[posi]=0x12;
       
   533             bufb[posj]=0x12;
       
   534 
       
   535             offi=posi-ii;
       
   536             offj=posj-jj;
       
   537 
       
   538             leni=511-ii;
       
   539             lenj=511-jj;
       
   540 //
       
   541 // Make sure that outside the compare range is different
       
   542 //
       
   543             Mem::Fill(&bufa[ii+leni],4,a11);
       
   544             Mem::Fill(&bufb[jj+lenj],4,a99);
       
   545 
       
   546             kk=Mem::Compare(&bufa[ii],leni,&bufb[jj],lenj);
       
   547 
       
   548             if(offi==offj) // Wrong byte is at same offset
       
   549                 {
       
   550                 if(ii==jj) // Same lengths being compared, so should compare ok
       
   551                     {
       
   552                     if(kk!=0)
       
   553                         {
       
   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);
       
   557                         //test.Getch();
       
   558                         failed=KErrGeneral;
       
   559                         }
       
   560                     }
       
   561                 else // Different lengths, so should return difference of lengths
       
   562                     {
       
   563                     if(kk!=leni-lenj)
       
   564                         {
       
   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);
       
   568                         //test.Getch();
       
   569                         failed=KErrGeneral;
       
   570                         }
       
   571                     }
       
   572                 }
       
   573             if(offi!=offj) // Wrong byte at different offset
       
   574                 {
       
   575                 if(offi<offj && kk!=0x12-0x55)
       
   576                     {
       
   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);
       
   580                     //test.Getch();
       
   581                     failed=KErrGeneral;
       
   582                     }
       
   583                 if(offj<offi && kk!=0x55-0x12)
       
   584                     {
       
   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);
       
   588                     //test.Getch();
       
   589                     failed=KErrGeneral;
       
   590                     }
       
   591                 }
       
   592             }
       
   593         }
       
   594     }
       
   595 
       
   596 void testDesNum()
       
   597     {
       
   598 
       
   599     TBuf<36> aBuf;
       
   600 
       
   601     test.Next(_L("Des::Num(EHex)"));
       
   602     aBuf.Num(0x1b34a678,EHex);
       
   603     if(aBuf!=_L("1b34a678"))
       
   604         testFailed(_L("Des::Num(0x1b34a678,EHex)"));
       
   605     else
       
   606         {
       
   607         aBuf.Num(0x1234,EHex);
       
   608         if(aBuf!=_L("1234"))
       
   609             testFailed(_L("Des::Num(0x1234,EHex)"));
       
   610         }
       
   611 
       
   612     test.Next(_L("Des::Num(EDecimal)"));
       
   613     aBuf.Num(7462521,EDecimal);
       
   614     if(aBuf!=_L("7462521"))
       
   615         testFailed(_L("Des::Num(7462521,EDecimal)"));
       
   616     else
       
   617         {
       
   618         aBuf.Num(1234,EDecimal);
       
   619         if(aBuf!=_L("1234"))
       
   620             testFailed(_L("Des::Num(1234,EDecimal)"));
       
   621         }
       
   622 
       
   623     test.Next(_L("Des::Num(EOctal)"));
       
   624 
       
   625     aBuf.Num(03521,EOctal);
       
   626     if(aBuf!=_L("3521"))
       
   627         testFailed(_L("Des::Num(03521,EOctal)"));
       
   628     else
       
   629         {
       
   630         aBuf.Num(0706321,EOctal);
       
   631         if(aBuf!=_L("706321"))
       
   632             testFailed(_L("Des::Num(0706321,EOctal)"));
       
   633         }
       
   634 
       
   635     test.Next(_L("Des::Num(EBinary)"));
       
   636     aBuf.Num(0x92074625,EBinary);
       
   637     if(aBuf!=_L("10010010000001110100011000100101"))
       
   638         {
       
   639         testFailed(_L("Des::Num(0x92074625,EBinary)"));
       
   640         }
       
   641     else
       
   642         {
       
   643         aBuf.Num(0x4625,EBinary);
       
   644         if(aBuf!=_L("100011000100101"))
       
   645             testFailed(_L("Des::Num(0x4625,EBinary)"));
       
   646         }
       
   647     }
       
   648 
       
   649 void testDesNumUC()
       
   650     {
       
   651 
       
   652     TBuf<36> aBuf;
       
   653 
       
   654     test.Next(_L("Des::NumUC(EHex)"));
       
   655     aBuf.NumUC(0x1b3ca678,EHex);
       
   656     if(aBuf!=_L("1B3CA678"))
       
   657         testFailed(_L("Des::NumUC(0x1b3ca678,EHex)"));
       
   658     else
       
   659         {
       
   660         aBuf.NumUC(0x89abcdef,EHex);
       
   661         if(aBuf!=_L("89ABCDEF"))
       
   662             testFailed(_L("Des::NumUC(0x89abcdef,EHex)"));
       
   663         }
       
   664 
       
   665     test.Next(_L("Des::NumUC(EDecimal)"));
       
   666     aBuf.NumUC(7462521,EDecimal);
       
   667     if(aBuf!=_L("7462521"))
       
   668         testFailed(_L("Des::NumUC(7462521,EDecimal)"));
       
   669     else
       
   670         {
       
   671         aBuf.NumUC(1234,EDecimal);
       
   672         if(aBuf!=_L("1234"))
       
   673             testFailed(_L("Des::NumUC(1234,EDecimal)"));
       
   674         }
       
   675 
       
   676     test.Next(_L("Des::NumUC(EOctal)"));
       
   677 
       
   678     aBuf.NumUC(03521,EOctal);
       
   679     if(aBuf!=_L("3521"))
       
   680         testFailed(_L("Des::NumUC(03521,EOctal)"));
       
   681     else
       
   682         {
       
   683         aBuf.NumUC(0706321,EOctal);
       
   684         if(aBuf!=_L("706321"))
       
   685             testFailed(_L("Des::NumUC(0706321,EOctal)"));
       
   686         }
       
   687 
       
   688     test.Next(_L("Des::NumUC(EBinary)"));
       
   689     aBuf.NumUC(0x92074625,EBinary);
       
   690     if(aBuf!=_L("10010010000001110100011000100101"))
       
   691         {
       
   692         testFailed(_L("Des::NumUC(0x92074625,EBinary)"));
       
   693         }
       
   694     else
       
   695         {
       
   696         aBuf.NumUC(0x4625,EBinary);
       
   697         if(aBuf!=_L("100011000100101"))
       
   698             testFailed(_L("Des::NumUC(0x4625,EBinary)"));
       
   699         }
       
   700     }
       
   701 
       
   702 void testDivTen(TInt aInc)
       
   703 //
       
   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.
       
   707 //
       
   708     {
       
   709 
       
   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
       
   714 
       
   715     TUint ar=68417814/10;
       
   716     TInt br=-48910759/10;
       
   717     TInt cr=2147483647/10;
       
   718     TUint dr=3147484647u/10;
       
   719 
       
   720     a=a/10;
       
   721     b=b/10;
       
   722     c=c/10;
       
   723     d=d/10;
       
   724 
       
   725     test.Next(_L("Integer divide by 10"));
       
   726 
       
   727     if(a!=ar)
       
   728         {
       
   729         test.Printf(_L("68417814/10 gives %u\n"),a);
       
   730         failed=KErrGeneral;
       
   731         }
       
   732     if(b!=br)
       
   733         {
       
   734         test.Printf(_L("-48910759/10 gives %d\n"),b);
       
   735         failed=KErrGeneral;
       
   736         }
       
   737     if(c!=cr)
       
   738         {
       
   739         test.Printf(_L("2147483647/10 gives %d\n"),c);
       
   740         failed=KErrGeneral;
       
   741         }
       
   742     if(d!=dr)
       
   743         {
       
   744         test.Printf(_L("3147484647/10 gives %u\n"),d);
       
   745         failed=KErrGeneral;
       
   746         }
       
   747     }
       
   748 
       
   749 void testDivSeven(TInt aInc)
       
   750 //
       
   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.
       
   754 //
       
   755     {
       
   756 
       
   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
       
   761 
       
   762     TUint ar=68417814/7;
       
   763     TInt br=-48910759/7;
       
   764     TInt cr=2147483647/7;
       
   765     TUint dr=3147484647u/7;
       
   766 
       
   767     a=a/7;
       
   768     b=b/7;
       
   769     c=c/7;
       
   770     d=d/7;
       
   771 
       
   772     test.Next(_L("Integer divide by 7"));
       
   773 
       
   774     if(a!=ar)
       
   775         {
       
   776         test.Printf(_L("68417814/7 gives %u\n"),a);
       
   777         failed=KErrGeneral;
       
   778         }
       
   779     if(b!=br)
       
   780         {
       
   781         test.Printf(_L("-48910759/7 gives %d\n"),b);
       
   782         failed=KErrGeneral;
       
   783         }
       
   784     if(c!=cr)
       
   785         {
       
   786         test.Printf(_L("2147483647/7 gives %d\n"),c);
       
   787         failed=KErrGeneral;
       
   788         }
       
   789     if(d!=dr)
       
   790         {
       
   791         test.Printf(_L("3147484647/7 gives %u\n"),d);
       
   792         failed=KErrGeneral;
       
   793         }
       
   794     }
       
   795 
       
   796 void testDivFive(TInt aInc)
       
   797 //
       
   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.
       
   801 //
       
   802     {
       
   803 
       
   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
       
   808 
       
   809     TUint ar=68417814/5;
       
   810     TInt br=-48910759/5;
       
   811     TInt cr=2147483647/5;
       
   812     TUint dr=3147484647u/5;
       
   813 
       
   814     a=a/5;
       
   815     b=b/5;
       
   816     c=c/5;
       
   817     d=d/5;
       
   818 
       
   819     test.Next(_L("Integer divide by 5"));
       
   820 
       
   821     if(a!=ar)
       
   822         {
       
   823         test.Printf(_L("68417814/5 gives %u\n"),a);
       
   824         failed=KErrGeneral;
       
   825         }
       
   826     if(b!=br)
       
   827         {
       
   828         test.Printf(_L("-48910759/5 gives %d\n"),b);
       
   829         failed=KErrGeneral;
       
   830         }
       
   831     if(c!=cr)
       
   832         {
       
   833         test.Printf(_L("2147483647/5 gives %d\n"),c);
       
   834         failed=KErrGeneral;
       
   835         }
       
   836     if(d!=dr)
       
   837         {
       
   838         test.Printf(_L("3147484647/5 gives %u\n"),d);
       
   839         failed=KErrGeneral;
       
   840         }
       
   841     }
       
   842 
       
   843 void testDivSixteen(TInt aInc)
       
   844 //
       
   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.
       
   848 //
       
   849     {
       
   850 
       
   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
       
   855 
       
   856     TUint ar=68417814/16;
       
   857     TInt br=-48910759/16;
       
   858     TInt cr=2147483647/16;
       
   859     TUint dr=3147484647u/16;
       
   860 
       
   861     a=a/16;
       
   862     b=b/16;
       
   863     c=c/16;
       
   864     d=d/16;
       
   865 
       
   866     test.Next(_L("Integer divide by 16"));
       
   867 
       
   868     if(a!=ar)
       
   869         {
       
   870         test.Printf(_L("68417814/16 gives %u\n"),a);
       
   871         failed=KErrGeneral;
       
   872         }
       
   873     if(b!=br)
       
   874         {
       
   875         test.Printf(_L("-48910759/16 gives %d\n"),b);
       
   876         failed=KErrGeneral;
       
   877         }
       
   878     if(c!=cr)
       
   879         {
       
   880         test.Printf(_L("2147483647/16 gives %d\n"),c);
       
   881         failed=KErrGeneral;
       
   882         }
       
   883     if(d!=dr)
       
   884         {
       
   885         test.Printf(_L("3147484647/16 gives %u\n"),d);
       
   886         failed=KErrGeneral;
       
   887         }
       
   888     }
       
   889 
       
   890 void testModulo(TInt aInc)
       
   891     {
       
   892 
       
   893     test.Next(_L("Integer modulo"));
       
   894 
       
   895     TInt ii,kk;
       
   896 
       
   897     for(kk=1;kk<32;kk++)
       
   898         {
       
   899         for(ii=0;ii<kk;ii++)
       
   900             {
       
   901             TInt jj=(kk*73)+aInc+ii;
       
   902 
       
   903             if((jj%kk)!=ii)
       
   904                 {
       
   905                 test.Printf(_L("%d mod %d gives %d\n"),jj,kk,jj%kk);
       
   906                 failed=KErrGeneral;
       
   907                 }
       
   908             }
       
   909         }
       
   910     }
       
   911 
       
   912 TInt E32Main()
       
   913 //
       
   914 // Benchmark for Mem functions
       
   915 //
       
   916     {
       
   917 
       
   918     failed=KErrNone;
       
   919 
       
   920     test.Title();
       
   921     test.Start(_L("T_FUNC"));
       
   922 
       
   923     testMemCopy();
       
   924     testMemMove();
       
   925     testMemFill();
       
   926     testMemSwap();
       
   927     testMemCompare();
       
   928     testDesNum();
       
   929     testDesNumUC();
       
   930     testDivTen(0);
       
   931     testDivSixteen(0);
       
   932     testDivFive(0);
       
   933     testDivSeven(0);
       
   934     testModulo(0);
       
   935 
       
   936 	test(failed==KErrNone);
       
   937 
       
   938     test.End();
       
   939 	return(KErrNone);
       
   940     }
       
   941