diff -r 000000000000 -r 2c201484c85f crypto/weakcrypto/test/tbigint/tbasicmathsfb.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/crypto/weakcrypto/test/tbigint/tbasicmathsfb.cpp Wed Jul 08 11:25:26 2009 +0100 @@ -0,0 +1,341 @@ +/* +* Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of the License "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: +* +*/ + + +#include "tbasicmathsfb.h" +#include "t_input.h" +#include "t_output.h" +#include +#include + +CTestAction* CBasicMathsFB::NewL(RFs& aFs, CConsoleBase& aConsole, + Output& aOut, const TTestActionSpec& aTestActionSpec) + { + CTestAction* self = CBasicMathsFB::NewLC(aFs, aConsole, + aOut, aTestActionSpec); + CleanupStack::Pop(); + return self; + } + +CTestAction* CBasicMathsFB::NewLC(RFs& aFs, CConsoleBase& aConsole, + Output& aOut, const TTestActionSpec& aTestActionSpec) + { + CBasicMathsFB* self = new(ELeave) CBasicMathsFB(aFs, aConsole, aOut); + CleanupStack::PushL(self); + self->ConstructL(aTestActionSpec); + return self; + } + +CBasicMathsFB::~CBasicMathsFB() + { + delete iBody; + } + +CBasicMathsFB::CBasicMathsFB(RFs& aFs, CConsoleBase& aConsole, Output& aOut) + : CTestAction(aConsole, aOut), iFs(aFs) + { + } + +void CBasicMathsFB::ConstructL(const TTestActionSpec& aTestActionSpec) + { + CTestAction::ConstructL(aTestActionSpec); + + iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length()); + iBody->Des().Copy(aTestActionSpec.iActionBody); + + //HBufC8* length = Input::ParseElementHexL(*iBody, _L8("")); + TUint bits = Input::ParseIntElement(*iBody, _L8(""), _L8("")); + // the final /7 gives the number of times we have to increment by 7 to get + // to that number of bytes and hence bits. + iIterations = ((bits+7)/8)/7 + 1; + } + +void CBasicMathsFB::DoPerformPrerequisite(TRequestStatus& aStatus) + { + TRequestStatus* status = &aStatus; + User::RequestComplete(status, KErrNone); + iActionState = CTestAction::EAction; + } + +void CBasicMathsFB::DoPerformPostrequisite(TRequestStatus& aStatus) + { + TRequestStatus* status = &aStatus; + iFinished = ETrue; + User::RequestComplete(status, KErrNone); + } + +void CBasicMathsFB::DoReportAction(void) + { + } + +void CBasicMathsFB::DoCheckResult(TInt) + { + } + +void CBasicMathsFB::PerformAction(TRequestStatus& aStatus) + { + __UHEAP_MARK; + TRequestStatus* status = &aStatus; + iResult = ETrue; + + //min max values for NewRandomLC call + RInteger min = RInteger::NewL(10); + CleanupStack::PushL(min); + RInteger max = RInteger::NewL(100); + CleanupStack::PushL(max); + + //Generate iIterations*7 byte random sequences we are using 7 as it's a generator + //mod 8. Thus we'll cycle through every value (0-7) every 8 iterations. + //This gives us a better feeling that certain byte lengths (and thus bit + //lengths as the byte is chosen randomly) don't have errors. + for(TUint i=1; iDes(); + TRandom::RandomL(ptr); + + //This is this iteration's random number + RInteger initial = RInteger::NewL(ptr); + CleanupStack::PushL(initial); + + //get a number x | 10 < x < 100 + RInteger crange = RInteger::NewRandomL(min, max); + CleanupStack::PushL(crange); + TUint range = crange.ConvertToLongL(); + CleanupStack::PopAndDestroy(); //crange + + AddSub(initial, range); + MulDiv(initial, range); + + //GCD + CleanupStack::PopAndDestroy(); //initial + CleanupStack::PopAndDestroy();//buf + iConsole.Printf(_L(".")); + } + + //Test a single iteration where the initial random number is less than a + //word so the division and modulo routines that take words rather than + //TIntegers can run. + //do + { + //This is this iteration's random number + RInteger initial = RInteger::NewRandomL(31); + CleanupStack::PushL(initial); + //get a number x | 10 < x < 100 + RInteger crange = RInteger::NewRandomL(min, max); + CleanupStack::PushL(crange); + TUint range = crange.ConvertToLongL(); + CleanupStack::PopAndDestroy(&crange); //crange + + AddSub(initial, range); + MulDiv(initial, range); + CleanupStack::PopAndDestroy(&initial); //initial + iConsole.Printf(_L(".")); + } //while (0); + + CleanupStack::PopAndDestroy();//max + CleanupStack::PopAndDestroy(); //min + + User::RequestComplete(status, KErrNone); + iActionState = CTestAction::EPostrequisite; + __UHEAP_MARK; + } + +void CBasicMathsFB::AddSub(const TInteger& aInitial, TUint aRange) + { + __UHEAP_MARK; + //This is the copy we are going to do stuff to + RInteger a = RInteger::NewL(aInitial); + CleanupStack::PushL(a); + + // compute a*aRange using doubling + TUint j=1; + for(; j