package main import ( "fmt" "reflect" "testing" ) var deck = NewDeck(4) var capsettests = []struct { maxim int start, finish []Card }{ {20, []Card{"2222", "2221", "2212", "2211", "2122", "2121", "2112", "2111", "1222", "1221", "1210"}, []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, 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) } } } var scantests = []struct { in string out []Card err bool }{ {"", nil, true}, {"[]", []Card{}, false}, {"[0000]", []Card{"0000"}, false}, {"[0000 2222]", []Card{"0000", "2222"}, false}, {"[2]", nil, true}, {"not it", nil, true}, {"[0000", nil, true}, {"[ 0000 2222 ]", []Card{"0000", "2222"}, false}, } func TestScan(t *testing.T) { for _, tt := range scantests { cs := NewCapset(deck, -1) 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 := NewCapset(deck, -1) result := testing.AllocsPerRun(10, func() { cs.incrementTableau() }) if result > 0 { t.Errorf("Too many allocs: %v", result) } } func BenchmarkIncrement(b *testing.B) { cs := NewCapset(deck, -1) for i := 0; i < b.N; i++ { cs.incrementTableau() } } func BenchmarkIncrementHasSet(b *testing.B) { cs := NewCapset(deck, -1) for i := 0; i < b.N; i++ { cs.incrementTableau() cs.hasSet = hasSet(cs.tableau) } } func BenchmarkIncrementHasSetWithLast(b *testing.B) { cs := NewCapset(deck, -1) for i := 0; i < b.N; i++ { cs.incrementTableau() cs.hasSet = hasSetWithLast(cs.tableau) } }