Removed a useless len field for field lines
This commit is contained in:
parent
4ea0083ffc
commit
4407b1d67c
2 changed files with 128 additions and 136 deletions
|
@ -1,6 +1,6 @@
|
||||||
type
|
type
|
||||||
Line = ref object
|
Line = ref object
|
||||||
x, l: int
|
x: int
|
||||||
columns: seq[int]
|
columns: seq[int]
|
||||||
|
|
||||||
Field* = object
|
Field* = object
|
||||||
|
@ -12,57 +12,54 @@ func Blank*(f: var Field, x, y: int) =
|
||||||
if y < f.y or y >= f.y+f.ly: # outside the field
|
if y < f.y or y >= f.y+f.ly: # outside the field
|
||||||
return
|
return
|
||||||
var l = 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
|
if x < l.x or x >= l.x+l.columns.len: # outside the field
|
||||||
return
|
return
|
||||||
if x > l.x and x < l.x+l.l-1: # just set the value
|
if x > l.x and x < l.x+l.columns.len-1: # just set the value
|
||||||
l.columns[x-l.x] = int(' ')
|
l.columns[x-l.x] = int(' ')
|
||||||
return
|
return
|
||||||
if l.l == 1: # this was the last character on the line
|
if l.columns.len == 1: # this was the last character on the line
|
||||||
if y == f.y: # we need to trim the leading lines
|
if y == f.y: # we need to trim the leading lines
|
||||||
var i = 1
|
var i = 1
|
||||||
while f.lines[i] == nil or f.lines[i].l == 0:
|
while f.lines[i] == nil or f.lines[i].columns.len == 0:
|
||||||
inc i
|
inc i
|
||||||
f.y += i
|
f.y += i
|
||||||
f.lines = f.lines[i..<f.ly]
|
f.lines = f.lines[i..<f.ly]
|
||||||
f.ly -= i
|
f.ly -= i
|
||||||
elif y == f.y+f.ly-1: # we need to trim the trailing lines
|
elif y == f.y+f.ly-1: # we need to trim the trailing lines
|
||||||
var i = f.ly-2
|
var i = f.ly-2
|
||||||
while f.lines[i] == nil or f.lines[i].l == 0:
|
while f.lines[i] == nil or f.lines[i].columns.len == 0:
|
||||||
dec i
|
dec i
|
||||||
f.ly = i+1
|
f.ly = i+1
|
||||||
f.lines = f.lines[0..<f.ly]
|
f.lines = f.lines[0..<f.ly]
|
||||||
else: # it was a line in the middle
|
else: # it was a line in the middle
|
||||||
l.l = 0
|
|
||||||
l.columns = @[]
|
l.columns = @[]
|
||||||
elif x == l.x: # we need to remove leading spaces
|
elif x == l.x: # we need to remove leading spaces
|
||||||
var i = 1
|
var i = 1
|
||||||
while l.columns[i] == int(' '):
|
while l.columns[i] == int(' '):
|
||||||
inc i
|
inc i
|
||||||
l.x += i
|
l.x += i
|
||||||
l.columns = l.columns[i..<l.l]
|
l.columns = l.columns[i..<l.columns.len]
|
||||||
l.l -= i
|
elif x == l.columns.len+l.x-1: # we need to remove trailing spaces
|
||||||
elif x == l.l+l.x-1: # we need to remove trailing spaces
|
var i = l.columns.len-2
|
||||||
var i = l.l-2
|
|
||||||
while l.columns[i] == int(' '):
|
while l.columns[i] == int(' '):
|
||||||
dec i
|
dec i
|
||||||
l.l = i+1
|
l.columns = l.columns[0..i]
|
||||||
l.columns = l.columns[0..<l.l]
|
|
||||||
# we now need to calculate the new field limits
|
# we now need to calculate the new field limits
|
||||||
f.x = f.lines[0].x
|
f.x = f.lines[0].x
|
||||||
var x2 = f.lines[0].l + f.lines[0].x
|
var x2 = f.lines[0].columns.len + f.lines[0].x
|
||||||
for i in 1..<f.ly:
|
for i in 1..<f.ly:
|
||||||
if f.lines[i] == nil or f.lines[i].l == 0:
|
if f.lines[i] == nil or f.lines[i].columns.len == 0:
|
||||||
continue
|
continue
|
||||||
if f.x > f.lines[i].x:
|
if f.x > f.lines[i].x:
|
||||||
f.x = f.lines[i].x
|
f.x = f.lines[i].x
|
||||||
if x2 < f.lines[i].x + f.lines[i].l:
|
if x2 < f.lines[i].x + f.lines[i].columns.len:
|
||||||
x2 = f.lines[i].x + f.lines[i].l
|
x2 = f.lines[i].x + f.lines[i].columns.len
|
||||||
f.lx = x2-f.x
|
f.lx = x2-f.x
|
||||||
|
|
||||||
func Get*(f: Field, x, y: int): int =
|
func 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]
|
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.columns.len:
|
||||||
return l.columns[x-l.x]
|
return l.columns[x-l.x]
|
||||||
return int(' ')
|
return int(' ')
|
||||||
|
|
||||||
|
@ -83,12 +80,12 @@ proc Load*(filename: string): ref Field =
|
||||||
let n = file.readChars(data, 0, 4096)
|
let n = file.readChars(data, 0, 4096)
|
||||||
if n <= 0:
|
if n <= 0:
|
||||||
if f.ly == 0:
|
if f.ly == 0:
|
||||||
if l.l == 0: # we got en empty file!
|
if l.columns.len == 0: # we got en empty file!
|
||||||
return nil
|
return nil
|
||||||
f.x = l.x
|
f.x = l.x
|
||||||
if l.l > 0:
|
if l.columns.len > 0:
|
||||||
if f.lx < l.l+l.x-f.x:
|
if f.lx < l.columns.len+l.x-f.x:
|
||||||
f.lx = l.l+l.x-f.x
|
f.lx = l.columns.len+l.x-f.x
|
||||||
f.lines.add(l)
|
f.lines.add(l)
|
||||||
inc f.ly
|
inc f.ly
|
||||||
break
|
break
|
||||||
|
@ -103,14 +100,14 @@ proc Load*(filename: string): ref Field =
|
||||||
continue
|
continue
|
||||||
if data[i] == '\n' or data[i] == '\r':
|
if data[i] == '\n' or data[i] == '\r':
|
||||||
if f.ly == 0:
|
if f.ly == 0:
|
||||||
if l.l == 0:
|
if l.columns.len == 0:
|
||||||
return nil
|
return nil
|
||||||
f.x = l.x
|
f.x = l.x
|
||||||
if l.l > 0:
|
if l.columns.len > 0:
|
||||||
if f.x > l.x:
|
if f.x > l.x:
|
||||||
f.x = l.x
|
f.x = l.x
|
||||||
if f.lx < l.l+l.x-f.x:
|
if f.lx < l.columns.len+l.x-f.x:
|
||||||
f.lx = l.l+l.x-f.x
|
f.lx = l.columns.len+l.x-f.x
|
||||||
inc f.ly
|
inc f.ly
|
||||||
f.lines.add(l)
|
f.lines.add(l)
|
||||||
l = new(Line)
|
l = new(Line)
|
||||||
|
@ -122,22 +119,20 @@ proc Load*(filename: string): ref Field =
|
||||||
lastReadIsCR = true
|
lastReadIsCR = true
|
||||||
else:
|
else:
|
||||||
if data[i] == ' ':
|
if data[i] == ' ':
|
||||||
if l.l == 0: # trim leading spaces
|
if l.columns.len == 0: # trim leading spaces
|
||||||
inc l.x
|
inc l.x
|
||||||
else:
|
else:
|
||||||
inc trailingSpaces
|
inc trailingSpaces
|
||||||
else:
|
else:
|
||||||
if trailingSpaces > 0:
|
if trailingSpaces > 0:
|
||||||
let newL = l.l + trailingSpaces + 1
|
let oldL = l.columns.len
|
||||||
l.columns.setlen(newL)
|
l.columns.setlen(oldL+trailingSpaces+1)
|
||||||
for j in l.l..<newL-1:
|
for j in oldL..<l.columns.len-1:
|
||||||
l.columns[j] = int(' ')
|
l.columns[j] = int(' ')
|
||||||
l.columns[newL-1] = int(data[i])
|
l.columns[^1] = int(data[i])
|
||||||
l.l = newL
|
|
||||||
trailingSpaces = 0
|
trailingSpaces = 0
|
||||||
else:
|
else:
|
||||||
l.columns.add(int(data[i]))
|
l.columns.add(int(data[i]))
|
||||||
inc l.l
|
|
||||||
inc i
|
inc i
|
||||||
return f
|
return f
|
||||||
|
|
||||||
|
@ -147,11 +142,10 @@ func Set*(f: var Field, x, y, v: int) =
|
||||||
elif y >= f.y:
|
elif y >= f.y:
|
||||||
if y < f.y+f.ly: # the line exists
|
if y < f.y+f.ly: # the line exists
|
||||||
var l = f.lines[y-f.y]
|
var l = f.lines[y-f.y]
|
||||||
if l == nil or l.l == 0: # An empty line is a special case
|
if l == nil or l.columns.len == 0: # An empty line is a special case
|
||||||
if l == nil:
|
if l == nil:
|
||||||
new(l)
|
new(l)
|
||||||
l.x = x
|
l.x = x
|
||||||
l.l = 1
|
|
||||||
l.columns = @[v]
|
l.columns = @[v]
|
||||||
f.lines[y-f.y] = l
|
f.lines[y-f.y] = l
|
||||||
if f.x > x:
|
if f.x > x:
|
||||||
|
@ -160,35 +154,33 @@ func Set*(f: var Field, x, y, v: int) =
|
||||||
if f.lx < x-f.x+1:
|
if f.lx < x-f.x+1:
|
||||||
f.lx = x-f.x+1
|
f.lx = x-f.x+1
|
||||||
elif x >= l.x:
|
elif x >= l.x:
|
||||||
if x < l.x+l.l: # just set the value
|
if x < l.x+l.columns.len: # just set the value
|
||||||
l.columns[x-l.x] = v
|
l.columns[x-l.x] = v
|
||||||
else: # append columns
|
else: # append columns
|
||||||
let newL = x-l.x+1
|
let oldL = l.columns.len
|
||||||
l.columns.setlen(newL)
|
l.columns.setlen(x-l.x+1)
|
||||||
for i in l.l..<newL-1:
|
for i in oldL..<l.columns.len-1:
|
||||||
l.columns[i] = int(' ')
|
l.columns[i] = int(' ')
|
||||||
l.columns[newL-1] = v
|
l.columns[^1] = v
|
||||||
l.l = newL
|
if f.lx < l.columns.len+l.x-f.x:
|
||||||
if f.lx < l.l+l.x-f.x:
|
f.lx = l.columns.len+l.x-f.x
|
||||||
f.lx = l.l+l.x-f.x
|
|
||||||
else: # preprend columns
|
else: # preprend columns
|
||||||
let newL = l.l + l.x - x
|
let oldL = l.columns.len
|
||||||
var newline = newSeqUninitialized[int](newL)
|
var newline = newSeqUninitialized[int](oldL+l.x-x)
|
||||||
newline[0] = v
|
newline[0] = v
|
||||||
for i in 1..<l.x-x:
|
for i in 1..<l.x-x:
|
||||||
newline[i] = int(' ')
|
newline[i] = int(' ')
|
||||||
for i in l.x-x..<newL:
|
for i in l.x-x..<newline.len:
|
||||||
newline[i] = l.columns[i-l.x+x]
|
newline[i] = l.columns[i-l.x+x]
|
||||||
l.columns = newline
|
l.columns = newline
|
||||||
l.x = x
|
l.x = x
|
||||||
l.l = newL
|
|
||||||
if f.x > x:
|
if f.x > x:
|
||||||
f.lx = f.lx + f.x - x
|
f.lx = f.lx + f.x - x
|
||||||
f.x = x
|
f.x = x
|
||||||
else: # append lines
|
else: # append lines
|
||||||
f.ly = y-f.y+1
|
f.ly = y-f.y+1
|
||||||
f.lines.setlen(f.ly)
|
f.lines.setlen(f.ly)
|
||||||
f.lines[f.ly-1] = Line(x: x, l: 1, columns: @[v])
|
f.lines[f.ly-1] = Line(x: x, columns: @[v])
|
||||||
if f.x > x:
|
if f.x > x:
|
||||||
f.lx = f.lx + f.x - x
|
f.lx = f.lx + f.x - x
|
||||||
f.x = x
|
f.x = x
|
||||||
|
@ -197,7 +189,7 @@ func Set*(f: var Field, x, y, v: int) =
|
||||||
else: # prepend lines
|
else: # prepend lines
|
||||||
let newLy = f.ly+f.y-y
|
let newLy = f.ly+f.y-y
|
||||||
var newlines = newSeq[Line](newLy)
|
var newlines = newSeq[Line](newLy)
|
||||||
newlines[0] = Line(x: x, l: 1, columns: @[v])
|
newlines[0] = Line(x: x, columns: @[v])
|
||||||
for i in f.y-y..<newLy:
|
for i in f.y-y..<newLy:
|
||||||
newlines[i] = f.lines[i-f.y+y]
|
newlines[i] = f.lines[i-f.y+y]
|
||||||
f.lines = newlines
|
f.lines = newlines
|
||||||
|
|
174
tests/field.nim
174
tests/field.nim
|
@ -9,7 +9,7 @@ func `==`(a, b: Field): bool =
|
||||||
continue
|
continue
|
||||||
if a.lines[i] == nil xor b.lines[i] == nil:
|
if a.lines[i] == nil xor b.lines[i] == nil:
|
||||||
return false
|
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:
|
if a.lines[i].x != b.lines[i].x or a.lines[i].columns != b.lines[i].columns:
|
||||||
return false
|
return false
|
||||||
return true
|
return true
|
||||||
return false
|
return false
|
||||||
|
@ -19,92 +19,92 @@ func cols(a: openarray[char]): seq[int] =
|
||||||
for i in 0..<a.len:
|
for i in 0..<a.len:
|
||||||
result[i] = a[i].int()
|
result[i] = a[i].int()
|
||||||
|
|
||||||
let 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, columns: @[int('@')])])
|
||||||
|
|
||||||
suite "Field":
|
suite "Field":
|
||||||
test "Blank":
|
test "Blank":
|
||||||
var f = Field(x: -7, y: -5, lx: 17, ly: 10, lines: @[
|
var f = Field(x: -7, y: -5, lx: 17, ly: 10, lines: @[
|
||||||
Line(x: -5, l: 1, columns: @[int('x')]),
|
Line(x: -5, columns: @[int('x')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: 8, l: 1, columns: @[int('u')]),
|
Line(x: 8, columns: @[int('u')]),
|
||||||
Line(x: 9, l: 1, columns: @[int('e')]),
|
Line(x: 9, columns: @[int('e')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), int('z'), 32, 32, int('r')]),
|
Line(x: -2, columns: @[int('l'), 32, int('@'), int('z'), 32, 32, int('r')]),
|
||||||
Line(x: -3, l: 1, columns: @[int('f')]),
|
Line(x: -3, columns: @[int('f')]),
|
||||||
Line(x: 5, l: 1, columns: @[int('d')]),
|
Line(x: 5, columns: @[int('d')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: -7, l: 1, columns: @[int('y')]),
|
Line(x: -7, columns: @[int('y')]),
|
||||||
])
|
])
|
||||||
let 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: -5, columns: @[int('x')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: 8, l: 1, columns: @[int('u')]),
|
Line(x: 8, columns: @[int('u')]),
|
||||||
Line(x: 9, l: 1, columns: @[int('e')]),
|
Line(x: 9, columns: @[int('e')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
||||||
Line(x: -3, l: 1, columns: @[int('f')]),
|
Line(x: -3, columns: @[int('f')]),
|
||||||
Line(x: 5, l: 1, columns: @[int('d')]),
|
Line(x: 5, columns: @[int('d')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: -7, l: 1, columns: @[int('y')]),
|
Line(x: -7, columns: @[int('y')]),
|
||||||
])
|
])
|
||||||
f.Blank(1, 0)
|
f.Blank(1, 0)
|
||||||
check f == moinsz
|
check f == moinsz
|
||||||
let 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: -5, columns: @[int('x')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: 8, l: 1, columns: @[int('u')]),
|
Line(x: 8, columns: @[int('u')]),
|
||||||
Line(x: 9, l: 1, columns: @[int('e')]),
|
Line(x: 9, columns: @[int('e')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
||||||
Line(x: -3, l: 1, columns: @[int('f')]),
|
Line(x: -3, columns: @[int('f')]),
|
||||||
Line(x: 5, l: 1, columns: @[int('d')]),
|
Line(x: 5, columns: @[int('d')]),
|
||||||
])
|
])
|
||||||
f.Blank(-7, 4)
|
f.Blank(-7, 4)
|
||||||
check f == moinsy
|
check f == moinsy
|
||||||
let 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: 8, columns: @[int('u')]),
|
||||||
Line(x: 9, l: 1, columns: @[int('e')]),
|
Line(x: 9, columns: @[int('e')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
||||||
Line(x: -3, l: 1, columns: @[int('f')]),
|
Line(x: -3, columns: @[int('f')]),
|
||||||
Line(x: 5, l: 1, columns: @[int('d')]),
|
Line(x: 5, columns: @[int('d')]),
|
||||||
])
|
])
|
||||||
f.Blank(-5, -5)
|
f.Blank(-5, -5)
|
||||||
check f == moinsx
|
check f == moinsx
|
||||||
let 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: 8, columns: @[int('u')]),
|
||||||
Line(x: 9, l: 1, columns: @[int('e')]),
|
Line(x: 9, columns: @[int('e')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
||||||
Line(x: -3, l: 0, columns: @[]),
|
Line(x: -3, columns: @[]),
|
||||||
Line(x: 5, l: 1, columns: @[int('d')]),
|
Line(x: 5, columns: @[int('d')]),
|
||||||
])
|
])
|
||||||
f.Blank(-3, 1)
|
f.Blank(-3, 1)
|
||||||
check f == moinsf
|
check f == moinsf
|
||||||
let 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: 8, columns: @[int('u')]),
|
||||||
Line(x: 9, l: 0, columns: @[]),
|
Line(x: 9, columns: @[]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
||||||
Line(x: -3, l: 0, columns: @[]),
|
Line(x: -3, columns: @[]),
|
||||||
Line(x: 5, l: 1, columns: @[int('d')]),
|
Line(x: 5, columns: @[int('d')]),
|
||||||
])
|
])
|
||||||
f.Blank(9, -2)
|
f.Blank(9, -2)
|
||||||
check f == moinse
|
check f == moinse
|
||||||
let 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: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
||||||
Line(x: -3, l: 0, columns: @[]),
|
Line(x: -3, columns: @[]),
|
||||||
Line(x: 5, l: 1, columns: @[int('d')]),
|
Line(x: 5, columns: @[int('d')]),
|
||||||
])
|
])
|
||||||
f.Blank(8, -3)
|
f.Blank(8, -3)
|
||||||
check f == moinsu
|
check f == moinsu
|
||||||
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')])])
|
let moinsd = Field(x: -2, y: 0, lx: 7, ly: 1, lines: @[Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')])])
|
||||||
f.Blank(5, 2)
|
f.Blank(5, 2)
|
||||||
check f == moinsd
|
check f == moinsd
|
||||||
let 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, columns: @[int('@'), 32, 32, 32, int('r')])])
|
||||||
f.Blank(-2, 0)
|
f.Blank(-2, 0)
|
||||||
check f == moinsl
|
check f == moinsl
|
||||||
let 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, columns: @[int('@')])])
|
||||||
f.Blank(4, 0)
|
f.Blank(4, 0)
|
||||||
check f == moinsr
|
check f == moinsr
|
||||||
test "Get":
|
test "Get":
|
||||||
|
@ -117,101 +117,101 @@ suite "Field":
|
||||||
check Load("nonexistant") == nil
|
check Load("nonexistant") == nil
|
||||||
check Load("examples/invalid.b98") == nil
|
check Load("examples/invalid.b98") == nil
|
||||||
check Load("examples/empty.b98") == nil
|
check Load("examples/empty.b98") == nil
|
||||||
check Load("examples/minimal.b98")[] == Field(lx: 1, ly: 1, lines: @[Line(l: 1, columns: @['@'].cols)])
|
check Load("examples/minimal.b98")[] == Field(lx: 1, ly: 1, lines: @[Line(columns: @['@'].cols)])
|
||||||
let hello = Field(lx: 24, ly: 1, lines: @[Line(l: 24, columns: @['6', '4', '+', '"', '!', 'd', 'l', 'r', 'o', 'W', ' ', ',', 'o', 'l', 'l', 'e', 'H', '"', '>', ':', '#', ',', '_', '@'].cols)])
|
let hello = Field(lx: 24, ly: 1, lines: @[Line(columns: @['6', '4', '+', '"', '!', 'd', 'l', 'r', 'o', 'W', ' ', ',', 'o', 'l', 'l', 'e', 'H', '"', '>', ':', '#', ',', '_', '@'].cols)])
|
||||||
check Load("examples/hello.b98")[] == hello
|
check Load("examples/hello.b98")[] == hello
|
||||||
check Load("examples/rn.b98")[] == hello
|
check Load("examples/rn.b98")[] == hello
|
||||||
check Load("examples/hello2.b98")[] == Field(x: 1, lx: 33, ly: 2, lines: @[
|
check Load("examples/hello2.b98")[] == Field(x: 1, lx: 33, ly: 2, lines: @[
|
||||||
Line(x: 33, l: 1, columns: @['v'].cols),
|
Line(x: 33, columns: @['v'].cols),
|
||||||
Line(x: 1, l: 33, columns: @['@', ' ', '>', ' ', '#', ';', '>', ':', '#', ',', '_', 'e', '-', 'j', ';', ' ', '"', 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
|
Line(x: 1, columns: @['@', ' ', '>', ' ', '#', ';', '>', ':', '#', ',', '_', 'e', '-', 'j', ';', ' ', '"', 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
|
||||||
'"', 'd', 'a', '<'].cols)
|
'"', 'd', 'a', '<'].cols)
|
||||||
])
|
])
|
||||||
check Load("examples/factorial.b98")[] == Field(x: 0, lx: 15, ly: 2, lines: @[
|
check Load("examples/factorial.b98")[] == Field(x: 0, lx: 15, ly: 2, lines: @[
|
||||||
Line(x: 0, l: 15, columns: @['&', '>', ':', '1', '-', ':', 'v', ' ', 'v', ' ', '*', '_', '$', '.', '@'].cols),
|
Line(x: 0, columns: @['&', '>', ':', '1', '-', ':', 'v', ' ', 'v', ' ', '*', '_', '$', '.', '@'].cols),
|
||||||
Line(x: 1, l: 11, columns: @['^', ' ', ' ', ' ', ' ', '_', '$', '>', '\\', ':', '^'].cols)
|
Line(x: 1, columns: @['^', ' ', ' ', ' ', ' ', '_', '$', '>', '\\', ':', '^'].cols)
|
||||||
])
|
])
|
||||||
check Load("examples/dna.b98")[] == Field(x: 0, lx: 7, ly: 8, lines: @[
|
check Load("examples/dna.b98")[] == Field(x: 0, lx: 7, ly: 8, lines: @[
|
||||||
Line(x: 0, l: 7, columns: @['7', '^', 'D', 'N', '>', 'v', 'A'].cols),
|
Line(x: 0, columns: @['7', '^', 'D', 'N', '>', 'v', 'A'].cols),
|
||||||
Line(x: 0, l: 7, columns: @['v', '_', '#', 'v', '?', ' ', 'v'].cols),
|
Line(x: 0, columns: @['v', '_', '#', 'v', '?', ' ', 'v'].cols),
|
||||||
Line(x: 0, l: 7, columns: @['7', '^', '<', '"', '"', '"', '"'].cols),
|
Line(x: 0, columns: @['7', '^', '<', '"', '"', '"', '"'].cols),
|
||||||
Line(x: 0, l: 7, columns: @['3', ' ', ' ', 'A', 'C', 'G', 'T'].cols),
|
Line(x: 0, columns: @['3', ' ', ' ', 'A', 'C', 'G', 'T'].cols),
|
||||||
Line(x: 0, l: 7, columns: @['9', '0', '!', '"', '"', '"', '"'].cols),
|
Line(x: 0, columns: @['9', '0', '!', '"', '"', '"', '"'].cols),
|
||||||
Line(x: 0, l: 7, columns: @['4', '*', ':', '>', '>', '>', 'v'].cols),
|
Line(x: 0, columns: @['4', '*', ':', '>', '>', '>', 'v'].cols),
|
||||||
Line(x: 0, l: 7, columns: @['+', '8', '^', '-', '1', ',', '<'].cols),
|
Line(x: 0, columns: @['+', '8', '^', '-', '1', ',', '<'].cols),
|
||||||
Line(x: 0, l: 7, columns: @['>', ' ', ',', '+', ',', '@', ')'].cols),
|
Line(x: 0, columns: @['>', ' ', ',', '+', ',', '@', ')'].cols),
|
||||||
])
|
])
|
||||||
test "Set":
|
test "Set":
|
||||||
var f = Field(x: 0, y: 0, lx: 1, ly: 1, lines: @[Line(x: 0, l: 1, columns: @['>'].cols)])
|
var f = Field(x: 0, y: 0, lx: 1, ly: 1, lines: @[Line(x: 0, columns: @['>'].cols)])
|
||||||
f.Set(0, 0, int('@'))
|
f.Set(0, 0, int('@'))
|
||||||
check f == minimal
|
check f == minimal
|
||||||
f.Set(1, 0, int(' '))
|
f.Set(1, 0, int(' '))
|
||||||
check f == minimal
|
check f == minimal
|
||||||
let 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, columns: @[int('@'), 32, 32, 32, int('r')])])
|
||||||
f.Set(4, 0, int('r'))
|
f.Set(4, 0, int('r'))
|
||||||
check f == xappend
|
check f == xappend
|
||||||
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')])])
|
let xprepend = Field(x: -2, y: 0, lx: 7, ly: 1, lines: @[Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')])])
|
||||||
f.Set(-2, 0, int('l'))
|
f.Set(-2, 0, int('l'))
|
||||||
check f == xprepend
|
check f == xprepend
|
||||||
let 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: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: 5, l: 1, columns: @[int('d')]),
|
Line(x: 5, columns: @[int('d')]),
|
||||||
])
|
])
|
||||||
f.Set(5, 2, int('d'))
|
f.Set(5, 2, int('d'))
|
||||||
check f == yappend
|
check f == yappend
|
||||||
let 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: 8, columns: @[int('u')]),
|
||||||
nil,
|
nil,
|
||||||
nil,
|
nil,
|
||||||
Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: 5, l: 1, columns: @[int('d')]),
|
Line(x: 5, columns: @[int('d')]),
|
||||||
])
|
])
|
||||||
f.Set(8, -3, int('u'))
|
f.Set(8, -3, int('u'))
|
||||||
check f == yprepend
|
check f == yprepend
|
||||||
let 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: 8, columns: @[int('u')]),
|
||||||
Line(x: 9, l: 1, columns: @[int('e')]),
|
Line(x: 9, columns: @[int('e')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: 5, l: 1, columns: @[int('d')]),
|
Line(x: 5, columns: @[int('d')]),
|
||||||
])
|
])
|
||||||
f.Set(9, -2, int('e'))
|
f.Set(9, -2, int('e'))
|
||||||
check f == xappendEmptyline
|
check f == xappendEmptyline
|
||||||
let 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: 8, columns: @[int('u')]),
|
||||||
Line(x: 9, l: 1, columns: @[int('e')]),
|
Line(x: 9, columns: @[int('e')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
||||||
Line(x: -3, l: 1, columns: @[int('f')]),
|
Line(x: -3, columns: @[int('f')]),
|
||||||
Line(x: 5, l: 1, columns: @[int('d')]),
|
Line(x: 5, columns: @[int('d')]),
|
||||||
])
|
])
|
||||||
f.Set(-3, 1, int('f'))
|
f.Set(-3, 1, int('f'))
|
||||||
check f == xprependEmptyline
|
check f == xprependEmptyline
|
||||||
let 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: -5, columns: @[int('x')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: 8, l: 1, columns: @[int('u')]),
|
Line(x: 8, columns: @[int('u')]),
|
||||||
Line(x: 9, l: 1, columns: @[int('e')]),
|
Line(x: 9, columns: @[int('e')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
||||||
Line(x: -3, l: 1, columns: @[int('f')]),
|
Line(x: -3, columns: @[int('f')]),
|
||||||
Line(x: 5, l: 1, columns: @[int('d')]),
|
Line(x: 5, columns: @[int('d')]),
|
||||||
])
|
])
|
||||||
f.Set(-5, -5, int('x'))
|
f.Set(-5, -5, int('x'))
|
||||||
check f == xprependyprepend
|
check f == xprependyprepend
|
||||||
let 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: -5, columns: @[int('x')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: 8, l: 1, columns: @[int('u')]),
|
Line(x: 8, columns: @[int('u')]),
|
||||||
Line(x: 9, l: 1, columns: @[int('e')]),
|
Line(x: 9, columns: @[int('e')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: -2, l: 7, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
|
||||||
Line(x: -3, l: 1, columns: @[int('f')]),
|
Line(x: -3, columns: @[int('f')]),
|
||||||
Line(x: 5, l: 1, columns: @[int('d')]),
|
Line(x: 5, columns: @[int('d')]),
|
||||||
nil,
|
nil,
|
||||||
Line(x: -7, l: 1, columns: @[int('y')]),
|
Line(x: -7, columns: @[int('y')]),
|
||||||
])
|
])
|
||||||
f.Set(-7, 4, int('y'))
|
f.Set(-7, 4, int('y'))
|
||||||
check f == xprependyappend
|
check f == xprependyappend
|
||||||
|
|
Reference in a new issue