tdeepcopy.nim 981 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. discard """
  2. matrix: "--mm:refc"
  3. output: '''
  4. 13 abc
  5. called deepCopy for int
  6. called deepCopy for int
  7. done999 999
  8. '''
  9. """
  10. import threadpool
  11. block one:
  12. type
  13. PBinaryTree = ref object
  14. le, ri: PBinaryTree
  15. value: int
  16. proc main =
  17. var x: PBinaryTree
  18. deepCopy(x, PBinaryTree(ri: PBinaryTree(le: PBinaryTree(value: 13))))
  19. var y: string
  20. deepCopy y, "abc"
  21. echo x.ri.le.value, " ", y
  22. main()
  23. block two:
  24. type
  25. Bar[T] = object
  26. x: T
  27. proc `=deepCopy`[T](b: ref Bar[T]): ref Bar[T] =
  28. result.new
  29. result.x = b.x
  30. when T is int:
  31. echo "called deepCopy for int"
  32. else:
  33. echo "called deepCopy for something else"
  34. proc foo(b: ref Bar[int]): int = 999
  35. # test that the disjoint checker deals with 'a = spawn f(); g = spawn f()':
  36. proc main =
  37. var dummy: ref Bar[int]
  38. new(dummy)
  39. dummy.x = 44
  40. #parallel:
  41. let f = spawn foo(dummy)
  42. let b = spawn foo(dummy)
  43. echo "done", ^f, " ", ^b
  44. main()