-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhash_test.go
72 lines (63 loc) · 1.13 KB
/
hash_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
package bitacoin
import (
"bytes"
"fmt"
"testing"
)
func TestGenerateMask(t *testing.T) {
data := []struct {
In int
Out []byte
}{
{
In: 0,
Out: []byte{},
},
{
In: 1,
Out: []byte{0xf},
},
{
In: 2,
Out: []byte{0},
},
{
In: 3,
Out: []byte{0, 0xf},
},
}
for i, d := range data {
t.Run(fmt.Sprintf("Test %d", i), func(t *testing.T) {
t.Parallel()
out := GenerateMask(d.In)
if !bytes.Equal(out, d.Out) {
t.Errorf("Failed for %d, it should be %x but is %x", d.In, d.Out, out)
}
})
}
}
func TestDifficultHash(t *testing.T) {
mask := GenerateMask(2)
hash, nonce := DifficultHash(mask, "a", "b", []byte("abc"))
if !GoodEnough(mask, hash) {
t.Errorf("Hash is not compatibe with mask")
}
easy := EasyHash("a", "b", []byte("abc"), nonce)
if !bytes.Equal(easy, hash) {
t.Error("Hash is not valid")
}
}
func ExampleGenerateMask() {
mask := GenerateMask(3)
fmt.Printf("%x", mask)
// Output:
// 000f
}
func BenchmarkDifficultHash(b *testing.B) {
mask := GenerateMask(3)
b.ResetTimer()
for i := 0; i < b.N; i++ {
DifficultHash(mask, "a", "b", i)
}
b.ReportAllocs()
}