Files
Nim/tests/system/tdeepcopy.nim
ringabout 7739e23420 defaults to ORC (#19972)
* defaults to Orc

* bootstrap using refc

* use gc

* init orc defines

* unregister orc

* fix gc

* fix commands

* add prepareMutation for orc

* enable deepcopy for orc

* prepareMutation

* more fixes

* some cases

* bug #20081

* partial fixes

* partial fixes

* fixes command line

* more fixes

* build Nim with refc

* use gc

* more fixes

* rstore

* orc doesn't support threadpool

* more shallowCopy

* more fixes

* fixes unsafeNew

* workarounds

* small

* more fixes

* fixes some megatest

* tcodegenbugs1 refc

* fxies megatest

* build nimble with refc

* workaround tensordsl tests

* replace shallowCopy with move

* fixes action

* workaround

* add todo

* fixes important packages

* unpublic unregisterArcOrc

* fixes cpp

* enable windows

Co-authored-by: xflywind <43030857+xflywind@users.noreply.github.com>
2022-09-23 13:05:05 +02:00

96 lines
2.2 KiB
Nim

discard """
matrix: "--mm:refc; --mm:orc --deepcopy:on"
output: "ok"
"""
import tables, lists
type
ListTable[K, V] = object
valList: DoublyLinkedList[V]
table: Table[K, DoublyLinkedNode[V]]
ListTableRef*[K, V] = ref ListTable[K, V]
proc initListTable*[K, V](initialSize = 64): ListTable[K, V] =
result.valList = initDoublyLinkedList[V]()
result.table = initTable[K, DoublyLinkedNode[V]]()
proc newListTable*[K, V](initialSize = 64): ListTableRef[K, V] =
new(result)
result[] = initListTable[K, V](initialSize)
proc `[]=`*[K, V](t: var ListTable[K, V], key: K, val: V) =
if key in t.table:
t.table[key].value = val
else:
let node = newDoublyLinkedNode(val)
t.valList.append(node)
t.table[key] = node
proc `[]`*[K, V](t: ListTable[K, V], key: K): var V {.inline.} =
result = t.table[key].value
proc len*[K, V](t: ListTable[K, V]): Natural {.inline.} =
result = t.table.len
iterator values*[K, V](t: ListTable[K, V]): V =
for val in t.valList.items():
yield val
proc `[]=`*[K, V](t: ListTableRef[K, V], key: K, val: V) =
t[][key] = val
proc `[]`*[K, V](t: ListTableRef[K, V], key: K): var V {.inline.} =
t[][key]
proc len*[K, V](t: ListTableRef[K, V]): Natural {.inline.} =
t[].len
iterator values*[K, V](t: ListTableRef[K, V]): V =
for val in t[].values:
yield val
proc main() =
type SomeObj = ref object
for outer in 0..10_000:
let myObj = new(SomeObj)
let table = newListTable[int, SomeObj]()
table[0] = myObj
for i in 1..100:
table[i] = new(SomeObj)
var myObj2: SomeObj
for val in table.values():
if myObj2.isNil:
myObj2 = val
doAssert(myObj == myObj2) # passes
var tableCopy: ListTableRef[int, SomeObj]
deepCopy(tableCopy, table)
let myObjCopy = tableCopy[0]
var myObjCopy2: SomeObj = nil
for val in tableCopy.values():
if myObjCopy2.isNil:
myObjCopy2 = val
#echo cast[int](myObj)
#echo cast[int](myObjCopy)
#echo cast[int](myObjCopy2)
doAssert(myObjCopy == myObjCopy2) # fails
type
PtrTable = object
counter, max: int
data: array[0..99, (pointer, pointer)]
doAssert(sizeof(PtrTable) == 2*sizeof(int)+sizeof(pointer)*2*100)
main()
echo "ok"