2 // MonoTests.Mono.Math.PrimeTestingTest.cs
7 // Copyright (c) 2003 Ben Maurer. All rights reserved
12 using Mono
.Math
.Prime
.Generator
;
13 using NUnit
.Framework
;
15 namespace MonoTests
.Mono
.Math
{
18 public class SearchGenerator_Test
: SequentialSearchPrimeGeneratorBase
{
21 public ContextData (int bits
, uint testData
)
23 this.bits
= bits
; this.testData
= testData
;
29 protected override BigInteger
GenerateSearchBase (int bits
, object Context
)
31 BigInteger ret
= base.GenerateSearchBase (bits
, Context
);
33 ContextData ctx
= (ContextData
)Context
;
36 Assertion
.AssertEquals (ctx
.bits
, bits
);
37 uint d
= ctx
.testData
;
39 for (uint i
= (uint)bits
- 2; d
> 0; i
--, d
>>= 1)
40 ret
.setBit (i
, (d
&1) == 1);
46 protected override bool IsPrimeAcceptable (BigInteger bi
, object Context
)
48 return bi
.testBit (1);
52 public void TestPrimeGeneration ()
54 Random r
= new Random ();
55 for (int i
= 0; i
< 5; i
++) {
56 ContextData ctx
= new ContextData (128, (uint)r
.Next (int.MinValue
, int.MaxValue
));
57 BigInteger p
= GenerateNewPrime (128, ctx
);
58 Assertion
.Assert (p
.testBit (1));
59 uint d
= ctx
.testData
;
60 for (uint j
= 128 - 2; d
> 0; j
--, d
>>= 1)
61 Assertion
.AssertEquals ((d
&1) == 1, p
.testBit (j
));