summaryrefslogtreecommitdiff
path: root/capset_test.go
blob: 29a77dae095f9177fbdf62a5926dbc6475c33fed (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
package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"reflect"
	"testing"
)

var deck = NewDeck(4)

var capsettests = []struct {
	maxim         int
	start, finish []Card
}{
	{20, []Card{"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
		"1000", "1001", "1012"},
		[]Card{"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 {
		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 TestDim3(t *testing.T) {
	d := NewDeck(3)
	cs := NewCapset(d, -1)

	cmpfile, err := os.Open("./capset3.out")
	if err != nil {
		log.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())
		}
	}
}

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)
	}
}