sl@0: // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32test\math\t_float.cpp sl@0: // Data split into files: t_float1.cpp, t_float2.cpp and t_float3.cpp sl@0: // Overview: sl@0: // Test conversion of real to descriptors, descriptors to real and the sl@0: // Math::Round method. sl@0: // API Information: sl@0: // TDes8, TDes16, TLex8, TLex16, Math. sl@0: // Details: sl@0: // - Test conversion of specified TReal32, TReal64 into TBuf8, TBuf16 sl@0: // is as expected. sl@0: // - Round some numbers to a specified number of decimal places and check it sl@0: // is as expected. sl@0: // - Test conversion of specified TReal32, TReal64 into TBuf8, TBuf16 in sl@0: // calculator mode is as expected. sl@0: // - Check that errors returned during conversion from TReal32, TReal64 into sl@0: // TBuf8, TBuf16 are as expected. sl@0: // - Verify the result when a string is parsed to extract a real number. sl@0: // - Test string to real conversion in cases, which are not exact and check sl@0: // that results are as expected. sl@0: // - Test errors in string to real conversion are as expected. sl@0: // - Test that the conversion from string to real and then real to string, verify sl@0: // the results are as expected. sl@0: // - For specified strings values check parsing of string to extract real value, sl@0: // absolute value, token length is as expected. sl@0: // - Check the character representation of specified real numbers are as expected. sl@0: // Platforms/Drives/Compatibility: sl@0: // All sl@0: // Assumptions/Requirement/Pre-requisites: sl@0: // Failures and causes: sl@0: // Base Port information: sl@0: // sl@0: // sl@0: sl@0: #include "t_math.h" sl@0: #include "t_float.h" sl@0: sl@0: GLREF_D RtoB_TEST testd[]; sl@0: GLREF_D TUint size_testd; sl@0: GLREF_D RtoB_TEST testcalc[]; sl@0: GLREF_D TUint size_testcalc; sl@0: GLREF_D ERR_TEST testerr[]; sl@0: GLREF_D TUint size_testerr; sl@0: GLREF_D DtoR_ERR_TEST testerr2[]; sl@0: GLREF_D TUint size_testerr2; sl@0: GLREF_D DtoR_ERR_TEST testerr3[]; sl@0: GLREF_D TUint size_testerr3; sl@0: GLREF_D CALC_TEST calctest[]; sl@0: GLREF_D TUint size_calctest; sl@0: GLREF_D ROUND_TEST testround[]; sl@0: GLREF_D TUint size_testround; sl@0: GLREF_D DtoR_TEST64 testd2[]; sl@0: GLREF_D TUint size_testd2; sl@0: GLREF_D DtoR_TEST64 testApprox[]; sl@0: GLREF_D TUint size_testApprox; sl@0: GLREF_D DtoR_TEST32 testd32[]; sl@0: GLREF_D TUint size_testd32; sl@0: sl@0: GLDEF_D RTest test(_L("T_FLOAT")); sl@0: sl@0: LOCAL_C TInt rtob(RtoB_TEST& tst) sl@0: { sl@0: sl@0: TBuf<0x100> buf; sl@0: sl@0: TInt ret=buf.Num(tst.num,tst.format); sl@0: if (ret!=KErrGeneral) sl@0: { sl@0: buf.ZeroTerminate(); sl@0: TPtrC result(tst.res); sl@0: // test.Printf(_L("buf=%S\nres=%S\n"),&buf,&result); sl@0: test(buf.Compare(result)==0); sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: LOCAL_C void test1() sl@0: // sl@0: // Test RtoB using selected numbers sl@0: // sl@0: { sl@0: sl@0: for (TInt jj=0;jj=0); sl@0: } sl@0: sl@0: LOCAL_C void test2() sl@0: // sl@0: // Test Math::Round(_,_,_) for specific numbers sl@0: // sl@0: { sl@0: sl@0: TReal res; sl@0: TRealFormat pf; sl@0: pf.iType=KRealFormatExponent; sl@0: pf.iWidth=22; sl@0: pf.iPlaces=15; sl@0: pf.iPoint='.'; sl@0: pf.iTriad=','; sl@0: pf.iTriLen=1; sl@0: sl@0: for (TInt jj=0;jj=0); sl@0: } sl@0: sl@0: LOCAL_C void test4() sl@0: // sl@0: // Tests errors sl@0: // sl@0: { sl@0: sl@0: for (TInt jj=0;jjiLex; sl@0: test(l.Val(r64)==KErrNone); sl@0: test(tst64->iRes==r64); sl@0: } sl@0: sl@0: TReal32 r32; sl@0: sl@0: for (jj=0; jjiLex; sl@0: test(l.Val(r32)==KErrNone); sl@0: test(tst32->iRes==r32); sl@0: } sl@0: } sl@0: sl@0: LOCAL_C void test6() sl@0: // sl@0: // Test string to real conversion in cases which are not exact sl@0: // Added by AnnW, December 1996 sl@0: // sl@0: { sl@0: sl@0: TReal r64; sl@0: sl@0: for (TInt jj=0; jjiLex; sl@0: test(l.Val(r64)==KErrNone); sl@0: test(tst64->iRes==r64); sl@0: } sl@0: } sl@0: sl@0: LOCAL_C void test7() sl@0: // sl@0: // Test errors in string to real conversion sl@0: // added by AnnW, December 1996 sl@0: // sl@0: { sl@0: sl@0: TReal r64; sl@0: TInt jj; sl@0: sl@0: for (jj=0; jjiLex; sl@0: TInt err=errtst64->iErr; sl@0: test(l.Val(r64)==err); sl@0: } sl@0: sl@0: TReal32 r32; sl@0: sl@0: for (jj=0; jjiLex; sl@0: TInt err=errtst32->iErr; sl@0: test(l.Val(r32)==err); sl@0: } sl@0: } sl@0: sl@0: LOCAL_C void test8() sl@0: // sl@0: // Some tests for calc to check that a number entered is printed to same significance when sl@0: // returned sl@0: // do dtor and rtod test sl@0: // sl@0: { sl@0: sl@0: TLex l; sl@0: TBuf<0x100> buf; sl@0: TInt ret; sl@0: TReal64 num; sl@0: sl@0: for (TInt jj=0;jj=0); sl@0: } sl@0: } sl@0: sl@0: // Tag on a few extra tests relating to ascii and unicode sl@0: sl@0: template sl@0: class AsciiUnicode sl@0: { sl@0: public: sl@0: void Test1(); sl@0: }; sl@0: sl@0: template sl@0: void AsciiUnicode::Test1() sl@0: // R == TReal32 or TReal64 sl@0: // S == TText, TText8 or TText16 sl@0: // L == TLex, TLex8 or TLex16 sl@0: // B == TBuf, TBuf8 or TBuf16 sl@0: // T == TPtr, TPtr8 or TPtr16 sl@0: { sl@0: sl@0: L lex1(_TL("123.456")); sl@0: R real; // TReal32 or TReal64 sl@0: test(lex1.Val(real)==KErrNone); sl@0: test(Abs(real-123.456)<1E-5); sl@0: test(lex1.TokenLength()==7); sl@0: test((lex1.Remainder()).Compare(T(_TL(""),0,0))==0); sl@0: test(lex1.Val(real)==KErrGeneral); sl@0: sl@0: L lex2(_TL("123.456abc")); sl@0: test(lex2.Val(real)==KErrNone); sl@0: test(Abs(real-123.456)<1E-5); sl@0: test(lex2.TokenLength()==7); sl@0: test((lex2.Remainder()).Compare(T(_TL("abc"),3,3))==0); sl@0: test(lex2.Val(real)==KErrGeneral); sl@0: sl@0: L lex3; sl@0: real=(R)0.5; sl@0: test(lex3.Val(real)==KErrGeneral); sl@0: test(real==((R)0.5)); sl@0: test(lex3.TokenLength()==0); sl@0: sl@0: L lex4(_TL("abc123.45")); sl@0: real=(R)0.5; sl@0: test(lex4.Val(real)==KErrGeneral); sl@0: test(real==((R)0.5)); sl@0: sl@0: L Lex5(_TL("1.2e37")); sl@0: real=(R)0.5; sl@0: test(Lex5.Val(real)==KErrNone); sl@0: sl@0: L Lex6(_TL("1.2e")); sl@0: real=(R)0.5; sl@0: test(Lex6.Val(real)==KErrNone); sl@0: sl@0: TRealFormat format(20,3); // first param width, 2nd decimals sl@0: real=(R)12345.6789; sl@0: B buf1; sl@0: test(buf1.Num(real,format)==10); sl@0: test(buf1.Compare(T(_TL("12,345.679"),10,10))==0); sl@0: sl@0: B buf2(_TL("abc")); sl@0: test(buf2.AppendNum(real,format)==13); sl@0: test(buf2.Compare(T(_TL("abc12,345.679"),13,13))==0); sl@0: sl@0: B buf3; sl@0: test(buf3.AppendNum(real,format)==10); sl@0: test(buf3.Compare(T(_TL("12,345.679"),10,10))==0); sl@0: } sl@0: sl@0: #ifndef _DEBUG sl@0: #pragma warning (disable :4710) //Function not expanded sl@0: #pragma warning (disable :4702) //Unreachable code sl@0: #endif sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: { sl@0: test.Title(); sl@0: test.Start(_L("Test ftol")); sl@0: TReal f=34.567; sl@0: TInt i=TInt(f); sl@0: test(i==34); sl@0: test.Next(_L("Test TBuf8/16::Num() using selected numbers")); sl@0: test1(); sl@0: test.Next(_L("Test specific numbers for Math::Round(_,_,_)")); sl@0: test2(); sl@0: test.Next(_L("Test TBuf8/16::Num() using selected numbers in calculator mode")); sl@0: test3(); sl@0: test.Next(_L("Test errors")); sl@0: test4(); sl@0: test.Next(_L("Test TLex8/16::Val using selected values which give exact results")); sl@0: test5(); sl@0: test.Next(_L("Test TLex8/16::Val using values which do NOT give exact results")); sl@0: test6(); sl@0: test.Next(_L("Test errors")); sl@0: test7(); sl@0: test.Next(_L("Test for CALC")); sl@0: test8(); sl@0: // sl@0: test.Next(_L("Ascii/Unicode: TText")); sl@0: AsciiUnicode,TPtr> T1; sl@0: T1.Test1(); sl@0: test.Next(_L("Ascii/Unicode: TText8")); sl@0: AsciiUnicode,TPtr8> T2; sl@0: T2.Test1(); sl@0: test.Next(_L("Ascii/Unicode: TText16")); sl@0: AsciiUnicode,TPtr16> T3; sl@0: T3.Test1(); sl@0: // sl@0: test.End(); sl@0: return(KErrNone); sl@0: } sl@0: #pragma warning (default :4710) sl@0: #pragma warning (default :4702) sl@0: