|
| 1 | +package prime |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +var primeList = []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127} |
| 9 | +var testLimit = 127 |
| 10 | + |
| 11 | +type primalityTest func(int64) bool |
| 12 | + |
| 13 | +func primalityTestTestingHelper(t *testing.T, name string, f primalityTest) { |
| 14 | + arrayIndex := 0 |
| 15 | + for i := 1; i <= testLimit; i++ { |
| 16 | + isPrime := i == primeList[arrayIndex] |
| 17 | + |
| 18 | + testName := fmt.Sprintf("%s(%d)", name, i) |
| 19 | + t.Run(testName, func(t *testing.T) { |
| 20 | + result := f(int64(i)) |
| 21 | + |
| 22 | + if isPrime { |
| 23 | + arrayIndex++ |
| 24 | + } |
| 25 | + |
| 26 | + if result != isPrime { |
| 27 | + t.Errorf("%d: %s function returned %v\n", i, name, result) |
| 28 | + } |
| 29 | + }) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func primalityTestBenchmarkHelper(b *testing.B, f primalityTest) { |
| 34 | + for i := 0; i < b.N; i++ { |
| 35 | + f(104729) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// Miller-Rabin Probabilistic test |
| 40 | + |
| 41 | +func millerRabinProbabilisticTester(n int64) bool { |
| 42 | + result, err := MillerRabinProbabilistic(n, 40) |
| 43 | + if err != nil { |
| 44 | + panic(err) |
| 45 | + } |
| 46 | + |
| 47 | + return result |
| 48 | +} |
| 49 | + |
| 50 | +func TestMillerRabinProbabilistic(t *testing.T) { |
| 51 | + primalityTestTestingHelper(t, "Miller-Rabin Probabilistic", millerRabinProbabilisticTester) |
| 52 | +} |
| 53 | + |
| 54 | +func BenchmarkMillerRabinProbabilistic(b *testing.B) { |
| 55 | + primalityTestBenchmarkHelper(b, millerRabinProbabilisticTester) |
| 56 | +} |
| 57 | + |
| 58 | +// Miller-Rabin deterministic test |
| 59 | + |
| 60 | +func millerRabinDeterministicTester(n int64) bool { |
| 61 | + result, err := MillerRabinDeterministic(n) |
| 62 | + if err != nil { |
| 63 | + panic(err) |
| 64 | + } |
| 65 | + |
| 66 | + return result |
| 67 | +} |
| 68 | + |
| 69 | +func TestMillerRabinDeterministic(t *testing.T) { |
| 70 | + primalityTestTestingHelper(t, "Miller-Rabin Deterministic", millerRabinDeterministicTester) |
| 71 | +} |
| 72 | + |
| 73 | +func BenchmarkMillerRabinDeterministic(b *testing.B) { |
| 74 | + primalityTestBenchmarkHelper(b, millerRabinDeterministicTester) |
| 75 | +} |
| 76 | + |
| 77 | +// Trial Division test |
| 78 | + |
| 79 | +func TestTrialDivision(t *testing.T) { |
| 80 | + primalityTestTestingHelper(t, "Trial Division", TrialDivision) |
| 81 | +} |
| 82 | + |
| 83 | +func BenchmarkTrialDivision(b *testing.B) { |
| 84 | + primalityTestBenchmarkHelper(b, TrialDivision) |
| 85 | +} |
| 86 | + |
| 87 | +// Trial Division (optimized) |
| 88 | + |
| 89 | +func TestOptimizedTrialDivision(t *testing.T) { |
| 90 | + primalityTestTestingHelper(t, "Trial Division (optimized)", OptimizedTrialDivision) |
| 91 | +} |
| 92 | + |
| 93 | +func BenchmarkOptimizedTrialDivision(b *testing.B) { |
| 94 | + primalityTestBenchmarkHelper(b, OptimizedTrialDivision) |
| 95 | +} |
0 commit comments