Esempio n. 1
0
    def test_correct_expressions(self):
        expr_1 = PostfixExpression("(a+ 36)/ log(2 , 4)")
        self.assertEqual(expr_1.error_msg, None)
        self.assertEqual(expr_1.error_place, (None, None))
        self.assertEqual(expr_1.interpreted_expression, "(a+36)/log(2,4)")
        self.assertEqual(expr_1.result, Operand([18, 0.5]))

        expr_2 = PostfixExpression("log(log(log(log(log(2, 4), 4), 4), 4), 4)")
        self.assertEqual(expr_2.error_msg, None)
        self.assertEqual(expr_2.error_place, (None, None))
        self.assertEqual(expr_2.interpreted_expression,
                         "log(log(log(log(log(2,4),4),4),4),4)")
        self.assertEqual(expr_2.result, Operand([2]))

        expr_3 = PostfixExpression("3*a+3log(2(1+1), 16)-3a")
        self.assertEqual(expr_3.error_msg, None)
        self.assertEqual(expr_3.error_place, (None, None))
        self.assertEqual(expr_3.interpreted_expression,
                         "3*a+3*log(2*(1+1),16)-3*a")
        self.assertEqual(expr_3.result, Operand([6]))

        expr_4 = PostfixExpression(
            "2    * ( -    3 ) +    6  ( -a)-4*( -   log (2   , 4)      )")
        self.assertEqual(expr_4.error_msg, None)
        self.assertEqual(expr_4.error_place, (None, None))
        self.assertEqual(expr_4.interpreted_expression,
                         "2*(-3)+6*(-a)-4*(-log(2,4))")
        self.assertEqual(expr_4.result, Operand([2, -6]))
Esempio n. 2
0
 def exitOperand(self, ctx: tl69asmParser.OperandContext):
     child = ctx.children[0]
     if isinstance(child, tl69asmParser.LabelRefContext):
         ctx.operand = Operand(OperandType.LABEL, child.label_text)
     elif isinstance(child, tl69asmParser.IntegerContext):
         ctx.operand = Operand(OperandType.INTEGER, child.value)
     elif isinstance(child, tl69asmParser.RegisterContext):
         ctx.operand = Operand(OperandType.REGISTER, child.register_name)
 def test_constructor(self):
     op_0 = Operand([0, 0, 0, 0, 1])
     op_1 = Operand([0, 0, 0, 0])
     op_2 = Operand([0, 0, 0])
     op_3 = Operand([0, 0])
     op_4 = Operand([0])
     self.assertNotEqual(op_0, op_1)
     self.assertEqual(op_1, op_2)
     self.assertEqual(op_2, op_3)
     self.assertEqual(op_4, op_4)
    def test_log_polynomial(self):
        op_1 = Operand([0, 1])
        op_2 = Operand([2])
        with self.assertRaises(NotImplementedError):
            op_1.log(op_2)

        with self.assertRaises(NotImplementedError):
            op_2.log(op_1)
Esempio n. 5
0
    def exitTl69asmLine(self, ctx: tl69asmParser.Tl69asmLineContext):
        if ctx.label():
            label_name = ctx.label().label_name
            self.label_dict[label_name] = self.get_line_number()

        if ctx.instruction():
            self.lines[-1].instructions.append(ctx.instruction().instruction)
        elif ctx.directive():
            directive = ctx.directive().directive
            value = None
            if directive.expr[0] == OperandType.LABEL:
                label_name = directive.expr[1]
                if label_name in self.label_dict:
                    value = self.label_dict[label_name]
                else:
                    print("TODO: proper label resolution")
                    return
            elif directive.expr[0] == OperandType.INTEGER:
                value = directive.expr[1]

            if not value:
                return

            if directive.directive_type == DirectiveType.ORG:
                if self.lines[-1].empty():
                    self.lines[-1] = Chunk(value)
                else:
                    self.lines.append(Chunk(value))
            elif directive.directive_type == DirectiveType.DW:
                dw_instruction = UnformedInstruction(
                    "DW", [Operand(OperandType.INTEGER, value)])
                self.lines[-1].instructions.append(dw_instruction)
            elif directive.directive_type == DirectiveType.EQU:
                self.label_dict[directive.label] = value
Esempio n. 6
0
def main():
    print('--- ADVENT OF CODE 2020 DAY 8 ---')
    operands = []

    f = open(args.input, 'r')
    for row in f:
        o = Operand(row.strip())
        operands.append(o)


