aboutsummaryrefslogtreecommitdiff
path: root/pkg/pointer/pointer_test.go
blob: 4885d6b6ffea246b1ebc95d1f416e6d812e2d568 (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
package pointer

import (
	"os"
	"testing"

	"git.adyxax.org/adyxax/gofunge/pkg/field"
	"github.com/stretchr/testify/require"
)

func TestNewPointer(t *testing.T) {
	require.Equal(t, NewPointer(), &Pointer{dx: 1, ss: NewStackStack()})
}

func TestSplit(t *testing.T) {
	file, err := os.Open("../field/test_data/hello.b98")
	require.NoError(t, err, "Failed to open file")
	defer file.Close()
	f, err := field.Load(file)
	require.NoError(t, err)
	p := NewPointer()
	p2 := p.Split()
	// We check that p2 is a real copy
	p.Step(*f)
	p2.Step(*f)
	require.Equal(t, &Pointer{x: 1, y: 0, dx: 1, ss: NewStackStack()}, p)
	require.Equal(t, &Pointer{x: 1, y: 0, dx: 1, ss: NewStackStack()}, p2)
}

func TestStep(t *testing.T) { // Step is thoroughly tested in the field package
	defaultPointer := NewPointer()
	// File of one char
	file, err := os.Open("../field/test_data/minimal.b98")
	require.NoError(t, err, "Failed to open file")
	defer file.Close()
	f, err := field.Load(file)
	require.NoError(t, err)
	p := NewPointer()
	p.Step(*f)
	require.Equal(t, defaultPointer, p)
}

func TestGet(t *testing.T) {
	// File of one char
	file, err := os.Open("../field/test_data/minimal.b98")
	require.NoError(t, err, "Failed to open file")
	defer file.Close()
	f, err := field.Load(file)
	p := NewPointer()
	v := p.Get(*f)
	require.Equal(t, int('@'), v)
}

func TestSet(t *testing.T) {
	p := NewPointer()
	p.Set(3, 14)
	require.Equal(t, 3, p.x)
	require.Equal(t, 14, p.y)
}

func TestRedirectTo(t *testing.T) {
	p := NewPointer()
	p.RedirectTo(3, 14)
	require.Equal(t, 3, p.dx)
	require.Equal(t, 14, p.dy)
}

func TestReverse(t *testing.T) {
	p := NewPointer()
	p.RedirectTo(3, 14)
	p.Reverse()
	require.Equal(t, -3, p.dx)
	require.Equal(t, -14, p.dy)
}

func TestRedirect(t *testing.T) {
	testCases := []struct {
		name       string
		input      byte
		expectedDx int
		expectedDy int
	}{
		{"up", '^', 0, -1},
		{"right", '>', 1, 0},
		{"down", 'v', 0, 1},
		{"left", '<', -1, 0},
		{"turn left", '[', 14, -3},
		{"turn right", ']', -14, 3},
		{"reverse", 'r', -3, -14},
		{"redirectTo", 'x', 2, 7},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			p := NewPointer()
			p.RedirectTo(3, 14)
			p.ss.Push(2)
			p.ss.Push(7)
			v := p.Redirect(int(tc.input))
			require.Equal(t, true, v)
			require.Equal(t, tc.expectedDx, p.dx, "Invalid dx value")
			require.Equal(t, tc.expectedDy, p.dy, "Invalid dy value")
		})
	}
	// We cannot really test random, can we? This just gives coverage
	p := NewPointer()
	v := p.Redirect(int('?'))
	require.Equal(t, true, v)
	// A character that does not redirect should return false
	require.Equal(t, false, p.Redirect(int('@')))
}