genericopenlibs/cstdlib/TSTLIB/TMISC.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 1997-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 "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 // This test has been converted from it original TMISC.C .
       
    15 // The original was a manaul test and didn't work!
       
    16 // Test code for bsearch, qsort etc. These are all functions which work directly
       
    17 // without using the MSystemInterface - as such none of them use errno to indicate
       
    18 // errors.
       
    19 // 
       
    20 //
       
    21 
       
    22 #include <e32test.h>
       
    23 #include <e32svr.h>
       
    24 
       
    25 #include <stdarg.h>
       
    26 #include <stdlib.h>
       
    27 #include <stdio.h>
       
    28 #include <math.h>  /* HUGE_VAL */
       
    29 #include <errno.h> /* ERANGE */
       
    30 #include "ESTLIB.H"
       
    31 
       
    32 // Defined in ctest.h but contains lots of other declarations we don't want
       
    33 #define test_Data		struct __testdata __td	/* declares global variable __td */
       
    34 
       
    35 //
       
    36 // Globals
       
    37 
       
    38 static RTest TheTest(_L("TMisc"));
       
    39 
       
    40 //
       
    41 //
       
    42 //Test macroses and functions
       
    43 
       
    44 static void Check(TInt aValue, TInt aLine)
       
    45 	{
       
    46 	if(!aValue)
       
    47 		{
       
    48 		TheTest(EFalse, aLine);
       
    49 		}
       
    50 	}
       
    51 static  void Check(TInt aValue, TInt aExpected, TInt aLine)
       
    52 	{
       
    53 	if(aValue != aExpected)
       
    54 		{
       
    55 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
       
    56 		TheTest(EFalse, aLine);
       
    57 		}
       
    58 	}
       
    59 
       
    60 #define TEST(arg) ::Check((arg), __LINE__)
       
    61 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
       
    62 
       
    63 //
       
    64 // Tests
       
    65 
       
    66 
       
    67 /* random numbers */
       
    68 
       
    69 void random_numbers()
       
    70 	{
       
    71 	int i,r;
       
    72 	int rs[20];
       
    73 
       
    74 	TheTest.Next(_L("Random numbers"));
       
    75 
       
    76 	for (i=0; i<20; i++)
       
    77 		{
       
    78 		r = rand();
       
    79 		printf("%d ", r);
       
    80 		}
       
    81 	printf("\n");
       
    82 	TheTest.Next(_L("Using srand()"));
       
    83 	srand(12345678);
       
    84 	for (i=0; i<20; i++)
       
    85 		{
       
    86 		rs[i]=rand();
       
    87 		printf("%d ",rs[i]);
       
    88 		}
       
    89 	printf("\n");
       
    90 	srand(12345678);
       
    91 	for (i=0; i<20; i++)
       
    92 		TEST(rand()==rs[i]);
       
    93 	}
       
    94 
       
    95 /* Sorting */
       
    96 
       
    97 typedef struct {
       
    98 	int a;
       
    99 	int b;
       
   100 	int c;
       
   101 	} sort_item;
       
   102 
       
   103 int compare_a (const void *left, const void *right)
       
   104 	{
       
   105 	sort_item *lp = (sort_item *)left;
       
   106 	sort_item *rp = (sort_item *)right;
       
   107 
       
   108 	if (lp->a < rp->a) return -1;
       
   109 	if (lp->a > rp->a) return  1;
       
   110 	return 0;
       
   111 	}
       
   112 
       
   113 int compare_ba (const void *left, const void *right)
       
   114 	{
       
   115 	sort_item *lp = (sort_item *)left;
       
   116 	sort_item *rp = (sort_item *)right;
       
   117 
       
   118 	if (lp->b < rp->b) return -1;
       
   119 	if (lp->b > rp->b) return  1;
       
   120 	/* b's are equal, sort on a's */
       
   121 	if (lp->a < rp->a) return -1;
       
   122 	if (lp->a > rp->a) return  1;
       
   123 	return 0;
       
   124 	}
       
   125 
       
   126 #define NITEMS 200
       
   127 sort_item list[NITEMS];
       
   128 
       
   129 void validate(sort_item *p)
       
   130 	{
       
   131 	int i;
       
   132 	for (i=0; i<NITEMS; i++,p++)
       
   133 		{
       
   134 		if (p->a==9)
       
   135 			continue;
       
   136 		TEST((p->a/4)+p->b == 5000);
       
   137 		TEST((p->a*p->a)/4 == p->c);
       
   138 		}
       
   139 	}
       
   140 
       
   141 void sorting()
       
   142 	{
       
   143 	int i;
       
   144 	TheTest.Next(_L("Quicksort"));
       
   145 	for (i=1; i<NITEMS; i++)
       
   146 		{
       
   147 		list[i].a = 2*i;
       
   148 		list[i].b = 5000-(i/2);
       
   149 		list[i].c = i*i;
       
   150 		}
       
   151 
       
   152 	/* Sort into increasing order of a. Want item 0 to sort to position 4 */
       
   153 	list[0].a = 9;
       
   154 	list[0].b = 10;
       
   155 	list[0].c = 11;
       
   156 
       
   157 	qsort(list, NITEMS, sizeof(list[0]), compare_a);
       
   158 	TEST(list[4].c==11);
       
   159 	validate(list);
       
   160 
       
   161 	/* Resort into increasing order of b followed by a. Want item 0 to sort to position 5 */
       
   162 	list[0].a = 9;
       
   163 	list[0].b = 5000-((199-4)/2);
       
   164 	list[0].c = 13;
       
   165 
       
   166 	qsort(list, NITEMS, sizeof(list[0]), compare_ba);
       
   167 	TEST(list[5].c==13);
       
   168 	validate(list);
       
   169 	}
       
   170 
       
   171 void searching()
       
   172 	{
       
   173 	int i;
       
   174 	sort_item *answer;
       
   175 	sort_item not_there;
       
   176 	TheTest.Next(_L("Binary search for existing items"));
       
   177 	for (i=0; i<NITEMS; i++)
       
   178 		{
       
   179 		answer = (sort_item*)bsearch(&list[i],list,NITEMS,sizeof(list[0]),compare_ba);
       
   180 		TEST(answer==&list[i]);
       
   181 		}
       
   182 	TheTest.Next(_L("Binary search for missing items"));
       
   183 	for (i=0; i<NITEMS; i++)
       
   184 		{
       
   185 		not_there = list[i];
       
   186 		not_there.a++;
       
   187 		answer = (sort_item*)bsearch(&not_there,list,NITEMS,sizeof(list[0]),compare_ba);
       
   188 		if (answer!=NULL)
       
   189 			TEST(!compare_ba(answer,&not_there));
       
   190 		}
       
   191 	for (i=0; i<NITEMS; i++)
       
   192 		{
       
   193 		not_there = list[i];
       
   194 		not_there.a--;
       
   195 		answer = (sort_item*)bsearch(&not_there,list,NITEMS,sizeof(list[0]),compare_ba);
       
   196 		if (answer!=NULL)
       
   197 			TEST(!compare_ba(answer,&not_there));
       
   198 		}
       
   199 	}
       
   200 
       
   201 void sscanf_test()
       
   202 	{
       
   203 	int in[4], i;
       
   204 	TheTest.Next(_L("Simple SSCANF tests"));
       
   205 
       
   206 	in[0] = in[1] = in[2] = in[3] = 6789;
       
   207 	i = sscanf("1.2.3.4", "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
       
   208 	TEST (in[0]==1);
       
   209 	TEST (in[1]==2);
       
   210 	TEST (in[2]==3);
       
   211 	TEST (in[3]==4);
       
   212 	TEST (i==4);
       
   213 
       
   214 	i = sscanf("194.223.254.9", "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
       
   215 	TEST (in[0]==194);
       
   216 	TEST (in[1]==223);
       
   217 	TEST (in[2]==254);
       
   218 	TEST (in[3]==9);
       
   219 	TEST (i==4);
       
   220 
       
   221 	i = sscanf("17.183.hello.11", "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
       
   222 	TEST (in[0]==17);
       
   223 	TEST (in[1]==183);
       
   224 	TEST (in[2]==254);
       
   225 	TEST (in[3]==9);
       
   226 	TEST (i==2);
       
   227 	}
       
   228 
       
   229 /* setjmp, longjmp */
       
   230 
       
   231 #include <setjmp.h>
       
   232 #include <string.h>
       
   233 
       
   234 jmp_buf jbufs[4];
       
   235 int count=0;
       
   236 
       
   237 static int getStackPointer(void)
       
   238 	{
       
   239 	static char there;
       
   240 	char here;
       
   241 	return &here-&there;
       
   242 	}
       
   243 
       
   244 static int getgetStackPointer(void)
       
   245 	{
       
   246 	int n=4;	// local variable to cause change to stack pointer
       
   247 	n+=getStackPointer();
       
   248 	if (n==0)
       
   249 		n=getStackPointer()+37;	// this never happens - it's just to defeat the optimiser
       
   250 	return (n-4);
       
   251 	}
       
   252 
       
   253 void jmp_nest(int n)
       
   254 	{
       
   255 	int m=n+100;	// local variable to induce a stack frame
       
   256 	if (n>0)
       
   257 		jmp_nest(n-1);
       
   258 	longjmp(jbufs[3],m);
       
   259 	}
       
   260 
       
   261 #ifdef __VC32__
       
   262 #pragma optimize( "", off )		//stop the compiler breaking this TEST
       
   263 #endif
       
   264 
       
   265 void setjmp_longjmp()
       
   266 	{
       
   267 	volatile int i,r,j,k,l,m; // volatile to rid us of warnings
       
   268 	volatile int sp1,sp2;
       
   269 	TheTest.Next(_L("Setjmp"));
       
   270 
       
   271 	sp1=getStackPointer();
       
   272 	sp2=getgetStackPointer();
       
   273 	TEST(sp1!=sp2);	// call from deeper function nesting should give a different answer
       
   274 
       
   275 	memset(jbufs[0],0,sizeof(jmp_buf));
       
   276 	memset(jbufs[1],0,sizeof(jmp_buf));
       
   277 	memset(jbufs[2],0,sizeof(jmp_buf));
       
   278 
       
   279 	r=setjmp(jbufs[0]);
       
   280 	TEST(r==0);
       
   281 
       
   282 	for (i=0,j=1,k=2,l=3,m=4; i<3; i++,j*=3,k+=j,l*=m,m-=2)
       
   283 		{
       
   284 		r=setjmp(jbufs[i]);
       
   285 		TEST(r==0);
       
   286 		TEST(j>i);
       
   287 		}
       
   288 	r=memcmp(jbufs[0],jbufs[2], sizeof(jmp_buf));
       
   289 
       
   290 	if (r!=0)
       
   291 		{
       
   292 		RDebug::Print(_L("  Test code appears to be using preserved registers (a good thing)\n"));
       
   293 		RDebug::Print(_L("  buf @   %08x %08x\n"), jbufs[0], jbufs[2]);
       
   294 		for (i=0;i<16;i++)
       
   295 			{
       
   296 			if (jbufs[0][i] != jbufs[2][i])
       
   297 				RDebug::Print(_L("  buf+%02d: %08x %08x\n"), i*4, jbufs[0][i], jbufs[2][i]);
       
   298 			}
       
   299 		}
       
   300 	else
       
   301 		RDebug::Print(_L("  Test code appears not to exercise preserved registers\n"));
       
   302 
       
   303 	r=setjmp(jbufs[0]);
       
   304 	TEST(r==0);
       
   305 	r=setjmp(jbufs[2]);
       
   306 	TEST(r==0);
       
   307 	r=memcmp(jbufs[0],jbufs[2], sizeof(jmp_buf));
       
   308 	TEST(r!=0);	/* must change the return address! */
       
   309 
       
   310 	TheTest.Next(_L("Setjmp and longjmp"));
       
   311 
       
   312 	r=setjmp(jbufs[0]);
       
   313 	if (r==0)
       
   314 		{
       
   315 		TEST(count==0);
       
   316 		count++;
       
   317 		longjmp(jbufs[0],7);
       
   318 		}
       
   319 	else if (r==7)
       
   320 		{
       
   321 		TEST(count==1);
       
   322 		count++;
       
   323 		longjmp(jbufs[0],3);
       
   324 		}
       
   325 	else if (r==3)
       
   326 		{
       
   327 		TEST(count==2);
       
   328 		count++;
       
   329 		longjmp(jbufs[0],0);	/* 0 must be turned into 1 */
       
   330 		}
       
   331 	else
       
   332 		{
       
   333 		TEST(r==1);
       
   334 		TEST(count==3);
       
   335 		count++;
       
   336 		}
       
   337 
       
   338 	sp1=getStackPointer();
       
   339 	r=setjmp(jbufs[3]);
       
   340 	if (r==0)
       
   341 		{
       
   342 		sp2=getStackPointer();
       
   343 		TEST(sp1==sp2);
       
   344 		longjmp(jbufs[3],1);		// simple setjmp/longjmp
       
   345 		}
       
   346 	else if (r==1)
       
   347 		{
       
   348 		sp2=getStackPointer();
       
   349 		TEST(sp1==sp2);
       
   350 		jmp_nest(20);			// more complex example
       
   351 		}
       
   352 	else
       
   353 		{
       
   354 		TEST(r==100);
       
   355 		sp2=getStackPointer();
       
   356 		TEST(sp1==sp2);
       
   357 		}
       
   358 	}
       
   359 
       
   360 #ifdef __VC32__
       
   361 #pragma optimize( "", on )	//stop the compiler breaking this TEST
       
   362 #endif
       
   363 
       
   364 /* case-insensitive comparison */
       
   365 
       
   366 #include <string.h>
       
   367 char agrave[2]= {191,0};
       
   368 
       
   369 void casecmp()
       
   370 	{
       
   371 	int r;
       
   372 	char *s1,*s2;
       
   373 	TheTest.Next(_L("Case-insensitive string comparison"));
       
   374 
       
   375 	s1="abcd";
       
   376 	r=strcasecmp(s1,s1);
       
   377 	TEST(r==0);
       
   378 
       
   379 	s2="abcde";
       
   380 	r=strcasecmp(s1,s2);
       
   381 	TEST(r<0);
       
   382 
       
   383 	r=strcasecmp(s2,s1);
       
   384 	TEST(r>0);
       
   385 
       
   386 	r=strncasecmp(s1,s2,10);
       
   387 	TEST(r<0);
       
   388 
       
   389 	r=strncasecmp(s1,s2,4);
       
   390 	TEST(r==0);
       
   391 
       
   392 	s2="ABcD";
       
   393 	r=strcmp(s1,s2);
       
   394 	TEST(r!=0);
       
   395 	r=strcasecmp(s1,s2);
       
   396 	TEST(r==0);
       
   397 
       
   398 #if 0
       
   399 	/* Need some way to set up a proper folding example */
       
   400 	r=strncasecmp(s2,agrave,1);
       
   401 	TEST(r==0);
       
   402 #endif
       
   403 	}
       
   404 
       
   405 void arguments(int argc, char *argv[])
       
   406 	{
       
   407 	int i;
       
   408 
       
   409 	TheTest.Next(_L("Command line arguments"));
       
   410 	TEST(argc>0);
       
   411 	TEST(argv!=0);
       
   412 	printf("  argc=%d\r\n", argc);
       
   413 	for (i=0; i<argc; i++)
       
   414 		{
       
   415 		int j;
       
   416 		int length=strlen(argv[i]);
       
   417 		TEST(argv[i]!=0);
       
   418 		printf("  argv[%d]= ", i);
       
   419 		for (j=0;j<4;j++)
       
   420 			{
       
   421 			printf("%02x ", ((unsigned char *)argv[i])[j]);
       
   422 			if (argv[i][j]=='\0')
       
   423 				break;
       
   424 			}
       
   425 		for (;j<3;j++)
       
   426 			printf("   ");
       
   427 		printf(" \"%s\" length %d\r\n", argv[i], length);
       
   428 		}
       
   429 	}
       
   430 
       
   431 struct double_conv {
       
   432 	char* source;
       
   433 	double answer;
       
   434 	int tail_offset;
       
   435 	};
       
   436 
       
   437 struct double_conv strtods[] = {
       
   438 	{ "1",		1.0,	1 },
       
   439 	{ "0.1",	0.1,	3 },
       
   440 	{ ".1",		0.1,	2 },
       
   441 	{ " 0.1",	0.1,	4 },
       
   442 	{ " 1 ",	1.0,	2 },
       
   443 	{ "1x",		1.0,	1 },
       
   444 	{ "x",		0.0,	0 },
       
   445 	{ "1.5.3",	1.5,	3 },
       
   446 	{ "1.0e-1m solid red;}...",	0.1,	6 },
       
   447 	{ "1.0e2blah",	100.0,	5 },
       
   448 	{ "3.1415e2blah",	314.15,	8 },
       
   449 	{ "0.2em solid red;}...",	0.2,	3 },
       
   450 	{ "0.2e5m solid red;}...",	20000.0,	5 },
       
   451 	{ 0 }
       
   452 	};
       
   453 
       
   454 void strtod_test()
       
   455 	{
       
   456 	double d;
       
   457 	char* p;
       
   458 	struct double_conv *dc;
       
   459 
       
   460 	TheTest.Next(_L("text to double conversion"));
       
   461 	for (dc=strtods; dc->source; dc++)
       
   462 		{
       
   463 		d=strtod(dc->source,NULL);
       
   464 		TEST(d==dc->answer);
       
   465 		}
       
   466 	for (dc=strtods; dc->source; dc++)
       
   467 		{
       
   468 		p=(char*)1;
       
   469 		d=strtod(dc->source,&p);
       
   470 		TEST(d==dc->answer);
       
   471 		TEST(p==dc->source+dc->tail_offset);
       
   472 		}
       
   473 
       
   474 	/* overflow positive number */
       
   475 	d=strtod("9e9999", NULL);
       
   476 	TEST(d==HUGE_VAL && errno==ERANGE);
       
   477 
       
   478 	/* overflow negative number */
       
   479 	d=strtod("-9e9999", NULL);
       
   480 	TEST(d==-HUGE_VAL && errno==ERANGE);
       
   481 
       
   482 	/* underflow number */
       
   483 	d=strtod("9e-9999", NULL);
       
   484 	TEST(d==0 && errno==ERANGE);
       
   485 	d=strtod("-9e-9999", NULL);
       
   486 	TEST(d==0 && errno==ERANGE);
       
   487 }
       
   488 
       
   489 #define TEST_8	0x88
       
   490 #define TEST_16	0x1617
       
   491 #define TEST_32	0x32333435
       
   492 
       
   493 #define TEST_LIST \
       
   494 	TEST_8,  TEST_16, TEST_32, \
       
   495 	TEST_8,  TEST_32, TEST_16, \
       
   496 	TEST_16, TEST_8,  TEST_32, \
       
   497 	TEST_16, TEST_32, TEST_8,  \
       
   498 	TEST_32, TEST_8,  TEST_16, \
       
   499 	TEST_32, TEST_16, TEST_8
       
   500 
       
   501 /*
       
   502    Suppressing RVCT compiler warning (for char/short/long types):
       
   503    Warning:  #1256-D: "unsigned char" would have been promoted to "int" when passed through the ellipsis parameter;
       
   504    use the latter type instead.
       
   505           b = va_arg(ap, unsigned char);
       
   506               ^
       
   507 */
       
   508 
       
   509 #pragma diag_suppress 1256
       
   510 
       
   511 // The above RCVT warning is a compiler error in GCC. Below is the GGC friendly version of the function.
       
   512 #ifdef __GCC32__
       
   513 
       
   514 void va_va_bwlblwwblwlblbwlwb(va_list ap)
       
   515 	{
       
   516 	unsigned long l;
       
   517 	unsigned short w;
       
   518 	unsigned char b;
       
   519 
       
   520 	l=0; w=0; b=0;
       
   521 	b = va_arg(ap, int);
       
   522 	TEST(b==TEST_8);
       
   523 	w = va_arg(ap, int);
       
   524 	TEST(w==TEST_16);
       
   525 	l = va_arg(ap, unsigned long);
       
   526 	TEST(l==TEST_32);
       
   527 
       
   528 	l=0; w=0; b=0;
       
   529 	b = va_arg(ap, int);
       
   530 	TEST(b==TEST_8);
       
   531 	l = va_arg(ap, unsigned long);
       
   532 	TEST(l==TEST_32);
       
   533 	w = va_arg(ap, int);
       
   534 	TEST(w==TEST_16);
       
   535 
       
   536 	l=0; w=0; b=0;
       
   537 	w = va_arg(ap, int);
       
   538 	TEST(w==TEST_16);
       
   539 	b = va_arg(ap, int);
       
   540 	TEST(b==TEST_8);
       
   541 	l = va_arg(ap, unsigned long);
       
   542 	TEST(l==TEST_32);
       
   543 
       
   544 	l=0; w=0; b=0;
       
   545 	w = va_arg(ap,int);
       
   546 	TEST(w==TEST_16);
       
   547 	l = va_arg(ap, unsigned long);
       
   548 	TEST(l==TEST_32);
       
   549 	b = va_arg(ap, int);
       
   550 	TEST(b==TEST_8);
       
   551 
       
   552 	l=0; w=0; b=0;
       
   553 	l = va_arg(ap, unsigned long);
       
   554 	TEST(l==TEST_32);
       
   555 	b = va_arg(ap, int);
       
   556 	TEST(b==TEST_8);
       
   557 	w = va_arg(ap, int);
       
   558 	TEST(w==TEST_16);
       
   559 
       
   560 	l=0; w=0; b=0;
       
   561 	l = va_arg(ap, unsigned long);
       
   562 	TEST(l==TEST_32);
       
   563 	w = va_arg(ap, int);
       
   564 	TEST(w==TEST_16);
       
   565 	b = va_arg(ap, int);
       
   566 	TEST(b==TEST_8);
       
   567 	}
       
   568 
       
   569 
       
   570 #else
       
   571 
       
   572 void va_va_bwlblwwblwlblbwlwb(va_list ap)
       
   573 	{
       
   574 	unsigned long l;
       
   575 	unsigned short w;
       
   576 	unsigned char b;
       
   577 
       
   578 	l=0; w=0; b=0;
       
   579 	b = va_arg(ap, unsigned char);
       
   580 	TEST(b==TEST_8);
       
   581 	w = va_arg(ap, unsigned short);
       
   582 	TEST(w==TEST_16);
       
   583 	l = va_arg(ap, unsigned long);
       
   584 	TEST(l==TEST_32);
       
   585 
       
   586 	l=0; w=0; b=0;
       
   587 	b = va_arg(ap, unsigned char);
       
   588 	TEST(b==TEST_8);
       
   589 	l = va_arg(ap, unsigned long);
       
   590 	TEST(l==TEST_32);
       
   591 	w = va_arg(ap, unsigned short);
       
   592 	TEST(w==TEST_16);
       
   593 
       
   594 	l=0; w=0; b=0;
       
   595 	w = va_arg(ap, unsigned short);
       
   596 	TEST(w==TEST_16);
       
   597 	b = va_arg(ap, unsigned char);
       
   598 	TEST(b==TEST_8);
       
   599 	l = va_arg(ap, unsigned long);
       
   600 	TEST(l==TEST_32);
       
   601 
       
   602 	l=0; w=0; b=0;
       
   603 	w = va_arg(ap, unsigned short);
       
   604 	TEST(w==TEST_16);
       
   605 	l = va_arg(ap, unsigned long);
       
   606 	TEST(l==TEST_32);
       
   607 	b = va_arg(ap, unsigned char);
       
   608 	TEST(b==TEST_8);
       
   609 
       
   610 	l=0; w=0; b=0;
       
   611 	l = va_arg(ap, unsigned long);
       
   612 	TEST(l==TEST_32);
       
   613 	b = va_arg(ap, unsigned char);
       
   614 	TEST(b==TEST_8);
       
   615 	w = va_arg(ap, unsigned short);
       
   616 	TEST(w==TEST_16);
       
   617 
       
   618 	l=0; w=0; b=0;
       
   619 	l = va_arg(ap, unsigned long);
       
   620 	TEST(l==TEST_32);
       
   621 	w = va_arg(ap, unsigned short);
       
   622 	TEST(w==TEST_16);
       
   623 	b = va_arg(ap, unsigned char);
       
   624 	TEST(b==TEST_8);
       
   625 	}
       
   626 
       
   627 #endif
       
   628 
       
   629 
       
   630 #pragma diag_default 1256
       
   631 
       
   632 void va_lbwlblwwblwlblbwlwb(unsigned long x, ...)
       
   633 	{
       
   634 	va_list ap;
       
   635 	TEST(x==TEST_32);
       
   636 	va_start(ap, x);
       
   637 
       
   638 	va_va_bwlblwwblwlblbwlwb(ap);
       
   639 	}
       
   640 
       
   641 void va_args_test()
       
   642 	{
       
   643 	TheTest.Next(_L("variadic functions"));
       
   644 
       
   645 	va_lbwlblwwblwlblbwlwb( TEST_32, TEST_LIST);
       
   646 	}
       
   647 
       
   648 void sprintf_test()
       
   649 	{
       
   650 	char buf[256];
       
   651 	char* hw = "hello, world";
       
   652 
       
   653 	TheTest.Next(_L("sprintf function"));
       
   654 
       
   655 	/* WAP TOG Defect */
       
   656     sprintf(buf, "%.*f", 0, 10.1234);
       
   657 	TEST(strcmp(buf, "10")==0);
       
   658 	sprintf(buf, "%.0f", 10.1234);
       
   659 	TEST(strcmp(buf, "10")==0);
       
   660 
       
   661 	/* From K&R */
       
   662 	sprintf(buf, ":%s:", hw);
       
   663 	TEST(strcmp(buf, ":hello, world:")==0);
       
   664 
       
   665 	sprintf(buf, ":%10s:", hw);
       
   666 	TEST(strcmp(buf, ":hello, world:")==0);
       
   667 
       
   668 	sprintf(buf, ":%.10s:", hw);
       
   669 	TEST(strcmp(buf, ":hello, wor:")==0);
       
   670 
       
   671 	sprintf(buf, ":%-10s:", hw);
       
   672 	TEST(strcmp(buf, ":hello, world:")==0);
       
   673 
       
   674 	sprintf(buf, ":%.15s:", hw);
       
   675 	TEST(strcmp(buf, ":hello, world:")==0);
       
   676 
       
   677 	sprintf(buf, ":%-15s:", hw);
       
   678 	TEST(strcmp(buf, ":hello, world   :")==0);
       
   679 
       
   680 	sprintf(buf, ":%15.10s:", hw);
       
   681 	TEST(strcmp(buf, ":     hello, wor:")==0);
       
   682 
       
   683 	sprintf(buf, ":%-15.10s:", hw);
       
   684 	TEST(strcmp(buf, ":hello, wor     :")==0);
       
   685 	}
       
   686 
       
   687 
       
   688 static void MainL()
       
   689 	{
       
   690 	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-STDLIB-LEGACY-TMISC-0001 TMISC tests "));
       
   691 
       
   692 	random_numbers();
       
   693 	sorting();
       
   694 	searching();
       
   695 	setjmp_longjmp();
       
   696 	casecmp();
       
   697 	sscanf_test();
       
   698 
       
   699 	int argsc = 5;
       
   700 	char *argsv[]=
       
   701 		{
       
   702 		"tmisc", "This", "is", "a", "test.",
       
   703 		};
       
   704 	arguments(argsc, argsv);
       
   705 
       
   706 	strtod_test();
       
   707 	va_args_test();
       
   708 	sprintf_test();
       
   709 	}
       
   710 
       
   711 
       
   712 TInt E32Main()
       
   713 	{
       
   714 	__UHEAP_MARK;
       
   715 
       
   716 	CTrapCleanup* tc = CTrapCleanup::New();
       
   717     TEST(tc != NULL);
       
   718 
       
   719 	TheTest.Title();
       
   720     TRAPD(err, ::MainL());
       
   721 	TEST2(err, KErrNone);
       
   722 
       
   723 	TheTest.End();
       
   724 	TheTest.Close();
       
   725 
       
   726 	delete tc;
       
   727 	CloseSTDLIB();
       
   728 
       
   729 	__UHEAP_MARKEND;
       
   730 
       
   731 	User::Heap().Check();
       
   732 	return KErrNone;
       
   733 	}