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 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_realx.cpp
15 // T_REALX.CPP - Test routines for TRealX
17 // Test the functionality of a class that encapsulates 64 bit extended precision
22 // - Test the constructor and assignment operator with signed, unsigned, 64 bit
23 // integer, float and double values.
24 // - Check the conversion of TRealX value to signed, unsigned, 64 bit integer,
25 // float and double values is as expected.
26 // - Set TRealX variable with some special values and check it is as expected.
27 // - Test addition, multiplication, division of TRealX values is as expected.
28 // - Test Unary operators, comparison of two TRealX values are as expected.
29 // Platforms/Drives/Compatibility:
31 // Assumptions/Requirement/Pre-requisites:
32 // Failures and causes:
33 // Base Port information:
38 #include <e32std_private.h>
43 LOCAL_D RTest test(_L("T_TREALX"));
45 // Conversion functions TInt64 <-> TReal64
46 LOCAL_C void TReal64ToTInt64(TInt64 *aDest, const TReal64 *aSrc)
48 TInt *pD=(TInt*)aDest;
49 const TInt *pS=(TInt*)aSrc;
50 #ifdef __DOUBLE_WORDS_SWAPPED__
51 pD[1]=pS[0]; // word containing sign,exp
54 pD[1]=pS[1]; // word containing sign,exp
59 LOCAL_C void TInt64ToTReal64(TReal64 *aDest, const TInt64 *aSrc)
61 TInt *pD=(TInt*)aDest;
62 const TInt *pS=(TInt*)aSrc;
63 #ifdef __DOUBLE_WORDS_SWAPPED__
64 pD[0]=pS[1]; // word containing sign,exp
67 pD[1]=pS[1]; // word containing sign,exp
73 SRealX::SRealX(TUint a, TUint b, TUint c)
80 SRealX::SRealX(const TRealX& aRealX)
82 const TUint *pR = (const TUint *)&aRealX;
88 SRealX& SRealX::operator=(const TRealX& aRealX)
90 const TUint *pR = (const TUint *)&aRealX;
97 SRealX::operator TRealX() const
100 TUint *pR=(TUint *)&r;
107 TBool SRealX::operator==(const SRealX& aSRealX) const
109 TUint e1=iExpFlagSign >> 16;
110 TUint e2=aSRealX.iExpFlagSign >> 16;
111 TUint s1=iExpFlagSign & 0x00000001;
112 TUint s2=aSRealX.iExpFlagSign & 0x00000001;
113 TUint f1=iExpFlagSign & 0x00000300;
114 TUint f2=aSRealX.iExpFlagSign & 0x00000300;
115 return( e1==e2 && s1==s2 && (e1==0 || (f1==f2 && iMantHigh==aSRealX.iMantHigh && iMantLow==aSRealX.iMantLow)) );
118 SConvertFrom32BitTest::SConvertFrom32BitTest(TInt op, const SRealX& res, TInt r)
125 void SConvertFrom32BitTest::Test(TConversionFrom32Bits aConversion) const
153 TUint uint=(TUint)iOperand;
160 TUint uint=(TUint)iOperand;
168 TUint uint=(TUint)iOperand;
174 case EConstructFloat:
204 if (sr==iResult && r==iReturnCode)
206 test.Printf(_L("Conversion %d from 32 bit operand test failed\noperand = %08X\n"),
207 (TInt)aConversion, iOperand );
208 test.Printf(_L("Result %08X %08X %08X\nReturn code %d\n"),
209 sr.iExpFlagSign, sr.iMantHigh, sr.iMantLow, r );
210 test.Printf(_L("Correct result %08X %08X %08X\nCorrect return code %d\n"),
211 iResult.iExpFlagSign, iResult.iMantHigh, iResult.iMantLow, iReturnCode );
216 SConvertFrom64BitTest::SConvertFrom64BitTest(TInt64 op, const SRealX& res, TInt r)
223 void SConvertFrom64BitTest::Test(TConversionFrom64Bits aConversion) const
229 case EConstructInt64:
249 /* case EConstructUint:
251 TUint uint=(TUint)iOperand;
258 TUint uint=(TUint)iOperand;
266 TUint uint=(TUint)iOperand;
272 case EConstructDouble:
275 TInt64ToTReal64(&d, &iOperand);
283 TInt64ToTReal64(&d, &iOperand);
292 TInt64ToTReal64(&d, &iOperand);
299 if (sr==iResult && r==iReturnCode)
301 test.Printf(_L("Conversion %d from 64 bit operand test failed\noperand = %08X %08X\n"),
302 (TInt)aConversion, I64HIGH(iOperand), I64LOW(iOperand) );
303 test.Printf(_L("Result %08X %08X %08X\nReturn code %d\n"),
304 sr.iExpFlagSign, sr.iMantHigh, sr.iMantLow, r );
305 test.Printf(_L("Correct result %08X %08X %08X\nCorrect return code %d\n"),
306 iResult.iExpFlagSign, iResult.iMantHigh, iResult.iMantLow, iReturnCode );
311 SConvertTo32BitTest::SConvertTo32BitTest(const SRealX& op, TInt res, TInt r)
318 void SConvertTo32BitTest::Test(TConversionTo32Bits aConversion) const
321 TRealX op=(TRealX)iOperand;
335 case EOperatorTReal32:
350 if (result==iResult && r==iReturnCode)
352 test.Printf(_L("Conversion %d to 32 bit operand test failed\noperand = %08X %08X %08X\n"),
353 (TInt)aConversion, iOperand.iExpFlagSign, iOperand.iMantHigh, iOperand.iMantLow );
354 test.Printf(_L("Result %08X\nReturn code %d\n"),
356 test.Printf(_L("Correct result %08X\nCorrect return code %d\n"),
357 iResult, iReturnCode );
362 SConvertTo64BitTest::SConvertTo64BitTest(const SRealX& op, TInt64 res, TInt r)
369 void SConvertTo64BitTest::Test(TConversionTo64Bits aConversion) const
372 TRealX op=(TRealX)iOperand;
377 result=op.operator TInt64(); // odd conversion syntax required for VC5 compilation
379 case EOperatorTReal64:
383 TReal64ToTInt64(&result, &d);
390 TReal64ToTInt64(&result, &d);
394 if (result==iResult && r==iReturnCode)
396 test.Printf(_L("Conversion %d to 64 bit operand test failed\noperand = %08X %08X %08X\n"),
397 (TInt)aConversion, iOperand.iExpFlagSign, iOperand.iMantHigh, iOperand.iMantLow );
398 test.Printf(_L("Result %08X %08X\nReturn code %d\n"),
399 I64HIGH(result), I64LOW(result), r );
400 test.Printf(_L("Correct result %08X %08X\nCorrect return code %d\n"),
401 I64HIGH(iResult), I64LOW(iResult), iReturnCode );
406 SCompareTest::SCompareTest(const SRealX& o1, const SRealX& o2, TInt r)
413 void SCompareTest::Test() const
415 TRealX op1=(TRealX)iOperand1;
416 TRealX op2=(TRealX)iOperand2;
417 TRealX::TRealXOrder r=op1.Compare(op2);
424 if ((TInt)r==iReturnCode)
428 case TRealX::ELessThan:
429 if (lt && le && !gt && !ge && !eq && ne)
434 if (!lt && le && !gt && ge && eq && !ne)
437 case TRealX::EGreaterThan:
438 if (!lt && !le && gt && ge && !eq && ne)
442 case TRealX::EUnordered:
443 if (!lt && !le && !gt && !ge && !eq && ne)
448 test.Printf(_L("Compare test failed\nop1 = %08X %08X %08X\nop2 = %08X %08X %08X\n"),
449 iOperand1.iExpFlagSign, iOperand1.iMantHigh, iOperand1.iMantLow,
450 iOperand2.iExpFlagSign, iOperand2.iMantHigh, iOperand2.iMantLow );
451 test.Printf(_L("Return code %d\nlt=%d, le=%d, gt=%d, ge=%d, eq=%d, ne=%d\n"),
452 r, lt, le, gt, ge, eq, ne );
457 SOneOpTest::SOneOpTest(const SRealX& op, const SRealX& res, TInt r)
464 TInt SOneOpTest::DoTest(TUnaryOperation anOperation, TRealX *aResult) const
468 *aResult=(TRealX)iOperand;
473 *aResult=+(*aResult);
476 *aResult=-(*aResult);
486 if (!(SRealX(op)==iOperand))
491 if (!(SRealX(op)==iOperand))
498 TInt OneOpTestThreadFunction(TAny *anInfo)
500 SOneOpTestThreadInfo *pI=(SOneOpTestThreadInfo *)anInfo;
501 TInt r=pI->iTest->DoTest(pI->iOperation,pI->iResult);
505 void SOneOpTest::Test(TUnaryOperation anOperation) const
507 SOneOpTestThreadInfo info;
510 info.iOperation=anOperation;
511 info.iResult=&result;
513 TInt r=t.Create(_L("TestThread"),OneOpTestThreadFunction,0x1000,0x1000,0x100000,&info);
515 t.SetPriority(EPriorityMore); // so we will not run again until thread terminates
519 User::WaitForRequest(s);
520 TExitType exittype=t.ExitType();
524 if (sr==iResult && r==iReturnCode)
526 if (anOperation>EUnaryMinus)
528 if (r==KErrNone && exittype==EExitKill)
530 if (r!=KErrNone && exittype==EExitPanic)
535 if (exittype==EExitKill)
540 test.Printf(_L("Unary operation %d test failed\nop = %08X %08X %08X\n"),
542 iOperand.iExpFlagSign, iOperand.iMantHigh, iOperand.iMantLow );
543 test.Printf(_L("Result %08X %08X %08X\nReturn code %d Exit type %d\n"),
544 sr.iExpFlagSign, sr.iMantHigh, sr.iMantLow, r, (TInt)exittype );
545 test.Printf(_L("Correct result %08X %08X %08X\nCorrect return code %d\n"),
546 iResult.iExpFlagSign, iResult.iMantHigh, iResult.iMantLow, iReturnCode );
551 STwoOpTest::STwoOpTest(const SRealX& o1, const SRealX& o2, const SRealX& res, TInt r)
559 TInt STwoOpTest::DoTest(TBinaryOperation anOperation, TRealX *aResult, TBool aSwap) const
565 op2=(TRealX)iOperand1;
566 op1=(TRealX)iOperand2;
567 *aResult=(TRealX)iOperand2;
571 op1=(TRealX)iOperand1;
572 op2=(TRealX)iOperand2;
573 *aResult=(TRealX)iOperand1;
579 r=aResult->AddEq(op2);
582 r=aResult->SubEq(op2);
585 r=aResult->MultEq(op2);
588 r=aResult->DivEq(op2);
591 r=op1.Add(*aResult,op2);
594 r=op1.Sub(*aResult,op2);
597 r=op1.Mult(*aResult,op2);
600 r=op1.Div(*aResult,op2);
630 TInt TwoOpTestThreadFunction(TAny *anInfo)
632 STwoOpTestThreadInfo *pI=(STwoOpTestThreadInfo *)anInfo;
633 TInt r=pI->iTest->DoTest(pI->iOperation,pI->iResult,pI->iSwap);
637 void STwoOpTest::Test(TBinaryOperation anOperation, TBool aSwap) const
639 STwoOpTestThreadInfo info;
642 info.iOperation=anOperation;
643 info.iResult=&result;
646 TInt r=t.Create(_L("TestThread"),TwoOpTestThreadFunction,0x1000,0x1000,0x100000,&info);
648 t.SetPriority(EPriorityMore); // so we will not run again until thread terminates
652 User::WaitForRequest(s);
653 TExitType exittype=t.ExitType();
657 if (anOperation>=EPlus && exittype==EExitPanic)
659 // if +,-,*,/ operation paniced, result will be lost
662 if (sr==iResult && r==iReturnCode)
664 if (anOperation>=EPlusEq)
666 if (r==KErrNone && exittype==EExitKill)
668 if (r!=KErrNone && exittype==EExitPanic)
673 if (exittype==EExitKill)
678 test.Printf(_L("Binary operation %d test failed\nop1 = %08X %08X %08X\nop2 = %08X %08X %08X\n"),
680 iOperand1.iExpFlagSign, iOperand1.iMantHigh, iOperand1.iMantLow,
681 iOperand2.iExpFlagSign, iOperand2.iMantHigh, iOperand2.iMantLow );
682 test.Printf(_L("Result %08X %08X %08X\nReturn code %d Exit type %d\n"),
683 sr.iExpFlagSign, sr.iMantHigh, sr.iMantLow, r, (TInt)exittype );
684 test.Printf(_L("Correct result %08X %08X %08X\nCorrect return code %d\n"),
685 iResult.iExpFlagSign, iResult.iMantHigh, iResult.iMantLow, iReturnCode );
690 SSpecialValueTest::SSpecialValueTest(const SRealX& op, TInt aResults)
693 iIsZero=aResults & 8;
695 iIsInfinite=aResults & 2;
696 iIsFinite=aResults & 1;
699 LOCAL_C TBool same(TBool a, TBool b)
701 return( (a && b) || (!a && !b) );
704 void SSpecialValueTest::Test() const
706 TRealX x=(TRealX)iOperand;
707 TBool isZero=x.IsZero();
708 TBool isNaN=x.IsNaN();
709 TBool isInfinite=x.IsInfinite();
710 TBool isFinite=x.IsFinite();
711 if ( same(isZero,iIsZero) && same(isNaN,iIsNaN) && same(isInfinite,iIsInfinite) && same(isFinite,iIsFinite) )
713 test.Printf(_L("Special value test failed\nOperand %08X %08X %08X\n"),
714 iOperand.iExpFlagSign, iOperand.iMantHigh, iOperand.iMantLow );
715 test.Printf(_L("Results IsZero=%d, IsNaN=%d, IsInfinite=%d, IsFinite=%d "),
716 isZero, isNaN, isInfinite, isFinite );
717 test.Printf(_L("Correct results IsZero=%d, IsNaN=%d, IsInfinite=%d, IsFinite=%d "),
718 iIsZero, iIsNaN, iIsInfinite, iIsFinite );
723 LOCAL_C void TestAssignConstruct()
727 // test default constructor
730 test(sz.iExpFlagSign==0);
731 test(sz.iMantHigh==0);
732 test(sz.iMantLow==0);
734 for (i=0; i<NumConvertFromIntTests; i++)
736 const SConvertFrom32BitTest *pT=&(ConvertFromIntTests[i]);
737 pT->Test(EConstructInt);
738 pT->Test(EAssignInt);
741 for (i=0; i<NumConvertFromUintTests; i++)
743 const SConvertFrom32BitTest *pT=&(ConvertFromUintTests[i]);
744 pT->Test(EConstructUint);
745 pT->Test(EAssignUint);
748 for (i=0; i<NumConvertFromInt64Tests; i++)
750 const SConvertFrom64BitTest *pT=&(ConvertFromInt64Tests[i]);
751 pT->Test(EConstructInt64);
752 pT->Test(EAssignInt64);
755 for (i=0; i<NumConvertFromFloatTests; i++)
757 const SConvertFrom32BitTest *pT=&(ConvertFromFloatTests[i]);
758 pT->Test(EConstructFloat);
759 pT->Test(EAssignFloat);
762 for (i=0; i<NumConvertFromDoubleTests; i++)
764 const SConvertFrom64BitTest *pT=&(ConvertFromDoubleTests[i]);
765 pT->Test(EConstructDouble);
766 pT->Test(EAssignDouble);
767 pT->Test(ESetDouble);
771 LOCAL_C void TestConversions()
774 for (i=0; i<NumConvertToIntTests; i++)
776 const SConvertTo32BitTest *pT=&(ConvertToIntTests[i]);
777 pT->Test(EOperatorInt);
779 for (i=0; i<NumConvertToUintTests; i++)
781 const SConvertTo32BitTest *pT=&(ConvertToUintTests[i]);
782 pT->Test(EOperatorUint);
784 for (i=0; i<NumConvertToInt64Tests; i++)
786 const SConvertTo64BitTest *pT=&(ConvertToInt64Tests[i]);
787 pT->Test(EOperatorInt64);
789 for (i=0; i<NumConvertToFloatTests; i++)
791 const SConvertTo32BitTest *pT=&(ConvertToFloatTests[i]);
792 pT->Test(EOperatorTReal32);
793 pT->Test(EGetTReal32);
795 for (i=0; i<NumConvertToDoubleTests; i++)
797 const SConvertTo64BitTest *pT=&(ConvertToDoubleTests[i]);
798 pT->Test(EOperatorTReal64);
799 pT->Test(EGetTReal64);
803 LOCAL_C void TestSpecials()
807 x.SetInfinite(EFalse);
809 test(sx.iExpFlagSign==0xFFFF0000);
810 test(sx.iMantHigh==0x80000000);
811 test(sx.iMantLow==0);
814 test(sx.iExpFlagSign==0x00000000);
815 x.SetInfinite(ETrue);
817 test(sx.iExpFlagSign==0xFFFF0001);
818 test(sx.iMantHigh==0x80000000);
819 test(sx.iMantLow==0);
822 test(sx.iExpFlagSign==0xFFFF0001);
823 test(sx.iMantHigh==0xC0000000);
824 test(sx.iMantLow==0);
827 test(sx.iExpFlagSign==0x00000001);
830 for(i=0; i<NumSpecialValueTests; i++)
832 const SSpecialValueTest *pT=&(SpecialValueTests[i]);
837 LOCAL_C void TestUnaryOperators()
840 for (i=0; i<NumUnaryPlusTests; i++)
842 const SOneOpTest *pT=&(UnaryPlusTests[i]);
843 pT->Test(EUnaryPlus);
845 for (i=0; i<NumUnaryMinusTests; i++)
847 const SOneOpTest *pT=&(UnaryMinusTests[i]);
848 pT->Test(EUnaryMinus);
850 for (i=0; i<NumIncTests; i++)
852 const SOneOpTest *pT=&(IncTests[i]);
856 for (i=0; i<NumDecTests; i++)
858 const SOneOpTest *pT=&(DecTests[i]);
864 LOCAL_C void TestAddition()
867 for (i=0; i<NumAdditionTests; i++)
869 const STwoOpTest *pT=&(AdditionTests[i]);
870 pT->Test(EAddEq,EFalse);
871 pT->Test(EAddEq,ETrue);
872 pT->Test(EAdd,EFalse);
873 pT->Test(EAdd,ETrue);
874 pT->Test(EPlusEq,EFalse);
875 pT->Test(EPlusEq,ETrue);
876 pT->Test(EPlus,EFalse);
877 pT->Test(EPlus,ETrue);
879 for (i=0; i<NumBinaryOpNaNTests; i++)
881 const STwoOpTest *pT=&(BinaryOpNaNTests[i]);
882 pT->Test(EAddEq,EFalse);
883 pT->Test(EAddEq,ETrue);
884 pT->Test(EAdd,EFalse);
885 pT->Test(EAdd,ETrue);
886 pT->Test(EPlusEq,EFalse);
887 pT->Test(EPlusEq,ETrue);
888 pT->Test(EPlus,EFalse);
889 pT->Test(EPlus,ETrue);
893 LOCAL_C void TestMultiplication()
896 for (i=0; i<NumMultiplicationTests; i++)
898 const STwoOpTest *pT=&(MultiplicationTests[i]);
899 pT->Test(EMultEq,EFalse);
900 pT->Test(EMultEq,ETrue);
901 pT->Test(EMult,EFalse);
902 pT->Test(EMult,ETrue);
903 pT->Test(EStarEq,EFalse);
904 pT->Test(EStarEq,ETrue);
905 pT->Test(EStar,EFalse);
906 pT->Test(EStar,ETrue);
908 for (i=0; i<NumBinaryOpNaNTests; i++)
910 const STwoOpTest *pT=&(BinaryOpNaNTests[i]);
911 pT->Test(EMultEq,EFalse);
912 pT->Test(EMultEq,ETrue);
913 pT->Test(EMult,EFalse);
914 pT->Test(EMult,ETrue);
915 pT->Test(EStarEq,EFalse);
916 pT->Test(EStarEq,ETrue);
917 pT->Test(EStar,EFalse);
918 pT->Test(EStar,ETrue);
922 LOCAL_C void TestDivision()
925 for (i=0; i<NumDivisionTests; i++)
927 const STwoOpTest *pT=&(DivisionTests[i]);
928 pT->Test(EDivEq,EFalse);
929 pT->Test(EDiv,EFalse);
930 pT->Test(ESlashEq,EFalse);
931 pT->Test(ESlash,EFalse);
933 for (i=0; i<NumBinaryOpNaNTests; i++)
935 const STwoOpTest *pT=&(BinaryOpNaNTests[i]);
936 pT->Test(EDivEq,EFalse);
937 pT->Test(EDiv,EFalse);
938 pT->Test(ESlashEq,EFalse);
939 pT->Test(ESlash,EFalse);
943 LOCAL_C void TestComparison()
946 for (i=0; i<NumComparisonTests; i++)
948 const SCompareTest *pT=&(ComparisonTests[i]);
954 GLDEF_C TInt E32Main()
960 User::SetJustInTime(EFalse);
962 test.Start(_L("Assignment Operator and Constructors"));
963 TestAssignConstruct();
964 test.Next(_L("Conversions"));
966 test.Next(_L("Setting and Checking Specials"));
968 test.Next(_L("Addition"));
970 test.Next(_L("Multiplication"));
971 TestMultiplication();
972 test.Next(_L("Division"));
974 test.Next(_L("Unary Operators"));
975 TestUnaryOperators();
976 test.Next(_L("Comparisons"));