-
Notifications
You must be signed in to change notification settings - Fork 79
/
fingerprint_test.go
92 lines (75 loc) · 1.83 KB
/
fingerprint_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//go:build cgo
// +build cgo
package pg_query_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
"testing"
pg_query "github.com/pganalyze/pg_query_go/v6"
)
type fingerprintTest struct {
Input string
ExpectedParts []string
ExpectedHash string
}
func TestFingerprint(t *testing.T) {
var fingerprintTests []fingerprintTest
file, err := ioutil.ReadFile("./testdata/fingerprint.json")
if err != nil {
t.Errorf("Could not load test file: %v\n", err)
}
err = json.Unmarshal(file, &fingerprintTests)
if err != nil {
t.Errorf("Could not parse test file: %v\n", err)
}
for _, test := range fingerprintTests {
fmt.Printf(".")
fingerprint, err := pg_query.Fingerprint(test.Input)
if err != nil {
t.Errorf("Fingerprint(%s)\nparse error %s\n\n", test.Input, err)
}
if string(fingerprint) != test.ExpectedHash {
t.Errorf("Fingerprint(%s)\nexpected %s\nactual %s\n\n", test.Input, test.ExpectedHash, fingerprint)
}
fingerprintInt, err := pg_query.FingerprintToUInt64(test.Input)
if err != nil {
t.Errorf("FingerprintToUInt64(%s)\nparse error %s\n\n", test.Input, err)
}
expectedInt, _ := strconv.ParseUint(test.ExpectedHash, 16, 64)
if fingerprintInt != expectedInt {
t.Errorf("FingerprintToUInt64(%s)\nexpected %d\nactual %d\n\n", test.Input, expectedInt, fingerprintInt)
}
}
fmt.Printf("\n")
}
var hashTests = []struct {
input string
seed uint64
expected uint64
}{
{
"TEST",
0,
11717748491247689214,
},
{
"TEST",
42,
10412276358662179996,
},
{
"Something else",
0,
14679351602596009561,
},
}
func TestHashXXH3_64(t *testing.T) {
for _, test := range hashTests {
actual := pg_query.HashXXH3_64([]byte(test.input), test.seed)
if actual != test.expected {
t.Errorf("HashXXH3_64(%s)\nexpected %d\nactual %d\n\n", test.input, test.expected, actual)
}
}
}