Beispiel #1
0
    def test_to_match_with_one_of_type_combo(self):
        func = function_lit(
            one_of_type([string_type(), int_type(),
                         bool_type()]),
            return_op(
                match_op(dereference("argument"), [
                    prepared_function(int_type(),
                                      literal_op("int is not a string")),
                    prepared_function(bool_type(),
                                      literal_op("bool is not a string")),
                    prepared_function(inferred_type(), dereference("argument"))
                ])))

        _, result = bootstrap_function(func, argument=2)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "int is not a string")
        _, result = bootstrap_function(func, argument=True)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "bool is not a string")
        _, result = bootstrap_function(func, argument="hello world")
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "hello world")

        prepared_func = prepare(func, PythonObject({}), FrameManager())
        self.assertEquals(len(prepared_func.break_types), 1)
        self.assertTrue("return" in prepared_func.break_types)
        for return_break_type in prepared_func.break_types["return"]:
            self.assertTrue(StringType().is_copyable_from(
                return_break_type["out"], DUMMY_REASONER))
Beispiel #2
0
    def test_to_string_from_int(self):
        func = function_lit(
            any_type(),
            return_op(
                match_op(dereference("argument"), [
                    prepared_function(unit_type(1), literal_op("one")),
                    prepared_function(unit_type(2), literal_op("two")),
                    prepared_function(unit_type(3), literal_op("three")),
                    prepared_function(unit_type(4), literal_op("four")),
                    prepared_function(any_type(), literal_op("invalid"))
                ])))

        _, result = bootstrap_function(func, argument=1)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "one")
        _, result = bootstrap_function(func, argument=2)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "two")
        _, result = bootstrap_function(func, argument=3)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "three")
        _, result = bootstrap_function(func, argument=4)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "four")
        _, result = bootstrap_function(func, argument=5)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "invalid")
Beispiel #3
0
    def test_interesting(self):
        func = function_lit(
            any_type(), infer_all(),
            match_op(dereference("argument"), [
                prepared_function(
                    object_type({"foo": int_type()}),
                    return_op(
                        addition_op(dereference("argument.foo"),
                                    literal_op(3)))),
                prepared_function(any_type(), return_op(literal_op("invalid")))
            ]))

        _, result = bootstrap_function(func,
                                       argument=PythonObject(
                                           {"foo": 39},
                                           bind=UniversalObjectType(
                                               {"foo": IntegerType()})))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)

        _, result = bootstrap_function(func,
                                       argument=PythonObject({"foo": "hello"}))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "invalid")
Beispiel #4
0
 def test_invalid_list_assignment(self):
     code = parse("""
         function() {
             List<int> foo = [ { bar: 2 }, { bar: 3 } ];
             return foo[0].bar;
         }
     """)
     with self.assertRaises(PreparationException):
         bootstrap_function(code)
Beispiel #5
0
 def test_precidence(self):
     code = parse("""
         function() { return (1 + 1) * 23 - 2 * 1 + 2; }
     """)
     _, result = bootstrap_function(code)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 42)
Beispiel #6
0
 def test_4(self):
     code = parse("""
         function() {
             int bestResult = 0, i = 999;
             while(i >= 100) {
                 int j = 999;
                 while(j >= i) {
                     int testResult = i * j;
                     if(testResult <= bestResult) {
                         break;
                     };
                     if(testResult > 100000
                             && testResult > bestResult
                             && testResult / 1 % 10 == testResult / 100000 % 10
                             && testResult / 10 % 10 == testResult / 10000 % 10
                             && testResult / 100 % 10 == testResult / 1000 % 10
                     ) {
                         bestResult = testResult;
                     };
                     j = j - 1;
                 };
                 i = i - 1;
             };
             return bestResult;
         }
     """,
                  debug=True)
     _, result = bootstrap_function(code)
     self.assertEquals(result.value, 906609)
Beispiel #7
0
    def test_immediate_return(self):
        _, result = bootstrap_function(
            function_lit(no_value_type(), build_break_types(int_type()),
                         loop_op(return_op(literal_op(42)))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Beispiel #8
0
 def test_returns_string(self):
     code = parse("""
         function() { return "hello"; }
     """)
     _, result = bootstrap_function(code)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, "hello")
Beispiel #9
0
 def test_initialize_and_return_local(self):
     code = parse("""
         function() { int foo = 40; return foo + 2; }
     """)
     _, result = bootstrap_function(code)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 42)
