trecursive.nim 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. discard """
  2. output: '''
  3. test1 OK
  4. '''
  5. """
  6. import smart_ptr
  7. type
  8. Node[T] = object
  9. value: T
  10. next: SharedPtr[Node[T]]
  11. ForwardList[T] = object
  12. first: SharedPtr[Node[T]]
  13. len: Natural
  14. proc pushFront*[T] (list: var ForwardList[T], val: sink T) =
  15. var newNode = newSharedPtr(Node[T](value: val))
  16. var result = false
  17. while not result:
  18. var head = list.first
  19. newNode.get.next = head
  20. result = list.first.cas(head, newNode)
  21. list.len.atomicInc()
  22. proc test1() =
  23. var list: ForwardList[int]
  24. list.pushFront(1)
  25. doAssert list.len == 1
  26. echo "test1 OK"
  27. test1()
  28. #------------------------------------------------------------------------------
  29. # issue #14217
  30. type
  31. MyObject = object
  32. p: ptr int
  33. proc `=destroy`(x: var MyObject) =
  34. if x.p != nil:
  35. deallocShared(x.p)
  36. proc `=`(x: var MyObject, y: MyObject) {.error.}
  37. proc newMyObject(i: int): MyObject =
  38. result.p = create(int)
  39. result.p[] = i
  40. proc test: seq[MyObject] =
  41. for i in 0..3:
  42. let x = newMyObject(i)
  43. result.add x
  44. var x = test()
  45. for i in 0..3:
  46. doAssert(x[i].p[] == i)