aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2021-10-02 00:59:38 +0200
committerJulien Dessaux2021-10-02 00:59:38 +0200
commit483d5c6a46950334610b369ee3ec253eea132aa0 (patch)
tree719995e60449da4c959026ff1d1f3226f94f4811
parentFixed some more resizing bugs in certain situations not covered by mycology (diff)
downloadgofunge98-483d5c6a46950334610b369ee3ec253eea132aa0.tar.gz
gofunge98-483d5c6a46950334610b369ee3ec253eea132aa0.tar.bz2
gofunge98-483d5c6a46950334610b369ee3ec253eea132aa0.zip
Fixed a file loading bug not covered by the mycology test suite
-rw-r--r--pkg/field/field.go28
-rw-r--r--pkg/field/field_test.go16
-rw-r--r--pkg/field/test_data/hello2.b982
3 files changed, 38 insertions, 8 deletions
diff --git a/pkg/field/field.go b/pkg/field/field.go
index a31f4da..72890fe 100644
--- a/pkg/field/field.go
+++ b/pkg/field/field.go
@@ -36,13 +36,19 @@ func Load(fd io.Reader) (*Field, error) {
data := make([]byte, 4096)
if n, errRead := fd.Read(data); errRead != nil {
if errRead == io.EOF {
- if f.ly == 0 && l.l == 0 {
- return nil, newDecodeError("No instruction on the first line of the file produces an unusable program in Befunge98")
+ if f.ly == 0 {
+ if l.l == 0 {
+ return nil, newDecodeError("No instruction on the first line of the file produces an unusable program in Befunge98")
+ }
+ f.x = l.x
}
if l.l > 0 {
f.ly++
- if f.lx-f.x < l.l-l.x {
- f.lx = l.l - l.x + f.x
+ if f.x < l.x {
+ f.x = l.x
+ }
+ if f.lx < l.l+l.x-f.x {
+ f.lx = l.l + l.x - f.x
}
f.lines = append(f.lines, *l)
}
@@ -56,12 +62,18 @@ func Load(fd io.Reader) (*Field, error) {
continue
}
if data[i] == '\n' || data[i] == '\r' {
- if f.ly == 0 && l.l == 0 {
- return nil, newDecodeError("No instruction on the first line of the file produces an unusable program in Befunge98")
+ if f.ly == 0 {
+ if l.l == 0 {
+ return nil, newDecodeError("No instruction on the first line of the file produces an unusable program in Befunge98")
+ }
+ f.x = l.x
}
f.ly++
- if f.lx < l.l {
- f.lx = l.l
+ if f.x > l.x {
+ f.x = l.x
+ }
+ if f.lx < l.l+l.x-f.x {
+ f.lx = l.l + l.x - f.x
}
f.lines = append(f.lines, *l)
l = new(Line)
diff --git a/pkg/field/field_test.go b/pkg/field/field_test.go
index 66b0291..77c04b8 100644
--- a/pkg/field/field_test.go
+++ b/pkg/field/field_test.go
@@ -38,6 +38,21 @@ func TestLoad(t *testing.T) {
},
},
}
+ // hello2 b98 file
+ hello2Field := Field{
+ x: 1,
+ y: 0,
+ lx: 33,
+ ly: 2,
+ lines: []Line{
+ Line{x: 33, l: 1, columns: []int{'v'}},
+ Line{
+ x: 1,
+ l: 33,
+ columns: []int{'@', ' ', '>', ' ', '#', ';', '>', ':', '#', ',', '_', 'e', '-', 'j', ';', ' ', '"', 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!', '"', 'd', 'a', '<'},
+ },
+ },
+ }
// factorial b98 file
factorialField := Field{
x: 0,
@@ -134,6 +149,7 @@ func TestLoad(t *testing.T) {
{"io error", "test_data/minimal.b98", iotest.TimeoutReader, nil, &ReadError{}},
{"minimal", "test_data/minimal.b98", nil, &minimalField, nil},
{"hello", "test_data/hello.b98", nil, &helloField, nil},
+ {"hello2", "test_data/hello2.b98", nil, &hello2Field, nil},
{"factorial", "test_data/factorial.b98", nil, &factorialField, nil},
{"dna", "test_data/dna.b98", nil, &dnaField, nil},
{"\\r\\n file", "test_data/rn.b98", nil, &rnField, nil},
diff --git a/pkg/field/test_data/hello2.b98 b/pkg/field/test_data/hello2.b98
new file mode 100644
index 0000000..c8a0ff9
--- /dev/null
+++ b/pkg/field/test_data/hello2.b98
@@ -0,0 +1,2 @@
+ v
+ @ > #;>:#,_e-j; "Hello world!"da<