First public contribution.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\math\t_float.cpp
15 // Data split into files: t_float1.cpp, t_float2.cpp and t_float3.cpp
17 // Test conversion of real to descriptors, descriptors to real and the
18 // Math::Round method.
20 // TDes8, TDes16, TLex8, TLex16, Math.
22 // - Test conversion of specified TReal32, TReal64 into TBuf8, TBuf16
24 // - Round some numbers to a specified number of decimal places and check it
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:
41 // Assumptions/Requirement/Pre-requisites:
42 // Failures and causes:
43 // Base Port information:
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;
71 GLDEF_D RTest test(_L("T_FLOAT"));
73 LOCAL_C TInt rtob(RtoB_TEST& tst)
78 TInt ret=buf.Num(tst.num,tst.format);
82 TPtrC result(tst.res);
83 // test.Printf(_L("buf=%S\nres=%S\n"),&buf,&result);
84 test(buf.Compare(result)==0);
91 // Test RtoB using selected numbers
95 for (TInt jj=0;jj<TInt(size_testd/sizeof(RtoB_TEST));jj++)
96 test(rtob(testd[jj])>=0);
101 // Test Math::Round(_,_,_) for specific numbers
107 pf.iType=KRealFormatExponent;
114 for (TInt jj=0;jj<TInt(size_testround/sizeof(ROUND_TEST));jj++)
116 test(Math::Round(res,testround[jj].num,testround[jj].plcs)==KErrNone);
117 TReal err=(res-testround[jj].res);
120 test(Abs(err)<=1E-15);
126 // Test RtoB in calculator mode using selected numbers
127 // Added by AnnW, November 1996
131 for (TInt jj=0;jj<TInt(size_testcalc/sizeof(RtoB_TEST));jj++)
132 test(rtob(testcalc[jj])>=0);
141 for (TInt jj=0;jj<TInt(size_testerr/sizeof(ERR_TEST));jj++)
143 RtoB_TEST tst=testerr[jj].rtob;
145 test(testerr[jj].err==ret);
149 // End of translated test code from plib. Now some tests from string o real conversions:
153 // Test string to real conversion with selected numbers
154 // added by AnnW, December 1996
162 for (jj=0; jj<TInt(size_testd2/sizeof(DtoR_TEST64)); jj++)
164 const DtoR_TEST64* tst64=&testd2[jj];
166 test(l.Val(r64)==KErrNone);
167 test(tst64->iRes==r64);
172 for (jj=0; jj<TInt(size_testd32/sizeof(DtoR_TEST32)); jj++)
174 const DtoR_TEST32* tst32=&testd32[jj];
176 test(l.Val(r32)==KErrNone);
177 test(tst32->iRes==r32);
183 // Test string to real conversion in cases which are not exact
184 // Added by AnnW, December 1996
190 for (TInt jj=0; jj<TInt(size_testApprox/sizeof(DtoR_TEST64)); jj++)
192 const DtoR_TEST64* tst64=&testApprox[jj];
194 test(l.Val(r64)==KErrNone);
195 test(tst64->iRes==r64);
201 // Test errors in string to real conversion
202 // added by AnnW, December 1996
209 for (jj=0; jj<TInt(size_testerr2/sizeof(DtoR_ERR_TEST)); jj++)
211 const DtoR_ERR_TEST* errtst64=&testerr2[jj];
212 TLex l=errtst64->iLex;
213 TInt err=errtst64->iErr;
214 test(l.Val(r64)==err);
219 for (jj=0; jj<TInt(size_testerr3/sizeof(DtoR_ERR_TEST)); jj++)
221 const DtoR_ERR_TEST* errtst32=&testerr3[jj];
222 TLex l=errtst32->iLex;
223 TInt err=errtst32->iErr;
224 test(l.Val(r32)==err);
230 // Some tests for calc to check that a number entered is printed to same significance when
232 // do dtor and rtod test
241 for (TInt jj=0;jj<TInt(size_calctest/sizeof(CALC_TEST));jj++)
244 const CALC_TEST tst=calctest[jj];
246 test(l.Val(num)==KErrNone);
249 ret=buf.Num(num,tst.iFormat);
250 if (ret!=KErrGeneral)
253 test(buf.Compare(TPtrC(tst.iRes))==0);
259 // Tag on a few extra tests relating to ascii and unicode
261 template<class R,class S,class L,class B,class T>
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
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);
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);
294 test(lex3.Val(real)==KErrGeneral);
295 test(real==((R)0.5));
296 test(lex3.TokenLength()==0);
298 L lex4(_TL("abc123.45"));
300 test(lex4.Val(real)==KErrGeneral);
301 test(real==((R)0.5));
303 L Lex5(_TL("1.2e37"));
305 test(Lex5.Val(real)==KErrNone);
309 test(Lex6.Val(real)==KErrNone);
311 TRealFormat format(20,3); // first param width, 2nd decimals
314 test(buf1.Num(real,format)==10);
315 test(buf1.Compare(T(_TL("12,345.679"),10,10))==0);
318 test(buf2.AppendNum(real,format)==13);
319 test(buf2.Compare(T(_TL("abc12,345.679"),13,13))==0);
322 test(buf3.AppendNum(real,format)==10);
323 test(buf3.Compare(T(_TL("12,345.679"),10,10))==0);
327 #pragma warning (disable :4710) //Function not expanded
328 #pragma warning (disable :4702) //Unreachable code
331 GLDEF_C TInt E32Main()
334 test.Start(_L("Test ftol"));
338 test.Next(_L("Test TBuf8/16::Num() using selected numbers"));
340 test.Next(_L("Test specific numbers for Math::Round(_,_,_)"));
342 test.Next(_L("Test TBuf8/16::Num() using selected numbers in calculator mode"));
344 test.Next(_L("Test errors"));
346 test.Next(_L("Test TLex8/16::Val using selected values which give exact results"));
348 test.Next(_L("Test TLex8/16::Val using values which do NOT give exact results"));
350 test.Next(_L("Test errors"));
352 test.Next(_L("Test for CALC"));
355 test.Next(_L("Ascii/Unicode: TText"));
356 AsciiUnicode<TReal64,TText,TLex,TBuf<0x20>,TPtr> T1;
358 test.Next(_L("Ascii/Unicode: TText8"));
359 AsciiUnicode<TReal32,TText8,TLex8,TBuf8<0x20>,TPtr8> T2;
361 test.Next(_L("Ascii/Unicode: TText16"));
362 AsciiUnicode<TReal32,TText16,TLex16,TBuf16<0x20>,TPtr16> T3;
368 #pragma warning (default :4710)
369 #pragma warning (default :4702)