summaryrefslogtreecommitdiff
path: root/capset_test.go
blob: 5382aa8d9061f20b36142cb3468b798a8754b87e (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
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
}