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