-
Notifications
You must be signed in to change notification settings - Fork 4
/
singletons_test.go
75 lines (56 loc) · 1.86 KB
/
singletons_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
74
75
package lxr
import (
"bytes"
"fmt"
"testing"
)
func testSize(t *testing.T, bits uint64, buf []byte, reference string) {
one := Init(Seed, bits, HashSize, Passes)
two := Init(Seed, bits, HashSize, Passes)
defer Release(one)
defer Release(two)
if one != two {
t.Errorf("[%d] two separate pointers for singleton: %x and %x", bits, &one, &two)
}
three := new(LXRHash)
three.Init(Seed, bits, HashSize, Passes)
if one == three {
t.Errorf("[%d] separate instance is the same as singleton: %x and %x", bits, &one, &three)
}
res := fmt.Sprintf("%x", one.Hash(buf))
if res != reference {
t.Errorf("[%d] incorrect hash result, want = %s got = %s", bits, reference, res)
}
if !bytes.Equal(one.Hash(buf), two.Hash(buf)) {
t.Errorf("[%d] one and two provided different hash results", bits)
}
if !bytes.Equal(one.Hash(buf), three.Hash(buf)) {
t.Errorf("[%d] one and three provided different hash results", bits)
}
}
func TestInit(t *testing.T) {
buf := []byte("test string")
testSize(t, 8, buf, "abab21b95cee68a5d70d871161e092530638b3b4bd4e88cadab3a5d6bbcf5f80")
testSize(t, 9, buf, "c56d9652bc709713af194e2a64e0e2ff1bc0980b2395c772187186fbbf6cf9a9")
testSize(t, 10, buf, "3c7df3fac7481d630571926c9be01056b36822baccdf2b1872936194477df2ff")
}
func TestRelease(t *testing.T) {
one := Init(Seed, 8, HashSize, Passes)
oneB := Init(Seed, 8, HashSize, Passes)
Release(one)
two := Init(Seed, 8, HashSize, Passes)
if one != two {
t.Errorf("released the instance despite another reference")
}
Release(oneB)
Release(two)
oneB = Init(Seed, 8, HashSize, Passes)
if one == oneB {
t.Errorf("singleton wasn't released after all references destroyed")
}
buf := []byte("test string")
res := fmt.Sprintf("%x", one.Hash(buf))
if res != "abab21b95cee68a5d70d871161e092530638b3b4bd4e88cadab3a5d6bbcf5f80" {
t.Errorf("original singleton was destroyed during release")
}
}