-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimes_test.go
73 lines (61 loc) · 1.53 KB
/
primes_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"crypto/md5"
"fmt"
"testing"
)
func Test_Allocate(t *testing.T) {
var expected = map[uint64]int{
0: 0,
1: 0,
2: 0,
3: 1,
130: 1,
131: 2,
4294967297: 33554432,
4294967298: 33554432,
4294967299: 33554433,
}
for limit, ints := range expected {
primes := allocate(limit)
if len(primes) != ints {
t.Errorf("allocate() didn't allocate the correct number of ints: limit=%v ints=%v expected=%v", limit, len(primes), ints)
return
}
}
t.Log("allocate() tests completed")
}
func Test_Sieve(t *testing.T) {
var expected = map[uint64]string{
1: "26ab0db90d72e28ad0ba1e22ee510510",
100: "45d886e08500b82881519d5cf5cbe1d6",
1000: "2d2382f376350089fd94503d7da478db",
10000: "c37d039ddada44d3976ed948c3d0ef21",
100000: "ba89921a4ba02bb51ee28d66dbfc3451",
1000000: "c13929ee9d2aea8f83aa076236079e94",
10000000: "60e34d268bad671a5f299e1ecc988ff6",
}
for limit, md5sum := range expected {
primes := allocate(limit)
sieve(limit, primes)
m := md5.New()
fmt.Fprintln(m, 2)
for k, p := range primes {
for l := uint8(0); l < 64; l++ {
if (p>>l)&1 == 0 {
fmt.Fprintln(m, (k*64+int(l))*2+3)
}
}
}
hash := fmt.Sprintf("%x", m.Sum(nil))
if hash != md5sum {
t.Errorf("md5 doesn't match: limit=%v md5=%v expected=%v", limit, hash, md5sum)
}
}
t.Log("sieve() tests completed")
}
func Benchmark_Primes(b *testing.B) {
primes := allocate(uint64(b.N))
b.ResetTimer()
sieve(uint64(b.N), primes)
}