-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflags_test.go
103 lines (91 loc) · 1.95 KB
/
flags_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package identifiers
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMust(t *testing.T) {
sample := func(a bool) (struct{}, error) {
if a {
return struct{}{}, errors.New("error")
}
return struct{}{}, nil
}
require.NotPanics(t, func() {
must(sample(false))
})
require.Panics(t, func() {
must(sample(true))
})
}
func TestGetFlag(t *testing.T) {
tests := []struct {
value byte
index uint8
want bool
}{
{0b00000001, 0, true},
{0b00000000, 0, false},
{0b10000000, 7, true},
{0b00000000, 7, false},
{0b10101010, 1, true},
{0b10101010, 2, false},
}
for _, tt := range tests {
assert.Equal(t, tt.want, getFlag(tt.value, tt.index))
}
}
func TestSetFlag(t *testing.T) {
tests := []struct {
value byte
index uint8
set bool
want byte
}{
{0b00000000, 0, true, 0b00000001},
{0b00000001, 0, false, 0b00000000},
{0b00000000, 7, true, 0b10000000},
{0b10000000, 7, false, 0b00000000},
{0b10101010, 1, true, 0b10101010},
{0b10101010, 2, true, 0b10101110},
{0b10101010, 1, false, 0b10101000},
}
for _, tt := range tests {
assert.Equal(t, tt.want, setFlag(tt.value, tt.index, tt.set))
}
}
func TestMakeFlag(t *testing.T) {
tests := []struct {
kind IdentifierKind
index uint8
version uint8
want Flag
}{
{
KindParticipant, 0, 0,
Flag{index: 0, support: map[IdentifierKind]uint8{KindParticipant: 0}},
},
{
KindAsset, 1, 1,
Flag{index: 1, support: map[IdentifierKind]uint8{KindAsset: 1}},
},
{
KindLogic, 10, 1,
Flag{index: 1, support: map[IdentifierKind]uint8{KindAsset: 1}},
},
{
KindLogic, 1, 20,
Flag{index: 1, support: map[IdentifierKind]uint8{KindAsset: 1}},
},
}
for _, tt := range tests {
if tt.index > 7 || tt.version > 15 {
require.Panics(t, func() {
makeFlag(tt.kind, tt.index, tt.version)
})
} else {
assert.Equal(t, tt.want, makeFlag(tt.kind, tt.index, tt.version))
}
}
}