Skip to content

Commit 77f5679

Browse files
committed
types/key: add test for NodePublic.Shard
Updates #cleanup Signed-off-by: Brad Fitzpatrick <[email protected]>
1 parent 1377618 commit 77f5679

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

types/key/node_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,26 @@ func TestChallenge(t *testing.T) {
157157
t.Errorf("didn't round trip: %v != %v", back, pub)
158158
}
159159
}
160+
161+
// Test that NodePublic.Shard is uniformly distributed.
162+
func TestShard(t *testing.T) {
163+
const N = 1_000
164+
var shardCount [256]int
165+
for i := 0; i < N; i++ {
166+
shardCount[NewNode().Public().Shard()]++
167+
}
168+
e := float64(N) / 256 // expected
169+
var x2 float64 // chi-squared
170+
for _, c := range shardCount {
171+
r := float64(c) - e // residual
172+
x2 += r * r / e
173+
}
174+
t.Logf("x^2 = %v", x2)
175+
if x2 > 512 { // really want x^2 =~ (256 - 1), but leave slop
176+
t.Errorf("too much variation in shard distribution")
177+
for i, c := range shardCount {
178+
rj := float64(c) - e
179+
t.Logf("shard[%v] = %v (off by %v)", i, c, rj)
180+
}
181+
}
182+
}

0 commit comments

Comments
 (0)