-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathpn_counter_test.go
76 lines (59 loc) · 1.16 KB
/
pn_counter_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
package crdt
import "testing"
func TestPNCounter(t *testing.T) {
for _, tt := range []struct {
incOne, decOne int
incTwo, decTwo int
result int
}{
{5, 5, 6, 6, 0},
{5, 6, 7, 8, -2},
{8, 7, 6, 5, 2},
{5, 0, 6, 0, 11},
{0, 5, 0, 6, -11},
} {
pOne, pTwo := NewPNCounter(), NewPNCounter()
for i := 0; i < tt.incOne; i++ {
pOne.Inc()
}
for i := 0; i < tt.decOne; i++ {
pOne.Dec()
}
for i := 0; i < tt.incTwo; i++ {
pTwo.Inc()
}
for i := 0; i < tt.decTwo; i++ {
pTwo.Dec()
}
pOne.Merge(pTwo)
if pOne.Count() != tt.result {
t.Errorf("expected the total count to be: %d, actual: %d",
tt.result,
pOne.Count())
}
pTwo.Merge(pOne)
if pTwo.Count() != tt.result {
t.Errorf("expected the total count to be: %d, actual: %d",
tt.result,
pTwo.Count())
}
}
}
func TestPNCounterInvalidP(t *testing.T) {
pn := NewPNCounter()
defer func() {
if r := recover(); r == nil {
t.Fatalf("panic expected here")
}
}()
pn.IncVal(-5)
}
func TestPNCounterInvalidN(t *testing.T) {
pn := NewPNCounter()
defer func() {
if r := recover(); r == nil {
t.Fatalf("panic expected here")
}
}()
pn.DecVal(-5)
}