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.
12 func checkDistribution(t
*testing
.T
, data
[]*SRV
, margin
float64) {
14 for _
, srv
:= range data
{
15 sum
+= int(srv
.Weight
)
18 results
:= make(map[string]int)
21 for j
:= 0; j
< count
; j
++ {
22 d
:= make([]*SRV
, len(data
))
24 byPriorityWeight(d
).shuffleByWeight()
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
)
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) {
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) {
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)