Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
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
28 #include <math.h> /* HUGE_VAL */
29 #include <errno.h> /* ERANGE */
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 */
38 static RTest TheTest(_L("TMisc"));
42 //Test macroses and functions
44 static void Check(TInt aValue, TInt aLine)
48 TheTest(EFalse, aLine);
51 static void Check(TInt aValue, TInt aExpected, TInt aLine)
53 if(aValue != aExpected)
55 RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
56 TheTest(EFalse, aLine);
60 #define TEST(arg) ::Check((arg), __LINE__)
61 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
74 TheTest.Next(_L("Random numbers"));
82 TheTest.Next(_L("Using srand()"));
103 int compare_a (const void *left, const void *right)
105 sort_item *lp = (sort_item *)left;
106 sort_item *rp = (sort_item *)right;
108 if (lp->a < rp->a) return -1;
109 if (lp->a > rp->a) return 1;
113 int compare_ba (const void *left, const void *right)
115 sort_item *lp = (sort_item *)left;
116 sort_item *rp = (sort_item *)right;
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;
127 sort_item list[NITEMS];
129 void validate(sort_item *p)
132 for (i=0; i<NITEMS; i++,p++)
136 TEST((p->a/4)+p->b == 5000);
137 TEST((p->a*p->a)/4 == p->c);
144 TheTest.Next(_L("Quicksort"));
145 for (i=1; i<NITEMS; i++)
148 list[i].b = 5000-(i/2);
152 /* Sort into increasing order of a. Want item 0 to sort to position 4 */
157 qsort(list, NITEMS, sizeof(list[0]), compare_a);
161 /* Resort into increasing order of b followed by a. Want item 0 to sort to position 5 */
163 list[0].b = 5000-((199-4)/2);
166 qsort(list, NITEMS, sizeof(list[0]), compare_ba);
176 TheTest.Next(_L("Binary search for existing items"));
177 for (i=0; i<NITEMS; i++)
179 answer = (sort_item*)bsearch(&list[i],list,NITEMS,sizeof(list[0]),compare_ba);
180 TEST(answer==&list[i]);
182 TheTest.Next(_L("Binary search for missing items"));
183 for (i=0; i<NITEMS; i++)
187 answer = (sort_item*)bsearch(¬_there,list,NITEMS,sizeof(list[0]),compare_ba);
189 TEST(!compare_ba(answer,¬_there));
191 for (i=0; i<NITEMS; i++)
195 answer = (sort_item*)bsearch(¬_there,list,NITEMS,sizeof(list[0]),compare_ba);
197 TEST(!compare_ba(answer,¬_there));
204 TheTest.Next(_L("Simple SSCANF tests"));
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]);
214 i = sscanf("194.223.254.9", "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
221 i = sscanf("17.183.hello.11", "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
229 /* setjmp, longjmp */
237 static int getStackPointer(void)
244 static int getgetStackPointer(void)
246 int n=4; // local variable to cause change to stack pointer
247 n+=getStackPointer();
249 n=getStackPointer()+37; // this never happens - it's just to defeat the optimiser
255 int m=n+100; // local variable to induce a stack frame
262 #pragma optimize( "", off ) //stop the compiler breaking this TEST
265 void setjmp_longjmp()
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"));
271 sp1=getStackPointer();
272 sp2=getgetStackPointer();
273 TEST(sp1!=sp2); // call from deeper function nesting should give a different answer
275 memset(jbufs[0],0,sizeof(jmp_buf));
276 memset(jbufs[1],0,sizeof(jmp_buf));
277 memset(jbufs[2],0,sizeof(jmp_buf));
282 for (i=0,j=1,k=2,l=3,m=4; i<3; i++,j*=3,k+=j,l*=m,m-=2)
288 r=memcmp(jbufs[0],jbufs[2], sizeof(jmp_buf));
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]);
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]);
301 RDebug::Print(_L(" Test code appears not to exercise preserved registers\n"));
307 r=memcmp(jbufs[0],jbufs[2], sizeof(jmp_buf));
308 TEST(r!=0); /* must change the return address! */
310 TheTest.Next(_L("Setjmp and longjmp"));
329 longjmp(jbufs[0],0); /* 0 must be turned into 1 */
338 sp1=getStackPointer();
342 sp2=getStackPointer();
344 longjmp(jbufs[3],1); // simple setjmp/longjmp
348 sp2=getStackPointer();
350 jmp_nest(20); // more complex example
355 sp2=getStackPointer();
361 #pragma optimize( "", on ) //stop the compiler breaking this TEST
364 /* case-insensitive comparison */
367 char agrave[2]= {191,0};
373 TheTest.Next(_L("Case-insensitive string comparison"));
386 r=strncasecmp(s1,s2,10);
389 r=strncasecmp(s1,s2,4);
399 /* Need some way to set up a proper folding example */
400 r=strncasecmp(s2,agrave,1);
405 void arguments(int argc, char *argv[])
409 TheTest.Next(_L("Command line arguments"));
412 printf(" argc=%d\r\n", argc);
413 for (i=0; i<argc; i++)
416 int length=strlen(argv[i]);
418 printf(" argv[%d]= ", i);
421 printf("%02x ", ((unsigned char *)argv[i])[j]);
422 if (argv[i][j]=='\0')
427 printf(" \"%s\" length %d\r\n", argv[i], length);
437 struct double_conv strtods[] = {
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 },
458 struct double_conv *dc;
460 TheTest.Next(_L("text to double conversion"));
461 for (dc=strtods; dc->source; dc++)
463 d=strtod(dc->source,NULL);
466 for (dc=strtods; dc->source; dc++)
469 d=strtod(dc->source,&p);
471 TEST(p==dc->source+dc->tail_offset);
474 /* overflow positive number */
475 d=strtod("9e9999", NULL);
476 TEST(d==HUGE_VAL && errno==ERANGE);
478 /* overflow negative number */
479 d=strtod("-9e9999", NULL);
480 TEST(d==-HUGE_VAL && errno==ERANGE);
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);
490 #define TEST_16 0x1617
491 #define TEST_32 0x32333435
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
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);
509 #pragma diag_suppress 1256
511 // The above RCVT warning is a compiler error in GCC. Below is the GGC friendly version of the function.
514 void va_va_bwlblwwblwlblbwlwb(va_list ap)
525 l = va_arg(ap, unsigned long);
531 l = va_arg(ap, unsigned long);
541 l = va_arg(ap, unsigned long);
547 l = va_arg(ap, unsigned long);
553 l = va_arg(ap, unsigned long);
561 l = va_arg(ap, unsigned long);
572 void va_va_bwlblwwblwlblbwlwb(va_list ap)
579 b = va_arg(ap, unsigned char);
581 w = va_arg(ap, unsigned short);
583 l = va_arg(ap, unsigned long);
587 b = va_arg(ap, unsigned char);
589 l = va_arg(ap, unsigned long);
591 w = va_arg(ap, unsigned short);
595 w = va_arg(ap, unsigned short);
597 b = va_arg(ap, unsigned char);
599 l = va_arg(ap, unsigned long);
603 w = va_arg(ap, unsigned short);
605 l = va_arg(ap, unsigned long);
607 b = va_arg(ap, unsigned char);
611 l = va_arg(ap, unsigned long);
613 b = va_arg(ap, unsigned char);
615 w = va_arg(ap, unsigned short);
619 l = va_arg(ap, unsigned long);
621 w = va_arg(ap, unsigned short);
623 b = va_arg(ap, unsigned char);
630 #pragma diag_default 1256
632 void va_lbwlblwwblwlblbwlwb(unsigned long x, ...)
638 va_va_bwlblwwblwlblbwlwb(ap);
643 TheTest.Next(_L("variadic functions"));
645 va_lbwlblwwblwlblbwlwb( TEST_32, TEST_LIST);
651 char* hw = "hello, world";
653 TheTest.Next(_L("sprintf function"));
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);
662 sprintf(buf, ":%s:", hw);
663 TEST(strcmp(buf, ":hello, world:")==0);
665 sprintf(buf, ":%10s:", hw);
666 TEST(strcmp(buf, ":hello, world:")==0);
668 sprintf(buf, ":%.10s:", hw);
669 TEST(strcmp(buf, ":hello, wor:")==0);
671 sprintf(buf, ":%-10s:", hw);
672 TEST(strcmp(buf, ":hello, world:")==0);
674 sprintf(buf, ":%.15s:", hw);
675 TEST(strcmp(buf, ":hello, world:")==0);
677 sprintf(buf, ":%-15s:", hw);
678 TEST(strcmp(buf, ":hello, world :")==0);
680 sprintf(buf, ":%15.10s:", hw);
681 TEST(strcmp(buf, ": hello, wor:")==0);
683 sprintf(buf, ":%-15.10s:", hw);
684 TEST(strcmp(buf, ":hello, wor :")==0);
690 TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-STDLIB-LEGACY-TMISC-0001 TMISC tests "));
702 "tmisc", "This", "is", "a", "test.",
704 arguments(argsc, argsv);
716 CTrapCleanup* tc = CTrapCleanup::New();
720 TRAPD(err, ::MainL());
721 TEST2(err, KErrNone);
731 User::Heap().Check();