types/key: add test for NodePublic.Shard

Updates #cleanup

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/8456/head
Brad Fitzpatrick 1 year ago committed by Brad Fitzpatrick
parent 1377618dbc
commit 77f56794c9

@ -157,3 +157,26 @@ func TestChallenge(t *testing.T) {
t.Errorf("didn't round trip: %v != %v", back, pub)
}
}
// Test that NodePublic.Shard is uniformly distributed.
func TestShard(t *testing.T) {
const N = 1_000
var shardCount [256]int
for i := 0; i < N; i++ {
shardCount[NewNode().Public().Shard()]++
}
e := float64(N) / 256 // expected
var x2 float64 // chi-squared
for _, c := range shardCount {
r := float64(c) - e // residual
x2 += r * r / e
}
t.Logf("x^2 = %v", x2)
if x2 > 512 { // really want x^2 =~ (256 - 1), but leave slop
t.Errorf("too much variation in shard distribution")
for i, c := range shardCount {
rj := float64(c) - e
t.Logf("shard[%v] = %v (off by %v)", i, c, rj)
}
}
}

Loading…
Cancel
Save