/
set_test.go
186 lines (161 loc) · 3.59 KB
/
set_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2015 Kevin Gillette. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package set_test
import (
"fmt"
"sort"
"testing"
"github.com/xtgo/set"
)
const format = "%s(%v) = %v, want %v"
type (
BoolSpec struct {
Name string
Op func(sort.Interface, int) bool
Sel func(SetTest) bool
}
SliceSpec struct {
Name string
Op func(sort.Interface, int) int
Sel func(SetTest) string
}
)
func setup(a, b string) (s string, pivot int) {
return a + b, len(a)
}
func runMutator(t *testing.T, spec SliceSpec) {
for _, tt := range tests {
in, pivot := setup(tt.a, tt.b)
s := Letters(in)
//iface := LoggingInterface{t, s, pivot}
iface := s
l := spec.Op(iface, pivot)
want := spec.Sel(tt)
if want != string(s[:l]) {
in := Letters(in).Mark(pivot, -1, -1)
s := s.Mark(l, -1, -1)
want := Letters(want).Mark(len(want), -1, -1)
t.Errorf(format, spec.Name, in, s, want)
}
}
}
func TestInter(t *testing.T) {
runMutator(t, SliceSpec{
"Inter",
set.Inter,
func(tt SetTest) string { return tt.inter },
})
}
func TestDiff(t *testing.T) {
runMutator(t, SliceSpec{
"Diff",
set.Diff,
func(tt SetTest) string { return tt.diff },
})
}
func TestSymDiff(t *testing.T) {
runMutator(t, SliceSpec{
"SymDiff",
set.SymDiff,
func(tt SetTest) string { return tt.symdiff },
})
}
func runBoolean(t *testing.T, spec BoolSpec) {
for _, tt := range tests {
in, pivot := setup(tt.a, tt.b)
s := Letters(in)
ok := spec.Op(s, pivot)
want := spec.Sel(tt)
if ok != want {
t.Errorf(format, spec.Name, s, ok, want)
}
}
}
func TestIsSub(t *testing.T) {
runBoolean(t, BoolSpec{
"IsSub",
set.IsSub,
func(tt SetTest) bool { return tt.issub },
})
}
func TestIsSuper(t *testing.T) {
runBoolean(t, BoolSpec{
"IsSuper",
set.IsSuper,
func(tt SetTest) bool { return tt.issuper },
})
}
func TestIsInter(t *testing.T) {
runBoolean(t, BoolSpec{
"IsInter",
set.IsInter,
func(tt SetTest) bool { return tt.isinter },
})
}
func TestIsEqual(t *testing.T) {
runBoolean(t, BoolSpec{
"IsEqual",
set.IsEqual,
func(tt SetTest) bool { return tt.isequal },
})
}
func TestUniq(t *testing.T) {
for _, tt := range uniqs {
// make copy of the input
s := sort.IntSlice(append([]int(nil), tt.in...))
size := set.Uniq(s)
s = s[:size]
if len(s) != len(tt.out) {
t.Errorf(format, "Uniq", tt.in, s, tt.out)
continue
}
for i := range s {
if s[i] != tt.out[i] {
t.Errorf(format, "Uniq", tt.in, s, tt.out)
break
}
}
}
}
type Letters []byte
func (s Letters) Len() int { return len(s) }
func (s Letters) Less(i, j int) bool { return s[i] < s[j] }
func (s Letters) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s Letters) String() string { return string(s) }
func (s Letters) Mark(pivot, i, j int) string {
t := append([]byte(nil), s...)
switch {
case i != j:
t[j] -= 'a' - 'A'
fallthrough
case i >= 0:
t[i] -= 'a' - 'A'
}
return fmt.Sprintf("[%s|%s]", t[:pivot], t[pivot:])
}
type LoggingInterface struct {
t *testing.T
s Letters
pivot int
}
func (s LoggingInterface) Len() int {
v := s.s.Len()
s.t.Logf(">>> Len() = %v", v)
return v
}
func (s LoggingInterface) Less(i, j int) bool {
v := s.s.Less(i, j)
pre := s.s.Mark(s.pivot, i, j)
s.t.Logf(">>> %s Less(%d,%d) = %v", pre, i, j, v)
return v
}
func (s LoggingInterface) Swap(i, j int) {
pre := s.s.Mark(s.pivot, i, j)
s.s.Swap(i, j)
post := s.s.Mark(s.pivot, i, j)
s.t.Logf(">>> %s Swap(%d,%d) %s", pre, i, j, post)
}
func (s LoggingInterface) String() string {
return fmt.Sprint(s.s)
}