**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / Mono.Math / SearchGeneratorTest.cs
blob04539b946a2e73f543ab207e920af74ba51852e6
1 //
2 // MonoTests.Mono.Math.PrimeTestingTest.cs
3 //
4 // Authors:
5 // Ben Maurer
6 //
7 // Copyright (c) 2003 Ben Maurer. All rights reserved
8 //
10 using System;
11 using Mono.Math;
12 using Mono.Math.Prime.Generator;
13 using NUnit.Framework;
15 namespace MonoTests.Mono.Math {
17 [TestFixture]
18 public class SearchGenerator_Test : SequentialSearchPrimeGeneratorBase {
20 struct ContextData {
21 public ContextData (int bits, uint testData)
23 this.bits = bits; this.testData = testData;
25 public int bits;
26 public uint 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);
42 return ret;
46 protected override bool IsPrimeAcceptable (BigInteger bi, object Context)
48 return bi.testBit (1);
51 [Test]
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));