summaryrefslogtreecommitdiff
path: root/capset_test.go
blob: a807b3b11096111e29bdc00bea4d5ad7a398efbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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)
	}
}