Exemplo n.º 1
0
 def test_unroll_for_loop(self):
     program = """
     def main() {
         real x = 1.0;
         for (int i = 0; i < 9; i = i + 1) {
             x = x + 2.0;
         }
         return x;
     }
     """
     x = Variable('x', real_type)
     fix_expr = flow_to_meta_state(parse(program))[x]
     depth = 3
     unrolled = list(unroll_fix_expr(fix_expr, depth))
     program = """
     def main() {
         real x = 1.0;
         for (int i = 0; i < 8; i = i + 2) {
             x = (x + 2.0) + 2.0;
         }
         x = x + 2.0;
         return x;
     }
     """
     test_expr = flow_to_meta_state(parse(program))[x]
     self.assertEqual(test_expr, unrolled[1])
     inputs = BoxState(bottom=True)
     for unrolled_expr in unrolled:
         self.assertEqual(
             arith_eval(fix_expr, inputs),
             arith_eval(unrolled_expr, inputs))
Exemplo n.º 2
0
    def test_SelectExpr(self):
        test_expr = SelectExpr(
            BinaryBoolExpr(operators.LESS_OP, self.x, ErrorSemantics(1.5)),
            IntegerInterval(0), self.y)
        test_value = IntegerInterval(0)
        value = arith_eval(test_expr, self.state)
        self.assertEqual(test_value, value)

        test_expr = SelectExpr(
            BinaryBoolExpr(operators.LESS_OP, self.z, ErrorSemantics(1.5)),
            IntegerInterval(0), self.y)
        test_value = IntegerInterval([0, 3])
        value = arith_eval(test_expr, self.state)
        self.assertEqual(test_value, value)
Exemplo n.º 3
0
 def test_MetaState(self):
     meta_state = MetaState({
         self.x: BinaryArithExpr(
             operators.ADD_OP, self.x, IntegerInterval(1)),
         self.y: BinaryArithExpr(
             operators.MULTIPLY_OP, self.x, IntegerInterval(2)),
     })
     test_state = BoxState(x=[2, 3], y=[2, 4])
     state = arith_eval(meta_state, self.state)
     self.assertEqual(test_state, state)
Exemplo n.º 4
0
 def test_FixExpr(self):
     bool_expr = BinaryBoolExpr(operators.LESS_OP, self.x, self.y)
     loop_state = MetaState({
         self.x: BinaryArithExpr(
             operators.ADD_OP, self.x, IntegerInterval(1)),
         self.y: self.y,
     })
     init_state = MetaState({
         self.x: self.x, self.y: self.y,
     })
     test_expr = FixExpr(bool_expr, loop_state, self.x, init_state)
     test_value = IntegerInterval(3)
     value = arith_eval(test_expr, self.state)
     self.assertEqual(test_value, value)
Exemplo n.º 5
0
 def test_UpdateExpr(self):
     test_expr = UpdateExpr(self.a, (self.x, self.y), IntegerInterval(2))
     test_value = IntegerIntervalArray(
         [[IntegerInterval([1, 2]), 2], [IntegerInterval([2, 3]), 4]])
     value = arith_eval(test_expr, self.array_state)
     self.assertEqual(test_value, value)
Exemplo n.º 6
0
 def test_AccessExpr(self):
     test_expr = AccessExpr(self.a, (self.x, self.y))
     test_value = IntegerInterval([1, 3])
     value = arith_eval(test_expr, self.array_state)
     self.assertEqual(test_value, value)
Exemplo n.º 7
0
 def test_BinaryArithExpr(self):
     test_expr = BinaryArithExpr(operators.ADD_OP, self.x, self.y)
     test_value = self.state['x'] + self.state['y']
     value = arith_eval(test_expr, self.state)
     self.assertEqual(test_value, value)
Exemplo n.º 8
0
 def test_UnaryArithExpr(self):
     test_expr = UnaryArithExpr(operators.UNARY_SUBTRACT_OP, self.x)
     test_value = -self.state['x']
     value = arith_eval(test_expr, self.state)
     self.assertEqual(test_value, value)
Exemplo n.º 9
0
 def test_Variable(self):
     test_expr = self.x
     test_value = self.state[test_expr]
     value = arith_eval(test_expr, self.state)
     self.assertEqual(test_value, value)
Exemplo n.º 10
0
 def test_numeral(self):
     test_value = self.state['x']
     value = arith_eval(test_value, self.state)
     self.assertEqual(test_value, value)