-
Notifications
You must be signed in to change notification settings - Fork 3
/
equalizer_test.go
69 lines (52 loc) · 1.89 KB
/
equalizer_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
package equalizer
import (
"strings"
"testing"
"github.com/reugn/equalizer/internal/assert"
)
func TestEqualizer(t *testing.T) {
offset := NewStepOffset(32, 5)
eq, err := NewEqualizer(32, 8, offset)
assert.IsNil(t, err)
assert.Equal(t, eq.tape.Text(2), strings.Repeat("1", 32))
assert.Equal(t, eq.mask.Text(2), strings.Repeat("1", 8)+strings.Repeat("0", 24))
assert.Equal(t, eq.mask.Bit(0), uint(0))
eq.Failure(16)
assert.Equal(t, eq.tape.Text(2), strings.Repeat("1", 16)+strings.Repeat("0", 16))
assert.Equal(t, eq.TryAcquire(), false)
assert.Equal(t, eq.TryAcquire(), false)
assert.Equal(t, eq.TryAcquire(), false)
assert.Equal(t, eq.TryAcquire(), true)
eq.Success(10)
assert.Equal(t, eq.tape.Text(2), strings.Repeat("1", 16)+strings.Repeat("0", 14)+
strings.Repeat("1", 2))
eq.Failure(1)
assert.Equal(t, eq.tape.Text(2), strings.Repeat("1", 16)+strings.Repeat("0", 14)+
strings.Repeat("1", 2))
eq.Success(2)
assert.Equal(t, eq.tape.Text(2), strings.Repeat("1", 16)+strings.Repeat("0", 11)+
"110"+strings.Repeat("1", 2))
eq.Failure(32)
assert.Equal(t, eq.tape.Text(2), strings.Repeat("1", 8)+strings.Repeat("0", 24))
eq.Reset()
assert.Equal(t, eq.tape.Text(2), strings.Repeat("1", 32))
eq.Failure(-1)
assert.Equal(t, eq.tape.Text(2), strings.Repeat("1", 32))
eq.Purge()
assert.Equal(t, eq.tape.Text(2), strings.Repeat("1", 8)+strings.Repeat("0", 24))
}
func TestEqualizer_Errors(t *testing.T) {
eq, err := NewEqualizer(32, 8, nil)
assert.ErrorContains(t, err, "offset is nil")
assert.IsNil(t, eq)
offset := NewStepOffset(32, 5)
eq, err = NewEqualizer(-1, 8, offset)
assert.ErrorContains(t, err, "nonpositive size")
assert.IsNil(t, eq)
eq, err = NewEqualizer(32, -1, offset)
assert.ErrorContains(t, err, "nonpositive reserved")
assert.IsNil(t, eq)
eq, err = NewEqualizer(8, 32, offset)
assert.ErrorContains(t, err, "reserved must not exceed size")
assert.IsNil(t, eq)
}