diff options
author | Julien Dessaux | 2021-10-01 19:06:33 +0200 |
---|---|---|
committer | Julien Dessaux | 2021-10-01 19:06:33 +0200 |
commit | 40257e354d054c0a007adb7139a031ca7cb941fa (patch) | |
tree | cabdc6e6095ddf8fb30c4f363a07d07bac5e3f1c | |
parent | Began implementing the funge field (diff) | |
download | nimfunge98-40257e354d054c0a007adb7139a031ca7cb941fa.tar.gz nimfunge98-40257e354d054c0a007adb7139a031ca7cb941fa.tar.bz2 nimfunge98-40257e354d054c0a007adb7139a031ca7cb941fa.zip |
Continued implementing the funge space field
-rw-r--r-- | nimfunge98.nimble | 7 | ||||
-rw-r--r-- | src/field.nim | 140 | ||||
-rw-r--r-- | tests/testField.nim | 173 |
3 files changed, 296 insertions, 24 deletions
diff --git a/nimfunge98.nimble b/nimfunge98.nimble index e860c43..71d4e92 100644 --- a/nimfunge98.nimble +++ b/nimfunge98.nimble @@ -13,12 +13,13 @@ requires "nim >= 1.4.8" # Tasks -task integration, "Runs the test suite": - exec "testament all" +task test, "Runs the test suite": + exec "testament pattern \"tests/*.nim\"" task coverage, "Run all tests and calculate coverage": exec "coco --target 'tests/**/*.nim' --cov '!tests,!nimcache'" task clean, "Clean": exec "rm -rf coverage lcov.info nimcache" - exec "rm -rf outputGotten.txt testresults tests/megatest tests/megatest.nim" + exec "rm -rf outputGotten.txt testresults tests/megatest.nim" + exec "find tests/ -type f -executable -delete" diff --git a/src/field.nim b/src/field.nim index a9d26fc..224e21d 100644 --- a/src/field.nim +++ b/src/field.nim @@ -1,37 +1,135 @@ type - Line = ref object + Line = object x, l: int columns: seq[int] - Field* = ref object + Field* = object x, y: int lx, ly: int lines: seq[Line] +proc blank*(f: var Field, x, y: int) = + if y < f.y or y >= f.y+f.ly: # outside the field + return + var l = addr f.lines[y-f.y] + if x < l.x or x >= l.x+l.l: # outside the field + return + if x > l.x and x < l.x+l.l-1: # just set the value + l.columns[x-l.x] = int(' ') + return + if l.l == 1: # this was the last character on the line + if y == f.y: # we need to trim the leading lines + var i = 1 + while f.lines[i].l == 0: + inc i + f.y += i + f.lines = f.lines[i..<f.ly] + f.ly -= i + elif y == f.y+f.ly-1: # we need to trim the trailing lines + var i = f.ly-2 + while f.lines[i].l == 0: + dec i + f.ly = i+1 + f.lines = f.lines[0..<f.ly] + else: # it was a line in the middle + l.l = 0 + l.columns = @[] + elif x == l.x: # we need to remove leading spaces + var i = 1 + while l.columns[i] == int(' '): + inc i + l.x += i + l.columns = l.columns[i..<l.l] + l.l -= i + elif x == l.l+l.x-1: # we need to remove trailing spaces + var i = l.l-2 + while l.columns[i] == int(' '): + dec i + l.l = i+1 + l.columns = l.columns[0..<l.l] + # we now need to calculate the new field limits + f.x = f.lines[0].x + var x2 = f.lines[0].l + f.lines[0].x + for i in 1..<f.ly: + if f.lines[i].l == 0: + continue + if f.x > f.lines[i].x: + f.x = f.lines[i].x + if x2 < f.lines[i].x + f.lines[i].l: + x2 = f.lines[i].x + f.lines[i].l + f.lx = x2-f.x + proc get*(f: Field, x, y: int): int = - if y >= f.y and y < f.y + f.ly: + if y >= f.y and y < f.y+f.ly: let l = f.lines[y-f.y] - if x >= l.x and x < l.x + l.l: + if x >= l.x and x < l.x+l.l: return l.columns[x-l.x] return int(' ') proc isIn*(f: Field, x, y: int): bool = return x >= f.x and y >= f.y and x < f.x+f.lx and y < f.y+f.ly -when defined(unitTesting): - let minimal = Field( - x: 0, - y: 0, - lx: 1, - ly: 1, - lines: @[ - Line(x: 0, l: 1, columns: @[int('@')]) - ] - ) - suite "Field": - test "Field.get": - check minimal.get(0,0) == int('@') - check minimal.get(1,0) == int(' ') - test "Field.isIn": - check minimal.isIn(0, 0) == true - check minimal.isIn(1, 0) == false +proc set*(f: var Field, x, y, v: int) = + if v == int(' '): + f.blank(x, y) + elif y >= f.y: + if y < f.y+f.ly: # the line exists + var l = addr f.lines[y-f.y] + if l.l == 0: # An empty line is a special case + l.x = x + l.l = 1 + l.columns = @[v] + if f.x > x: + f.lx = f.lx+f.x-x + f.x = x + if f.lx < x-f.x+1: + f.lx = x-f.x+1 + elif x >= l.x: + if x < l.x+l.l: # just set the value + l.columns[x-l.x] = v + else: # append columns + let newL = x-l.x+1 + l.columns.setlen(newL) + for i in l.l..<newL-1: + l.columns[i] = int(' ') + l.columns[newL-1] = v + l.l = newL + if f.lx < l.l+l.x-f.x: + f.lx = l.l+l.x-f.x + else: # preprend columns + let newL = l.l + l.x - x + var newline = newSeqUninitialized[int](newL) + newline[0] = v + for i in 1..<l.x-x: + newline[i] = int(' ') + for i in l.x-x..<newL: + newline[i] = l.columns[i-l.x+x] + l.columns = newline + l.x = x + l.l = newL + if f.x > x: + f.lx = f.lx + f.x - x + f.x = x + else: # append lines + f.ly = y-f.y+1 + f.lines.setlen(f.ly) + f.lines[f.ly-1] = Line(x: x, l: 1, columns: @[v]) + if f.x > x: + f.lx = f.lx + f.x - x + f.x = x + if f.lx < x-f.x+1: + f.lx = x-f.x+1 + else: # prepend lines + let newLy = f.ly+f.y-y + var newlines = newSeq[Line](newLy) + newlines[0] = Line(x: x, l: 1, columns: @[v]) + for i in f.y-y..<newLy: + newlines[i] = f.lines[i-f.y+y] + f.lines = newlines + f.y = y + f.ly = newLy + if f.x > x: + f.lx = f.lx+f.x-x + f.x = x + if f.lx < x-f.x+1: + f.lx = x-f.x+1 diff --git a/tests/testField.nim b/tests/testField.nim index 74400b8..7c49149 100644 --- a/tests/testField.nim +++ b/tests/testField.nim @@ -1,3 +1,176 @@ import unittest include field + +proc `==`(a, b: Line): bool = a.x == b.x and a.l == b.l and a.columns == b.columns +proc `==`(a, b: Field): bool = a.x == b.x and a.lx == b.lx and a.y == b.y and a.ly == b.ly and a.lines == b.lines + +const minimal = Field(x: 0, y: 0, lx: 1, ly: 1, lines: @[Line(x: 0, l: 1, columns: @[int('@')])]) + +suite "Field": + test "Field.blank": + var f = Field(x: -7, y: -5, lx: 17, ly: 10, lines: @[ + Line(x: -5, l: 1, columns: @[int('x')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: 8, l: 1, columns: @[int('u')]), + Line(x: 9, l: 1, columns: @[int('e')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), int('z'), 32, 32, int('r')]), + Line(x: -3, l: 1, columns: @[int('f')]), + Line(x: 5, l: 1, columns: @[int('d')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: -7, l: 1, columns: @[int('y')]), + ]) + const moinsz = Field(x: -7, y: -5, lx: 17, ly: 10, lines: @[ + Line(x: -5, l: 1, columns: @[int('x')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: 8, l: 1, columns: @[int('u')]), + Line(x: 9, l: 1, columns: @[int('e')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]), + Line(x: -3, l: 1, columns: @[int('f')]), + Line(x: 5, l: 1, columns: @[int('d')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: -7, l: 1, columns: @[int('y')]), + ]) + f.blank(1, 0) + check f == moinsz + const moinsy = Field(x: -5, y: -5, lx: 15, ly: 8, lines: @[ + Line(x: -5, l: 1, columns: @[int('x')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: 8, l: 1, columns: @[int('u')]), + Line(x: 9, l: 1, columns: @[int('e')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]), + Line(x: -3, l: 1, columns: @[int('f')]), + Line(x: 5, l: 1, columns: @[int('d')]), + ]) + f.blank(-7, 4) + check f == moinsy + const moinsx = Field(x: -3, y: -3, lx: 13, ly: 6, lines: @[ + Line(x: 8, l: 1, columns: @[int('u')]), + Line(x: 9, l: 1, columns: @[int('e')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]), + Line(x: -3, l: 1, columns: @[int('f')]), + Line(x: 5, l: 1, columns: @[int('d')]), + ]) + f.blank(-5, -5) + check f == moinsx + const moinsf = Field(x: -2, y: -3, lx: 12, ly: 6, lines: @[ + Line(x: 8, l: 1, columns: @[int('u')]), + Line(x: 9, l: 1, columns: @[int('e')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]), + Line(x: -3, l: 0, columns: @[]), + Line(x: 5, l: 1, columns: @[int('d')]), + ]) + f.blank(-3, 1) + check f == moinsf + const moinse = Field(x: -2, y: -3, lx: 11, ly: 6, lines: @[ + Line(x: 8, l: 1, columns: @[int('u')]), + Line(x: 9, l: 0, columns: @[]), + Line(x: 0, l: 0, columns: @[]), + Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]), + Line(x: -3, l: 0, columns: @[]), + Line(x: 5, l: 1, columns: @[int('d')]), + ]) + f.blank(9, -2) + check f == moinse + const moinsu = Field(x: -2, y: 0, lx: 8, ly: 3, lines: @[ + Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]), + Line(x: -3, l: 0, columns: @[]), + Line(x: 5, l: 1, columns: @[int('d')]), + ]) + f.blank(8, -3) + check f == moinsu + const moinsd = Field(x: -2, y: 0, lx: 7, ly: 1, lines: @[Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')])]) + f.blank(5, 2) + check f == moinsd + const moinsl = Field(x: 0, y: 0, lx: 5, ly: 1, lines: @[Line(x: 0, l: 5, columns: @[int('@'), 32, 32, 32, int('r')])]) + f.blank(-2,0) + check f == moinsl + const moinsr = Field(x: 0, y: 0, lx: 1, ly: 1, lines: @[Line(x: 0, l: 1, columns: @[int('@')])]) + f.blank(4,0) + check f == moinsr + test "Field.get": + check minimal.get(0,0) == int('@') + check minimal.get(1,0) == int(' ') + test "Field.isIn": + check minimal.isIn(0, 0) == true + check minimal.isIn(1, 0) == false + test "Field.set": + var f = Field(x: 0, y: 0, lx: 1, ly: 1, lines: @[Line(x: 0, l: 1, columns: @[int('>')])]) + f.set(0,0,int('@')) + check f == minimal + f.set(1,0,int(' ')) + check f == minimal + const xappend = Field(x: 0, y: 0, lx: 5, ly: 1, lines: @[Line(x: 0, l: 5, columns: @[int('@'), 32, 32, 32, int('r')])]) + f.set(4, 0, int('r')) + check f == xappend + const xprepend = Field(x: -2, y: 0, lx: 7, ly: 1, lines: @[Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')])]) + f.set(-2, 0, int('l')) + check f == xprepend + const yappend = Field(x: -2, y: 0, lx: 8, ly: 3, lines: @[ + Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: 5, l: 1, columns: @[int('d')]), + ]) + f.set(5, 2, int('d')) + check f == yappend + const yprepend = Field(x: -2, y: -3, lx: 11, ly: 6, lines: @[ + Line(x: 8, l: 1, columns: @[int('u')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: 0, l: 0, columns: @[]), + Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: 5, l: 1, columns: @[int('d')]), + ]) + f.set(8, -3, int('u')) + check f == yprepend + const xappendEmptyline = Field(x: -2, y: -3, lx: 12, ly: 6, lines: @[ + Line(x: 8, l: 1, columns: @[int('u')]), + Line(x: 9, l: 1, columns: @[int('e')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: 5, l: 1, columns: @[int('d')]), + ]) + f.set(9, -2, int('e')) + check f == xappendEmptyline + const xprependEmptyline = Field(x: -3, y: -3, lx: 13, ly: 6, lines: @[ + Line(x: 8, l: 1, columns: @[int('u')]), + Line(x: 9, l: 1, columns: @[int('e')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]), + Line(x: -3, l: 1, columns: @[int('f')]), + Line(x: 5, l: 1, columns: @[int('d')]), + ]) + f.set(-3, 1, int('f')) + check f == xprependEmptyline + const xprependyprepend = Field(x: -5, y: -5, lx: 15, ly: 8, lines: @[ + Line(x: -5, l: 1, columns: @[int('x')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: 8, l: 1, columns: @[int('u')]), + Line(x: 9, l: 1, columns: @[int('e')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]), + Line(x: -3, l: 1, columns: @[int('f')]), + Line(x: 5, l: 1, columns: @[int('d')]), + ]) + f.set(-5, -5, int('x')) + check f == xprependyprepend + const xprependyappend = Field(x: -7, y: -5, lx: 17, ly: 10, lines: @[ + Line(x: -5, l: 1, columns: @[int('x')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: 8, l: 1, columns: @[int('u')]), + Line(x: 9, l: 1, columns: @[int('e')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]), + Line(x: -3, l: 1, columns: @[int('f')]), + Line(x: 5, l: 1, columns: @[int('d')]), + Line(x: 0, l: 0, columns: @[]), + Line(x: -7, l: 1, columns: @[int('y')]), + ]) + f.set(-7, 4, int('y')) + check f == xprependyappend |