os/kernelhwsrv/kerneltest/e32test/math/t_float.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32test\math\t_float.cpp
    15 // Data split into files: t_float1.cpp, t_float2.cpp and t_float3.cpp
    16 // Overview:
    17 // Test conversion of real to descriptors, descriptors to real and the 
    18 // Math::Round method.
    19 // API Information:
    20 // TDes8, TDes16, TLex8, TLex16, Math.  
    21 // Details:
    22 // - Test conversion of specified TReal32, TReal64 into TBuf8, TBuf16 
    23 // is as expected.
    24 // - Round some numbers to a specified number of decimal places and check it 
    25 // is as expected.
    26 // - Test conversion of specified TReal32, TReal64 into TBuf8, TBuf16 in
    27 // calculator mode is as expected.
    28 // - Check that errors returned during conversion from TReal32, TReal64 into 
    29 // TBuf8, TBuf16 are as expected.
    30 // - Verify the result when a string is parsed to extract a real number.
    31 // - Test string to real conversion in cases, which are not exact and check
    32 // that results are as expected.
    33 // - Test errors in string to real conversion are as expected.
    34 // - Test that the conversion from string to real and then real to string, verify
    35 // the results are as expected.
    36 // - For specified strings values check parsing of string to extract real value, 
    37 // absolute value, token length is as expected.
    38 // - Check the character representation of specified real numbers are as expected.
    39 // Platforms/Drives/Compatibility:
    40 // All 
    41 // Assumptions/Requirement/Pre-requisites:
    42 // Failures and causes:
    43 // Base Port information:
    44 // 
    45 //
    46 
    47 #include "t_math.h"
    48 #include "t_float.h"
    49 
    50 GLREF_D RtoB_TEST testd[];
    51 GLREF_D TUint size_testd;
    52 GLREF_D RtoB_TEST testcalc[];
    53 GLREF_D TUint size_testcalc;
    54 GLREF_D ERR_TEST testerr[];
    55 GLREF_D TUint size_testerr;
    56 GLREF_D DtoR_ERR_TEST testerr2[];
    57 GLREF_D TUint size_testerr2;
    58 GLREF_D DtoR_ERR_TEST testerr3[];
    59 GLREF_D TUint size_testerr3;
    60 GLREF_D CALC_TEST calctest[];
    61 GLREF_D TUint size_calctest;
    62 GLREF_D ROUND_TEST testround[];
    63 GLREF_D TUint size_testround;
    64 GLREF_D DtoR_TEST64 testd2[];
    65 GLREF_D TUint size_testd2;
    66 GLREF_D DtoR_TEST64 testApprox[];
    67 GLREF_D TUint size_testApprox;
    68 GLREF_D DtoR_TEST32 testd32[];
    69 GLREF_D TUint size_testd32;
    70 
    71 GLDEF_D RTest test(_L("T_FLOAT"));
    72 
    73 LOCAL_C TInt rtob(RtoB_TEST& tst)
    74     {
    75 
    76     TBuf<0x100> buf;
    77 
    78  	TInt ret=buf.Num(tst.num,tst.format);
    79 	if (ret!=KErrGeneral)
    80 		{
    81 		buf.ZeroTerminate();
    82 		TPtrC result(tst.res);
    83 //		test.Printf(_L("buf=%S\nres=%S\n"),&buf,&result);
    84 		test(buf.Compare(result)==0);
    85 		}
    86     return(ret);
    87     }
    88 
    89 LOCAL_C void test1()
    90 // 
    91 // Test RtoB using selected numbers
    92 //
    93     {
    94 
    95   	for (TInt jj=0;jj<TInt(size_testd/sizeof(RtoB_TEST));jj++)
    96         test(rtob(testd[jj])>=0);
    97 	}
    98 
    99 LOCAL_C void test2()
   100 //
   101 // Test Math::Round(_,_,_) for specific numbers
   102 //
   103     {
   104 
   105     TReal res;
   106     TRealFormat pf;
   107 	pf.iType=KRealFormatExponent;
   108 	pf.iWidth=22;
   109 	pf.iPlaces=15;
   110 	pf.iPoint='.';
   111 	pf.iTriad=',';
   112 	pf.iTriLen=1;
   113 					
   114     for (TInt jj=0;jj<TInt(size_testround/sizeof(ROUND_TEST));jj++) 
   115         {
   116         test(Math::Round(res,testround[jj].num,testround[jj].plcs)==KErrNone);
   117         TReal err=(res-testround[jj].res);
   118         if (res)
   119             err/=res;
   120         test(Abs(err)<=1E-15);
   121         }
   122     }
   123 
   124 LOCAL_C void test3()
   125 // 
   126 // Test RtoB in calculator mode using selected numbers
   127 // Added by AnnW, November 1996
   128 //
   129     {
   130 
   131   	for (TInt jj=0;jj<TInt(size_testcalc/sizeof(RtoB_TEST));jj++)
   132         test(rtob(testcalc[jj])>=0);
   133     }
   134 
   135 LOCAL_C void test4()
   136 //
   137 // Tests errors
   138 //
   139     {
   140 
   141     for (TInt jj=0;jj<TInt(size_testerr/sizeof(ERR_TEST));jj++)
   142         {
   143         RtoB_TEST tst=testerr[jj].rtob;
   144 		TInt ret=rtob(tst);
   145 		test(testerr[jj].err==ret);
   146         }
   147     }
   148 
   149 // End of translated test code from plib.  Now some tests from string o real conversions:
   150 
   151 LOCAL_C void test5()
   152 //
   153 // Test string to real conversion with selected numbers
   154 // added by AnnW, December 1996
   155 //
   156 	{
   157 	
   158 	TLex l;
   159 	TReal64 r64;
   160 	TInt jj;
   161 	
   162 	for (jj=0; jj<TInt(size_testd2/sizeof(DtoR_TEST64)); jj++)
   163 		{
   164 		const DtoR_TEST64* tst64=&testd2[jj];
   165 		l=tst64->iLex;
   166 		test(l.Val(r64)==KErrNone);
   167 		test(tst64->iRes==r64);
   168 		}
   169 	
   170 	TReal32 r32;
   171 
   172 	for (jj=0; jj<TInt(size_testd32/sizeof(DtoR_TEST32)); jj++)
   173 		{
   174 		const DtoR_TEST32* tst32=&testd32[jj];
   175 		l=tst32->iLex;
   176 		test(l.Val(r32)==KErrNone);
   177 		test(tst32->iRes==r32);
   178 		}
   179 	}	
   180 
   181 LOCAL_C void test6()
   182 //
   183 // Test string to real conversion in cases which are not exact
   184 // Added by AnnW, December 1996
   185 //
   186 	{
   187 	
   188 	TReal r64;
   189 	
   190 	for (TInt jj=0; jj<TInt(size_testApprox/sizeof(DtoR_TEST64)); jj++)
   191 		{
   192 		const DtoR_TEST64* tst64=&testApprox[jj];
   193 		TLex l=tst64->iLex;
   194 		test(l.Val(r64)==KErrNone);
   195 		test(tst64->iRes==r64);
   196 		}
   197 	}
   198 
   199 LOCAL_C void test7()
   200 //
   201 // Test errors in string to real conversion
   202 // added by AnnW, December 1996
   203 //
   204 	{
   205 
   206 	TReal r64;
   207 	TInt jj;
   208 
   209 	for (jj=0; jj<TInt(size_testerr2/sizeof(DtoR_ERR_TEST)); jj++)
   210 		{
   211 		const DtoR_ERR_TEST* errtst64=&testerr2[jj];
   212 		TLex l=errtst64->iLex;
   213 		TInt err=errtst64->iErr;
   214 		test(l.Val(r64)==err);
   215 		}
   216 
   217 	TReal32 r32;
   218 
   219 	for (jj=0; jj<TInt(size_testerr3/sizeof(DtoR_ERR_TEST)); jj++)
   220 		{
   221 		const DtoR_ERR_TEST* errtst32=&testerr3[jj];
   222 		TLex l=errtst32->iLex;
   223 		TInt err=errtst32->iErr;
   224 		test(l.Val(r32)==err);
   225 		}
   226 	}
   227 
   228 LOCAL_C void test8()
   229 //
   230 // Some tests for calc to check that a number entered is printed to same significance when
   231 // returned
   232 // do dtor and rtod test
   233 //
   234 	{
   235 
   236 	TLex l;
   237 	TBuf<0x100> buf;
   238 	TInt ret;
   239 	TReal64 num;
   240 
   241 	for (TInt jj=0;jj<TInt(size_calctest/sizeof(CALC_TEST));jj++)
   242 		{
   243 		// string to real
   244 		const CALC_TEST tst=calctest[jj];
   245 		l=tst.iLex;
   246 		test(l.Val(num)==KErrNone);
   247 		
   248 		// real to string
   249  		ret=buf.Num(num,tst.iFormat);
   250 		if (ret!=KErrGeneral)
   251 			{
   252 			buf.ZeroTerminate();
   253 			test(buf.Compare(TPtrC(tst.iRes))==0);
   254 			}
   255 		test(ret>=0);    
   256 		}
   257 	}
   258 
   259 // Tag on a few extra tests relating to ascii and unicode
   260 
   261 template<class R,class S,class L,class B,class T> 		
   262 class AsciiUnicode
   263 	{
   264 public:
   265 	void Test1();
   266 	};
   267 
   268 template<class R, class S,class L,class B,class T>
   269 void AsciiUnicode<R,S,L,B,T>::Test1()
   270 // R == TReal32 or TReal64
   271 // S == TText, TText8 or TText16
   272 // L == TLex, TLex8 or TLex16
   273 // B == TBuf, TBuf8 or TBuf16
   274 // T == TPtr, TPtr8 or TPtr16
   275 	{
   276 
   277 	L lex1(_TL("123.456"));
   278 	R real; // TReal32 or TReal64 
   279 	test(lex1.Val(real)==KErrNone);							 
   280 	test(Abs(real-123.456)<1E-5);
   281 	test(lex1.TokenLength()==7);
   282 	test((lex1.Remainder()).Compare(T(_TL(""),0,0))==0);
   283 	test(lex1.Val(real)==KErrGeneral);
   284 
   285 	L lex2(_TL("123.456abc"));
   286 	test(lex2.Val(real)==KErrNone);
   287 	test(Abs(real-123.456)<1E-5);
   288 	test(lex2.TokenLength()==7);
   289 	test((lex2.Remainder()).Compare(T(_TL("abc"),3,3))==0);
   290 	test(lex2.Val(real)==KErrGeneral);
   291 
   292 	L lex3;
   293 	real=(R)0.5;
   294 	test(lex3.Val(real)==KErrGeneral);
   295 	test(real==((R)0.5));
   296 	test(lex3.TokenLength()==0);
   297 
   298 	L lex4(_TL("abc123.45"));
   299 	real=(R)0.5;
   300 	test(lex4.Val(real)==KErrGeneral);
   301 	test(real==((R)0.5));
   302 
   303 	L Lex5(_TL("1.2e37"));
   304 	real=(R)0.5;
   305 	test(Lex5.Val(real)==KErrNone);
   306 	
   307 	L Lex6(_TL("1.2e"));
   308 	real=(R)0.5;
   309 	test(Lex6.Val(real)==KErrNone);
   310 
   311 	TRealFormat format(20,3); // first param width, 2nd decimals
   312 	real=(R)12345.6789;
   313 	B buf1;
   314 	test(buf1.Num(real,format)==10);
   315 	test(buf1.Compare(T(_TL("12,345.679"),10,10))==0);
   316 
   317 	B buf2(_TL("abc"));
   318 	test(buf2.AppendNum(real,format)==13);
   319 	test(buf2.Compare(T(_TL("abc12,345.679"),13,13))==0); 
   320 
   321 	B buf3;
   322 	test(buf3.AppendNum(real,format)==10);
   323 	test(buf3.Compare(T(_TL("12,345.679"),10,10))==0);
   324 	}
   325 
   326 #ifndef _DEBUG
   327 #pragma warning (disable :4710) //Function not expanded
   328 #pragma warning (disable :4702) //Unreachable code
   329 #endif
   330 
   331 GLDEF_C TInt E32Main()
   332     {
   333 	test.Title();
   334 	test.Start(_L("Test ftol"));
   335     TReal f=34.567;
   336     TInt i=TInt(f);
   337     test(i==34);
   338 	test.Next(_L("Test TBuf8/16::Num() using selected numbers"));
   339 	test1();
   340 	test.Next(_L("Test specific numbers for Math::Round(_,_,_)"));
   341 	test2();
   342 	test.Next(_L("Test TBuf8/16::Num() using selected numbers in calculator mode"));
   343 	test3();
   344 	test.Next(_L("Test errors"));
   345 	test4();
   346 	test.Next(_L("Test TLex8/16::Val using selected values which give exact results"));
   347 	test5();
   348 	test.Next(_L("Test TLex8/16::Val using values which do NOT give exact results"));
   349 	test6();
   350 	test.Next(_L("Test errors"));
   351 	test7();
   352 	test.Next(_L("Test for CALC"));
   353 	test8();
   354 	//
   355 	test.Next(_L("Ascii/Unicode: TText"));
   356 	AsciiUnicode<TReal64,TText,TLex,TBuf<0x20>,TPtr> T1;
   357 	T1.Test1();
   358 	test.Next(_L("Ascii/Unicode: TText8"));
   359 	AsciiUnicode<TReal32,TText8,TLex8,TBuf8<0x20>,TPtr8> T2;
   360 	T2.Test1();
   361 	test.Next(_L("Ascii/Unicode: TText16"));
   362 	AsciiUnicode<TReal32,TText16,TLex16,TBuf16<0x20>,TPtr16> T3;
   363 	T3.Test1();											
   364 	//
   365 	test.End();
   366 	return(KErrNone);
   367 	}
   368 #pragma warning (default :4710)
   369 #pragma warning (default :4702)
   370