summaryrefslogtreecommitdiff
path: root/capset_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'capset_test.go')
-rw-r--r--capset_test.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/capset_test.go b/capset_test.go
new file mode 100644
index 0000000..5382aa8
--- /dev/null
+++ b/capset_test.go
@@ -0,0 +1,91 @@
+package main
+
+import "testing"
+import "reflect"
+import "fmt"
+
+var capsettests = []struct {
+ maxim int
+ start, finish []string
+}{
+ {20, []string{"2222", "2221", "2212", "2211", "2122", "2121", "2112", "2111",
+ "1222", "1221", "1210"},
+ []string{"2222", "2221", "2212", "2211", "2122", "2121", "2112", "2111",
+ "1222", "1221", "1210", "1200", "1120", "1020", "0210", "0120", "0112",
+ "0111", "0100", "0010"}},
+}
+
+func TestFindNextCapset(t *testing.T) {
+ for _, tt := range capsettests {
+ cs := capset{tableau: tt.start, maxim: tt.maxim}
+ cs.findNextCapset()
+ expected := capset{tableau: tt.finish, maxim: len(tt.finish)}
+ if !reflect.DeepEqual(cs, expected) {
+ t.Fatalf("Got \n%v but expected \n%v", cs, expected)
+ }
+ }
+}
+
+var scantests = []struct {
+ in string
+ out []string
+ err bool
+}{
+ {"", nil, true},
+ {"[]", []string{}, false},
+ {"[0000]", []string{"0000"}, false},
+ {"[0000 2222]", []string{"0000", "2222"}, false},
+ {"[2]", nil, true},
+ {"not it", nil, true},
+ {"[0000", nil, true},
+ {"[ 0000 2222 ]", []string{"0000", "2222"}, false},
+}
+
+func TestScan(t *testing.T) {
+ for _, tt := range scantests {
+ cs := capset{tableau: []string{}}
+ if testing.Verbose() {
+ fmt.Println("Trying input", tt.in)
+ }
+ _, err := fmt.Sscanln(tt.in, &cs)
+ if tt.err {
+ if err == nil {
+ t.Errorf("Sscanln(%v) => %v, want an error", tt.in, cs.tableau)
+ }
+ } else {
+ if err != nil {
+ t.Errorf("Sscanln(%v). Unexpected error %v", tt.in, err)
+ } else {
+ if !reflect.DeepEqual(cs.tableau, tt.out) {
+ t.Errorf("Sscanln(%v) => %v, want %v", tt.in, cs.tableau, tt.out)
+ }
+ }
+ }
+ }
+}
+
+func testNoAllocs(t *testing.T) {
+ cs := capset{tableau: []string{"2222"}}
+ result := testing.AllocsPerRun(10, func() { cs.incrementTableau(false) })
+ if result > 0 {
+ t.Errorf("Too many allocs: %v", result)
+ }
+}
+
+func BenchmarkFindNextCapset(b *testing.B) {
+ cs := capset{tableau: []string{"2222"}}
+ useMap = true
+ for i := 0; i < b.N; i++ {
+ cs.incrementTableau(false)
+ }
+}
+
+func BenchmarkFindNextCapsetNoMap(b *testing.B) {
+ cs := capset{tableau: []string{"2222"}}
+ useMap = false
+ for i := 0; i < b.N; i++ {
+ cs.incrementTableau(false)
+ }
+ useMap = true
+}
+