aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2021-10-10 17:17:01 +0200
committerJulien Dessaux2021-10-10 17:17:01 +0200
commit4ea0083ffcad9b73057951d871bcf2dbd855bc7c (patch)
tree55f5875f427459342bf0316d106767eb34397b77
parentAdded release build flags (diff)
downloadnimfunge98-4ea0083ffcad9b73057951d871bcf2dbd855bc7c.tar.gz
nimfunge98-4ea0083ffcad9b73057951d871bcf2dbd855bc7c.tar.bz2
nimfunge98-4ea0083ffcad9b73057951d871bcf2dbd855bc7c.zip
Refactoring to test the feel of changing a value object to a ref object in nim
Diffstat (limited to '')
-rw-r--r--src/field.nim33
-rw-r--r--tests/field.nim96
2 files changed, 70 insertions, 59 deletions
diff --git a/src/field.nim b/src/field.nim
index e63a64e..215a0e3 100644
--- a/src/field.nim
+++ b/src/field.nim
@@ -1,5 +1,5 @@
type
- Line = object
+ Line = ref object
x, l: int
columns: seq[int]
@@ -11,7 +11,7 @@ type
func 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]
+ var l = 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
@@ -20,14 +20,14 @@ func Blank*(f: var Field, x, y: int) =
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:
+ while f.lines[i] == nil or 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:
+ while f.lines[i] == nil or f.lines[i].l == 0:
dec i
f.ly = i+1
f.lines = f.lines[0..<f.ly]
@@ -51,7 +51,7 @@ func Blank*(f: var Field, x, y: int) =
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:
+ if f.lines[i] == nil or f.lines[i].l == 0:
continue
if f.x > f.lines[i].x:
f.x = f.lines[i].x
@@ -74,10 +74,8 @@ proc Load*(filename: string): ref Field =
if not open(file, filename):
return nil
defer: file.close()
- var f: ref Field
- new(f)
- f.lines.add(Line())
- var l = addr f.lines[0]
+ var f = new(Field)
+ var l = new(Line)
var trailingSpaces = 0
var data: array[4096, char]
var lastReadIsCR = false
@@ -89,9 +87,10 @@ proc Load*(filename: string): ref Field =
return nil
f.x = l.x
if l.l > 0:
- inc f.ly
if f.lx < l.l+l.x-f.x:
f.lx = l.l+l.x-f.x
+ f.lines.add(l)
+ inc f.ly
break
var i = 0
while i < n:
@@ -107,14 +106,14 @@ proc Load*(filename: string): ref Field =
if l.l == 0:
return nil
f.x = l.x
- inc f.ly
if l.l > 0:
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.add(Line())
- l = addr f.lines[^1]
+ inc f.ly
+ f.lines.add(l)
+ l = new(Line)
trailingSpaces = 0
if data[i] == '\r':
if i+1 < n and data[i+1] == '\n':
@@ -140,7 +139,6 @@ proc Load*(filename: string): ref Field =
l.columns.add(int(data[i]))
inc l.l
inc i
- f.lines = f.lines[0..<f.ly]
return f
func Set*(f: var Field, x, y, v: int) =
@@ -148,11 +146,14 @@ func Set*(f: var Field, x, y, 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
+ var l = f.lines[y-f.y]
+ if l == nil or l.l == 0: # An empty line is a special case
+ if l == nil:
+ new(l)
l.x = x
l.l = 1
l.columns = @[v]
+ f.lines[y-f.y] = l
if f.x > x:
f.lx = f.lx+f.x-x
f.x = x
diff --git a/tests/field.nim b/tests/field.nim
index 455d81f..6196d97 100644
--- a/tests/field.nim
+++ b/tests/field.nim
@@ -2,99 +2,109 @@ import unittest
include ../src/field
-func `==`(a, b: Line): bool = a.x == b.x and a.l == b.l and a.columns == b.columns
-func `==`(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
+func `==`(a, b: Field): bool =
+ if a.x == b.x and a.lx == b.lx and a.y == b.y and a.ly == b.ly:
+ for i in 0..<a.ly:
+ if a.lines[i] == nil and b.lines[i] == nil:
+ continue
+ if a.lines[i] == nil xor b.lines[i] == nil:
+ return false
+ if a.lines[i].x != b.lines[i].x or a.lines[i].l != b.lines[i].l or a.lines[i].columns != b.lines[i].columns:
+ return false
+ return true
+ return false
+
func cols(a: openarray[char]): seq[int] =
result.setlen(a.len)
for i in 0..<a.len:
result[i] = a[i].int()
-const minimal = Field(x: 0, y: 0, lx: 1, ly: 1, lines: @[Line(x: 0, l: 1, columns: @[int('@')])])
+let minimal = Field(x: 0, y: 0, lx: 1, ly: 1, lines: @[Line(x: 0, l: 1, columns: @[int('@')])])
suite "Field":
test "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: @[]),
+ nil,
Line(x: 8, l: 1, columns: @[int('u')]),
Line(x: 9, l: 1, columns: @[int('e')]),
- Line(x: 0, l: 0, columns: @[]),
+ nil,
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: @[]),
+ nil,
Line(x: -7, l: 1, columns: @[int('y')]),
])
- const moinsz = Field(x: -7, y: -5, lx: 17, ly: 10, lines: @[
+ let 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: @[]),
+ nil,
Line(x: 8, l: 1, columns: @[int('u')]),
Line(x: 9, l: 1, columns: @[int('e')]),
- Line(x: 0, l: 0, columns: @[]),
+ nil,
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: @[]),
+ nil,
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: @[
+ let 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: @[]),
+ nil,
Line(x: 8, l: 1, columns: @[int('u')]),
Line(x: 9, l: 1, columns: @[int('e')]),
- Line(x: 0, l: 0, columns: @[]),
+ nil,
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: @[
+ let 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: @[]),
+ nil,
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: @[
+ let 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: @[]),
+ nil,
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: @[
+ let 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: @[]),
+ nil,
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: @[
+ let 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')])])
+ let 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')])])
+ let 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('@')])])
+ let 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 "Get":
@@ -136,71 +146,71 @@ suite "Field":
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')])])
+ let 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')])])
+ let 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: @[
+ let 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: @[]),
+ nil,
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: @[
+ let 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: @[]),
+ nil,
+ nil,
Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
- Line(x: 0, l: 0, columns: @[]),
+ nil,
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: @[
+ let 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: @[]),
+ nil,
Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
- Line(x: 0, l: 0, columns: @[]),
+ nil,
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: @[
+ let 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: @[]),
+ nil,
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: @[
+ let 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: @[]),
+ nil,
Line(x: 8, l: 1, columns: @[int('u')]),
Line(x: 9, l: 1, columns: @[int('e')]),
- Line(x: 0, l: 0, columns: @[]),
+ nil,
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: @[
+ let 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: @[]),
+ nil,
Line(x: 8, l: 1, columns: @[int('u')]),
Line(x: 9, l: 1, columns: @[int('e')]),
- Line(x: 0, l: 0, columns: @[]),
+ nil,
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: @[]),
+ nil,
Line(x: -7, l: 1, columns: @[int('y')]),
])
f.Set(-7, 4, int('y'))