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 }