|
1 /* |
|
2 * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
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". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * tprimeproblem.cpp |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include <bigint.h> |
|
21 #include <random.h> |
|
22 #include "tprimegenvector.h" |
|
23 #include "t_input.h" |
|
24 #include "t_output.h" |
|
25 #include "tutils.h" |
|
26 #include "../../source/bigint/mont.h" |
|
27 |
|
28 CTestAction* CPrimeGenVector::NewL(RFs& aFs, CConsoleBase& aConsole, |
|
29 Output& aOut, const TTestActionSpec& aTestActionSpec) |
|
30 { |
|
31 CTestAction* self = CPrimeGenVector::NewLC(aFs, aConsole, |
|
32 aOut, aTestActionSpec); |
|
33 CleanupStack::Pop(); |
|
34 return self; |
|
35 } |
|
36 |
|
37 CTestAction* CPrimeGenVector::NewLC(RFs& aFs, CConsoleBase& aConsole, |
|
38 Output& aOut, const TTestActionSpec& aTestActionSpec) |
|
39 { |
|
40 CPrimeGenVector* self = new(ELeave) CPrimeGenVector(aFs, aConsole, aOut); |
|
41 CleanupStack::PushL(self); |
|
42 self->ConstructL(aTestActionSpec); |
|
43 return self; |
|
44 } |
|
45 |
|
46 CPrimeGenVector::~CPrimeGenVector() |
|
47 { |
|
48 delete iBody; |
|
49 delete iRandomDes; |
|
50 delete iPrimeDes; |
|
51 iRandom.Close(); |
|
52 iPrime.Close(); |
|
53 } |
|
54 |
|
55 CPrimeGenVector::CPrimeGenVector(RFs& aFs, CConsoleBase& aConsole, Output& aOut) |
|
56 : CTestAction(aConsole, aOut), iFs(aFs) |
|
57 { |
|
58 } |
|
59 |
|
60 void CPrimeGenVector::ConstructL(const TTestActionSpec& aTestActionSpec) |
|
61 { |
|
62 CTestAction::ConstructL(aTestActionSpec); |
|
63 |
|
64 iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length()); |
|
65 iBody->Des().Copy(aTestActionSpec.iActionBody); |
|
66 |
|
67 iBits = Input::ParseIntElement(*iBody, _L8("<bits>"), _L8("</bits>")); |
|
68 iRandomDes = Input::ParseElementHexL(*iBody, _L8("<random>")); |
|
69 iPrimeDes = Input::ParseElementHexL(*iBody, _L8("<prime>")); |
|
70 } |
|
71 |
|
72 void CPrimeGenVector::DoPerformPrerequisite(TRequestStatus& aStatus) |
|
73 { |
|
74 iRandom = RInteger::NewL(*iRandomDes); |
|
75 iPrime = RInteger::NewL(*iPrimeDes); |
|
76 TRequestStatus* status = &aStatus; |
|
77 User::RequestComplete(status, KErrNone); |
|
78 iActionState = CTestAction::EAction; |
|
79 } |
|
80 |
|
81 void CPrimeGenVector::DoPerformPostrequisite(TRequestStatus& aStatus) |
|
82 { |
|
83 TRequestStatus* status = &aStatus; |
|
84 iFinished = ETrue; |
|
85 User::RequestComplete(status, KErrNone); |
|
86 } |
|
87 |
|
88 void CPrimeGenVector::DoReportAction(void) |
|
89 { |
|
90 } |
|
91 |
|
92 void CPrimeGenVector::DoCheckResult(TInt) |
|
93 { |
|
94 iResult = (iResult && iExpectedResult) || (!iResult && !iExpectedResult); |
|
95 if (iResult) |
|
96 iConsole.Printf(_L(".")); |
|
97 else |
|
98 iConsole.Printf(_L("X")); |
|
99 } |
|
100 |
|
101 void CPrimeGenVector::PerformAction(TRequestStatus& aStatus) |
|
102 { |
|
103 TRAPD(res, PerformActionL()); |
|
104 __ASSERT_ALWAYS(!res, User::Panic(_L("CPrimeGenVector::PerformAction"), res)); |
|
105 TRequestStatus* status = &aStatus; |
|
106 User::RequestComplete(status, KErrNone); |
|
107 iActionState = CTestAction::EPostrequisite; |
|
108 } |
|
109 |
|
110 void CPrimeGenVector::PerformActionL() |
|
111 { |
|
112 __UHEAP_MARK; |
|
113 |
|
114 iResult = EFalse;; |
|
115 |
|
116 //Returned number should be the next prime after our initial "random" number |
|
117 |
|
118 //This CRandomSetSource stuff is a little dodgy as: |
|
119 // 1) it has no NewL's with is fine as long as it doesn't allocate memory |
|
120 // 2) more importantly, the prime generation routines use random numbers to |
|
121 // determine whether or not the numbers it generates are prime. |
|
122 // Since CRandomSetSource has already exhausted its given list of "random" |
|
123 // numbers it returns 0's for use with anything else that calls it. It |
|
124 // makes the primality testing a little bogus if you were using it for real |
|
125 // things, but the test vectors that are there have already been verified |
|
126 // and are known to be prime. |
|
127 CRandomSetSource* incrementingRandom = new(ELeave)CRandomSetSource(*iRandomDes); |
|
128 SetThreadRandomLC(incrementingRandom); |
|
129 |
|
130 RInteger prime = RInteger::NewPrimeL(iBits); |
|
131 |
|
132 if(prime == iPrime) |
|
133 { |
|
134 iResult = ETrue; |
|
135 } |
|
136 prime.Close(); |
|
137 |
|
138 CleanupStack::PopAndDestroy(1); //SetThreadRandomLC |
|
139 |
|
140 __UHEAP_MARKEND; |
|
141 } |
|
142 |