aboutsummaryrefslogtreecommitdiff
path: root/go/game/range_test.go
blob: 4b6e05ad5cd358a05f85c25eb68eeeffe76903db (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
package game

import (
	"testing"

	"golang.org/x/exp/slices"
)

func TestPositionsInRange(t *testing.T) {
	origin := Position{0, 0}
	positionsInRange1 := []Position{{0, 1}, {1, 0}, {1, 1}}
	calculatedPositions := PositionsInRange(origin, 1, false)
	if len(positionsInRange1) != len(calculatedPositions) {
		t.Fatalf("Unexpected number of calculated positions")
	}

	for _, p := range positionsInRange1 {
		if !slices.Contains(calculatedPositions, p) {
			t.Fatalf("Position %v not in calculated positions", p)
		}
	}

	origin = Position{2, 2}
	positionsInRange2 := []Position{
		{0, 1},
		{0, 2},
		{0, 3},
		{1, 0},
		{1, 1},
		{1, 2},
		{1, 3},
		{1, 4},
		{2, 0},
		{2, 1},
		{2, 3},
		{2, 4},
		{3, 0},
		{3, 1},
		{3, 2},
		{3, 3},
		{3, 4},
		{4, 1},
		{4, 2},
		{4, 3},
	}
	calculatedPositions = PositionsInRange(origin, 2, false)

	for _, p := range positionsInRange2 {
		if !slices.Contains(calculatedPositions, p) {
			t.Fatalf("Position %v not in calculated positions", p)
		}
	}
}

func TestPositionNotInRange(t *testing.T) {
	o := Position{5, 0}
	p := Position{6, 3}
	if IsPositionInRange(o, p, 2) {
		t.Fatalf("Invalid range inclusion %v not in %v", o, PositionsInRange(o, 2, false))
	}
}