class TestIntArray(unittest.TestCase): def _list_elements_to_interval(self, values): return [[IntegerInterval(e) for e in l] for l in values] def setUp(self): self.vector_values = [1, 2, 3] self.vector = IntArray(self.vector_values) self.matrix_values = self._list_elements_to_interval([[[1, 5], [2, 6], [3, 7]], [[4, 8], [5, 9], [6, 9]], [[7, 9], [8, 9], [9, 9]]]) self.matrix = IntArray(self.matrix_values) def test_matrix_shape(self): self.assertEqual(self.vector.shape, (3, )) self.assertEqual(self.matrix.shape, (3, 3)) def test_matrix_content(self): intervals = [IntegerInterval(v) for v in self.vector_values] self.assertListEqual(intervals, list(self.vector)) x_shape, y_shape = self.matrix.shape for x, y in itertools.product(range(x_shape), range(y_shape)): val = IntegerInterval(self.matrix_values[x][y]) self.assertEqual(val, self.matrix[x, y]) def test_matrix_getter(self): self.assertEqual(self.matrix[1, 2], IntegerInterval([6, 9])) self.assertEqual(self.matrix[[0, 1], 1], IntegerInterval([2, 9])) def test_matrix_update(self): mat = self.matrix.update((1, 2), 1) self.assertEqual(mat[1, 2], IntegerInterval(1)) mat = self.matrix.update(([1, 2], 1), 1) val = IntegerInterval([1, 9]) self.assertEqual(mat[1, 1], val) self.assertEqual(mat[2, 1], val) def test_join_and_meet(self): other_matrix = IntArray([[9, 8, 7], [6, 5, 4], [3, 2, 1]]) join_test_matrix = IntArray( self._list_elements_to_interval([[[1, 9], [2, 8], [3, 7]], [[4, 8], [5, 9], [4, 9]], [[3, 9], [2, 9], [1, 9]]])) self.assertEqual(self.matrix | other_matrix, join_test_matrix) bot = IntegerInterval(bottom=True) meet_test_matrix = IntArray([[bot, bot, 7], [6, 5, bot], [bot, bot, bot]]) self.assertEqual(self.matrix & other_matrix, meet_test_matrix) def test_to_nested_list(self): nested_list = self.matrix.to_nested_list() self.assertEqual(nested_list, self.matrix_values) def test_transpose(self): return NotImplemented matrix = IntArray([[1, 2], [3, 4], [5, 6]]) transpose_test_matrix = IntArray([[1, 3, 5], [2, 4, 6]]) self.assertEqual(matrix.transpose(), transpose_test_matrix)
def setUp(self): self.a = Variable('a', IntegerArrayType([2, 2])) self.x = Variable('x', int_type) self.y = Variable('y', int_type) self.z = Variable('z', real_type) self.state = BoxState(x=[1, 2], y=3, z=[1.0, 2.0]) self.array_state = BoxState( a=IntegerIntervalArray([[1, 2], [3, 4]]), x=[0, 1], y=0)
def test_UpdateExpr(self): subscript = Subscript(self.y) expr = UpdateExpr(self.z, subscript, self.x) label_subscript_expr = Subscript(self.y_label) subscript_label = self.context.Label( subscript, IntegerIntervalArray([self.state[self.y]]), None) label_expr = UpdateExpr(self.z_label, subscript_label, self.x_label) new_bound = IntegerIntervalArray([1, 2, 3, IntegerInterval([1, 2])]) label = self.context.Label(expr, new_bound, None) env = { label: label_expr, subscript_label: label_subscript_expr, self.x_label: self.x, self.y_label: self.y, self.z_label: self.z, } test_value = LabelSemantics(label, env) self.compare(expr, test_value)
def setUp(self): self.vector_values = [1, 2, 3] self.vector = IntArray(self.vector_values) self.matrix_values = self._list_elements_to_interval([[[1, 5], [2, 6], [3, 7]], [[4, 8], [5, 9], [6, 9]], [[7, 9], [8, 9], [9, 9]]]) self.matrix = IntArray(self.matrix_values)
def setUp(self): self.context = LabelContext('test_context') mat = IntegerIntervalArray([1, 2, 3, 4]) self.x = Variable('x', int_type) self.y = Variable('y', int_type) self.z = Variable('z', IntegerArrayType([4])) self.state = BoxState({ self.x: [1, 2], self.y: 3, self.z: mat, }) self.x_label = self.context.Label( self.x, IntegerInterval([1, 2]), None) self.y_label = self.context.Label(self.y, IntegerInterval(3), None) self.z_label = self.context.Label(self.z, mat, None)
def test_AccessExpr(self): subscript = Subscript(self.y) expr = AccessExpr(self.z, subscript) label_subscript_expr = Subscript(self.y_label) subscript_label = self.context.Label( subscript, IntegerIntervalArray([self.state[self.y]]), None) label_expr = AccessExpr(self.z_label, subscript_label) label = self.context.Label(expr, IntegerInterval(4), None) env = { label: label_expr, subscript_label: label_subscript_expr, self.y_label: self.y, self.z_label: self.z, } test_value = LabelSemantics(label, env) self.compare(expr, test_value)
def test_transpose(self): return NotImplemented matrix = IntArray([[1, 2], [3, 4], [5, 6]]) transpose_test_matrix = IntArray([[1, 3, 5], [2, 4, 6]]) self.assertEqual(matrix.transpose(), transpose_test_matrix)
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)