summaryrefslogtreecommitdiff
path: root/capset_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'capset_test.go')
-rw-r--r--capset_test.go65
1 files changed, 37 insertions, 28 deletions
diff --git a/capset_test.go b/capset_test.go
index 5382aa8..cd2715e 100644
--- a/capset_test.go
+++ b/capset_test.go
@@ -1,25 +1,29 @@
package main
-import "testing"
-import "reflect"
-import "fmt"
+import (
+ "fmt"
+ "reflect"
+ "testing"
+)
+
+var deck = NewDeck(4)
var capsettests = []struct {
maxim int
- start, finish []string
+ start, finish []Card
}{
- {20, []string{"2222", "2221", "2212", "2211", "2122", "2121", "2112", "2111",
+ {20, []Card{"2222", "2221", "2212", "2211", "2122", "2121", "2112", "2111",
"1222", "1221", "1210"},
- []string{"2222", "2221", "2212", "2211", "2122", "2121", "2112", "2111",
+ []Card{"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)}
+ cs := Capset{tableau: tt.start, maxim: tt.maxim, d: deck}
+ cs.FindNextCapset()
+ expected := Capset{tableau: tt.finish, maxim: len(tt.finish), d: deck}
if !reflect.DeepEqual(cs, expected) {
t.Fatalf("Got \n%v but expected \n%v", cs, expected)
}
@@ -28,26 +32,26 @@ func TestFindNextCapset(t *testing.T) {
var scantests = []struct {
in string
- out []string
+ out []Card
err bool
}{
{"", nil, true},
- {"[]", []string{}, false},
- {"[0000]", []string{"0000"}, false},
- {"[0000 2222]", []string{"0000", "2222"}, false},
+ {"[]", []Card{}, false},
+ {"[0000]", []Card{"0000"}, false},
+ {"[0000 2222]", []Card{"0000", "2222"}, false},
{"[2]", nil, true},
{"not it", nil, true},
{"[0000", nil, true},
- {"[ 0000 2222 ]", []string{"0000", "2222"}, false},
+ {"[ 0000 2222 ]", []Card{"0000", "2222"}, false},
}
func TestScan(t *testing.T) {
for _, tt := range scantests {
- cs := capset{tableau: []string{}}
+ cs := NewCapset(deck, -1)
if testing.Verbose() {
fmt.Println("Trying input", tt.in)
}
- _, err := fmt.Sscanln(tt.in, &cs)
+ _, err := fmt.Sscanln(tt.in, cs)
if tt.err {
if err == nil {
t.Errorf("Sscanln(%v) => %v, want an error", tt.in, cs.tableau)
@@ -64,28 +68,33 @@ func TestScan(t *testing.T) {
}
}
-func testNoAllocs(t *testing.T) {
- cs := capset{tableau: []string{"2222"}}
- result := testing.AllocsPerRun(10, func() { cs.incrementTableau(false) })
+func TestNoAllocs(t *testing.T) {
+ cs := NewCapset(deck, -1)
+ result := testing.AllocsPerRun(10, func() { cs.incrementTableau() })
if result > 0 {
t.Errorf("Too many allocs: %v", result)
}
}
-func BenchmarkFindNextCapset(b *testing.B) {
- cs := capset{tableau: []string{"2222"}}
- useMap = true
+func BenchmarkIncrement(b *testing.B) {
+ cs := NewCapset(deck, -1)
for i := 0; i < b.N; i++ {
- cs.incrementTableau(false)
+ cs.incrementTableau()
}
}
-func BenchmarkFindNextCapsetNoMap(b *testing.B) {
- cs := capset{tableau: []string{"2222"}}
- useMap = false
+func BenchmarkIncrementHasSet(b *testing.B) {
+ cs := NewCapset(deck, -1)
for i := 0; i < b.N; i++ {
- cs.incrementTableau(false)
+ cs.incrementTableau()
+ cs.hasSet = hasSet(cs.tableau)
}
- useMap = true
}
+func BenchmarkIncrementHasSetWithLast(b *testing.B) {
+ cs := NewCapset(deck, -1)
+ for i := 0; i < b.N; i++ {
+ cs.incrementTableau()
+ cs.hasSet = hasSetWithLast(cs.tableau)
+ }
+}