Пример #1
0
 def test_print_high_precedence(self):
     root = EvalTreeNode(OPERATORS['m'], None,
                         EvalTreeNode(OPERATORS['!'], EvalTreeNode(5)))
     tree = EvalTree(None)
     tree.root = root
     # it evaluates ! before display because of its high precedence
     self.assertEqual(tree.verbose_result(), '-120 = -120')
Пример #2
0
 def test_print_parentheses(self):
     root = EvalTreeNode(
         OPERATORS['*'], EvalTreeNode(2),
         EvalTreeNode(OPERATORS['+'], EvalTreeNode(4), EvalTreeNode(8)))
     tree = EvalTree(None)
     tree.root = root
     self.assertEqual(tree.verbose_result(), '2*(4+8) = 24')
Пример #3
0
 def test_tree_simple_parse(self):
     expr = '4d6'
     tree = EvalTree(expr)
     expected = EvalTree(None)
     expected.root = EvalTreeNode(OPERATORS['d'], EvalTreeNode(4),
                                  EvalTreeNode(6))
     self.assertEqual(tree, expected)
Пример #4
0
 def test_compile_int(self):
     expected = EvalTree(None)
     expected.root = EvalTreeNode(2)
     self.assertEqual(compile(2), expected)
     expected.root = EvalTreeNode(OPERATORS['+'], EvalTreeNode(2),
                                  EvalTreeNode(4))
     self.assertEqual(compile(2, 4), expected)
Пример #5
0
 def test_compile_modifier(self):
     expected = EvalTree(None)
     expected.root = EvalTreeNode(
         OPERATORS['+'],
         EvalTreeNode(OPERATORS['d'], EvalTreeNode(3), EvalTreeNode(4)),
         EvalTreeNode(2))
     self.assertEqual(compile('3d4', 2), expected)
Пример #6
0
 def test_node(self):
     node = EvalTreeNode(4)
     self.assertEqual(node.payload, 4)
     self.assertIs(node.left, None)
     self.assertIs(node.right, None)
     self.assertIs(node.value, None)
     node.evaluate()
     self.assertEqual(node.payload, node.value)
Пример #7
0
 def test_tree_parentheses(self):
     expr = '3+(8-5)'
     tree = EvalTree(expr)
     expected = EvalTree(None)
     expected.root = EvalTreeNode(
         OPERATORS['+'], EvalTreeNode(3),
         EvalTreeNode(OPERATORS['-'], EvalTreeNode(8), EvalTreeNode(5)))
     self.assertEqual(tree, expected)
Пример #8
0
 def test_print_roll(self):
     root = EvalTreeNode(
         OPERATORS['+'], EvalTreeNode(1),
         EvalTreeNode(OPERATORS['d'], EvalTreeNode(4), EvalTreeNode(20)))
     tree = EvalTree(None)
     tree.root = root
     self.assertEqual(tree.verbose_result(),
                      '1+' + str(Roll([1, 20, 1, 20], 20)) + ' = 43')
Пример #9
0
 def test_averageify(self):
     root = EvalTreeNode(
         OPERATORS['d'], EvalTreeNode(1),
         EvalTreeNode(OPERATORS['d'], EvalTreeNode(4), EvalTreeNode(20)))
     tree = EvalTree(None)
     tree.root = root
     tree.averageify()
     self.assertEqual(tree.root.payload, OPERATORS['da'])
     self.assertEqual(tree.root.right.payload, OPERATORS['da'])
Пример #10
0
 def test_tree_basic_precedence(self):
     expr = '2d6 + 5*2^2'
     tree = EvalTree(expr)
     expected = EvalTree(None)
     expected.root = EvalTreeNode(
         OPERATORS['+'],
         EvalTreeNode(OPERATORS['d'], EvalTreeNode(2), EvalTreeNode(6)),
         EvalTreeNode(
             OPERATORS['*'], EvalTreeNode(5),
             EvalTreeNode(OPERATORS['^'], EvalTreeNode(2),
                          EvalTreeNode(2))))
     self.assertEqual(tree, expected)
Пример #11
0
 def test_tree_associativity(self):
     expr = '3+4-5'
     tree = EvalTree(expr)
     expected = EvalTree(None)
     expected.root = EvalTreeNode(
         OPERATORS['-'],
         EvalTreeNode(OPERATORS['+'], EvalTreeNode(3), EvalTreeNode(4)),
         EvalTreeNode(5))
     self.assertEqual(tree, expected)
     expr = '3 ^ 2 ^ 4'
     tree = EvalTree(expr)
     expected = EvalTree(None)
     expected.root = EvalTreeNode(
         OPERATORS['^'], EvalTreeNode(3),
         EvalTreeNode(OPERATORS['^'], EvalTreeNode(2), EvalTreeNode(4)))
     self.assertEqual(tree, expected)
