-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrapidhash_test.go
More file actions
135 lines (117 loc) · 2.93 KB
/
rapidhash_test.go
File metadata and controls
135 lines (117 loc) · 2.93 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package rapidhash
import (
"encoding/binary"
"encoding/csv"
"fmt"
"math/rand/v2"
"os"
"strings"
"testing"
)
func TestRapidhash(t *testing.T) {
testCases := []struct {
input string
expected uint64
}{
{"test", Rapidhash([]byte("test"))},
{"hello", Rapidhash([]byte("hello"))},
{"world", Rapidhash([]byte("world"))},
{"", Rapidhash([]byte(""))},
{"123456789", Rapidhash([]byte("123456789"))},
}
for _, testCase := range testCases {
t.Run(testCase.input, func(t *testing.T) {
result := Rapidhash([]byte(testCase.input))
if result != testCase.expected {
t.Errorf("Rapidhash(%q) = %x; want %x", testCase.input, result, testCase.expected)
} else {
t.Logf("Rapidhash(%q) = %x", testCase.input, result) // Log the result
}
})
}
}
func unescapeString(s string) string {
s = strings.ReplaceAll(s, "\\t", "\t")
s = strings.ReplaceAll(s, "\\n", "\n")
s = strings.ReplaceAll(s, "\\r", "\r")
s = strings.ReplaceAll(s, "\\\\", "\\")
return s
}
func TestRapidhashFileInput(t *testing.T) {
file, err := os.Open("additional/file.csv")
if err != nil {
t.Fatalf("Cannot open file %v", err)
}
defer file.Close()
reader := csv.NewReader(file)
records, err := reader.ReadAll()
if err != nil {
t.Fatalf("Cannot read csv file %v", err)
}
for _, record := range records {
if len(record) != 2 {
t.Errorf("Invalid record format: %v", record)
continue
}
input := unescapeString(record[0])
if err != nil {
t.Errorf("Unable to unquote %s: %v", record[0], err)
}
expectedHash := record[1]
result := fmt.Sprintf("%x", Rapidhash([]byte(input)))
if result != expectedHash {
t.Errorf("Rapidhash(%q) = %s; want %s", input, result, expectedHash)
} else {
// t.Logf("Rapidhash(%q) = %s", input, result)
}
}
}
func getRandomBytes(r rand.Source, len int) []byte {
data := make([]byte, len)
i := len
cur := 0
for i >= 8 {
binary.LittleEndian.PutUint64(data[cur:], r.Uint64())
i -= 8
cur += 8
}
if i > 4 {
binary.LittleEndian.PutUint32(data[cur:], uint32(r.Uint64()))
cur += 4
}
return data
}
func TestRapidhashVariantsMatching(t *testing.T) {
source := rand.NewPCG(1991, 2025)
r := rand.New(source)
const N int = 10
datas := make([][]byte, N)
// redundant for len 0, but makes code simpler.
for idx := range N {
datas[idx] = getRandomBytes(r, 81)
}
for i := 0; i <= 48; i++ {
for j := range N {
bytes := datas[j][:i]
var expected = Rapidhash(bytes)
micro := RapidhashMicro(bytes)
nano := RapidhashNano(bytes)
if micro != expected {
t.Errorf("RapidhashMicro(%q) = %d; want %d", bytes, micro, expected)
}
if nano != expected {
t.Errorf("RapidhashNano(%q) = %d; want %d", bytes, nano, expected)
}
}
}
for i := 49; i <= 80; i++ {
for j := range N {
bytes := datas[j][:i]
var expected = Rapidhash(bytes)
micro := RapidhashMicro(bytes)
if micro != expected {
t.Errorf("RapidhashMicro(%q) = %d; want %d", bytes, micro, expected)
}
}
}
}