-
Notifications
You must be signed in to change notification settings - Fork 97
/
enumprimes_test.go
48 lines (41 loc) · 1.75 KB
/
enumprimes_test.go
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
// Copyright (c) 2015, Peter Mrekaj. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.txt file.
package arrays
import (
"reflect"
"testing"
)
type genPrimeFn func(uint) ([]uint, bool)
func testGenPrimesFn(t *testing.T, fn genPrimeFn, fnName string) {
for _, test := range []struct {
in uint
want []uint
ok bool
}{
{1, nil, true},
{2, []uint{2}, true},
{3, []uint{2, 3}, true},
{7, []uint{2, 3, 5, 7}, true},
{15, []uint{2, 3, 5, 7, 11, 13}, true},
{25, []uint{2, 3, 5, 7, 11, 13, 17, 19, 23}, true},
{100, []uint{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}, true},
} {
if got, ok := fn(test.in); !reflect.DeepEqual(got, test.want) || ok != test.ok {
t.Errorf("%s(%d) = %v, %t; want %v, %t", fnName, test.in, got, ok, test.want, test.ok)
}
}
}
func TestGenPrimesTrialDiv(t *testing.T) { testGenPrimesFn(t, GenPrimesTrialDiv, "GenPrimesTrialDiv") }
func TestGenPrimesSieve(t *testing.T) { testGenPrimesFn(t, GenPrimesSieve, "GenPrimesSieve") }
func benchGenPrimesFn(b *testing.B, size uint, fn genPrimeFn) {
for i := 0; i < b.N; i++ {
fn(size)
}
}
func BenchmarkGenPrimesTrialDiv1e2(b *testing.B) { benchGenPrimesFn(b, 1e2, GenPrimesTrialDiv) }
func BenchmarkGenPrimesSieve1e2(b *testing.B) { benchGenPrimesFn(b, 1e2, GenPrimesSieve) }
func BenchmarkGenPrimesTrialDiv1e4(b *testing.B) { benchGenPrimesFn(b, 1e4, GenPrimesTrialDiv) }
func BenchmarkGenPrimesSieve1e4(b *testing.B) { benchGenPrimesFn(b, 1e4, GenPrimesSieve) }
func BenchmarkGenPrimesTrialDiv1e6(b *testing.B) { benchGenPrimesFn(b, 1e6, GenPrimesTrialDiv) }
func BenchmarkGenPrimesSieve1e6(b *testing.B) { benchGenPrimesFn(b, 1e6, GenPrimesSieve) }