示例#1
0
 def test_global_id_not_found(self):
     s = 'a = 0;' \
         'ret a + b;'
     should_fail(self,
                 s,
                 expected_error_code=ErrorCode.ID_NOT_FOUND,
                 expected_id='b')
示例#2
0
 def test_create_local_var(self):
     s = 'fun f()' \
         '   a = 1;' \
         'ret a;'
     should_fail(self, s,
                 expected_error_code=ErrorCode.ID_NOT_FOUND,
                 expected_id='a')
示例#3
0
    def test_round(self):
        s = 'a = 4.5;' \
            'ret round(a);'
        self.assertEqual(round(4.5), interpret(s).to_py())

        s = 'a = 4.4;' \
            'ret round(a);'
        self.assertEqual(round(4.4), interpret(s).to_py())

        s = 'a = 4.8;' \
            'ret round(a);'
        self.assertEqual(round(4.8), interpret(s).to_py())

        s = 'a = 4.0;' \
            'ret round(a);'
        self.assertEqual(round(4.0), interpret(s).to_py())

        s = 'a = [1, 2; 3, 4];' \
            'ret round(a);'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.UNSUPPORTED_TYPE_ROUND)

        s = 'a = "a";' \
            'ret round(a);'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.UNSUPPORTED_TYPE_ROUND)
示例#4
0
    def test_single_index_out_of_range(self):
        s = 'a = [1, 2, 3, 4];' \
            'a[5];'
        should_fail(self, s, expected_error_code=ErrorCode.OUT_OF_RANGE)

        s = 'a = [1, 2, 3, 4];' \
            'a[-1];'
        should_fail(self, s, expected_error_code=ErrorCode.OUT_OF_RANGE)
示例#5
0
 def test_local_scope_id_not_found(self):
     s = 'a = 32;' \
         '{' \
         '   ret b;' \
         '}'
     should_fail(self,
                 s,
                 expected_error_code=ErrorCode.ID_NOT_FOUND,
                 expected_id='b')
示例#6
0
    def test_arguments_number_mismatch(self):
        s = 'fun f(a, b);' \
            'f(1);'
        should_fail(self, s,
                    expected_error_code=ErrorCode.NUMBER_OF_PARAMS_MISMATCH,
                    expected_id='f')

        s = 'fun f(a, b);' \
            'f(1, 2, 3);'
        should_fail(self, s,
                    expected_error_code=ErrorCode.NUMBER_OF_PARAMS_MISMATCH,
                    expected_id='f')
示例#7
0
    def test_row_index_out_of_range(self):
        s = 'a = [1, 2; 3, 4];' \
            'a[2, 2];'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.ROW_INDEX_OUT_OF_RANGE)

        s = 'a = [1, 2; 3, 4];' \
            'a[2, :];'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.ROW_INDEX_OUT_OF_RANGE)
示例#8
0
    def test_fun_not_defined(self):
        s = 'f();'
        should_fail(self, s,
                    expected_error_code=ErrorCode.FUN_NOT_DEFINED,
                    expected_id='f')

        s = '{' \
            '   {' \
            '       f();' \
            '   }' \
            '}'
        should_fail(self, s,
                    expected_error_code=ErrorCode.FUN_NOT_DEFINED,
                    expected_id='f')
示例#9
0
    def test_created_scope(self):
        s = 'if (1>0)' \
            '   b = 0;' \
            'ret b;'
        self.assertEqual(0, interpret(s).to_py())

        s = 'if (1>0) {' \
            '   b = 0;' \
            '}' \
            'ret b;'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.ID_NOT_FOUND,
                    expected_id='b')
示例#10
0
 def test_nested_scopes_id_not_found(self):
     s = 'a = 1;' \
         '{' \
         '   b = 2;' \
         '   {' \
         '       c = 3;' \
         '       d = a + b + c;' \
         '   }' \
         '   d = a + b;' \
         '}' \
         'ret d;'
     should_fail(self,
                 s,
                 expected_error_code=ErrorCode.ID_NOT_FOUND,
                 expected_id='d')
示例#11
0
    def test_column_index_out_of_range(self):
        s = 'a = [1, 2, 3;' \
            '     4, 5, 6;' \
            '     7, 8, 9];' \
            'a[1, 4];'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.COLUMN_INDEX_OUT_OF_RANGE)

        s = 'a = [1, 2, 3;' \
            '     4, 5, 6;' \
            '     7, 8, 9];' \
            'a[:, 4];'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.COLUMN_INDEX_OUT_OF_RANGE)
