1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/math/t_realx.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,981 @@
1.4 +// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32test\math\t_realx.cpp
1.18 +// T_REALX.CPP - Test routines for TRealX
1.19 +// Overview:
1.20 +// Test the functionality of a class that encapsulates 64 bit extended precision
1.21 +// real values.
1.22 +// API Information:
1.23 +// TRealX
1.24 +// Details:
1.25 +// - Test the constructor and assignment operator with signed, unsigned, 64 bit
1.26 +// integer, float and double values.
1.27 +// - Check the conversion of TRealX value to signed, unsigned, 64 bit integer,
1.28 +// float and double values is as expected.
1.29 +// - Set TRealX variable with some special values and check it is as expected.
1.30 +// - Test addition, multiplication, division of TRealX values is as expected.
1.31 +// - Test Unary operators, comparison of two TRealX values are as expected.
1.32 +// Platforms/Drives/Compatibility:
1.33 +// All
1.34 +// Assumptions/Requirement/Pre-requisites:
1.35 +// Failures and causes:
1.36 +// Base Port information:
1.37 +//
1.38 +//
1.39 +
1.40 +#include <e32std.h>
1.41 +#include <e32std_private.h>
1.42 +#include <e32test.h>
1.43 +#include <e32math.h>
1.44 +#include "t_realxd.h"
1.45 +
1.46 +LOCAL_D RTest test(_L("T_TREALX"));
1.47 +
1.48 +// Conversion functions TInt64 <-> TReal64
1.49 +LOCAL_C void TReal64ToTInt64(TInt64 *aDest, const TReal64 *aSrc)
1.50 + {
1.51 + TInt *pD=(TInt*)aDest;
1.52 + const TInt *pS=(TInt*)aSrc;
1.53 +#ifdef __DOUBLE_WORDS_SWAPPED__
1.54 + pD[1]=pS[0]; // word containing sign,exp
1.55 + pD[0]=pS[1];
1.56 +#else
1.57 + pD[1]=pS[1]; // word containing sign,exp
1.58 + pD[0]=pS[0];
1.59 +#endif
1.60 + }
1.61 +
1.62 +LOCAL_C void TInt64ToTReal64(TReal64 *aDest, const TInt64 *aSrc)
1.63 + {
1.64 + TInt *pD=(TInt*)aDest;
1.65 + const TInt *pS=(TInt*)aSrc;
1.66 +#ifdef __DOUBLE_WORDS_SWAPPED__
1.67 + pD[0]=pS[1]; // word containing sign,exp
1.68 + pD[1]=pS[0];
1.69 +#else
1.70 + pD[1]=pS[1]; // word containing sign,exp
1.71 + pD[0]=pS[0];
1.72 +#endif
1.73 + }
1.74 +
1.75 +// Test functions
1.76 +SRealX::SRealX(TUint a, TUint b, TUint c)
1.77 + {
1.78 + iExpFlagSign=a;
1.79 + iMantHigh=b;
1.80 + iMantLow=c;
1.81 + }
1.82 +
1.83 +SRealX::SRealX(const TRealX& aRealX)
1.84 + {
1.85 + const TUint *pR = (const TUint *)&aRealX;
1.86 + iExpFlagSign=pR[2];
1.87 + iMantHigh=pR[1];
1.88 + iMantLow=pR[0];
1.89 + }
1.90 +
1.91 +SRealX& SRealX::operator=(const TRealX& aRealX)
1.92 + {
1.93 + const TUint *pR = (const TUint *)&aRealX;
1.94 + iExpFlagSign=pR[2];
1.95 + iMantHigh=pR[1];
1.96 + iMantLow=pR[0];
1.97 + return *this;
1.98 + }
1.99 +
1.100 +SRealX::operator TRealX() const
1.101 + {
1.102 + TRealX r;
1.103 + TUint *pR=(TUint *)&r;
1.104 + pR[2]=iExpFlagSign;
1.105 + pR[1]=iMantHigh;
1.106 + pR[0]=iMantLow;
1.107 + return r;
1.108 + }
1.109 +
1.110 +TBool SRealX::operator==(const SRealX& aSRealX) const
1.111 + {
1.112 + TUint e1=iExpFlagSign >> 16;
1.113 + TUint e2=aSRealX.iExpFlagSign >> 16;
1.114 + TUint s1=iExpFlagSign & 0x00000001;
1.115 + TUint s2=aSRealX.iExpFlagSign & 0x00000001;
1.116 + TUint f1=iExpFlagSign & 0x00000300;
1.117 + TUint f2=aSRealX.iExpFlagSign & 0x00000300;
1.118 + return( e1==e2 && s1==s2 && (e1==0 || (f1==f2 && iMantHigh==aSRealX.iMantHigh && iMantLow==aSRealX.iMantLow)) );
1.119 + }
1.120 +
1.121 +SConvertFrom32BitTest::SConvertFrom32BitTest(TInt op, const SRealX& res, TInt r)
1.122 + {
1.123 + iOperand=op;
1.124 + iResult=res;
1.125 + iReturnCode=r;
1.126 + }
1.127 +
1.128 +void SConvertFrom32BitTest::Test(TConversionFrom32Bits aConversion) const
1.129 + {
1.130 + TInt r=iReturnCode;
1.131 + SRealX sr;
1.132 + switch(aConversion)
1.133 + {
1.134 + case EConstructInt:
1.135 + {
1.136 + TRealX x(iOperand);
1.137 + sr=(SRealX)x;
1.138 + break;
1.139 + }
1.140 + case EAssignInt:
1.141 + {
1.142 + TRealX x;
1.143 + x=iOperand;
1.144 + sr=(SRealX)x;
1.145 + break;
1.146 + }
1.147 + case ESetInt:
1.148 + {
1.149 + TRealX x;
1.150 + r=x.Set(iOperand);
1.151 + sr=(SRealX)x;
1.152 + break;
1.153 + }
1.154 + case EConstructUint:
1.155 + {
1.156 + TUint uint=(TUint)iOperand;
1.157 + TRealX x(uint);
1.158 + sr=(SRealX)x;
1.159 + break;
1.160 + }
1.161 + case EAssignUint:
1.162 + {
1.163 + TUint uint=(TUint)iOperand;
1.164 + TRealX x;
1.165 + x=uint;
1.166 + sr=(SRealX)x;
1.167 + break;
1.168 + }
1.169 + case ESetUint:
1.170 + {
1.171 + TUint uint=(TUint)iOperand;
1.172 + TRealX x;
1.173 + r=x.Set(uint);
1.174 + sr=(SRealX)x;
1.175 + break;
1.176 + }
1.177 + case EConstructFloat:
1.178 + {
1.179 + TReal32 f;
1.180 + TInt *pF=(TInt*)&f;
1.181 + *pF=iOperand;
1.182 + TRealX x(f);
1.183 + sr=(SRealX)x;
1.184 + break;
1.185 + }
1.186 + case EAssignFloat:
1.187 + {
1.188 + TReal32 f;
1.189 + TInt *pF=(TInt*)&f;
1.190 + *pF=iOperand;
1.191 + TRealX x;
1.192 + x=f;
1.193 + sr=(SRealX)x;
1.194 + break;
1.195 + }
1.196 + case ESetFloat:
1.197 + {
1.198 + TReal32 f;
1.199 + TInt *pF=(TInt*)&f;
1.200 + *pF=iOperand;
1.201 + TRealX x;
1.202 + r=x.Set(f);
1.203 + sr=(SRealX)x;
1.204 + break;
1.205 + }
1.206 + }
1.207 + if (sr==iResult && r==iReturnCode)
1.208 + return;
1.209 + test.Printf(_L("Conversion %d from 32 bit operand test failed\noperand = %08X\n"),
1.210 + (TInt)aConversion, iOperand );
1.211 + test.Printf(_L("Result %08X %08X %08X\nReturn code %d\n"),
1.212 + sr.iExpFlagSign, sr.iMantHigh, sr.iMantLow, r );
1.213 + test.Printf(_L("Correct result %08X %08X %08X\nCorrect return code %d\n"),
1.214 + iResult.iExpFlagSign, iResult.iMantHigh, iResult.iMantLow, iReturnCode );
1.215 + //test.Getch();
1.216 + test(0);
1.217 + }
1.218 +
1.219 +SConvertFrom64BitTest::SConvertFrom64BitTest(TInt64 op, const SRealX& res, TInt r)
1.220 + {
1.221 + iOperand=op;
1.222 + iResult=res;
1.223 + iReturnCode=r;
1.224 + }
1.225 +
1.226 +void SConvertFrom64BitTest::Test(TConversionFrom64Bits aConversion) const
1.227 + {
1.228 + TInt r=iReturnCode;
1.229 + SRealX sr;
1.230 + switch(aConversion)
1.231 + {
1.232 + case EConstructInt64:
1.233 + {
1.234 + TRealX x(iOperand);
1.235 + sr=(SRealX)x;
1.236 + break;
1.237 + }
1.238 + case EAssignInt64:
1.239 + {
1.240 + TRealX x;
1.241 + x=iOperand;
1.242 + sr=(SRealX)x;
1.243 + break;
1.244 + }
1.245 + case ESetInt64:
1.246 + {
1.247 + TRealX x;
1.248 + r=x.Set(iOperand);
1.249 + sr=(SRealX)x;
1.250 + break;
1.251 + }
1.252 +/* case EConstructUint:
1.253 + {
1.254 + TUint uint=(TUint)iOperand;
1.255 + TRealX x(uint);
1.256 + sr=(SRealX)x;
1.257 + break;
1.258 + }
1.259 + case EAssignUint:
1.260 + {
1.261 + TUint uint=(TUint)iOperand;
1.262 + TRealX x;
1.263 + x=uint;
1.264 + sr=(SRealX)x;
1.265 + break;
1.266 + }
1.267 + case ESetUint:
1.268 + {
1.269 + TUint uint=(TUint)iOperand;
1.270 + TRealX x;
1.271 + r=x.Set(uint);
1.272 + sr=(SRealX)x;
1.273 + break;
1.274 + }*/
1.275 + case EConstructDouble:
1.276 + {
1.277 + TReal64 d;
1.278 + TInt64ToTReal64(&d, &iOperand);
1.279 + TRealX x(d);
1.280 + sr=(SRealX)x;
1.281 + break;
1.282 + }
1.283 + case EAssignDouble:
1.284 + {
1.285 + TReal64 d;
1.286 + TInt64ToTReal64(&d, &iOperand);
1.287 + TRealX x;
1.288 + x=d;
1.289 + sr=(SRealX)x;
1.290 + break;
1.291 + }
1.292 + case ESetDouble:
1.293 + {
1.294 + TReal64 d;
1.295 + TInt64ToTReal64(&d, &iOperand);
1.296 + TRealX x;
1.297 + r=x.Set(d);
1.298 + sr=(SRealX)x;
1.299 + break;
1.300 + }
1.301 + }
1.302 + if (sr==iResult && r==iReturnCode)
1.303 + return;
1.304 + test.Printf(_L("Conversion %d from 64 bit operand test failed\noperand = %08X %08X\n"),
1.305 + (TInt)aConversion, I64HIGH(iOperand), I64LOW(iOperand) );
1.306 + test.Printf(_L("Result %08X %08X %08X\nReturn code %d\n"),
1.307 + sr.iExpFlagSign, sr.iMantHigh, sr.iMantLow, r );
1.308 + test.Printf(_L("Correct result %08X %08X %08X\nCorrect return code %d\n"),
1.309 + iResult.iExpFlagSign, iResult.iMantHigh, iResult.iMantLow, iReturnCode );
1.310 + //test.Getch();
1.311 + test(0);
1.312 + }
1.313 +
1.314 +SConvertTo32BitTest::SConvertTo32BitTest(const SRealX& op, TInt res, TInt r)
1.315 + {
1.316 + iOperand=op;
1.317 + iResult=res;
1.318 + iReturnCode=r;
1.319 + }
1.320 +
1.321 +void SConvertTo32BitTest::Test(TConversionTo32Bits aConversion) const
1.322 + {
1.323 + TInt r=iReturnCode;
1.324 + TRealX op=(TRealX)iOperand;
1.325 + TInt result=0;
1.326 + switch(aConversion)
1.327 + {
1.328 + case EOperatorInt:
1.329 + result=(TInt)op;
1.330 + break;
1.331 + case EOperatorUint:
1.332 + {
1.333 + TUint uint;
1.334 + uint=(TUint)op;
1.335 + result=(TInt)uint;
1.336 + break;
1.337 + }
1.338 + case EOperatorTReal32:
1.339 + {
1.340 + TReal32 x;
1.341 + x=(TReal32)op;
1.342 + result=*((TInt*)&x);
1.343 + break;
1.344 + }
1.345 + case EGetTReal32:
1.346 + {
1.347 + TReal32 x;
1.348 + r=op.GetTReal(x);
1.349 + result=*((TInt*)&x);
1.350 + break;
1.351 + }
1.352 + }
1.353 + if (result==iResult && r==iReturnCode)
1.354 + return;
1.355 + test.Printf(_L("Conversion %d to 32 bit operand test failed\noperand = %08X %08X %08X\n"),
1.356 + (TInt)aConversion, iOperand.iExpFlagSign, iOperand.iMantHigh, iOperand.iMantLow );
1.357 + test.Printf(_L("Result %08X\nReturn code %d\n"),
1.358 + result, r );
1.359 + test.Printf(_L("Correct result %08X\nCorrect return code %d\n"),
1.360 + iResult, iReturnCode );
1.361 + //test.Getch();
1.362 + test(0);
1.363 + }
1.364 +
1.365 +SConvertTo64BitTest::SConvertTo64BitTest(const SRealX& op, TInt64 res, TInt r)
1.366 + {
1.367 + iOperand=op;
1.368 + iResult=res;
1.369 + iReturnCode=r;
1.370 + }
1.371 +
1.372 +void SConvertTo64BitTest::Test(TConversionTo64Bits aConversion) const
1.373 + {
1.374 + TInt r=iReturnCode;
1.375 + TRealX op=(TRealX)iOperand;
1.376 + TInt64 result=0;
1.377 + switch(aConversion)
1.378 + {
1.379 + case EOperatorInt64:
1.380 + result=op.operator TInt64(); // odd conversion syntax required for VC5 compilation
1.381 + break;
1.382 + case EOperatorTReal64:
1.383 + {
1.384 + TReal64 d;
1.385 + d=(TReal64)op;
1.386 + TReal64ToTInt64(&result, &d);
1.387 + break;
1.388 + }
1.389 + case EGetTReal64:
1.390 + {
1.391 + TReal64 d;
1.392 + r=op.GetTReal(d);
1.393 + TReal64ToTInt64(&result, &d);
1.394 + break;
1.395 + }
1.396 + }
1.397 + if (result==iResult && r==iReturnCode)
1.398 + return;
1.399 + test.Printf(_L("Conversion %d to 64 bit operand test failed\noperand = %08X %08X %08X\n"),
1.400 + (TInt)aConversion, iOperand.iExpFlagSign, iOperand.iMantHigh, iOperand.iMantLow );
1.401 + test.Printf(_L("Result %08X %08X\nReturn code %d\n"),
1.402 + I64HIGH(result), I64LOW(result), r );
1.403 + test.Printf(_L("Correct result %08X %08X\nCorrect return code %d\n"),
1.404 + I64HIGH(iResult), I64LOW(iResult), iReturnCode );
1.405 + //test.Getch();
1.406 + test(0);
1.407 + }
1.408 +
1.409 +SCompareTest::SCompareTest(const SRealX& o1, const SRealX& o2, TInt r)
1.410 + {
1.411 + iOperand1=o1;
1.412 + iOperand2=o2;
1.413 + iReturnCode=r;
1.414 + }
1.415 +
1.416 +void SCompareTest::Test() const
1.417 + {
1.418 + TRealX op1=(TRealX)iOperand1;
1.419 + TRealX op2=(TRealX)iOperand2;
1.420 + TRealX::TRealXOrder r=op1.Compare(op2);
1.421 + TBool lt=op1<op2;
1.422 + TBool le=op1<=op2;
1.423 + TBool gt=op1>op2;
1.424 + TBool ge=op1>=op2;
1.425 + TBool eq=op1==op2;
1.426 + TBool ne=op1!=op2;
1.427 + if ((TInt)r==iReturnCode)
1.428 + {
1.429 + switch(r)
1.430 + {
1.431 + case TRealX::ELessThan:
1.432 + if (lt && le && !gt && !ge && !eq && ne)
1.433 + return;
1.434 + break;
1.435 +
1.436 + case TRealX::EEqual:
1.437 + if (!lt && le && !gt && ge && eq && !ne)
1.438 + return;
1.439 + break;
1.440 + case TRealX::EGreaterThan:
1.441 + if (!lt && !le && gt && ge && !eq && ne)
1.442 + return;
1.443 + break;
1.444 +
1.445 + case TRealX::EUnordered:
1.446 + if (!lt && !le && !gt && !ge && !eq && ne)
1.447 + return;
1.448 + break;
1.449 + }
1.450 + }
1.451 + test.Printf(_L("Compare test failed\nop1 = %08X %08X %08X\nop2 = %08X %08X %08X\n"),
1.452 + iOperand1.iExpFlagSign, iOperand1.iMantHigh, iOperand1.iMantLow,
1.453 + iOperand2.iExpFlagSign, iOperand2.iMantHigh, iOperand2.iMantLow );
1.454 + test.Printf(_L("Return code %d\nlt=%d, le=%d, gt=%d, ge=%d, eq=%d, ne=%d\n"),
1.455 + r, lt, le, gt, ge, eq, ne );
1.456 + //test.Getch();
1.457 + test(0);
1.458 + }
1.459 +
1.460 +SOneOpTest::SOneOpTest(const SRealX& op, const SRealX& res, TInt r)
1.461 + {
1.462 + iOperand=op;
1.463 + iResult=res;
1.464 + iReturnCode=r;
1.465 + }
1.466 +
1.467 +TInt SOneOpTest::DoTest(TUnaryOperation anOperation, TRealX *aResult) const
1.468 + {
1.469 + TInt r=KErrNone;
1.470 + TRealX op;
1.471 + *aResult=(TRealX)iOperand;
1.472 +
1.473 + switch(anOperation)
1.474 + {
1.475 + case EUnaryPlus:
1.476 + *aResult=+(*aResult);
1.477 + break;
1.478 + case EUnaryMinus:
1.479 + *aResult=-(*aResult);
1.480 + break;
1.481 + case EPreInc:
1.482 + ++*aResult;
1.483 + break;
1.484 + case EPreDec:
1.485 + --*aResult;
1.486 + break;
1.487 + case EPostInc:
1.488 + op=(*aResult)++;
1.489 + if (!(SRealX(op)==iOperand))
1.490 + r=KErrGeneral;
1.491 + break;
1.492 + case EPostDec:
1.493 + op=(*aResult)--;
1.494 + if (!(SRealX(op)==iOperand))
1.495 + r=KErrGeneral;
1.496 + break;
1.497 + }
1.498 + return r;
1.499 + }
1.500 +
1.501 +TInt OneOpTestThreadFunction(TAny *anInfo)
1.502 + {
1.503 + SOneOpTestThreadInfo *pI=(SOneOpTestThreadInfo *)anInfo;
1.504 + TInt r=pI->iTest->DoTest(pI->iOperation,pI->iResult);
1.505 + return r;
1.506 + }
1.507 +
1.508 +void SOneOpTest::Test(TUnaryOperation anOperation) const
1.509 + {
1.510 + SOneOpTestThreadInfo info;
1.511 + TRealX result;
1.512 + info.iTest=this;
1.513 + info.iOperation=anOperation;
1.514 + info.iResult=&result;
1.515 + RThread t;
1.516 + TInt r=t.Create(_L("TestThread"),OneOpTestThreadFunction,0x1000,0x1000,0x100000,&info);
1.517 + test(r==KErrNone);
1.518 + t.SetPriority(EPriorityMore); // so we will not run again until thread terminates
1.519 + TRequestStatus s;
1.520 + t.Logon(s);
1.521 + t.Resume();
1.522 + User::WaitForRequest(s);
1.523 + TExitType exittype=t.ExitType();
1.524 + r=s.Int();
1.525 + CLOSE_AND_WAIT(t);
1.526 + SRealX sr(result);
1.527 + if (sr==iResult && r==iReturnCode)
1.528 + {
1.529 + if (anOperation>EUnaryMinus)
1.530 + {
1.531 + if (r==KErrNone && exittype==EExitKill)
1.532 + return;
1.533 + if (r!=KErrNone && exittype==EExitPanic)
1.534 + return;
1.535 + }
1.536 + else
1.537 + {
1.538 + if (exittype==EExitKill)
1.539 + return;
1.540 + }
1.541 + }
1.542 +
1.543 + test.Printf(_L("Unary operation %d test failed\nop = %08X %08X %08X\n"),
1.544 + (TInt)anOperation,
1.545 + iOperand.iExpFlagSign, iOperand.iMantHigh, iOperand.iMantLow );
1.546 + test.Printf(_L("Result %08X %08X %08X\nReturn code %d Exit type %d\n"),
1.547 + sr.iExpFlagSign, sr.iMantHigh, sr.iMantLow, r, (TInt)exittype );
1.548 + test.Printf(_L("Correct result %08X %08X %08X\nCorrect return code %d\n"),
1.549 + iResult.iExpFlagSign, iResult.iMantHigh, iResult.iMantLow, iReturnCode );
1.550 + //test.Getch();
1.551 + test(0);
1.552 + }
1.553 +
1.554 +STwoOpTest::STwoOpTest(const SRealX& o1, const SRealX& o2, const SRealX& res, TInt r)
1.555 + {
1.556 + iOperand1=o1;
1.557 + iOperand2=o2;
1.558 + iResult=res;
1.559 + iReturnCode=r;
1.560 + }
1.561 +
1.562 +TInt STwoOpTest::DoTest(TBinaryOperation anOperation, TRealX *aResult, TBool aSwap) const
1.563 + {
1.564 + TInt r=KErrNone;
1.565 + TRealX op1, op2;
1.566 + if (aSwap)
1.567 + {
1.568 + op2=(TRealX)iOperand1;
1.569 + op1=(TRealX)iOperand2;
1.570 + *aResult=(TRealX)iOperand2;
1.571 + }
1.572 + else
1.573 + {
1.574 + op1=(TRealX)iOperand1;
1.575 + op2=(TRealX)iOperand2;
1.576 + *aResult=(TRealX)iOperand1;
1.577 + }
1.578 +
1.579 + switch(anOperation)
1.580 + {
1.581 + case EAddEq:
1.582 + r=aResult->AddEq(op2);
1.583 + break;
1.584 + case ESubEq:
1.585 + r=aResult->SubEq(op2);
1.586 + break;
1.587 + case EMultEq:
1.588 + r=aResult->MultEq(op2);
1.589 + break;
1.590 + case EDivEq:
1.591 + r=aResult->DivEq(op2);
1.592 + break;
1.593 + case EAdd:
1.594 + r=op1.Add(*aResult,op2);
1.595 + break;
1.596 + case ESub:
1.597 + r=op1.Sub(*aResult,op2);
1.598 + break;
1.599 + case EMult:
1.600 + r=op1.Mult(*aResult,op2);
1.601 + break;
1.602 + case EDiv:
1.603 + r=op1.Div(*aResult,op2);
1.604 + break;
1.605 + case EPlusEq:
1.606 + *aResult+=op2;
1.607 + break;
1.608 + case EMinusEq:
1.609 + *aResult-=op2;
1.610 + break;
1.611 + case EStarEq:
1.612 + *aResult*=op2;
1.613 + break;
1.614 + case ESlashEq:
1.615 + *aResult/=op2;
1.616 + break;
1.617 + case EPlus:
1.618 + *aResult=op1+op2;
1.619 + break;
1.620 + case EMinus:
1.621 + *aResult=op1-op2;
1.622 + break;
1.623 + case EStar:
1.624 + *aResult=op1*op2;
1.625 + break;
1.626 + case ESlash:
1.627 + *aResult=op1/op2;
1.628 + break;
1.629 + }
1.630 + return r;
1.631 + }
1.632 +
1.633 +TInt TwoOpTestThreadFunction(TAny *anInfo)
1.634 + {
1.635 + STwoOpTestThreadInfo *pI=(STwoOpTestThreadInfo *)anInfo;
1.636 + TInt r=pI->iTest->DoTest(pI->iOperation,pI->iResult,pI->iSwap);
1.637 + return r;
1.638 + }
1.639 +
1.640 +void STwoOpTest::Test(TBinaryOperation anOperation, TBool aSwap) const
1.641 + {
1.642 + STwoOpTestThreadInfo info;
1.643 + TRealX result;
1.644 + info.iTest=this;
1.645 + info.iOperation=anOperation;
1.646 + info.iResult=&result;
1.647 + info.iSwap=aSwap;
1.648 + RThread t;
1.649 + TInt r=t.Create(_L("TestThread"),TwoOpTestThreadFunction,0x1000,0x1000,0x100000,&info);
1.650 + test(r==KErrNone);
1.651 + t.SetPriority(EPriorityMore); // so we will not run again until thread terminates
1.652 + TRequestStatus s;
1.653 + t.Logon(s);
1.654 + t.Resume();
1.655 + User::WaitForRequest(s);
1.656 + TExitType exittype=t.ExitType();
1.657 + r=s.Int();
1.658 + CLOSE_AND_WAIT(t);
1.659 + SRealX sr(result);
1.660 + if (anOperation>=EPlus && exittype==EExitPanic)
1.661 + {
1.662 + // if +,-,*,/ operation paniced, result will be lost
1.663 + sr=iResult;
1.664 + }
1.665 + if (sr==iResult && r==iReturnCode)
1.666 + {
1.667 + if (anOperation>=EPlusEq)
1.668 + {
1.669 + if (r==KErrNone && exittype==EExitKill)
1.670 + return;
1.671 + if (r!=KErrNone && exittype==EExitPanic)
1.672 + return;
1.673 + }
1.674 + else
1.675 + {
1.676 + if (exittype==EExitKill)
1.677 + return;
1.678 + }
1.679 + }
1.680 +
1.681 + test.Printf(_L("Binary operation %d test failed\nop1 = %08X %08X %08X\nop2 = %08X %08X %08X\n"),
1.682 + (TInt)anOperation,
1.683 + iOperand1.iExpFlagSign, iOperand1.iMantHigh, iOperand1.iMantLow,
1.684 + iOperand2.iExpFlagSign, iOperand2.iMantHigh, iOperand2.iMantLow );
1.685 + test.Printf(_L("Result %08X %08X %08X\nReturn code %d Exit type %d\n"),
1.686 + sr.iExpFlagSign, sr.iMantHigh, sr.iMantLow, r, (TInt)exittype );
1.687 + test.Printf(_L("Correct result %08X %08X %08X\nCorrect return code %d\n"),
1.688 + iResult.iExpFlagSign, iResult.iMantHigh, iResult.iMantLow, iReturnCode );
1.689 + //test.Getch();
1.690 + test(0);
1.691 + }
1.692 +
1.693 +SSpecialValueTest::SSpecialValueTest(const SRealX& op, TInt aResults)
1.694 + {
1.695 + iOperand=op;
1.696 + iIsZero=aResults & 8;
1.697 + iIsNaN=aResults & 4;
1.698 + iIsInfinite=aResults & 2;
1.699 + iIsFinite=aResults & 1;
1.700 + }
1.701 +
1.702 +LOCAL_C TBool same(TBool a, TBool b)
1.703 + {
1.704 + return( (a && b) || (!a && !b) );
1.705 + }
1.706 +
1.707 +void SSpecialValueTest::Test() const
1.708 + {
1.709 + TRealX x=(TRealX)iOperand;
1.710 + TBool isZero=x.IsZero();
1.711 + TBool isNaN=x.IsNaN();
1.712 + TBool isInfinite=x.IsInfinite();
1.713 + TBool isFinite=x.IsFinite();
1.714 + if ( same(isZero,iIsZero) && same(isNaN,iIsNaN) && same(isInfinite,iIsInfinite) && same(isFinite,iIsFinite) )
1.715 + return;
1.716 + test.Printf(_L("Special value test failed\nOperand %08X %08X %08X\n"),
1.717 + iOperand.iExpFlagSign, iOperand.iMantHigh, iOperand.iMantLow );
1.718 + test.Printf(_L("Results IsZero=%d, IsNaN=%d, IsInfinite=%d, IsFinite=%d "),
1.719 + isZero, isNaN, isInfinite, isFinite );
1.720 + test.Printf(_L("Correct results IsZero=%d, IsNaN=%d, IsInfinite=%d, IsFinite=%d "),
1.721 + iIsZero, iIsNaN, iIsInfinite, iIsFinite );
1.722 + //test.Getch();
1.723 + test(0);
1.724 + }
1.725 +
1.726 +LOCAL_C void TestAssignConstruct()
1.727 + {
1.728 + TInt i;
1.729 +
1.730 + // test default constructor
1.731 + TRealX z;
1.732 + SRealX sz(z);
1.733 + test(sz.iExpFlagSign==0);
1.734 + test(sz.iMantHigh==0);
1.735 + test(sz.iMantLow==0);
1.736 +
1.737 + for (i=0; i<NumConvertFromIntTests; i++)
1.738 + {
1.739 + const SConvertFrom32BitTest *pT=&(ConvertFromIntTests[i]);
1.740 + pT->Test(EConstructInt);
1.741 + pT->Test(EAssignInt);
1.742 + pT->Test(ESetInt);
1.743 + }
1.744 + for (i=0; i<NumConvertFromUintTests; i++)
1.745 + {
1.746 + const SConvertFrom32BitTest *pT=&(ConvertFromUintTests[i]);
1.747 + pT->Test(EConstructUint);
1.748 + pT->Test(EAssignUint);
1.749 + pT->Test(ESetUint);
1.750 + }
1.751 + for (i=0; i<NumConvertFromInt64Tests; i++)
1.752 + {
1.753 + const SConvertFrom64BitTest *pT=&(ConvertFromInt64Tests[i]);
1.754 + pT->Test(EConstructInt64);
1.755 + pT->Test(EAssignInt64);
1.756 + pT->Test(ESetInt64);
1.757 + }
1.758 + for (i=0; i<NumConvertFromFloatTests; i++)
1.759 + {
1.760 + const SConvertFrom32BitTest *pT=&(ConvertFromFloatTests[i]);
1.761 + pT->Test(EConstructFloat);
1.762 + pT->Test(EAssignFloat);
1.763 + pT->Test(ESetFloat);
1.764 + }
1.765 + for (i=0; i<NumConvertFromDoubleTests; i++)
1.766 + {
1.767 + const SConvertFrom64BitTest *pT=&(ConvertFromDoubleTests[i]);
1.768 + pT->Test(EConstructDouble);
1.769 + pT->Test(EAssignDouble);
1.770 + pT->Test(ESetDouble);
1.771 + }
1.772 + }
1.773 +
1.774 +LOCAL_C void TestConversions()
1.775 + {
1.776 + TInt i;
1.777 + for (i=0; i<NumConvertToIntTests; i++)
1.778 + {
1.779 + const SConvertTo32BitTest *pT=&(ConvertToIntTests[i]);
1.780 + pT->Test(EOperatorInt);
1.781 + }
1.782 + for (i=0; i<NumConvertToUintTests; i++)
1.783 + {
1.784 + const SConvertTo32BitTest *pT=&(ConvertToUintTests[i]);
1.785 + pT->Test(EOperatorUint);
1.786 + }
1.787 + for (i=0; i<NumConvertToInt64Tests; i++)
1.788 + {
1.789 + const SConvertTo64BitTest *pT=&(ConvertToInt64Tests[i]);
1.790 + pT->Test(EOperatorInt64);
1.791 + }
1.792 + for (i=0; i<NumConvertToFloatTests; i++)
1.793 + {
1.794 + const SConvertTo32BitTest *pT=&(ConvertToFloatTests[i]);
1.795 + pT->Test(EOperatorTReal32);
1.796 + pT->Test(EGetTReal32);
1.797 + }
1.798 + for (i=0; i<NumConvertToDoubleTests; i++)
1.799 + {
1.800 + const SConvertTo64BitTest *pT=&(ConvertToDoubleTests[i]);
1.801 + pT->Test(EOperatorTReal64);
1.802 + pT->Test(EGetTReal64);
1.803 + }
1.804 + }
1.805 +
1.806 +LOCAL_C void TestSpecials()
1.807 + {
1.808 + TRealX x;
1.809 + SRealX sx;
1.810 + x.SetInfinite(EFalse);
1.811 + sx=x;
1.812 + test(sx.iExpFlagSign==0xFFFF0000);
1.813 + test(sx.iMantHigh==0x80000000);
1.814 + test(sx.iMantLow==0);
1.815 + x.SetZero();
1.816 + sx=x;
1.817 + test(sx.iExpFlagSign==0x00000000);
1.818 + x.SetInfinite(ETrue);
1.819 + sx=x;
1.820 + test(sx.iExpFlagSign==0xFFFF0001);
1.821 + test(sx.iMantHigh==0x80000000);
1.822 + test(sx.iMantLow==0);
1.823 + x.SetNaN();
1.824 + sx=x;
1.825 + test(sx.iExpFlagSign==0xFFFF0001);
1.826 + test(sx.iMantHigh==0xC0000000);
1.827 + test(sx.iMantLow==0);
1.828 + x.SetZero(ETrue);
1.829 + sx=x;
1.830 + test(sx.iExpFlagSign==0x00000001);
1.831 +
1.832 + TInt i;
1.833 + for(i=0; i<NumSpecialValueTests; i++)
1.834 + {
1.835 + const SSpecialValueTest *pT=&(SpecialValueTests[i]);
1.836 + pT->Test();
1.837 + }
1.838 + }
1.839 +
1.840 +LOCAL_C void TestUnaryOperators()
1.841 + {
1.842 + TInt i;
1.843 + for (i=0; i<NumUnaryPlusTests; i++)
1.844 + {
1.845 + const SOneOpTest *pT=&(UnaryPlusTests[i]);
1.846 + pT->Test(EUnaryPlus);
1.847 + }
1.848 + for (i=0; i<NumUnaryMinusTests; i++)
1.849 + {
1.850 + const SOneOpTest *pT=&(UnaryMinusTests[i]);
1.851 + pT->Test(EUnaryMinus);
1.852 + }
1.853 + for (i=0; i<NumIncTests; i++)
1.854 + {
1.855 + const SOneOpTest *pT=&(IncTests[i]);
1.856 + pT->Test(EPreInc);
1.857 + pT->Test(EPostInc);
1.858 + }
1.859 + for (i=0; i<NumDecTests; i++)
1.860 + {
1.861 + const SOneOpTest *pT=&(DecTests[i]);
1.862 + pT->Test(EPreDec);
1.863 + pT->Test(EPostDec);
1.864 + }
1.865 + }
1.866 +
1.867 +LOCAL_C void TestAddition()
1.868 + {
1.869 + TInt i;
1.870 + for (i=0; i<NumAdditionTests; i++)
1.871 + {
1.872 + const STwoOpTest *pT=&(AdditionTests[i]);
1.873 + pT->Test(EAddEq,EFalse);
1.874 + pT->Test(EAddEq,ETrue);
1.875 + pT->Test(EAdd,EFalse);
1.876 + pT->Test(EAdd,ETrue);
1.877 + pT->Test(EPlusEq,EFalse);
1.878 + pT->Test(EPlusEq,ETrue);
1.879 + pT->Test(EPlus,EFalse);
1.880 + pT->Test(EPlus,ETrue);
1.881 + }
1.882 + for (i=0; i<NumBinaryOpNaNTests; i++)
1.883 + {
1.884 + const STwoOpTest *pT=&(BinaryOpNaNTests[i]);
1.885 + pT->Test(EAddEq,EFalse);
1.886 + pT->Test(EAddEq,ETrue);
1.887 + pT->Test(EAdd,EFalse);
1.888 + pT->Test(EAdd,ETrue);
1.889 + pT->Test(EPlusEq,EFalse);
1.890 + pT->Test(EPlusEq,ETrue);
1.891 + pT->Test(EPlus,EFalse);
1.892 + pT->Test(EPlus,ETrue);
1.893 + }
1.894 + }
1.895 +
1.896 +LOCAL_C void TestMultiplication()
1.897 + {
1.898 + TInt i;
1.899 + for (i=0; i<NumMultiplicationTests; i++)
1.900 + {
1.901 + const STwoOpTest *pT=&(MultiplicationTests[i]);
1.902 + pT->Test(EMultEq,EFalse);
1.903 + pT->Test(EMultEq,ETrue);
1.904 + pT->Test(EMult,EFalse);
1.905 + pT->Test(EMult,ETrue);
1.906 + pT->Test(EStarEq,EFalse);
1.907 + pT->Test(EStarEq,ETrue);
1.908 + pT->Test(EStar,EFalse);
1.909 + pT->Test(EStar,ETrue);
1.910 + }
1.911 + for (i=0; i<NumBinaryOpNaNTests; i++)
1.912 + {
1.913 + const STwoOpTest *pT=&(BinaryOpNaNTests[i]);
1.914 + pT->Test(EMultEq,EFalse);
1.915 + pT->Test(EMultEq,ETrue);
1.916 + pT->Test(EMult,EFalse);
1.917 + pT->Test(EMult,ETrue);
1.918 + pT->Test(EStarEq,EFalse);
1.919 + pT->Test(EStarEq,ETrue);
1.920 + pT->Test(EStar,EFalse);
1.921 + pT->Test(EStar,ETrue);
1.922 + }
1.923 + }
1.924 +
1.925 +LOCAL_C void TestDivision()
1.926 + {
1.927 + TInt i;
1.928 + for (i=0; i<NumDivisionTests; i++)
1.929 + {
1.930 + const STwoOpTest *pT=&(DivisionTests[i]);
1.931 + pT->Test(EDivEq,EFalse);
1.932 + pT->Test(EDiv,EFalse);
1.933 + pT->Test(ESlashEq,EFalse);
1.934 + pT->Test(ESlash,EFalse);
1.935 + }
1.936 + for (i=0; i<NumBinaryOpNaNTests; i++)
1.937 + {
1.938 + const STwoOpTest *pT=&(BinaryOpNaNTests[i]);
1.939 + pT->Test(EDivEq,EFalse);
1.940 + pT->Test(EDiv,EFalse);
1.941 + pT->Test(ESlashEq,EFalse);
1.942 + pT->Test(ESlash,EFalse);
1.943 + }
1.944 + }
1.945 +
1.946 +LOCAL_C void TestComparison()
1.947 + {
1.948 + TInt i;
1.949 + for (i=0; i<NumComparisonTests; i++)
1.950 + {
1.951 + const SCompareTest *pT=&(ComparisonTests[i]);
1.952 + pT->Test();
1.953 + }
1.954 + }
1.955 +
1.956 +
1.957 +GLDEF_C TInt E32Main()
1.958 +//
1.959 +// Test TRealX
1.960 +//
1.961 + {
1.962 +
1.963 + User::SetJustInTime(EFalse);
1.964 + test.Title();
1.965 + test.Start(_L("Assignment Operator and Constructors"));
1.966 + TestAssignConstruct();
1.967 + test.Next(_L("Conversions"));
1.968 + TestConversions();
1.969 + test.Next(_L("Setting and Checking Specials"));
1.970 + TestSpecials();
1.971 + test.Next(_L("Addition"));
1.972 + TestAddition();
1.973 + test.Next(_L("Multiplication"));
1.974 + TestMultiplication();
1.975 + test.Next(_L("Division"));
1.976 + TestDivision();
1.977 + test.Next(_L("Unary Operators"));
1.978 + TestUnaryOperators();
1.979 + test.Next(_L("Comparisons"));
1.980 + TestComparison();
1.981 +
1.982 + test.End();
1.983 + return(KErrNone);
1.984 + }