First public contribution.
2 * Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Implementation for testing TInt encoding/decoding
21 #include "tasn1normaltest.h"
29 CTestInt* CTestInt::NewL(CASN1NormalTest &aASN1Action)
31 CTestInt* test = new (ELeave) CTestInt(aASN1Action);
35 CTestInt::CTestInt(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
40 void CTestInt::GetName(TDes& aBuf)
42 aBuf.Copy(_L("Test TInt"));
45 void CTestInt::FillParameterArray(void)
47 iParameters->Append(CTestParameter::EInt);
52 TBool CTestInt::PerformTest(CConsoleBase& aConsole, const TInt &aTest, const TInt &aTestNumber, const TInt &aTotalTests)
54 // Choose value to encode
60 CASN1EncInt* encoder = CASN1EncInt::NewLC(encodedValue);
63 TUint length = encoder->LengthDER();
64 HBufC8* buf = HBufC8::NewMaxLC(length);
65 TPtr8 tBuf = buf->Des();
67 // Write into the buffer
68 TUint writeLength = 0;
69 encoder->WriteDERL(tBuf, writeLength);
72 TASN1DecInteger decoder;
74 TInt decodedValue = decoder.DecodeDERShortL(tBuf, readLength);
76 // Check lengths of reads + values
77 if ((writeLength != STATIC_CAST(TUint, readLength)) || (decodedValue != encodedValue))
79 aConsole.Printf(_L("\nERROR! Problem integer: %d\n"), encodedValue);
80 iASN1Action.ReportProgressL(KErrASN1EncodingError, aTestNumber, aTotalTests);
81 CleanupStack::PopAndDestroy(2); // buf, encoder
86 iASN1Action.ReportProgressL(KErrNone, aTestNumber, aTotalTests);
87 CleanupStack::PopAndDestroy(2); // buf, encoder
92 TBool CTestInt::PerformTestsL(CConsoleBase& aConsole)
94 TInt nLow = Math::Random();
95 TInt64 nHigh((TInt)Math::Random());
96 TInt64 seed((nHigh << 32) + nLow);
98 TInt totalTests, currentTest=0;
100 if(!CountTests(totalTests)) return(EFalse);
102 for(TInt pos = 0; pos < iValues->Count(); pos++)
104 test = (*iValues)[pos];
105 switch(test->GetType())
107 case CTestParameter::EInt :
109 CIntTestParameter *rangeInt = REINTERPRET_CAST(CIntTestParameter*, test);
111 if(!PerformTest(aConsole, rangeInt->Value(), currentTest, totalTests))
118 case CTestParameter::EIntRange :
120 CIntRangeTestParameter *rangeInt = REINTERPRET_CAST(CIntRangeTestParameter*, test);
122 for(TInt test = rangeInt->Start(); test <= rangeInt->Finish(); test++)
124 if(!PerformTest(aConsole, test, currentTest, totalTests))
132 case CTestParameter::ERandom :
134 CRandomTestParameter *randomInt = REINTERPRET_CAST(CRandomTestParameter*, test);
137 for(TInt test = 0; test <= randomInt->Interations(); test++)
139 encodedValue = Math::Rand(seed) >> (8 * (test % 4));
140 if(!PerformTest(aConsole, encodedValue, currentTest, totalTests))
154 iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests);