Implemented function to get a field value
This commit is contained in:
parent
0c52da6714
commit
e8662451d4
2 changed files with 43 additions and 0 deletions
|
@ -1,5 +1,15 @@
|
||||||
package field
|
package field
|
||||||
|
|
||||||
|
func (f Field) Get(x, y int) int {
|
||||||
|
if y >= f.y && y < f.y+f.ly {
|
||||||
|
l := f.lines[y-f.y]
|
||||||
|
if x >= l.x && x < l.x+l.l {
|
||||||
|
return l.columns[x-l.x]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ' '
|
||||||
|
}
|
||||||
|
|
||||||
func (f Field) isIn(x, y int) bool {
|
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
|
return x >= f.x && x < f.x+f.lx && y >= f.y && y < f.y+f.ly
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,39 @@ import (
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestGet(t *testing.T) {
|
||||||
|
// Test cases
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
inputX int
|
||||||
|
inputY int
|
||||||
|
expected int
|
||||||
|
}{
|
||||||
|
{"minimal0,0", "test_data/minimal.b98", 0, 0, '@'},
|
||||||
|
{"minimal-1,0", "test_data/minimal.b98", -1, 0, ' '},
|
||||||
|
{"minimal1,0", "test_data/minimal.b98", 1, 0, ' '},
|
||||||
|
{"minimal0,-1", "test_data/minimal.b98", 0, -1, ' '},
|
||||||
|
{"minimal0,1", "test_data/minimal.b98", 0, 1, ' '},
|
||||||
|
{"hello3,0", "test_data/hello.b98", 3, 0, '"'},
|
||||||
|
{"hello3,1", "test_data/hello.b98", 3, 1, ' '},
|
||||||
|
{"factorial0,1", "test_data/factorial.b98", 0, 1, ' '},
|
||||||
|
{"factorial14,1", "test_data/factorial.b98", 14, 1, ' '},
|
||||||
|
{"factorial15,1", "test_data/factorial.b98", 15, 1, ' '},
|
||||||
|
}
|
||||||
|
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.Get(tc.inputX, tc.inputY)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, tc.expected, valid, "Invalid value")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestIsIn(t *testing.T) {
|
func TestIsIn(t *testing.T) {
|
||||||
// Test cases
|
// Test cases
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue