-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweightedoption_32bit_test.go
58 lines (53 loc) · 1.53 KB
/
weightedoption_32bit_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
//go:build 386 || arm || mips || mipsle
// +build 386 arm mips mipsle
package weightedoption
import (
"math"
"testing"
)
func TestNewSelector32Bit(t *testing.T) {
t.Parallel()
u32tests := []struct {
name string
cs []Option[rune, uint32]
wantErr error
}{
{
name: "weight overflow from single option exceeding system's math.MaxInt",
cs: []Option[rune, uint32]{{Data: 'a', Weight: uint32(math.MaxInt32) + 1}},
wantErr: ErrWeightOverflow,
},
{
name: "weight doesn't overflow if a single option is equal to the system's math.MaxInt",
cs: []Option[rune, uint32]{{Data: 'a', Weight: uint32(math.MaxInt32)}},
wantErr: nil,
},
{
name: "weight overflow from three options exceeding the system's math.MaxInt",
cs: []Option[rune, uint32]{
{Data: 'a', Weight: uint32(math.MaxInt32)/3 + 1},
{Data: 'b', Weight: uint32(math.MaxInt32)/3 + 1},
{Data: 'c', Weight: uint32(math.MaxInt32)/3 + 1},
},
wantErr: ErrWeightOverflow,
},
{
name: "weight doesn't overflow from three options close to but not exceeding the system's math.MaxInt",
cs: []Option[rune, uint32]{
{Data: 'a', Weight: uint32(math.MaxInt32) / 3},
{Data: 'b', Weight: uint32(math.MaxInt32) / 3},
{Data: 'c', Weight: uint32(math.MaxInt32)/3 + 1},
},
wantErr: nil,
},
}
for _, tt := range u32tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
_, err := NewSelector(tt.cs...)
if err != tt.wantErr {
t.Errorf("NewSelector() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}