Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SearchGeneratorTest.cs « Mono.Math « Test « Mono.Security « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 80434f9983ca1dd3296fe96cfdfbdf61372dc34f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// MonoTests.Mono.Math.PrimeTestingTest.cs
//
// Authors:
//	Ben Maurer
//
// Copyright (c) 2003 Ben Maurer. All rights reserved
//

using System;
using Mono.Math;
using Mono.Math.Prime;
using Mono.Math.Prime.Generator;
using NUnit.Framework;

namespace MonoTests.Mono.Math {

	[TestFixture]
	public class SearchGenerator_Test : SequentialSearchPrimeGeneratorBase {

		struct ContextData {
			public ContextData (int bits, uint testData)
			{
				this.bits = bits; this.testData = testData;
			}
			public int bits;
			public uint testData;
		}

		protected override BigInteger GenerateSearchBase (int bits, object Context)
		{
			BigInteger ret = base.GenerateSearchBase (bits, Context);

			ContextData ctx = (ContextData)Context;

			
			Assertion.AssertEquals (ctx.bits, bits);
			uint d = ctx.testData;

			for (uint i = (uint)bits - 2; d > 0; i--, d >>= 1)
				ret.SetBit (i, (d&1) == 1);

			return ret;
			
		}

		public override PrimalityTest PrimalityTest {
			get {
				return new PrimalityTest (PrimalityTests.SmallPrimeSppTest);
			}
		}

		protected override bool IsPrimeAcceptable (BigInteger bi, object Context)
		{
			return bi.TestBit (1);
		}

		[Test]
		public void TestPrimeGeneration ()
		{
			Random r = new Random ();
			for (int i = 0; i < 5; i++) {
				ContextData ctx = new ContextData (128, (uint)r.Next (int.MinValue, int.MaxValue));
				BigInteger p = GenerateNewPrime (128, ctx);
				Assert.IsTrue (p.TestBit (1));
				uint d = ctx.testData;
				for (uint j = 128 - 2; d > 0; j--, d >>= 1)
					Assertion.AssertEquals ((d&1) == 1, p.TestBit (j));
			}
		}
	}
}