1
exercism fetch go queen-attack

queen_attack_test.go

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
package queenattack

import "testing"

// Arguments to CanQueenAttack are in algebraic notation.
// See http://en.wikipedia.org/wiki/Algebraic_notation_(chess)

var tests = []struct {
	w, b   string
	attack bool
	ok     bool
}{
	{"b4", "b4", false, false}, // same square
	{"a8", "b9", false, false}, // off board
	{"a0", "b1", false, false},
	{"g3", "i5", false, false},
	{"here", "there", false, false}, // invalid
	{"", "", false, false},

	{"b3", "d7", false, true}, // no attack
	{"a1", "f8", false, true},
	{"b4", "b7", true, true}, // same file
	{"e4", "b4", true, true}, // same rank
	{"a1", "f6", true, true}, // common diagonals
	{"a6", "b7", true, true},
	{"d1", "f3", true, true},
	{"f1", "a6", true, true},
	{"a1", "h8", true, true},
	{"a8", "h1", true, true},
}

func TestCanQueenAttack(t *testing.T) {
	for _, test := range tests {
		switch attack, err := CanQueenAttack(test.w, test.b); {
		case err != nil:
			var _ error = err
			if test.ok {
				t.Fatalf("CanQueenAttack(%s, %s) returned error %q.  "+
					"Error not expected.",
					test.w, test.b, err)
			}
		case !test.ok:
			t.Fatalf("CanQueenAttack(%s, %s) = %t, %v.  Expected error.",
				test.w, test.b, attack, err)
		case attack != test.attack:
			t.Fatalf("CanQueenAttack(%s, %s) = %t, want %t.",
				test.w, test.b, attack, test.attack)
		}
	}
}

// Benchmark combined time for all test cases
func BenchmarkCanQueenAttack(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, test := range tests {
			CanQueenAttack(test.w, test.b)
		}
	}
}