From 2f66ddcb912ebf031594427b149c5acb83fa7693 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 1 Oct 2021 13:34:58 +0200 Subject: Fixed resizing bugs in certain situations not covered by mycology --- pkg/field/set.go | 10 +++--- pkg/field/set_test.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 6 deletions(-) (limited to 'pkg') diff --git a/pkg/field/set.go b/pkg/field/set.go index 5044898..c4cf2a4 100644 --- a/pkg/field/set.go +++ b/pkg/field/set.go @@ -25,8 +25,8 @@ func (f *Field) Set(x, y, v int) { } l.columns = append(l.columns, v) l.l = newL - if f.lx-f.x < l.l-l.x { - f.lx = l.l - l.x + if f.lx < l.l+l.x-f.x { + f.lx = l.l + l.x - f.x } } } else { @@ -60,8 +60,8 @@ func (f *Field) Set(x, y, v int) { f.lx = f.lx + f.x - x f.x = x } - if f.lx-f.x < x { - f.lx = x - f.x + if f.lx < x-f.x+1 { + f.lx = x - f.x + 1 } } } else { @@ -79,7 +79,7 @@ func (f *Field) Set(x, y, v int) { f.lx = f.lx + f.x - x f.x = x } - if f.lx-f.x < x { + if f.lx < x-f.x+1 { f.lx = x - f.x + 1 } } diff --git a/pkg/field/set_test.go b/pkg/field/set_test.go index 5b7f0ac..b66c434 100644 --- a/pkg/field/set_test.go +++ b/pkg/field/set_test.go @@ -87,7 +87,7 @@ func TestSetMinimalAppendTwo(t *testing.T) { bottomRight := Field{ x: 0, y: 0, - lx: 5, + lx: 6, ly: 3, lines: []Line{ Line{x: 0, l: 1, columns: []int{'@'}}, @@ -228,3 +228,88 @@ func TestSetMinimalAppendThree(t *testing.T) { }) } } + +func TestSetAppendResize(t *testing.T) { + base := Field{ + x: -1, y: -1, lx: 3, ly: 3, lines: []Line{ + Line{x: 0, l: 1, columns: []int{'u'}}, + Line{x: -1, l: 3, columns: []int{'l', '0', 'r'}}, + Line{x: 0, l: 1, columns: []int{'d'}}, + }, + } + xappend := Field{ + x: -1, y: -1, lx: 5, ly: 3, lines: []Line{ + Line{x: 0, l: 1, columns: []int{'u'}}, + Line{x: -1, l: 3, columns: []int{'l', '0', 'r'}}, + Line{x: 0, l: 4, columns: []int{'d', ' ', ' ', 'n'}}, + }, + } + xprepend := Field{ + x: -3, y: -1, lx: 5, ly: 3, lines: []Line{ + Line{x: 0, l: 1, columns: []int{'u'}}, + Line{x: -1, l: 3, columns: []int{'l', '0', 'r'}}, + Line{x: -3, l: 4, columns: []int{'n', ' ', ' ', 'd'}}, + }, + } + xprependyprepend := Field{ + x: -3, y: -3, lx: 5, ly: 5, lines: []Line{ + Line{x: -3, l: 1, columns: []int{'n'}}, + Line{}, + Line{x: 0, l: 1, columns: []int{'u'}}, + Line{x: -1, l: 3, columns: []int{'l', '0', 'r'}}, + Line{x: 0, l: 1, columns: []int{'d'}}, + }, + } + xappendyprepend := Field{ + x: -1, y: -3, lx: 5, ly: 5, lines: []Line{ + Line{x: 3, l: 1, columns: []int{'n'}}, + Line{}, + Line{x: 0, l: 1, columns: []int{'u'}}, + Line{x: -1, l: 3, columns: []int{'l', '0', 'r'}}, + Line{x: 0, l: 1, columns: []int{'d'}}, + }, + } + xprependyappend := Field{ + x: -3, y: -1, lx: 5, ly: 5, lines: []Line{ + Line{x: 0, l: 1, columns: []int{'u'}}, + Line{x: -1, l: 3, columns: []int{'l', '0', 'r'}}, + Line{x: 0, l: 1, columns: []int{'d'}}, + Line{}, + Line{x: -3, l: 1, columns: []int{'n'}}, + }, + } + xappendyappend := Field{ + x: -1, y: -1, lx: 5, ly: 5, lines: []Line{ + Line{x: 0, l: 1, columns: []int{'u'}}, + Line{x: -1, l: 3, columns: []int{'l', '0', 'r'}}, + Line{x: 0, l: 1, columns: []int{'d'}}, + Line{}, + Line{x: 3, l: 1, columns: []int{'n'}}, + }, + } + // Test cases + testCases := []struct { + name string + input Field + inputX int + inputY int + inputV int + expected Field + }{ + {"xappend", base, 3, 1, 'n', xappend}, + {"xprepend", base, -3, 1, 'n', xprepend}, + {"xprependyprepend", base, -3, -3, 'n', xprependyprepend}, + {"xappendyprepend", base, 3, -3, 'n', xappendyprepend}, + {"xprependyappend", base, -3, 3, 'n', xprependyappend}, + {"xappendyappend", base, 3, 3, 'n', xappendyappend}, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + field := tc.input + field.lines = make([]Line, field.ly) + copy(field.lines, tc.input.lines) + field.Set(tc.inputX, tc.inputY, tc.inputV) + require.Equal(t, tc.expected, field, "Invalid value") + }) + } +} -- cgit v1.2.3