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\math\t_r64.cpp
15 // T_R64.CPP - Test routines for TReal64
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.
25 // Data for tests from T_R64DTA.cpp
26 GLREF_D TReal64 addInput[];
27 GLREF_D TReal64 subInput[];
28 GLREF_D TReal64 multInput[];
29 GLREF_D TReal64 divInput[];
30 GLREF_D TReal64 divDenormalInput[];
31 GLREF_D TReal64 unaryInput[];
32 GLREF_D TReal64 incDecInput[];
35 GLREF_D TInt sizeMult;
37 GLREF_D TInt sizeDivDenormal;
38 GLREF_D TInt sizeUnary;
39 GLREF_D TInt sizeIncDec;
41 #if defined (__WINS__) || defined (__X86__)
42 // Functions from EMGCC64.CPP
43 GLREF_C TReal64 __adddf3(TReal64 a1,TReal64 a2);
44 GLREF_C TReal64 __subdf3(TReal64 a1,TReal64 a2);
45 GLREF_C TReal64 __muldf3(TReal64 a1,TReal64 a2);
46 GLREF_C TReal64 __divdf3(TReal64 a1,TReal64 a2);
49 // Special numbers for arithmetic tests
50 GLDEF_D TReal64 endsInFour;
54 GLDEF_D const TReal64 KMinDenormalTReal64=5E-324;
55 GLDEF_D const TReal KNaNTReal=TReal(KNaNTReal64);
56 GLDEF_D const TReal KPosInfTReal=TReal(KPosInfTReal64);;
57 GLDEF_D const TReal KNegInfTReal=TReal(KNegInfTReal64);;
58 GLDEF_D const TReal KMinDenormalTReal=5E-324;
67 LOCAL_D RTest test(_L("T_R64"));
69 #if defined (__WINS__) || defined (__X86__)
71 LOCAL_C void testConstants()
73 // Checks that constants are defined as expected in WINS, in case low and high words are swapped
77 test(TRealX(TReal32(KMinTReal32in64))==TRealX(KMinTReal32));
78 test(TRealX(TReal32(KMaxTReal32in64))==TRealX(KMaxTReal32));
83 LOCAL_C void initSpecialValues()
85 // Initialise special values, etc
89 SReal64* p64=(SReal64*)&endsInFour;
114 LOCAL_C void testConvert()
116 // Test conversion from TReal to TReal64
117 // N.B. Conversions between TRealX and TReal64 are tested by t_r96.cpp
123 KMaxTReal,KMinTReal,-KMaxTReal,-KMinTReal,
124 1.7976931348622E+308,-1.7976931348622E+308,2.2250738585073E-308,-2.2250738585073E-308,
125 KMaxTReal32inTReal,KMinTReal32inTReal,-KMaxTReal32inTReal,-KMinTReal32inTReal,
126 0.0,64.5,-64.5,1.54E+180,-1.54E+180,4.72E-225,-4.72E-225,
127 KNegZeroTReal,KPosInfTReal,KNegInfTReal,KMinDenormalTReal,
128 1.2345E-318,-2.4682E-321,1.0E-323,-2.0E-308
131 const TReal64 expect[]=
133 KMaxTReal64,KMinTReal64,-KMaxTReal64,-KMinTReal64,
134 1.7976931348622E+308,-1.7976931348622E+308,2.2250738585073E-308,-2.2250738585073E-308,
135 KMaxTReal32in64,KMinTReal32in64,-KMaxTReal32in64,-KMinTReal32in64,
136 0.0,64.5,-64.5,1.54E+180,-1.54E+180,4.72E-225,-4.72E-225,
137 KNegZeroTReal64,KPosInfTReal64,KNegInfTReal64,KMinDenormalTReal64,
138 1.2345E-318,-2.4682E-321,1.0E-323,-2.0E-308
141 TInt size = sizeof(input)/sizeof(TReal);
144 for (TInt ii=0; ii<size; ii++)
146 f=TRealX(TReal64(TRealX(input[ii])));
147 test(f==TRealX(expect[ii]));
152 // TReal64 b=KNaNTReal64;
153 f=TRealX(TReal64(TRealX(a)));
154 // test(f!=TRealX(b));
158 LOCAL_C void testAdd()
166 for (TInt ii=0; ii<sizeAdd-1; ii++)
174 // Test PC real addition using fp-hardware same as TRealX addition
177 y=(TReal64)hh; // need to do this since TRealX has too much precision
181 y=(TReal64)hh; // need to do this since TRealX has too much precision
183 // Test hex-encoded constants for TReal64s generated on PC using fp-hardware same as
185 test(*(TReal64*)&addArray[ii]==f+g);
186 test(*(TReal64*)&addArray[ii]==g+f);
187 // similarly to tests above ...
192 test(h==(TReal64)hh);
193 test(*(TReal64*)&addArray[ii]==h);
199 test(h==(TReal64)hh);
200 test(*(TReal64*)&addArray[ii]==h);
204 LOCAL_C void testSub()
206 // Subtraction tests - see notes in addition test above
212 for (TInt ii=0; ii<sizeSub-1; ii++)
221 test(TRealX(f-g)==ff-gg);
222 test(TRealX(g-f)==gg-ff);
223 test(*(TReal64*)&subArray[ii]==f-g);
224 test(*(TReal64*)&subArray[ii]==-(g-f));
228 test(TRealX(h-=f)==(hh-=ff));
230 test(*(TReal64*)&subArray[ii]==-h);
234 test(TRealX(h-=g)==(hh-=gg));
236 test(*(TReal64*)&subArray[ii]==h);
240 LOCAL_C void testMult()
242 // Multiplication test
248 for (TInt ii=0; ii<sizeMult-1; ii++)
257 test(TRealX(f*g)==ff*gg);
258 test(TRealX(g*f)==gg*ff);
259 test(*(TReal64*)&multArray[ii]==f*g);
260 test(*(TReal64*)&multArray[ii]==g*f);
264 test(TRealX(h*=g)==(hh*=gg));
266 test(*(TReal64*)&multArray[ii]==h);
270 test(TRealX(h*=f)==(hh*=ff));
272 test(*(TReal64*)&multArray[ii]==h);
276 LOCAL_C void testDiv()
285 // Panic (under ARM) - Divide by Zero - run in DEBUG build as a check only
290 for (TInt ii=0; ii<sizeDiv-1; ii++)
298 test(TRealX(f/g)==ff/gg);
299 test(*(TReal64*)&divArray[count]==f/g);
303 test(TRealX(h/=g)==(hh/=gg));
305 test(*(TReal64*)&divArray[count]==h);
310 test(TRealX(g/f)==gg/ff);
314 test(TRealX(h/=f)==(hh/=ff));
319 gg=TRealX(arg2)/TRealX(arg3);
326 test(TRealX(f/g)==ff/gg);
328 test(TRealX(h/=g)==ff/gg);
329 test(TRealX(h)==ff/gg);
332 #if defined (__WINS__) || defined (__X86__)
334 LOCAL_C void testArithmeticExceptionRaising()
336 // Test that UP_GCC.CPP raise exceptions correctly by calling functions from EMGCC64.CPP which
337 // are copies of those in UP_GCC.CPP. To be used in debugger only.
338 // Added by AnnW, December 1996
343 // Addition - possible errors are argument, overflow, or none
344 // NB cannot achieve underflow now denormals in use
347 h=__adddf3(f,f); // argument
350 h=__adddf3(f,f); // overflow
354 h=__adddf3(f,g); // none
357 // Subtraction - possible errors are argumnet, overflow or none
358 // NB cannot achieve underflow now denormals in use
361 h=__subdf3(f,f); // argument
365 h=__subdf3(f,g); // overflow
369 h=__subdf3(f,g); // none
372 // Multiplication - possible errors are argument, overflow, underflow or none
375 h=__muldf3(f,f); // argument
379 h=__muldf3(f,g); // overflow
381 f=KMinDenormalTReal64;
383 h=__muldf3(f,g); // underflow
387 h=__muldf3(f,g); // none
390 // Division - possible errors are overflow, underflow, divide by zero, argument or none
394 h=__divdf3(f,g); // overflow
396 f=KMinDenormalTReal64;
398 h=__divdf3(f,g); // underflow
402 h=__divdf3(f,g); // divide by zero
406 h=__divdf3(f,g); // argument
410 h=__divdf3(f,g); // none
416 LOCAL_C void testUnary()
418 // Unary operator tests
424 for (TInt ii=0; ii<sizeUnary-1; ii++)
428 test(TRealX(-f)==-g);
429 test(TRealX(-f)==TRealX(0-f));
431 test(TRealX(+f)==TRealX(0+f));
432 test(*(TReal64*)&unaryArray[ii]==-f);
436 LOCAL_C void testEqualities(const TReal& aA, TOrder aOrder, const TReal& aB)
438 // Test equality/inequality functions on aA and aB
439 // aOrder specifies the operand's relative sizes
443 test((aA>aA) ==FALSE);
444 test((aA<aA) ==FALSE);
445 test((aA>=aA)==TRUE);
446 test((aA<=aA)==TRUE);
447 test((aA==aA)==TRUE);
448 test((aA!=aA)==FALSE);
452 test((aA==aB)==FALSE);
453 test((aA!=aB)==TRUE);
456 if (aOrder==ELessThan)
458 test((aA<aB) ==TRUE);
459 test((aA<=aB)==TRUE);
460 test((aA>aB) ==FALSE);
461 test((aA>=aB)==FALSE);
466 test((aA==aB)==TRUE);
467 test((aA!=aB)==FALSE);
468 test((aA>=aB)==TRUE);
469 test((aA<=aB)==TRUE);
470 test((aA>aB)==FALSE);
471 test((aA<aB)==FALSE);
474 if (aOrder==EGreaterThan)
476 test((aA>aB) ==TRUE);
477 test((aA>=aB)==TRUE);
478 test((aA<aB) ==FALSE);
479 test((aA<=aB)==FALSE);
483 LOCAL_C void testEqualities()
485 // Test >, <, >=, <=, ==, !=
489 TReal64 lessThanMax = KMaxTReal64-TReal64(1.0E+294);
490 TReal64 greaterThanMin = TReal64(2.225075E-308);
494 {KMinTReal64,5.3824705392348592E-138,1.0,2387501,5.3824705392348592E+138,KMaxTReal64};
497 {2.0,KMaxTReal64,-lessThanMax,greaterThanMin,-KMinTReal64,10.40584821945060,-10.40584821945058,
498 1.244334567201E+105,1.244334567201E+105,-1.3420344230402E-106,132435.97865,5.0E-16,9.6,-8.0};
501 {1.0,lessThanMax,-KMaxTReal64,KMinTReal64,-greaterThanMin,10.40584821945058,-10.40584821945060,
502 50E-100,1.244334567201E+104,-5.03824705392348592E+58,-132435.97865,-5.1E-16,8.0,-9.6};
504 TReal64 equal[] = // Same as large[]
505 {2.0,KMaxTReal64,-lessThanMax,greaterThanMin,-KMinTReal64,10.40584821945060,-10.40584821945058,
506 1.244334567201E+105,1.244334567201E+105,-1.3420344230402E-106,132435.97865,5.0E-16,9.6,-8.0};
510 size = sizeof(positive)/sizeof(TReal64);
512 test.Start(_L("Zero"));
513 testEqualities(zero, EEqual, zero);
514 for (i=0; i<size; i++)
516 testEqualities(positive[i], EGreaterThan, zero);
517 testEqualities(-positive[i], ELessThan, zero);
518 testEqualities(zero, ELessThan, positive[i]);
519 testEqualities(zero, EGreaterThan, -positive[i]);
522 // Test boundary and other numbers
524 size = sizeof(large)/sizeof(TReal64);
526 test.Next(_L("Nonzero"));
527 for (i=0; i<size; i++)
529 testEqualities(large[i], EGreaterThan, small[i]);
530 testEqualities(small[i], ELessThan, large[i]);
531 testEqualities(large[i], EEqual, equal[i]);
537 LOCAL_C void testIncDec()
539 // Test Pre/Post-increment/decrement
546 test.Start(_L("Pre-increment"));
548 for (ii=0; ii<sizeIncDec; ii++)
553 test(TRealX(++f)==(++g));
554 test(*(TReal64*)&preIncArray1[ii]==f);
556 test(TRealX(++f)==(++g));
557 test(*(TReal64*)&preIncArray2[ii]==f);
561 test.Next(_L("Post-increment"));
563 for (ii=0; ii<sizeIncDec; ii++)
568 test(TRealX(f++)==(g++));
569 test(*(TReal64*)&postIncArray1[ii]==f);
571 test(TRealX(f++)==(g++));
572 test(*(TReal64*)&postIncArray2[ii]==f);
576 test.Next(_L("Pre-decrement"));
578 for (ii=0; ii<sizeIncDec; ii++)
583 test(TRealX(--f)==(--g));
584 test(*(TReal64*)&preDecArray1[ii]==f);
586 test(TRealX(--f)==(--g));
587 test(*(TReal64*)&preDecArray2[ii]==f);
591 test.Next(_L("Post-decrement"));
593 for (ii=0; ii<sizeIncDec; ii++)
598 test(TRealX(f--)==(g--));
599 test(*(TReal64*)&postDecArray1[ii]==f);
601 test(TRealX(f--)==(g--));
602 test(*(TReal64*)&postDecArray2[ii]==f);
608 LOCAL_C void _matherr(TExcType aType)
610 // Dummy function to handle exceptions
614 test.Printf(_L("_matherr: Exception type %u handled\n"),TUint(aType));
618 GLDEF_C TInt E32Main()
626 #if defined (__X86__)
629 test.Printf(_L("control word = 0x%x\n"),cw);
630 cw=0x27f; // WINS value
634 // Set exceptions to be handled
636 myThread.SetExceptionHandler(_matherr,KExceptionFpe);
640 #if defined (__WINS__) || defined (__X86__)
641 test.Start(_L("Checking double words not swapped..."));
643 test.Next(_L("Conversion from TReal to TReal64"));
646 test.Start(_L("Conversion from TReal to TReal64"));
649 test.Next(_L("Conversion from TReal to TReal64"));
651 test.Next(_L("Addition"));
653 test.Next(_L("Subtraction"));
655 test.Next(_L("Multiplication"));
657 test.Next(_L("Division"));
659 #if defined (__WINS__) || defined (__X86__)
660 test.Next(_L("Arithmetic which emulates UP_GCC and raises an exception"));
661 testArithmeticExceptionRaising();
663 test.Next(_L("Unary Operations"));
665 test.Next(_L("Equalities and Inequalities"));
667 test.Next(_L("Increment and Decrement"));