Skip to content

Commit

Permalink
fix #16384 and #17144 (#17126) [backport]
Browse files Browse the repository at this point in the history
(cherry picked from commit 6391f6e)
  • Loading branch information
ringabout authored and narimiran committed Mar 18, 2021
1 parent 5c88067 commit e996d98
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 60 deletions.
3 changes: 2 additions & 1 deletion compiler/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,8 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
let nc = regs[rc].node
if nb.kind != nc.kind: discard
elif (nb == nc) or (nb.kind == nkNilLit): ret = true # intentional
elif sameConstant(nb, nc): ret = true
elif (nb.kind in {nkSym, nkTupleConstr, nkClosure} and nb.typ.kind == tyProc) and sameConstant(nb, nc):
ret = true
# this also takes care of procvar's, represented as nkTupleConstr, e.g. (nil, nil)
elif nb.kind == nkIntLit and nc.kind == nkIntLit and nb.intVal == nc.intVal: # TODO: nkPtrLit
let tb = nb.getTyp
Expand Down
139 changes: 80 additions & 59 deletions tests/stdlib/tlists.nim
Original file line number Diff line number Diff line change
@@ -1,67 +1,88 @@
discard """
output: '''true'''
targets: "c js"
"""

import lists
import std/[lists, sequtils]

const
data = [1, 2, 3, 4, 5, 6]

block SinglyLinkedListTest1:
var L: SinglyLinkedList[int]
for d in items(data): L.prepend(d)
for d in items(data): L.append(d)
assert($L == "[6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6]")

assert(4 in L)

block SinglyLinkedListTest2:
var L: SinglyLinkedList[string]
for d in items(data): L.prepend($d)
assert($L == """["6", "5", "4", "3", "2", "1"]""")

assert("4" in L)


block DoublyLinkedListTest1:
var L: DoublyLinkedList[int]
for d in items(data): L.prepend(d)
for d in items(data): L.append(d)
L.remove(L.find(1))
assert($L == "[6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6]")

assert(4 in L)

block SinglyLinkedRingTest1:
var L: SinglyLinkedRing[int]
L.prepend(4)
assert($L == "[4]")
L.prepend(4)

assert($L == "[4, 4]")
assert(4 in L)


block DoublyLinkedRingTest1:
var L: DoublyLinkedRing[int]
L.prepend(4)
assert($L == "[4]")
L.prepend(4)

assert($L == "[4, 4]")
assert(4 in L)

L.append(3)
L.append(5)
assert($L == "[4, 4, 3, 5]")

L.remove(L.find(3))
L.remove(L.find(5))
L.remove(L.find(4))
L.remove(L.find(4))
assert($L == "[]")
assert(4 notin L)


echo "true"

template main =
block SinglyLinkedListTest1:
var L: SinglyLinkedList[int]
for d in items(data): L.prepend(d)
for d in items(data): L.append(d)
doAssert($L == "[6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6]")

doAssert(4 in L)

block SinglyLinkedListTest2:
var L: SinglyLinkedList[string]
for d in items(data): L.prepend($d)
doAssert($L == """["6", "5", "4", "3", "2", "1"]""")

doAssert("4" in L)


block DoublyLinkedListTest1:
var L: DoublyLinkedList[int]
for d in items(data): L.prepend(d)
for d in items(data): L.append(d)
L.remove(L.find(1))
doAssert($L == "[6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6]")

doAssert(4 in L)

block SinglyLinkedRingTest1:
var L: SinglyLinkedRing[int]
L.prepend(4)
doAssert($L == "[4]")
L.prepend(4)

doAssert($L == "[4, 4]")
doAssert(4 in L)


block DoublyLinkedRingTest1:
var L: DoublyLinkedRing[int]
L.prepend(4)
doAssert($L == "[4]")
L.prepend(4)

doAssert($L == "[4, 4]")
doAssert(4 in L)

L.append(3)
L.append(5)
doAssert($L == "[4, 4, 3, 5]")

L.remove(L.find(3))
L.remove(L.find(5))
L.remove(L.find(4))
L.remove(L.find(4))
doAssert($L == "[]")
doAssert(4 notin L)

block tlistsToString:
block:
var l = initDoublyLinkedList[int]()
l.append(1)
l.append(2)
l.append(3)
doAssert $l == "[1, 2, 3]"
block:
var l = initDoublyLinkedList[string]()
l.append("1")
l.append("2")
l.append("3")
doAssert $l == """["1", "2", "3"]"""
block:
var l = initDoublyLinkedList[char]()
l.append('1')
l.append('2')
l.append('3')
doAssert $l == """['1', '2', '3']"""

static: main()
main()

0 comments on commit e996d98

Please sign in to comment.