tnew.nim 1.2 KB

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