示例#12
0
    def test_shape(self):
        s = 'ret shape([1, 2, 3; 4, 5, 6]);'
        self.assertEqual([[2, 3]], interpret(s).to_py())

        s = 'ret shape(shape([1]));'
        self.assertEqual([[1, 2]], interpret(s).to_py())

        s = 'shape(2);'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.UNSUPPORTED_TYPE_SHAPE)

        s = 'a = "a";' \
            'shape(a);'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.UNSUPPORTED_TYPE_SHAPE)
示例#13
0
    def test_transpose(self):
        s = 'ret transpose([1, 2]);'
        self.assertEqual([[1], [2]], interpret(s).to_py())

        s = 'ret transpose([1; 2]);'
        self.assertEqual([[1, 2]], interpret(s).to_py())

        s = 'transpose(1);'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.UNSUPPORTED_TYPE_TRANSPOSE)

        s = 'a = "a";' \
            'transpose(a);'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.UNSUPPORTED_TYPE_TRANSPOSE)
示例#14
0
    def test_abs(self):
        s = 'a = -1;' \
            'ret abs(a);'
        self.assertEqual(1, interpret(s).to_py())

        s = 'a = 1;' \
            'ret abs(a);'
        self.assertEqual(1, interpret(s).to_py())

        s = 'a = [-1, -2];' \
            'abs(a);'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.UNSUPPORTED_TYPE_ABS)

        s = 'a = "string";' \
            'abs(a);'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.UNSUPPORTED_TYPE_ABS)
示例#15
0
    def test_len(self):
        s = 'a = [];' \
            'ret len(a);'
        self.assertEqual(0, interpret(s).to_py())

        s = 'a = [1, 2, 3;' \
            '     4, 5, 6];' \
            'ret len(a);'
        self.assertEqual(6, interpret(s).to_py())

        s = 'ret len("Hello");'
        self.assertEqual(5, interpret(s).to_py())

        s = 'a = "Hello";' \
            'ret len(a);'
        self.assertEqual(5, interpret(s).to_py())

        s = 'a = 1;' \
            'ret len(a);'
        should_fail(self, s, expected_error_code=ErrorCode.SCALAR_LEN)
示例#16
0
    def test_recursion_depth(self):
        s = 'fun recursion(depth) {' \
            '   if (depth > 0) {' \
            '       depth = depth - 1;' \
            '       recursion(depth);' \
            '   }' \
            '}' \
            'recursion(100);'
        should_fail(self, s,
                    expected_error_code=ErrorCode.MAX_RECURSION_DEPTH_EXCEED,
                    expected_id='recursion')

        s = 'fun recursion(depth) {' \
            '   if (depth > 0) {' \
            '       depth = depth - 1;' \
            '       recursion(depth);' \
            '   }' \
            '}' \
            'recursion(99);'
        interpret(s)
示例#17
0
    def test_min(self):
        s = 'a = 1;' \
            'b = 3;' \
            'ret min(a, b);'
        self.assertEqual(1, interpret(s).to_py())

        s = 'ret min([1], [1, 2, 3]);'
        self.assertEqual([[1]], interpret(s).to_py())

        s = 'a = "";' \
            'b = "a";' \
            'ret min(a, b);'
        self.assertEqual('', interpret(s).to_py())

        s = 'min(1, []);'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.UNSUPPORTED_TYPES_MIN)

        s = 'a = "a";' \
            'min(1, a);'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.UNSUPPORTED_TYPES_MIN)

        s = 'a = "a";' \
            'min(a, []);'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.UNSUPPORTED_TYPES_MIN)
示例#18
0
    def test_max(self):
        s = 'a = 1;' \
            'b = 3;' \
            'ret max(a, b);'
        self.assertEqual(3, interpret(s).to_py())

        s = 'ret max([], [1]);'
        self.assertEqual([[1]], interpret(s).to_py())

        s = 'a = "";' \
            'b = "a";' \
            'ret max(a, b);'
        self.assertEqual('a', interpret(s).to_py())

        s = 'max(1, []);'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.UNSUPPORTED_TYPES_MAX)

        s = 'a = "a";' \
            'max(1, a);'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.UNSUPPORTED_TYPES_MAX)

        s = 'a = "a";' \
            'max(a, []);'
        should_fail(self,
                    s,
                    expected_error_code=ErrorCode.UNSUPPORTED_TYPES_MAX)
示例#19
0
 def test_print_too_many_parametrs(self):
     s = 'print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);'
     should_fail(self, s, expected_error_code=ErrorCode.TOO_MANY_ARGUMENTS)