-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsubsetChooser_test.go
125 lines (112 loc) · 2.56 KB
/
subsetChooser_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
/*
* Copyright 2011 Colin Patrick McCabe
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"sort"
"testing"
)
type uintSlice []uint
type uintSliceSlice []uintSlice
func (arr uintSliceSlice) Len() int {
return len(arr)
}
func (arr uintSliceSlice) Less(i, j int) bool {
c := arr[i].Compare(arr[j])
return (c < 0)
}
func (arr uintSliceSlice) Swap(i, j int) {
arr[i], arr[j] = arr[j], arr[i]
}
func (a uintSlice) Compare(b uintSlice) int {
if (len(a) < len(b)) {
return -1
} else if (len(a) > len(b)) {
return 1;
}
for i := 0; i < len(a); i++ {
if (a[i] < b[i]) {
return -1
} else if (a[i] > b[i]) {
return 1
}
}
return 0
}
func (a uintSliceSlice) Compare(b uintSliceSlice) int {
if (len(a) < len(b)) {
return -1
} else if (len(a) > len(b)) {
return 1;
}
for i := 0; i < len(a); i++ {
c := a[i].Compare(b[i])
if (c != 0) {
return c
}
}
return 0
}
func (arr uintSlice) String() string {
var ret string
ret += "["
sep := ""
for i := 0; i < len(arr); i++ {
ret += sep
ret += fmt.Sprintf("%d", arr[i])
sep = ", "
}
ret += "]"
return ret
}
func (arr uintSliceSlice) String() string {
var ret string
ret += "["
sep := ""
for i := 0; i < len(arr); i++ {
ret += sep
ret += arr[i].String()
sep = ", "
}
ret += "]"
return ret
}
func test(t *testing.T, maxIdx uint, subsetSize uint,
expected *uintSliceSlice) {
var all uintSliceSlice
ch := NewSubsetChooser(maxIdx, subsetSize)
for ;; {
s := ch.Cur()
all = append(all, s)
if (!ch.Next()) {
break
}
}
sort.Sort(all)
sort.Sort(expected)
if (all.Compare(*expected) != 0) {
t.Errorf("expected:%s. got: %s\n",
expected.String(), all.String())
}
}
func TestSubsetChooser1(t *testing.T) {
test(t, 3, 2, &uintSliceSlice{ {0, 1}, {0, 2}, {1, 2} } )
test(t, 2, 1, &uintSliceSlice{ {0}, {1} } )
test(t, 3, 1, &uintSliceSlice{ {0}, {1}, {2} } )
test(t, 3, 3, &uintSliceSlice{ {0, 1, 2} } )
test(t, 4, 3, &uintSliceSlice{ {0, 1, 2}, {0, 1, 3}, {0, 2, 3}, {1, 2, 3} } )
test(t, 100, 0, &uintSliceSlice{ {} })
}