-
Notifications
You must be signed in to change notification settings - Fork 0
/
spread_test.go
124 lines (111 loc) · 2.93 KB
/
spread_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
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
package spread
import (
"bufio"
"crypto/sha1"
"fmt"
"os"
"strings"
"testing"
"github.com/montanaflynn/stats"
"github.com/google/uuid"
)
func TestSpreadDeterministicBehavior(t *testing.T) {
key := "my-special-key-51f4c00e-7e77-4da8-8fbe-28c676cd179c"
wanted := str(0.8422691961386)
s := New(nil)
for i := 0; i < 100; i++ {
got := str(s.Key(key))
if got != wanted {
t.Errorf("Generated fraction for key '%s' was incorrect, got: %s, want: %s", key, got, wanted)
}
}
}
func TestSpreadSHA1DistributionBehavior(t *testing.T) {
s := New(sha1.New())
const size = 100000
data := make([]float64, size)
for i := 0; i < size; i++ {
key, _ := uuid.NewRandom()
data[i] = s.Key(key.String())
}
q, _ := stats.Quartile(data)
q1 := fmt.Sprintf("%.2f", q.Q1)
if q1 != "0.25" {
t.Errorf("Generated distribution is not even. Q1 is not around 0.25, got: %s", q1)
}
q2 := fmt.Sprintf("%.2f", q.Q2)
if q2 != "0.50" {
t.Errorf("Generated distribution is not even. Q2 is not around 0.50, got: %s", q2)
}
q3 := fmt.Sprintf("%.2f", q.Q3)
if q3 != "0.75" {
t.Errorf("Generated distribution is not even. Q3 is not around 0.75, got: %s", q3)
}
}
func TestSpreadSHA256DistributionBehavior(t *testing.T) {
s := New(nil)
const size = 100000
data := make([]float64, size)
for i := 0; i < size; i++ {
key, _ := uuid.NewRandom()
data[i] = s.Key(key.String())
}
q, _ := stats.Quartile(data)
q1 := fmt.Sprintf("%.2f", q.Q1)
if q1 != "0.25" {
t.Errorf("Generated distribution is not even. Q1 is not around 0.25, got: %s", q1)
}
q2 := fmt.Sprintf("%.2f", q.Q2)
if q2 != "0.50" {
t.Errorf("Generated distribution is not even. Q2 is not around 0.50, got: %s", q2)
}
q3 := fmt.Sprintf("%.2f", q.Q3)
if q3 != "0.75" {
t.Errorf("Generated distribution is not even. Q3 is not around 0.75, got: %s", q3)
}
}
func TestSpreadSHA1JVMCompliance(t *testing.T) {
s := New(sha1.New())
f, _ := os.Open("internal/sha1.txt")
scanner := bufio.NewScanner(f)
for scanner.Scan() {
parts := strings.Split(scanner.Text(), ";")
key := parts[0]
wanted := parts[1]
got := str(s.Key(key))
if got != wanted {
t.Errorf("SHA1 Generated fraction for key '%s' was incorrect, got: %s, want: %s", key, got, wanted)
}
}
}
func TestSpreadSHA256JVMCompliance(t *testing.T) {
s := New(nil)
f, _ := os.Open("internal/sha256.txt")
scanner := bufio.NewScanner(f)
for scanner.Scan() {
parts := strings.Split(scanner.Text(), ";")
key := parts[0]
wanted := parts[1]
got := str(s.Key(key))
if got != wanted {
t.Errorf("SHA256 Generated fraction for key '%s' was incorrect, got: %s, want: %s", key, got, wanted)
}
}
}
func BenchmarkSHA1(b *testing.B) {
s := New(sha1.New())
key, _ := uuid.NewRandom()
for i := 0; i < b.N; i++ {
s.Key(key.String())
}
}
func BenchmarkSHA256(b *testing.B) {
s := New(nil)
key, _ := uuid.NewRandom()
for i := 0; i < b.N; i++ {
s.Key(key.String())
}
}
func str(f float64) string {
return fmt.Sprintf("%.32f", f)[:14]
}