Revert r215321.
[official-gcc.git] / libgo / go / net / dnsclient_test.go
blob435eb35506e142f604d996b2a1c1c2f81b52f49d
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package net
7 import (
8 "math/rand"
9 "testing"
12 func checkDistribution(t *testing.T, data []*SRV, margin float64) {
13 sum := 0
14 for _, srv := range data {
15 sum += int(srv.Weight)
18 results := make(map[string]int)
20 count := 1000
21 for j := 0; j < count; j++ {
22 d := make([]*SRV, len(data))
23 copy(d, data)
24 byPriorityWeight(d).shuffleByWeight()
25 key := d[0].Target
26 results[key] = results[key] + 1
29 actual := results[data[0].Target]
30 expected := float64(count) * float64(data[0].Weight) / float64(sum)
31 diff := float64(actual) - expected
32 t.Logf("actual: %v diff: %v e: %v m: %v", actual, diff, expected, margin)
33 if diff < 0 {
34 diff = -diff
36 if diff > (expected * margin) {
37 t.Errorf("missed target weight: expected %v, %v", expected, actual)
41 func testUniformity(t *testing.T, size int, margin float64) {
42 rand.Seed(1)
43 data := make([]*SRV, size)
44 for i := 0; i < size; i++ {
45 data[i] = &SRV{Target: string('a' + i), Weight: 1}
47 checkDistribution(t, data, margin)
50 func TestUniformity(t *testing.T) {
51 testUniformity(t, 2, 0.05)
52 testUniformity(t, 3, 0.10)
53 testUniformity(t, 10, 0.20)
54 testWeighting(t, 0.05)
57 func testWeighting(t *testing.T, margin float64) {
58 rand.Seed(1)
59 data := []*SRV{
60 {Target: "a", Weight: 60},
61 {Target: "b", Weight: 30},
62 {Target: "c", Weight: 10},
64 checkDistribution(t, data, margin)
67 func TestWeighting(t *testing.T) {
68 testWeighting(t, 0.05)