tnew.nim 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. discard """
  2. outputsub: '''
  3. Simple tree node allocation worked!
  4. Simple cycle allocation worked!
  5. '''
  6. joinable: false
  7. """
  8. # Test the implementation of the new operator
  9. # and the code generation for gc walkers
  10. # (and the garbage collector):
  11. type
  12. PNode = ref TNode
  13. TNode = object
  14. data: int
  15. str: string
  16. le, ri: PNode
  17. TStressTest = ref array[0..45, array[1..45, TNode]]
  18. proc finalizer(n: PNode) =
  19. write(stdout, n.data)
  20. write(stdout, " is now freed\n")
  21. proc newNode(data: int, le, ri: PNode): PNode =
  22. new(result, finalizer)
  23. result.le = le
  24. result.ri = ri
  25. result.data = data
  26. # now loop and build a tree
  27. proc main() =
  28. var
  29. i = 0
  30. p: TStressTest
  31. while i < 1000:
  32. var n: PNode
  33. n = newNode(i, nil, newNode(i + 10000, nil, nil))
  34. inc(i)
  35. new(p)
  36. write(stdout, "Simple tree node allocation worked!\n")
  37. i = 0
  38. while i < 1000:
  39. var m = newNode(i + 20000, nil, nil)
  40. var k = newNode(i + 30000, nil, nil)
  41. m.le = m
  42. m.ri = k
  43. k.le = m
  44. k.ri = k
  45. inc(i)
  46. write(stdout, "Simple cycle allocation worked!\n")
  47. main()