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 * Defines methods common to all test classes
24 _LIT(KParametersStart, "<parameters>");
25 _LIT(KValuesStart, "<values>");
26 _LIT(KRandomStart, "<random>");
27 const TInt KMaxValuesSize = 1024;
30 CTestBase::CTestBase(CASN1NormalTest &aASN1Action)
31 : iASN1Action(aASN1Action)
35 CTestBase::~CTestBase()
39 iValues->ResetAndDestroy();
43 CTestAction::TScriptError CTestBase::ConstructL(const TTestActionSpec& aTestActionSpec)
45 CTestAction::TScriptError syntaxError;
46 iParameters = new (ELeave)RArray<CTestParameter::TType>;
47 iValues = new (ELeave)RPointerArray<CTestParameter>;
50 HBufC* aBody = HBufC::NewLC(aTestActionSpec.iActionBody.Length());
52 aBody->Des().Copy(aTestActionSpec.iActionBody);
53 TPtrC parameters(Input::ParseElement(*aBody, KParametersStart));
55 syntaxError = CheckValueParametersL(parameters);
56 if(syntaxError==CTestAction::ENone)
57 syntaxError = CheckRandomParametersL(parameters);
59 CleanupStack::PopAndDestroy();
64 CTestAction::TScriptError CTestBase::CheckValueParametersL(const TDesC& aParameters)
66 TInt dummyPos = 0, valuePos = 0, lastDummy = 0;
67 TBuf<KMaxValueSize> singleValue;
68 CTestParameter *testParameter=0;
69 TBuf<KMaxValuesSize> values;
71 // finds values to run test with
75 values = Input::ParseElement(aParameters, KValuesStart, dummyPos);
77 // finds the next group of value
78 if(lastDummy != dummyPos)
80 // parses each value in group with its expeced type
81 for(TInt parameter = 0; parameter < iParameters->Count(); parameter++)
84 // checks if its last parameter
85 if(parameter < iParameters->Count() - 1)
88 valuePos = values.Find((_L(",")));
91 // fills singleValue with value
92 if(valuePos > KMaxValueSize)
94 iSyntaxErrorDescription.Copy(_L("value too long"));
95 return(CTestAction::ESyntax);
97 singleValue.Copy(values.Left(valuePos));
98 values = values.Mid(valuePos + 1);
102 // couldnt find it there is a problem
103 iSyntaxErrorDescription.Copy(_L("Invalid number of parameters"));
104 return(CTestAction::ESyntax);
109 // its the last value fill singleValue with it
110 if(values.Length() > KMaxValueSize)
112 iSyntaxErrorDescription.Copy(_L("value too long"));
113 return(CTestAction::ESyntax);
115 singleValue.Copy(values);
118 // trims any white space
120 // checks what type its supposed to be
121 switch((*iParameters)[parameter])
123 case CTestParameter::EInt:
125 // its an int, check to see if its a range
126 if(singleValue.Find(_L("..")) == KErrNotFound)
127 testParameter = CIntTestParameter::NewL(singleValue);
129 testParameter = CIntRangeTestParameter::NewL(singleValue);
132 case CTestParameter::EString:
135 testParameter = CStringTestParameter::NewL(singleValue);
138 case CTestParameter::EIntRange:
139 case CTestParameter::ERandom:
140 break; // Nothing to do
142 // if testparamer is found add it to the list
145 if(testParameter->iValid)
146 iValues->Append(testParameter);
149 delete testParameter;
150 iSyntaxErrorDescription.Copy(_L("Invalid value "));
151 iSyntaxErrorDescription.Append(singleValue);
152 return(CTestAction::ESyntax);
158 while(lastDummy != dummyPos);
160 return(CTestAction::ENone);
163 CTestAction::TScriptError CTestBase::CheckRandomParametersL(const TDesC& aParameters)
165 TInt dummyPos = 0, lastDummy = 0;
166 TBuf<KMaxValueSize> singleValue;
167 CTestParameter *testParameter=0;
168 TBuf<KMaxValuesSize> values;
170 // finds any random tests
173 lastDummy = dummyPos;
174 values = Input::ParseElement(aParameters, KRandomStart, dummyPos);
176 if(lastDummy != dummyPos)
178 if(values.Length() > KMaxValueSize)
180 iSyntaxErrorDescription.Copy(_L("value too long"));
181 return(CTestAction::ESyntax);
183 singleValue.Copy(values);
184 // found one create a random parameter with its interators
185 testParameter = CRandomTestParameter::NewL(singleValue);
189 if(testParameter->iValid)
190 iValues->Append(testParameter);
193 delete testParameter;
194 iSyntaxErrorDescription.Copy(_L("Invalid value "));
195 iSyntaxErrorDescription.Append(singleValue);
196 return(CTestAction::ESyntax);
201 while(lastDummy != dummyPos);
203 return(CTestAction::ENone);
206 void CTestBase::OutputEncodingL(CConsoleBase& aConsole, TDesC8& aData)
208 aConsole.Printf(_L("Encoding: Length = "));
210 bits.AppendNum(aData.Length());
211 bits.Append(_L(", data = "));
212 aConsole.Printf(bits);
214 TInt size = aData.Length();
215 for (TInt i = 0; i < size; ++i)
218 tbuf.AppendNumFixedWidth(aData[i], EHex, 2);
219 aConsole.Printf(tbuf);
222 aConsole.Printf(_L("\n"));
225 TBool CTestBase::CountTests(TInt &totalTests)
227 CTestParameter* test;
228 TInt totalRandomTests = 0;
229 TInt totalParameters = 0;
230 TInt totalRangeTests = 0;
234 // counts number of tests to do for type
235 for(TInt pos = 0; pos < iValues->Count(); pos++)
237 test = (*iValues)[pos];
238 switch(test->GetType())
240 case CTestParameter::EInt :
241 case CTestParameter::EString :
247 case CTestParameter::EIntRange :
249 CIntRangeTestParameter *rangeInt = REINTERPRET_CAST(CIntRangeTestParameter*, test);
250 totalRangeTests += rangeInt->Range();
254 case CTestParameter::ERandom :
256 CRandomTestParameter *randomInt = REINTERPRET_CAST(CRandomTestParameter*, test);
258 totalRandomTests+= randomInt->Interations();
264 // only count each test once, remember each test can only have one parameter field of
266 totalTests = totalRandomTests + totalRangeTests + (totalParameters / iParameters->Count());
267 // checks if tests correct number of parameters for type
268 return((totalParameters % iParameters->Count()) == 0);