Beispiel #10
0
    def test_14(self):
        code = parse("""
            function() => int {
                Dictionary<int : int> cachedResults = { 1: 1 };

                Function<int => int> testNumber = function(int number) => int {
                    return 0;
                };

                testNumber = function(int number) => int {
                    var cachedResult = cachedResults[number]?;

                    if(cachedResult is int) {
                        return cachedResult;
                    };

                    int calcedResult = testNumber(number % 2 == 0 ? number / 2 : number * 3 + 1) + 1;
                    cachedResults[number] = calcedResult;
                    return calcedResult;
                };

                Tuple<int...> results = for(var test in <list(range(1, 10))>) { continue testNumber(test); };
                return max(|results|);
            }
        """,
                     debug=True)
        _, result = bootstrap_function(code)
        self.assertEquals(result.value, 20)
Beispiel #11
0
    def test_12(self):
        # The original test goes to 500, but I can only do 10 atm...
        code = parse("""
            function() {
                var countDivisors = function(int number) {
                    int test = number, count = 1;
                    for(var test from range(1, number)) {
                        if(number % test == 0) {
                            count = count + 1;
                        };
                    };
                    return count;
                };

                int triangleNumber = 1, step = 2;
                while(countDivisors(triangleNumber) < 10) {
                    triangleNumber = triangleNumber + step;
                    step = step + 1;
                };
                return triangleNumber;
            }
        """,
                     debug=True)
        _, result = bootstrap_function(code)
        self.assertEquals(result.value, 120)
Beispiel #12
0
    def test_7_fast(self):
        code = parse("""
            function() => int {
                var isPrime = function(int number) => bool {
                    for(var i from range(2, number / 2)) {
                        if(number % i == 0) {
                            return false;
                        };
                    };
                    return true;
                };

                int count = 1, test = 3;
                loop {
                    if(isPrime(test)) {
                        count = count + 1;
                        if(count >= 100) {
                            return test;
                        };
                    };
                    test = test + 2;
                };
            }
        """,
                     debug=True)
        with environment(**fastest):
            _, result = bootstrap_function(code)
        self.assertEquals(result.value, 541)
Beispiel #13
0
 def test_addition(self):
     code = parse("""
         function() { return 12 + 30; }
     """)
     _, result = bootstrap_function(code)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 42)
