tconstructionorder.nim 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. discard """
  2. output: "SUCCESS"
  3. """
  4. # A test to ensure that the order in which a variant
  5. # object is constructed doesn't matter.
  6. type
  7. NodeKind = enum
  8. Literal, Operator
  9. Node = ref object
  10. case kind: NodeKind
  11. of Literal:
  12. value: int
  13. of Operator:
  14. left, right: Node
  15. operator: char
  16. # The trees used through out this test should
  17. # be the same after construction, the only difference
  18. # being the way we specify their construction.
  19. # This will test that all the values are what we expect.
  20. proc assertTree(root: Node) =
  21. # check root of tree
  22. assert root.kind == Operator
  23. assert root.operator == '*'
  24. # check left subtree
  25. assert root.left.value == 5
  26. assert root.left.kind == Literal
  27. # check right subtree
  28. assert root.right.kind == Operator
  29. assert root.right.operator == '+'
  30. assert root.right.left.value == 5
  31. assert root.right.left.kind == Literal
  32. assert root.right.right.value == 10
  33. assert root.right.right.kind == Literal
  34. proc newLiteralNode(value: int): Node =
  35. result = Node(
  36. kind: Literal,
  37. value: value
  38. )
  39. var rootOrder1 = Node(
  40. kind: Operator,
  41. operator: '*',
  42. left: newLiteralNode(5),
  43. right: Node(
  44. left: newLiteralNode(5),
  45. right: newLiteralNode(10),
  46. kind: Operator,
  47. operator: '+'
  48. )
  49. )
  50. assertTree(rootOrder1)
  51. var rootOrder2 = Node(
  52. operator: '*',
  53. kind: Operator,
  54. left: newLiteralNode(5),
  55. right: Node(
  56. left: newLiteralNode(5),
  57. right: newLiteralNode(10),
  58. kind: Operator,
  59. operator: '+'
  60. )
  61. )
  62. assertTree(rootOrder2)
  63. var rootOrder3 = Node(
  64. left: newLiteralNode(5),
  65. operator: '*',
  66. kind: Operator,
  67. right: Node(
  68. left: newLiteralNode(5),
  69. right: newLiteralNode(10),
  70. kind: Operator,
  71. operator: '+'
  72. )
  73. )
  74. assertTree(rootOrder3)
  75. var rootOrder4 = Node(
  76. left: newLiteralNode(5),
  77. operator: '*',
  78. kind: Operator,
  79. right: Node(
  80. left: newLiteralNode(5),
  81. kind: Operator,
  82. operator: '+',
  83. right: newLiteralNode(10)
  84. )
  85. )
  86. assertTree(rootOrder4)
  87. var rootOrder5 = Node(
  88. left: newLiteralNode(5),
  89. operator: '*',
  90. kind: Operator,
  91. right: Node(
  92. left: newLiteralNode(5),
  93. operator: '+',
  94. right: newLiteralNode(10),
  95. kind: Operator
  96. )
  97. )
  98. assertTree(rootOrder5)
  99. echo "SUCCESS"