sl@0: /* sl@0: * Copyright (c) 2002-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: * sl@0: */ sl@0: sl@0: sl@0: #include "tbasicmathsfb.h" sl@0: #include "t_input.h" sl@0: #include "t_output.h" sl@0: #include sl@0: #include sl@0: sl@0: CTestAction* CBasicMathsFB::NewL(RFs& aFs, CConsoleBase& aConsole, sl@0: Output& aOut, const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction* self = CBasicMathsFB::NewLC(aFs, aConsole, sl@0: aOut, aTestActionSpec); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CTestAction* CBasicMathsFB::NewLC(RFs& aFs, CConsoleBase& aConsole, sl@0: Output& aOut, const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CBasicMathsFB* self = new(ELeave) CBasicMathsFB(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: return self; sl@0: } sl@0: sl@0: CBasicMathsFB::~CBasicMathsFB() sl@0: { sl@0: delete iBody; sl@0: } sl@0: sl@0: CBasicMathsFB::CBasicMathsFB(RFs& aFs, CConsoleBase& aConsole, Output& aOut) sl@0: : CTestAction(aConsole, aOut), iFs(aFs) sl@0: { sl@0: } sl@0: sl@0: void CBasicMathsFB::ConstructL(const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction::ConstructL(aTestActionSpec); sl@0: sl@0: iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length()); sl@0: iBody->Des().Copy(aTestActionSpec.iActionBody); sl@0: sl@0: //HBufC8* length = Input::ParseElementHexL(*iBody, _L8("")); sl@0: TUint bits = Input::ParseIntElement(*iBody, _L8(""), _L8("")); sl@0: // the final /7 gives the number of times we have to increment by 7 to get sl@0: // to that number of bytes and hence bits. sl@0: iIterations = ((bits+7)/8)/7 + 1; sl@0: } sl@0: sl@0: void CBasicMathsFB::DoPerformPrerequisite(TRequestStatus& aStatus) sl@0: { sl@0: TRequestStatus* status = &aStatus; sl@0: User::RequestComplete(status, KErrNone); sl@0: iActionState = CTestAction::EAction; sl@0: } sl@0: sl@0: void CBasicMathsFB::DoPerformPostrequisite(TRequestStatus& aStatus) sl@0: { sl@0: TRequestStatus* status = &aStatus; sl@0: iFinished = ETrue; sl@0: User::RequestComplete(status, KErrNone); sl@0: } sl@0: sl@0: void CBasicMathsFB::DoReportAction(void) sl@0: { sl@0: } sl@0: sl@0: void CBasicMathsFB::DoCheckResult(TInt) sl@0: { sl@0: } sl@0: sl@0: void CBasicMathsFB::PerformAction(TRequestStatus& aStatus) sl@0: { sl@0: __UHEAP_MARK; sl@0: TRequestStatus* status = &aStatus; sl@0: iResult = ETrue; sl@0: sl@0: //min max values for NewRandomLC call sl@0: RInteger min = RInteger::NewL(10); sl@0: CleanupStack::PushL(min); sl@0: RInteger max = RInteger::NewL(100); sl@0: CleanupStack::PushL(max); sl@0: sl@0: //Generate iIterations*7 byte random sequences we are using 7 as it's a generator sl@0: //mod 8. Thus we'll cycle through every value (0-7) every 8 iterations. sl@0: //This gives us a better feeling that certain byte lengths (and thus bit sl@0: //lengths as the byte is chosen randomly) don't have errors. sl@0: for(TUint i=1; iDes(); sl@0: TRandom::RandomL(ptr); sl@0: sl@0: //This is this iteration's random number sl@0: RInteger initial = RInteger::NewL(ptr); sl@0: CleanupStack::PushL(initial); sl@0: sl@0: //get a number x | 10 < x < 100 sl@0: RInteger crange = RInteger::NewRandomL(min, max); sl@0: CleanupStack::PushL(crange); sl@0: TUint range = crange.ConvertToLongL(); sl@0: CleanupStack::PopAndDestroy(); //crange sl@0: sl@0: AddSub(initial, range); sl@0: MulDiv(initial, range); sl@0: //GCD sl@0: CleanupStack::PopAndDestroy(); //initial sl@0: CleanupStack::PopAndDestroy();//buf sl@0: iConsole.Printf(_L(".")); sl@0: } sl@0: sl@0: //Test a single iteration where the initial random number is less than a sl@0: //word so the division and modulo routines that take words rather than sl@0: //TIntegers can run. sl@0: //do sl@0: { sl@0: //This is this iteration's random number sl@0: RInteger initial = RInteger::NewRandomL(31); sl@0: CleanupStack::PushL(initial); sl@0: //get a number x | 10 < x < 100 sl@0: RInteger crange = RInteger::NewRandomL(min, max); sl@0: CleanupStack::PushL(crange); sl@0: TUint range = crange.ConvertToLongL(); sl@0: CleanupStack::PopAndDestroy(&crange); //crange sl@0: sl@0: AddSub(initial, range); sl@0: MulDiv(initial, range); sl@0: CleanupStack::PopAndDestroy(&initial); //initial sl@0: iConsole.Printf(_L(".")); sl@0: } //while (0); sl@0: sl@0: CleanupStack::PopAndDestroy();//max sl@0: CleanupStack::PopAndDestroy(); //min sl@0: sl@0: MiscDivL(); sl@0: sl@0: User::RequestComplete(status, KErrNone); sl@0: iActionState = CTestAction::EPostrequisite; sl@0: __UHEAP_MARK; sl@0: } sl@0: sl@0: void CBasicMathsFB::AddSub(const TInteger& aInitial, TUint aRange) sl@0: { sl@0: __UHEAP_MARK; sl@0: //This is the copy we are going to do stuff to sl@0: RInteger a = RInteger::NewL(aInitial); sl@0: CleanupStack::PushL(a); sl@0: sl@0: // compute a*aRange using doubling sl@0: TUint j=1; sl@0: for(; j= dividend; sl@0: if (!res0) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: sl@0: RInteger quotient; sl@0: CleanupStack::PushL(quotient); sl@0: // 10 / 2 = 5 sl@0: TRAPD(res, quotient = dividend.DividedByL(diviser)); sl@0: if (res != KErrNone) sl@0: { sl@0: User::Leave(res); sl@0: } sl@0: else if (quotient != (dividendInt/diviser)) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: sl@0: // Test for greater value TRUE and equality FALSE sl@0: res0 = dividend >= quotient; sl@0: if (!res0) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: sl@0: // Test for greater value FALSE and equality FALSE sl@0: res0 = quotient >= dividend; sl@0: if (res0) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: sl@0: // 10 / 10 = 1 sl@0: TRAPD(res1, dividend /= dividendInt); sl@0: if (res1 != KErrNone) sl@0: { sl@0: User::Leave(res); sl@0: } sl@0: else if (dividend != (dividendInt/seed)) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: sl@0: // 1 % 10 = 1 (dividend = 1, due to last step) sl@0: TRAPD(res2, dividend %= dividendInt); sl@0: if (res2 != KErrNone) sl@0: { sl@0: User::Leave(res); sl@0: } sl@0: else if (dividend != (dividendInt/seed)) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: sl@0: // 1 x 1 = 1 (dividend = 1, due to last step) sl@0: RInteger squaredInt = dividend.SquaredL(); sl@0: CleanupStack::PushL(squaredInt); sl@0: if ( squaredInt != (dividendInt/seed)) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: sl@0: RInteger expSeed = RInteger::NewL(10); sl@0: CleanupStack::PushL(expSeed); sl@0: RInteger exponent = RInteger::NewL(3); sl@0: CleanupStack::PushL(exponent); sl@0: RInteger expResult; sl@0: CleanupStack::PushL(expResult); sl@0: TRAPD(res3, expResult = expSeed.ExponentiateL(exponent)); sl@0: if (res3 != KErrNone) sl@0: { sl@0: User::Leave(res); sl@0: } sl@0: else if (expResult != (10*10*10)) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(6, ÷nd); // dividend, quotient, squardInt, expSeed, exponent, expResult sl@0: __UHEAP_MARKEND; sl@0: }