Beispiel #14
0
    def test_count_then_return(self):
        _, result = bootstrap_function(
            function_lit(
                no_value_type(), build_break_types(int_type()), int_type(),
                literal_op(0),
                loop_op(
                    comma_op(
                        assignment_op(
                            context_op(), literal_op("local"),
                            addition_op(
                                dereference_op(context_op(),
                                               literal_op("local"), True),
                                literal_op(1))),
                        condition_op(
                            equality_op(
                                dereference_op(context_op(),
                                               literal_op("local"), True),
                                literal_op(42)),
                            return_op(
                                dereference_op(context_op(),
                                               literal_op("local"))),
                            nop())))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Beispiel #15
0
    def test_basic_inferrence(self):
        _, result = bootstrap_function(
            function_lit(no_value_type(), build_break_types(inferred_type()),
                         return_op(literal_op(42))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Beispiel #16
0
    def test_9(self):
        code = parse("""
             function() {
                 int a = 1, topb = 998;
                 while(a < 998) {
                     int b = topb;
                     while(b > a) {
                         int c = 1000 - a - b;
                         int test = a * a + b * b - c * c;
                         if(test < 0) {
                             topb = b + 1;
                             break;
                         };
                         if(test == 0) {
                             return a * b * c;
                         };
                         b = b - 1;
                     };
                     a = a + 1;
                 };
             }

        """,
                     debug=True)
        _, result = bootstrap_function(code)
        self.assertEquals(result.value, 31875000)
Beispiel #17
0
    def test_infer_all(self):
        _, result = bootstrap_function(
            function_lit(no_value_type(), infer_all(),
                         return_op(literal_op(42))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Beispiel #18
0
    def test_return_with_dereference4(self):
        _, result = bootstrap_function(function_lit(
            int_type(),
            build_break_types(
                object_type({
                    "foo": any_type(),
                    "bar": any_type()
                })),
            comma_op(
                return_op(
                    object_template_op({
                        "foo":
                        literal_op(42),
                        "bar":
                        dereference_op(context_op(), literal_op("argument"),
                                       True)
                    })))),
                                       argument=42,
                                       check_safe_exit=True)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertTrue(isinstance(result.value, Universal))
        get_manager(
            result.value).add_composite_type(DEFAULT_READONLY_COMPOSITE_TYPE)
        self.assertEquals(result.value._get("foo"), 42)
        self.assertEquals(result.value._get("bar"), 42)
Beispiel #19
0
 def test_return_argument(self):
     code = parse("""
         function(|int|) { return argument; }
     """)
     _, result = bootstrap_function(code, argument=42)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 42)
Beispiel #20
0
 def test_dereference_argument_parameter(self):
     code = parse("""
         function(|Object { foo: int }|) { return foo; }
     """)
     _, result = bootstrap_function(code,
                                    argument=PythonObject({"foo": 42}))
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 42)
Beispiel #21
0
    def test_comma(self):
        _, result = bootstrap_function(function_lit(
            no_value_type(), build_break_types(int_type()),
            return_op(comma_op(literal_op(5), literal_op(8), literal_op(42)))),
                                       check_safe_exit=True)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Beispiel #22
0
    def test_static_value_dereference(self):
        _, result = bootstrap_function(
            function_lit(
                return_op(static_op(addition_op(literal_op(5),
                                                literal_op(37))))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Beispiel #23
0
    def test_infer_exception(self):
        _, result = bootstrap_function(function_lit(
            no_value_type(), infer_all(),
            addition_op(literal_op("hello"), literal_op(5))),
                                       check_safe_exit=False)

        self.assertEquals(result.caught_break_mode, "exception")
        self.assertEquals(result.value.type, "TypeError")
Beispiel #24
0
    def test_basic_function(self):
        func = function_lit(no_value_type(),
                            build_break_types(value_type=int_type()),
                            literal_op(42))

        _, result = bootstrap_function(func)

        self.assertEquals(result.caught_break_mode, "value")
        self.assertEquals(result.value, 42)
Beispiel #25
0
 def test_multiplication(self):
     code = parse("""
         function() {
             return 21 * 2;
         }
     """)
     with environment(transpile=True, return_value_optimization=True):
         _, result = bootstrap_function(code)
     self.assertEquals(result.value, 42)
Beispiel #26
0
 def test_1(self):
     code = parse("""
         function(int foo) => int {
             return foo;
         }
     """,
                  debug=True)
     _, result = bootstrap_function(code, argument=PythonList([5]))
     self.assertEqual(result.value, 5)
Beispiel #27
0
    def test_addition(self):
        func = function_lit(
            no_value_type(), build_break_types(int_type()),
            return_op(addition_op(literal_op(40), literal_op(2))))

        _, result = bootstrap_function(func)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
Beispiel #28
0
 def test_basic_false(self):
     _, result = bootstrap_function(
         function_lit(
             no_value_type(), build_break_types(int_type()),
             return_op(
                 condition_op(literal_op(False), literal_op(34),
                              literal_op(53)))))
     self.assertEquals(result.caught_break_mode, "return")
     self.assertEquals(result.value, 53)
Beispiel #29
0
 def test_return_with_dereference5(self):
     with self.assertRaises(Exception):
         bootstrap_function(function_lit(
             int_type(),
             build_break_types(
                 object_type({
                     "foo": any_type(),
                     "bar": unit_type(42)
                 })),
             return_op(
                 object_template_op({
                     "foo":
                     literal_op(42),
                     "bar":
                     dereference_op(context_op(), literal_op("argument"),
                                    True)
                 }))),
                            argument=42,
                            check_safe_exit=True)
Beispiel #30
0
    def test_simple_return_argument(self):
        func = function_lit(
            int_type(), build_break_types(int_type()),
            return_op(
                dereference_op(context_op(), literal_op("argument"), True)))

        _, result = bootstrap_function(func, argument=42)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)