aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pkg/field/step.go8
-rw-r--r--pkg/field/step_test.go33
-rw-r--r--pkg/field/utils.go5
-rw-r--r--pkg/field/utils_test.go41
4 files changed, 48 insertions, 39 deletions
diff --git a/pkg/field/step.go b/pkg/field/step.go
index 50147d2..eee6f95 100644
--- a/pkg/field/step.go
+++ b/pkg/field/step.go
@@ -2,19 +2,15 @@ package field
func (f Field) Step(x, y, dx, dy int) (int, int) {
x2, y2 := x+dx, y+dy
- if f.IsIn(x2, y2) {
+ if f.isIn(x2, y2) {
return x2, y2
}
// We are stepping outside, we need to wrap the Lahey-space
for {
x2, y2 := x-dx, y-dy
- if !f.IsIn(x2, y2) {
+ if !f.isIn(x2, y2) {
return x, y
}
x, y = x2, y2
}
}
-
-func (f Field) IsIn(x, y int) bool {
- return x >= f.x && x < f.x+f.lx && y >= f.y && y < f.y+f.ly
-}
diff --git a/pkg/field/step_test.go b/pkg/field/step_test.go
index f7da366..e21540c 100644
--- a/pkg/field/step_test.go
+++ b/pkg/field/step_test.go
@@ -40,36 +40,3 @@ func TestStep(t *testing.T) {
})
}
}
-
-func TestIsIn(t *testing.T) {
- // Test cases
- testCases := []struct {
- name string
- input string
- inputX int
- inputY int
- expected bool
- }{
- {"minimal0,0", "test_data/minimal.b98", 0, 0, true},
- {"minimal-1,0", "test_data/minimal.b98", -1, 0, false},
- {"minimal1,0", "test_data/minimal.b98", 1, 0, false},
- {"minimal0,-1", "test_data/minimal.b98", 0, -1, false},
- {"minimal0,1", "test_data/minimal.b98", 0, 1, false},
- {"hello3,0", "test_data/hello.b98", 3, 0, true},
- {"hello3,1", "test_data/hello.b98", 3, 1, false},
- {"factorial0,1", "test_data/factorial.b98", 0, 1, true},
- {"factorial14,1", "test_data/factorial.b98", 14, 1, true},
- {"factorial15,1", "test_data/factorial.b98", 15, 1, false},
- }
- for _, tc := range testCases {
- t.Run(tc.name, func(t *testing.T) {
- file, err := os.Open(tc.input)
- require.NoError(t, err, "Failed to open file")
- defer file.Close()
- field, err := LoadFile(file)
- valid := field.IsIn(tc.inputX, tc.inputY)
- require.NoError(t, err)
- require.Equal(t, tc.expected, valid, "Invalid value")
- })
- }
-}
diff --git a/pkg/field/utils.go b/pkg/field/utils.go
new file mode 100644
index 0000000..79a191b
--- /dev/null
+++ b/pkg/field/utils.go
@@ -0,0 +1,5 @@
+package field
+
+func (f Field) isIn(x, y int) bool {
+ return x >= f.x && x < f.x+f.lx && y >= f.y && y < f.y+f.ly
+}
diff --git a/pkg/field/utils_test.go b/pkg/field/utils_test.go
new file mode 100644
index 0000000..bcc8deb
--- /dev/null
+++ b/pkg/field/utils_test.go
@@ -0,0 +1,41 @@
+package field
+
+import (
+ "os"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestIsIn(t *testing.T) {
+ // Test cases
+ testCases := []struct {
+ name string
+ input string
+ inputX int
+ inputY int
+ expected bool
+ }{
+ {"minimal0,0", "test_data/minimal.b98", 0, 0, true},
+ {"minimal-1,0", "test_data/minimal.b98", -1, 0, false},
+ {"minimal1,0", "test_data/minimal.b98", 1, 0, false},
+ {"minimal0,-1", "test_data/minimal.b98", 0, -1, false},
+ {"minimal0,1", "test_data/minimal.b98", 0, 1, false},
+ {"hello3,0", "test_data/hello.b98", 3, 0, true},
+ {"hello3,1", "test_data/hello.b98", 3, 1, false},
+ {"factorial0,1", "test_data/factorial.b98", 0, 1, true},
+ {"factorial14,1", "test_data/factorial.b98", 14, 1, true},
+ {"factorial15,1", "test_data/factorial.b98", 15, 1, false},
+ }
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ file, err := os.Open(tc.input)
+ require.NoError(t, err, "Failed to open file")
+ defer file.Close()
+ field, err := LoadFile(file)
+ valid := field.isIn(tc.inputX, tc.inputY)
+ require.NoError(t, err)
+ require.Equal(t, tc.expected, valid, "Invalid value")
+ })
+ }
+}