Пример #12
0
def trees_equal(a: EvalTree, b: EvalTree) -> bool:
    for nodeA, nodeB in itertools.zip_longest(a.pre_order(),
                                              b.pre_order(),
                                              fillvalue=EvalTreeNode(None)):
        if nodeA.payload != nodeB.payload:
            return False
    return True
Пример #13
0
 def test_copy(self):
     root = EvalTreeNode(
         OPERATORS['d'], EvalTreeNode(1),
         EvalTreeNode(OPERATORS['d'], EvalTreeNode(4), EvalTreeNode(20)))
     tree = EvalTree(None)
     tree.root = root
     tree.evaluate()
     copy = tree.copy()
     self.assertEqual(tree, copy)
     self.assertIsNot(tree, copy)
     for origNode, copyNode in zip(tree.pre_order(), copy.pre_order()):
         self.assertIsNot(origNode, copyNode)
         # Avoid false positives from int interning
         if not isinstance(origNode.value, int):
             self.assertIsNot(origNode.value, copyNode.value)
         if not isinstance(origNode.payload, int):
             self.assertIsNot(origNode.payload, copyNode.payload)
Пример #14
0
 def test_tree_subtraction(self):
     expr1 = '2d20'
     tree1 = EvalTree(expr1)
     expr2 = '5+3'
     tree2 = EvalTree(expr2)
     tree = tree1 - tree2
     expected = EvalTree(None)
     expected.root = EvalTreeNode(
         OPERATORS['-'],
         EvalTreeNode(OPERATORS['d'], EvalTreeNode(2), EvalTreeNode(20)),
         EvalTreeNode(OPERATORS['+'], EvalTreeNode(5), EvalTreeNode(3)))
     self.assertEqual(tree, expected)
     tree1copy = EvalTree(None)
     tree1copy.root = EvalTreeNode(OPERATORS['d'], EvalTreeNode(2),
                                   EvalTreeNode(20))
     # Ensure that the original is untouched
     tree.evaluate()
     self.assertEqual(tree1, tree1copy)
     self.assertTrue(tree_empty(tree1))
Пример #15
0
 def test_tree_in_place_subtraction(self):
     expr1 = '2d20'
     tree1 = EvalTree(expr1)
     expr2 = '5+3'
     tree2 = EvalTree(expr2)
     # Also test in-place concatenation
     tree1 -= tree2
     expected = EvalTree(None)
     expected.root = EvalTreeNode(
         OPERATORS['-'],
         EvalTreeNode(OPERATORS['d'], EvalTreeNode(2), EvalTreeNode(20)),
         EvalTreeNode(OPERATORS['+'], EvalTreeNode(5), EvalTreeNode(3)))
     self.assertEqual(tree1, expected)
Пример #16
0
 def test_tree_unnecessary_parentheses(self):
     expr = '2*((8)+(4))'
     tree = EvalTree(expr)
     expected = EvalTree(None)
     expected.root = EvalTreeNode(
         OPERATORS['*'], EvalTreeNode(2),
         EvalTreeNode(OPERATORS['+'], EvalTreeNode(8), EvalTreeNode(4)))
     self.assertEqual(tree, expected)
     expr = '((3))'
     tree = EvalTree(expr)
     expected = EvalTree(None)
     expected.root = EvalTreeNode(3)
     self.assertEqual(tree, expected)
Пример #17
0
 def test_compile(self):
     tree = compile('3d4')
     expected = EvalTree(None)
     expected.root = EvalTreeNode(OPERATORS['d'], EvalTreeNode(3),
                                  EvalTreeNode(4))
     self.assertEqual(tree, expected)
Пример #18
0
 def test_unary_prefix(self):
     expr = '+3'
     tree = EvalTree(expr)
     expected = EvalTree(None)
     expected.root = EvalTreeNode(OPERATORS['p'], None, EvalTreeNode(3))
     self.assertEqual(tree, expected)
Пример #19
0
 def test_tree_from_existing(self):
     expected = EvalTree(None)
     expected.root = EvalTreeNode(OPERATORS['d'], EvalTreeNode(4),
                                  EvalTreeNode(6))
     fromExisting = EvalTree(expected)
     self.assertEqual(fromExisting, expected)
Пример #20
0
 def test_tree_from_tokens(self):
     fromTokens = EvalTree([4, OPERATORS['d'], 6])
     expected = EvalTree(None)
     expected.root = EvalTreeNode(OPERATORS['d'], EvalTreeNode(4),
                                  EvalTreeNode(6))
     self.assertEqual(fromTokens, expected)
Пример #21
0
 def test_unary_suffix(self):
     expr = '4!'
     tree = EvalTree(expr)
     expected = EvalTree(None)
     expected.root = EvalTreeNode(OPERATORS['!'], EvalTreeNode(4))
     self.assertEqual(tree, expected)
Пример #22
0
 def test_print_unary(self):
     root = EvalTreeNode(OPERATORS['m'], None, EvalTreeNode(4))
     tree = EvalTree(None)
     tree.root = root
     self.assertEqual(tree.verbose_result(), '-4 = -4')