Update contrib.
1 // Copyright (c) 1995-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\math\t_r32.cpp
15 // T_R32.CPP - Test routines for TReal32
16 // Also note that these tests do not generally include testing of special values. This is done
17 // in T_R96 tests and conversions are tested thoroughly, so explicit tests are unnecessary here.
19 // Test functionality of operations on 32bit real numbers.
23 // - Test the conversion from TReal to TReal32 is as expected.
24 // - Check addition, subtraction and multiplication of 32-bit floating point
25 // numbers are as expected.
26 // - Check division of 32-bit floating-point numbers and verify that it is
27 // panicked when divided by zero.
28 // - Test arithmetic exceptions are raised for
29 // - overflow error during addition, subtraction.
30 // - overflow, underflow errors during multiplication.
31 // - overflow, underflow, divided by zero errors during division.
32 // - overflow, underflow, invalid operation errors during conversion
33 // from double to float.
34 // - Check unary operator, equalities and inequalities operators, pre/post
35 // increment, decrement operators with TReal32 are as expected.
36 // Platforms/Drives/Compatibility:
38 // Assumptions/Requirement/Pre-requisites:
39 // Failures and causes:
40 // Base Port information:
48 // Some symbols generated by the VC++ compiler for floating point stuff.
50 GLDEF_D TInt _adj_fdiv_m32;
54 // Data for tests from T_R32DTA.cpp
55 GLREF_D TReal32 addInput[];
56 GLREF_D TReal32 subInput[];
57 GLREF_D TReal32 multInput[];
58 GLREF_D TReal32 divInput[];
59 GLREF_D TReal32 unaryInput[];
60 GLREF_D TReal32 incDecInput[];
63 GLREF_D TInt sizeMult;
65 GLREF_D TInt sizeUnary;
66 GLREF_D TInt sizeIncDec;
68 #if defined (__WINS__) || defined (__X86__)
69 // Functions from EMGCC32.CPP
70 GLREF_C TReal32 __addsf3(TReal32 a1,TReal32 a2);
71 GLREF_C TReal32 __subsf3(TReal32 a1,TReal32 a2);
72 GLREF_C TReal32 __mulsf3(TReal32 a1,TReal32 a2);
73 GLREF_C TReal32 __divsf3(TReal32 a1,TReal32 a2);
74 GLREF_C TReal32 __truncdfsf2(TReal64 a1);
77 GLDEF_D TReal32 NaNTReal32;
78 GLDEF_D TReal32 posInfTReal32;
79 GLDEF_D TReal32 negInfTReal32;
80 GLDEF_D const TReal32 minDenormalTReal32=1.4E-45f;
81 GLDEF_D TReal NaNTReal;
82 GLDEF_D TReal posInfTReal;
83 GLDEF_D TReal negInfTReal;
92 LOCAL_D RTest test(_L("T_R32"));
94 LOCAL_C void initSpecialValues()
96 // Initialise special values
100 SReal32 *p32=(SReal32*)&NaNTReal32;
102 p32->exp=KTReal32SpecialExponent;
105 p32=(SReal32*)&posInfTReal32;
107 p32->exp=KTReal32SpecialExponent;
110 p32=(SReal32*)&negInfTReal32;
112 p32->exp=KTReal32SpecialExponent;
115 SReal64 *p64=(SReal64*)&NaNTReal;
117 p64->exp=KTReal64SpecialExponent;
118 p64->lsm=0xffffffffu;
121 p64=(SReal64*)&posInfTReal;
123 p64->exp=KTReal64SpecialExponent;
127 p64=(SReal64*)&negInfTReal;
129 p64->exp=KTReal64SpecialExponent;
134 LOCAL_C void testConvert()
144 KMaxTReal32inTReal,KMinTReal32inTReal,-KMaxTReal32inTReal,-KMinTReal32inTReal,
145 KMaxTReal32inTReal,KMinTReal32inTReal,-KMaxTReal32inTReal,-KMinTReal32inTReal,
146 3.4027E+38,1.1755E-38,-3.4027E+38,-1.1755E-38,
147 0.0,64.5,-64.5,1.54E+18,-1.54E+18,4.72E-22,-4.72E-22,
148 posInfTReal,negInfTReal,KNegZeroTReal,
149 1.4E-45,-1.4E-45,2E-41,-2E-41,1E-38,-1E-38
154 KMaxTReal32,KMinTReal32,-KMaxTReal32,-KMinTReal32,
155 KMaxTReal32,KMinTReal32,-KMaxTReal32,-KMinTReal32,
156 3.4027E+38f,1.17550E-38f,-3.40270E+38f,-1.17550E-38f,
157 0.0f,64.5f,-64.5f,1.54E+18f,-1.54E+18f,4.72E-22f,-4.72E-22f,
158 posInfTReal32,negInfTReal32,KNegZeroTReal32,
159 1.4E-45f,-1.4E-45f,2E-41f,-2E-41f,1E-38f,-1E-38f
162 TInt size=sizeof(input)/sizeof(TReal);
164 for (TInt ii=0; ii<size; ii++)
166 f=TRealX(expect[ii]);
167 test(f==TRealX(TReal32(input[ii])));
172 TReal32 b=NaNTReal32;
174 // test(f!=TRealX(TReal32(a)));
177 // See EON Software Defects Bug Report no. HA-287
178 // There is a bug in MSDev compiler which means comparing TReal32's directly
179 // does not always work, hence...
181 test(BitTest(TReal32(3.40270E+38),3.40270E+38f)); // this works
182 // (BitTest() checks for all 32 bits being identical
184 TReal32 a=TReal32(3.40270E+38);
185 TReal32 b=3.40270E+38f;
186 TReal64 c=3.40270E+38;
187 TReal32 d=TReal32(c);
189 test(a==b); // this works
190 test(d==b); // this works
191 test(TRealX(TReal32(c))==TRealX(b)); // this works
192 test(TReal64(TReal32(c))==TReal64(b)); // this panics
193 test(TReal32(c)==b); // this panics
194 test(TReal32(3.40270E+38)==3.40270E+38f); // this panics
196 // As expected, all these work fine under ARM.
200 LOCAL_C void testAdd()
208 for (TInt ii=0; ii<sizeAdd-1; ii++)
216 // Test PC real addition using fp-hardware same as TRealX addition
217 test(TRealX(f+g)==TRealX(TReal32(ff+gg)));
218 test(TRealX(g+f)==TRealX(TReal32(ff+gg)));
219 // Test hex-encoded constants for TReal32s generated on PC using fp-hardware same as
221 test(TRealX(*(TReal32*)&addArray[ii])==TRealX(f+g));
222 test(TRealX(*(TReal32*)&addArray[ii])==TRealX(g+f));
223 // similarly to tests above ...
227 test(TRealX(h+=f)==TRealX(TReal32(hh)));
228 test(TRealX(h)==TRealX(TReal32(hh)));
229 test(TRealX(*(TReal32*)&addArray[ii])==TRealX(h));
234 test(TRealX(h+=g)==TRealX(TReal32(hh)));
235 test(h==TReal32(hh));
236 test(TRealX(*(TReal32*)&addArray[ii])==TRealX(h));
240 LOCAL_C void testSubt()
248 for (TInt ii=0; ii<sizeSub-1; ii++)
256 // This test fails on GCC (with -O1 switch). The reason is that
257 // comparing two intermediate floats is unpredictable.
258 // See http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.18
263 test(TRealX(f-g)==TRealX(TReal32(ff-gg)));
264 test(TRealX(g-f)==TRealX(TReal32(gg-ff)));
265 test(TRealX(*(TReal32*)&subArray[ii])==TRealX(f-g));
266 test(TRealX(*(TReal32*)&subArray[ii])==TRealX(-(g-f)));
271 test(TRealX(h-=f)==TRealX(TReal32(hh)));
272 test(TRealX(h)==TRealX(TReal32(hh)));
273 test(TRealX(*(TReal32*)&subArray[ii])==TRealX(-h));
278 test(TRealX(h-=g)==TRealX(TReal32(hh)));
279 test(TRealX(h)==TRealX(TReal32(hh)));
280 test(TRealX(*(TReal32*)&subArray[ii])==TRealX(h));
284 LOCAL_C void testMult()
286 // Multiplication test
292 for (TInt ii=0; ii<sizeMult-1; ii++)
301 test(TRealX(f*g)==TRealX(TReal32(ff*gg)));
302 test(TRealX(g*f)==TRealX(TReal32(gg*ff)));
303 test(TRealX(*(TReal32*)&multArray[ii])==TRealX(f*g));
304 test(TRealX(*(TReal32*)&multArray[ii])==TRealX(g*f));
309 test(TRealX(h*=g)==TRealX(TReal32(hh)));
310 test(TRealX(h)==TRealX(TReal32(hh)));
311 test(TRealX(*(TReal32*)&multArray[ii])==TRealX(h));
316 test(TRealX(h*=f)==TRealX(TReal32(hh)));
317 test(TRealX(h)==TRealX(TReal32(hh)));
318 test(TRealX(*(TReal32*)&multArray[ii])==TRealX(h));
322 LOCAL_C void testDiv()
331 // Panic: Divide by Zero
336 for (TInt ii=0; ii<sizeDiv-1; ii++)
344 test(TRealX(f/g)==TRealX(TReal32(ff/gg)));
345 test(TRealX(*(TReal32*)&divArray[count])==TRealX(f/g));
350 test(TRealX(h/=g)==TRealX(TReal32(hh)));
351 test(TRealX(h)==TRealX(TReal32(hh)));
352 test(TRealX(*(TReal32*)&divArray[count])==TRealX(h));
357 test(TRealX(g/f)==TRealX(TReal32(gg/ff)));
361 test(TRealX(h/=f)==TRealX(TReal32(hh)));
362 test(h==TReal32(hh));
371 test(TRealX(f/g)==TRealX(TReal32(ff/gg)));
375 test(TRealX(h/=g)==TRealX(TReal32(hh)));
376 test(TRealX(h)==TRealX(TReal32(hh)));
379 #if defined (__WINS__) || defined (__X86__)
381 LOCAL_C void testArithmeticExceptionRaising()
383 // Test that UP_GCC.CPP raise exceptions correctly by calling functions from EMGCC32.CPP which
384 // are copies of those in UP_GCC.CPP. To be used in debugger only.
385 // Added by AnnW, December 1996
390 // Addition - possible errors are overflow, argument or none
394 h=__addsf3(f,f); // argument
397 h=__addsf3(f,f); // overflow
401 h=__addsf3(f,g); // none
404 // Subtraction - possible errors are overflow, argument or none
408 h=__subsf3(f,f); // argument
412 h=__subsf3(f,g); // overflow
416 h=__subsf3(f,g); // none
419 // Multiplication - possible errors are argument, overflow, underflow or none
422 h=__mulsf3(f,f); // argument
426 h=__mulsf3(f,g); // overflow
428 f=minDenormalTReal32;
430 h=__mulsf3(f,g); // underflow
434 h=__mulsf3(f,g); // none
437 // Division - possible errors are overflow, underflow, divide by zero, argument or none
441 h=__divsf3(f,g); // overflow
443 f=minDenormalTReal32;
445 h=__divsf3(f,g); // underflow
449 h=__divsf3(f,g); // divide by zero
453 h=__divsf3(f,g); // argument
457 h=__divsf3(f,g); // none
460 // Converting double to float - possible errors are overflow, underflow, invalid operation or none
464 f=__truncdfsf2(d); // overflow
467 f=__truncdfsf2(d); // underflow
470 f=__truncdfsf2(d); // invalid operation
473 f=__truncdfsf2(d); // none
478 LOCAL_C void testUnary()
480 // Unary operator tests
486 for (TInt ii=0; ii<sizeUnary-1; ii++)
490 test(TRealX(-f)==TRealX(TReal32(-g)));
491 test(TRealX(-f)==TRealX(0.0f-f));
492 test(TRealX(+f)==TRealX(TReal32(g)));
493 test(TRealX(+f)==TRealX(0.0f+f));
494 test(TRealX(*(TReal32*)&unaryArray[ii])==TRealX(-f));
498 LOCAL_C void testEqualities(const TReal& aA, TOrder aOrder, const TReal& aB)
500 // Test equality/inequality functions on aA and aB
501 // aOrder specifies the operand's relative sizes
506 test((aA>aA) ==FALSE);
507 test((aA<aA) ==FALSE);
508 test((aA>=aA)==TRUE);
509 test((aA<=aA)==TRUE);
510 test((aA==aA)==TRUE);
511 test((aA!=aA)==FALSE);
514 test((aA==aB)==FALSE);
515 test((aA!=aB)==TRUE);
517 if (aOrder==ELessThan)
519 test((aA<aB) ==TRUE);
520 test((aA<=aB)==TRUE);
521 test((aA>aB) ==FALSE);
522 test((aA>=aB)==FALSE);
526 test((aA==aB)==TRUE);
527 test((aA!=aB)==FALSE);
528 test((aA>=aB)==TRUE);
529 test((aA<=aB)==TRUE);
530 test((aA>aB)==FALSE);
531 test((aA<aB)==FALSE);
533 if (aOrder==EGreaterThan)
535 test((aA>aB) ==TRUE);
536 test((aA>=aB)==TRUE);
537 test((aA<aB) ==FALSE);
538 test((aA<=aB)==FALSE);
542 LOCAL_C void testEqualities()
544 // Test >, <, >=, <=, ==, !=
548 TReal32 lessThanMax = KMaxTReal32-TReal32(1.0E+32);
549 TReal32 greaterThanMin = 1.17550E-38f;
553 {KMinTReal32,5.3824705E-26f,1.0f,2387501.0f,5.3824705E+28f,KMaxTReal32};
556 {2.0f,KMaxTReal32,-lessThanMax,greaterThanMin,-KMinTReal32,10.4058482f,-10.4058482f,
557 1.2443345E+14f,1.2443345E+14f,-1.3420344E-16f,132435.97f,5.0E-6f,9.6f,-8.0f};
560 {1.0f,lessThanMax,-KMaxTReal32,KMinTReal32,-greaterThanMin,10.4058474f,-10.4058496f,
561 5.0E-10f,1.2443345E+10f,-5.0382470E+25f,-132435.97f,-5.1E-6f,8.0f,-9.6f};
563 TReal32 equal[] = // Same as large[]
564 {2.0f,KMaxTReal32,-lessThanMax,greaterThanMin,-KMinTReal32,10.4058482f,-10.4058482f,
565 1.2443345E+14f,1.2443345E+14f,-1.3420344E-16f,132435.97f,5.0E-6f,9.6f,-8.0f};
570 size = sizeof(positive)/sizeof(TReal32);
572 test.Start(_L("Zero"));
573 testEqualities(zero, EEqual, zero);
574 for (i=0; i<size; i++)
576 testEqualities(positive[i], EGreaterThan, zero);
577 testEqualities(-positive[i], ELessThan, zero);
578 testEqualities(zero, ELessThan, positive[i]);
579 testEqualities(zero, EGreaterThan, -positive[i]);
582 // Test boundary and other numbers
584 size = sizeof(large)/sizeof(TReal32);
586 test.Next(_L("Nonzero"));
587 for (i=0; i<size; i++)
589 testEqualities(large[i], EGreaterThan, small[i]);
590 testEqualities(small[i], ELessThan, large[i]);
591 testEqualities(large[i], EEqual, equal[i]);
597 LOCAL_C void testIncDec()
599 // Test Pre/Post - increment/decrement
607 test.Start(_L("Pre-increment"));
609 for (ii=0; ii<sizeIncDec; ii++)
613 test(TRealX(f)==TRealX(TReal32(g)));
614 test(TRealX(++f)==TRealX(TReal32(++g)));
615 test(TRealX(*(TReal32*)&preIncArray1[ii])==TRealX(f));
616 test(TRealX(f)==TRealX(TReal32(g)));
617 test(TRealX(++f)==TRealX(TReal32(++g)));
618 test(TRealX(*(TReal32*)&preIncArray2[ii])==TRealX(f));
619 test(TRealX(f)==TRealX(TReal32(g)));
622 test.Next(_L("Post-increment"));
624 for (ii=0; ii<sizeIncDec; ii++)
628 test(TRealX(f)==TRealX(TReal32(g)));
629 test(TRealX(f++)==TRealX(TReal32(g++)));
630 test(TRealX(*(TReal32*)&postIncArray1[ii])==TRealX(f));
631 test(TRealX(f)==TRealX(TReal32(g)));
632 test(TRealX(f++)==TRealX(TReal32(g++)));
633 test(TRealX(*(TReal32*)&postIncArray2[ii])==TRealX(f));
634 test(TRealX(f)==TRealX(TReal32(g)));
637 test.Next(_L("Pre-decrement"));
639 for (ii=0; ii<sizeIncDec; ii++)
643 test(TRealX(f)==TRealX(TReal32(g)));
644 test(TRealX(--f)==TRealX(TReal32(--g)));
645 test(TRealX(*(TReal32*)&preDecArray1[ii])==TRealX(f));
646 test(TRealX(f)==TRealX(TReal32(g)));
647 test(TRealX(--f)==TRealX(TReal32(--g)));
648 test(TRealX(*(TReal32*)&preDecArray2[ii])==TRealX(f));
649 test(TRealX(f)==TRealX(TReal32(g)));
652 test.Next(_L("Post-decrement"));
654 for (ii=0; ii<sizeIncDec; ii++)
658 test(TRealX(f)==TRealX(TReal32(g)));
659 test(TRealX(f--)==TRealX(TReal32(g--)));
660 test(TRealX(*(TReal32*)&postDecArray1[ii])==TRealX(f));
661 test(TRealX(f)==TRealX(TReal32(g)));
662 test(TRealX(f--)==TRealX(TReal32(g--)));
663 test(TRealX(*(TReal32*)&postDecArray2[ii])==TRealX(f));
664 test(TRealX(f)==TRealX(TReal32(g)));
669 LOCAL_C void _matherr(TExcType aType)
671 test.Printf(_L("_matherr: Exception type %u handled\n"),TUint(aType));
674 GLDEF_C TInt E32Main()
682 User::SetExceptionHandler(_matherr,KExceptionFpe);
686 test.Start(_L("Conversion from TReal to TReal32"));
688 test.Next(_L("Addition"));
690 test.Next(_L("Subtraction"));
692 test.Next(_L("Multiplication"));
694 test.Next(_L("Division"));
696 #if defined (__WINS__) || defined (__X86__)
697 test.Next(_L("Arithmetic which emulates UP_GCC and raises an exception"));
698 testArithmeticExceptionRaising();
700 test.Next(_L("Unary Operators"));
702 test.Next(_L("Equalities and Inequalities"));
704 test.Next(_L("Increment and Decrement"));