package main import ( "bufio" "fmt" "os" "reflect" "testing" ) var deck = NewDeck(4) var hassettests = []struct { tableau []string hasSet bool hasSetLast bool }{ {[]string{"0000"}, false, false}, {[]string{"0000", "0001"}, false, false}, {[]string{"0000", "0001", "0002"}, true, true}, {[]string{"0000", "0001", "0011"}, false, false}, {[]string{"0000", "1111", "2222"}, true, true}, {[]string{"0000", "1111", "2222", "0100"}, true, false}, {[]string{"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1012"}, false, false}, {[]string{"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1012", "1022", "1102", "1202", "2012", "2102", "2110", "2111", "2122", "2212"}, false, false}, } func TestHasSet(t *testing.T) { for _, tt := range hassettests { tableau, err := deck.FindCards(tt.tableau) if err != nil { t.Error(err) continue } found := deck.hasSet(tableau) if tt.hasSet != found { t.Error("HasSet: For", tableau, "found", found, "but expected", tt.hasSet) } found = deck.hasSetWithLast(tableau) if tt.hasSetLast != found { t.Error("HasSetLast: For", tableau, "found", found, "but expected", tt.hasSet) } } } var capsettests = []struct { maxim int start, finish []string }{ {20, []string{"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1012"}, []string{"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1012", "1022", "1102", "1202", "2012", "2102", "2110", "2111", "2122", "2212"}}, } func TestFindNextCapset(t *testing.T) { for _, tt := range capsettests { start, err := deck.FindCards(tt.start) if err != nil { t.Error(err) continue } finish, err := deck.FindCards(tt.finish) if err != nil { t.Error(err) continue } cs := Capset{tableau: start, maxim: tt.maxim, d: deck} cs.FindNextCapset() expected := Capset{tableau: 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 []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 := 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 { out, err := deck.FindCards(tt.out) if err != nil { t.Error("For input", tt.in, "got unexpected error:", err) continue } if !reflect.DeepEqual(cs.tableau, 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 TestDim3(t *testing.T) { d := NewDeck(3) cs := NewCapset(d, -1) cmpfile, err := os.Open("./capset3.out") if err != nil { t.Fatal(err) } defer cmpfile.Close() scanner := bufio.NewScanner(cmpfile) for cs.FindNextCapset() { scanner.Scan() newline := fmt.Sprint(cs.GetTableau()) if newline != scanner.Text() { t.Errorf("Program returned %v\n capset3.out has %v", newline, scanner.Text()) } } if scanner.Scan() { t.Error("Did not find items starting with", scanner.Text()) } } 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 = deck.hasSet(cs.tableau) } } func BenchmarkIncrementHasSetWithLast(b *testing.B) { cs := NewCapset(deck, -1) for i := 0; i < b.N; i++ { cs.incrementTableau() cs.hasSet = deck.hasSetWithLast(cs.tableau) } }