#    print(operands)

    first_answer = part1(operands)
    second_answer = -1

    print('PART1: The value in the accumulator is %d!' % first_answer)
    print('PART2: The sum of these counts are %d!' % second_answer)
 def test_mul_res_zero(self):
     op_1 = Operand([0])
     op_2 = Operand([1, 2, 3, 4])
     result = Operand([0])
     self.assertEqual(op_1 * op_2, result)
 def test_varstring(self):
     op_0 = Operand([1, 2])
     op_1 = Operand([1., 2.])
     result = "1 + 2a"
     self.assertEqual(op_0.varstring("a"), result)
     self.assertEqual(op_0.varstring("a"), op_1.varstring("a"))
 def test_log(self):
     op_1 = Operand([2.71828])
     op_2 = Operand([7.35928])
     self.assertAlmostEqual(op_2.log(op_1).polynomial[0], 2, places=2)
 def test_add_neq_length(self):
     op_1 = Operand([1, 2, 3])
     op_2 = Operand([4, 5, 6, 0, 3])
     result = Operand([5, 7, 9, 0, 3])
     self.assertEqual(op_1 + op_2, result)
 def test_div_neg(self):
     op_1 = Operand([1])
     op_2 = Operand([0, 1])
     with self.assertRaises(NotImplementedError):
         op_1 / op_2
 def test_div_by_zero(self):
     op_1 = Operand([1])
     op_2 = Operand([0])
     with self.assertRaises(ZeroDivisionError):
         op_1 / op_2
 def test_div(self):
     op_1 = Operand([0, 3, 3])
     op_2 = Operand([3, 3])
     result = Operand([0, 1])
     self.assertEqual(op_1 / op_2, result)
import math
import unittest

from expressionchecker import ExpressionChecker
from operand import Operand

operators = {
    "+": lambda x, y: x + y,
    "-": lambda x, y: x - y,
    "*": lambda x, y: x * y,
    "/": lambda x, y: x / y,
    "~": lambda x: Operand([0]) - x
}  # Unary '-' operator

functions = {
    "log": lambda x, y: y.log(x),
    "ln": lambda x: x.log(Operand([math.e]))
}


class ExpressionCheckerTest(unittest.TestCase):
    def test_correct_expressions(self):
        checker = ExpressionChecker(operators=operators, functions=functions)
        expr_1 = ["x", "+", "12", "-", "3", "*", "(", "25", "+", "x", ")"]
        expr_2 = [
            "~", "3", "-", "4", "-", "5", "-", "6", "*", "(", "x", "+", "(",
            "~", "3", ")", ")"
        ]
        expr_3 = ["log", "(", "2", ",", "4", ")"]
        self.assertEqual(checker.consume_token_array(expr_1), (None, None))
        self.assertEqual(checker.consume_token_array(expr_2), (None, None))
 def test_mul(self):
     op_1 = Operand([2, 3])
     op_2 = Operand([0, 4, 2])
     result = Operand([0, 8, 16, 6])
     self.assertEqual(op_1 * op_2, result)
 def test_sub_res_zero(self):
     op_1 = Operand([0, 0, 0, 0, 3])
     op_2 = Operand([0, 0, 0, 0, 3])
     result = Operand([0])
     self.assertEqual(op_1 - op_2, result)
 def test_sub_neq_length(self):
     op_1 = Operand([1, 2, 3])
     op_2 = Operand([4, 5, 6, 0, 3])
     result = Operand([-3, -3, -3, 0, -3])
     self.assertEqual(op_1 - op_2, result)
 def test_add_res_zero(self):
     op_1 = Operand([0, -3])
     op_2 = Operand([0, 3])
     result = Operand([0])
     self.assertEqual(op_1 + op_2, result)
Esempio n. 19
0
import os
import sys

current_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.abspath(current_dir + "/../service"))

from operand import Operand

if __name__ == "__main__":
    obj_operand = Operand()
    print(obj_operand.func(5, 100, 'plus1'))
    print(obj_operand.func(2, 10, 'KUADRAT'))
    print(obj_operand.func(43, 1000, 'fiboNacci'))
Esempio n. 20
0
print("xxx/unittests/test_operand.py")

import os
import sys
path = os.path.normpath(
    os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))

if (not (path in sys.path)):
    sys.path.append(path)

from operand import DataChunk, Operand

op_e1_bessel1 = Operand("e1.bessel1")
op_e1_bessel1.inputs = {"n": DataChunk(0), "z": DataChunk(1.8 + 0.1j)}
op_e1_bessel1.execute()
print(op_e1_bessel1.__dict__)

op_e1_bessel2 = Operand("e1.bessel2")
op_e1_bessel2.inputs = {"n": DataChunk(0), "z": DataChunk(1.8 + 0.1j)}
op_e1_bessel2.execute()
print(op_e1_bessel2.__dict__)

op_sum = Operand("+")
op_sum.inputs = {"a": op_e1_bessel1.outputs, "b": op_e1_bessel2.outputs}
op_sum.execute()
print(op_sum.__dict__)
print("output:", op_sum.outputs.__dict__)