예제 #1
0
 def testUnaryOperation(self):
     for a in range(-10, 10):
         scope = model.Scope()
         with patch('sys.stdout', new_callable=StringIO) as output:
             operation = model.UnaryOperation("-", model.Number(a))
             self.assertIsInstance(operation, model.UnaryOperation)
             model.Print(operation.evaluate(scope)).evaluate(scope)
             self.assertEqual(int(output.getvalue()), int(-a))
         with patch('sys.stdout', new_callable=StringIO) as output:
             operation = model.UnaryOperation("!", model.Number(a))
             self.assertIsInstance(operation, model.UnaryOperation)
             model.Print(operation.evaluate(scope)).evaluate(scope)
             self.assertEqual(bool(int(output.getvalue())), bool(not a))
예제 #2
0
 def testBinaryOperation(self):
     funcs = {
         "+": lambda x, y: x + y,
         "-": lambda x, y: x - y,
         "*": lambda x, y: x * y,
         "/": lambda x, y: x // y,
         "%": lambda x, y: x % y,
         "==": lambda x, y: x == y,
         "!=": lambda x, y: x != y,
         "<": lambda x, y: x < y,
         ">": lambda x, y: x > y,
         "<=": lambda x, y: x <= y,
         ">=": lambda x, y: x >= y,
         "&&": lambda x, y: x and y,
         "||": lambda x, y: x or y,
     }
     for a in range(-10, 10):
         for b in range(-10, 10):
             for oper, func in funcs.items():
                 if b or not oper in ("%", "/"):
                     with patch('sys.stdout',
                                new_callable=StringIO) as output:
                         scope = model.Scope()
                         operation = model.BinaryOperation(
                             model.Number(a), oper, model.Number(b))
                         self.assertIsInstance(operation,
                                               model.BinaryOperation)
                         model.Print(
                             operation.evaluate(scope)).evaluate(scope)
                         if oper in ("+", "-", "*", "/", "%"):
                             self.assertEqual(int(output.getvalue()),
                                              int(func(a, b)))
                         else:
                             self.assertEqual(bool(int(output.getvalue())),
                                              bool(func(a, b)))
예제 #3
0
 def testPrint(self):
     patcher = patch("sys.stdout", new_callable=StringIO)
     output = patcher.start()
     scope = model.Scope()
     for num in range(20):
         model.Print(model.Number(num - 10)).evaluate(scope)
     self.assertEqual(output.getvalue(),
                      "\n".join(str(num - 10) for num in range(20)) + "\n")
     patcher.stop()
예제 #4
0
 def testFunction(self):
     scope = model.Scope()
     number = model.Number(3)
     function = model.Function(["arg"], [model.Number(1), model.Number(2)])
     self.assertIsInstance(function, model.Function)
     self.assertIsInstance(function.evaluate(scope), model.Number)
     patcher = patch("sys.stdout", new_callable=StringIO)
     output = patcher.start()
     model.Print(function.evaluate(scope)).evaluate(scope)
     self.assertEqual(output.getvalue(), "2\n")
     patcher.stop()
예제 #5
0
 def testConditional(self):
     scope = model.Scope()
     cond1 = model.Conditional(model.Number(1), [model.Number(1)],
                               [model.Number(0)])
     cond2 = model.Conditional(model.Number(0), [model.Number(1)],
                               [model.Number(0)])
     cond3 = model.Conditional(model.Number(1), [], [])
     cond4 = model.Conditional(model.Number(0), None, None)
     self.assertIsInstance(cond1, model.Conditional)
     self.assertIsInstance(cond2, model.Conditional)
     self.assertIsInstance(cond3, model.Conditional)
     self.assertIsInstance(cond4, model.Conditional)
     with patch('sys.stdout', new_callable=StringIO) as output:
         model.Print(cond1.evaluate(scope)).evaluate(scope)
         self.assertEqual(int(output.getvalue()), 1)
     with patch('sys.stdout', new_callable=StringIO) as output:
         model.Print(cond2.evaluate(scope)).evaluate(scope)
         self.assertEqual(int(output.getvalue()), 0)
     cond3.evaluate(scope)
     cond4.evaluate(scope)
예제 #6
0
 def testRead(self):
     patcher = patch(
         "sys.stdin",
         StringIO("\n".join(str(num - 10) for num in range(20)) + "\n"))
     patcher.start()
     scope = model.Scope()
     for num in range(20):
         model.Read("var" + str(num)).evaluate(scope)
     patcher.stop()
     patcher = patch("sys.stdout", new_callable=StringIO)
     output = patcher.start()
     for num in range(20):
         model.Print(scope["var" + str(num)]).evaluate(scope)
     self.assertEqual(output.getvalue(),
                      "\n".join(str(num - 10) for num in range(20)) + "\n")
     patcher.stop()
예제 #7
0
 def testFunctionCall(self):
     scope = model.Scope()
     func1 = model.Function(
         ["argc", "argv"],
         [model.Number(1), model.Reference("argc")])
     func2 = model.Function([], [])
     def1 = model.FunctionDefinition("f1", func1)
     def2 = model.FunctionDefinition("f2", func2)
     call1 = model.FunctionCall(def1, [model.Number(1), model.Number(2)])
     call2 = model.FunctionCall(def2, [])
     self.assertIsInstance(call1, model.FunctionCall)
     self.assertIsInstance(call2, model.FunctionCall)
     with patch('sys.stdout', new_callable=StringIO) as output:
         model.Print(call1.evaluate(scope)).evaluate(scope)
         self.assertEqual(int(output.getvalue()), 1)
     call2.evaluate(scope)
예제 #8
0
 def test_trash_func():
     scope = model.Scope()
     model.FunctionDefinition(
         'foo',
         model.Function(['x'], [
             model.FunctionDefinition(
                 'bar',
                 model.Function([], [
                     model.Read('l'),
                     model.BinaryOperation(model.Reference('l'), '%',
                                           model.Reference('x'))
                 ]))
         ])).evaluate(scope)
     model.Print(
         model.FunctionCall(
             model.FunctionCall(model.Reference('foo'), [model.Number(5)]),
             [])).evaluate(scope)
예제 #9
0
 def visitPrint(self, p):
     return model.Print(p.expr.accept(self))