Exemple #1
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)
Exemple #2
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)
Exemple #3
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)
Exemple #4
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)
Exemple #5
0
 def test_nested_object(self):
     ast = parse("""
         { "foo": {
             "bar": 42
         } }
     """)
     self.assertEqual(ast.foo.bar, 42)
Exemple #6
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)
Exemple #7
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")
Exemple #8
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)
Exemple #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)
Exemple #10
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)
Exemple #11
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)
Exemple #12
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)
Exemple #13
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)
Exemple #14
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)
Exemple #15
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)
Exemple #16
0
 def test_initialize_and_return_local_object(self):
     code = parse("""
         function() {
             Object { foo: int } bar = { foo: 40 };
             return bar.foo + 2;
         }
     """)
     _, result = bootstrap_function(code)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 42)
Exemple #17
0
 def test_list(self):
     code = parse("""
         function() {
             List<int> foo = [ 1, 2, 3 ];
             return foo[0] * foo[1] * foo[2];
         }
     """)
     _, result = bootstrap_function(code, check_safe_exit=False)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 6)
Exemple #18
0
 def test_list_of_objects(self):
     code = parse("""
         function() {
             List<Object { bar: int }> foo = [ { bar: 2 }, { bar: 3 } ];
             return foo[0].bar * foo[1].bar;
         }
     """)
     _, result = bootstrap_function(code, check_safe_exit=False)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 6)
Exemple #19
0
 def test_object_with_lists(self):
     code = parse("""
         function() {
             Object { foo: List<int> } bar = { foo: [ 1, 2, 3 ] };
             return bar.foo[0] * bar.foo[1] * bar.foo[2];
         }
     """)
     _, result = bootstrap_function(code, check_safe_exit=False)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 6)
Exemple #20
0
 def test_double_initialization_destructure(self):
     code = parse("""
         function() {
             [ int foo, int bar ] = [ 12, 30 ];
             return foo + bar;
         }
     """,
                  debug=True)
     _, result = bootstrap_function(code)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 42)
Exemple #21
0
 def test_3(self):
     code = parse("""
         function(any foo) {
             return foo + 3;
         }
     """,
                  debug=True)
     _, result = bootstrap_function(code,
                                    argument=PythonList([5]),
                                    check_safe_exit=False)
     self.assertEqual(result.value, 8)
Exemple #22
0
 def test_some_const_locals(self):
     code = parse("""
         function() {
             int foo = 1;
             int bar = foo + 37;
             return bar + 4;
         }
     """)
     _, result = bootstrap_function(code)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 42)
Exemple #23
0
 def test_basic_dictionary3(self):
     code = parse("""
         function() {
             Dictionary<int: int> foo = { 3 : 55 };
             return foo[6];
         }
     """)
     _, result = bootstrap_function(code, check_safe_exit=False)
     self.assertEquals(result.caught_break_mode, "exception")
     self.assertEquals(result.value._to_dict()["message"],
                       "DereferenceOp: invalid_dereference")
Exemple #24
0
 def test_inferred_types_in_destructure(self):
     code = parse("""
         function() {
             int foo = 0;
             [ foo, var bar ] = [ 12, 30 ];
             return foo + bar;
         }
     """)
     _, result = bootstrap_function(code)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 42)
Exemple #25
0
 def test_single_initialization_destructure(self):
     code = parse("""
         function() {
             { int foo } = { foo: 42 };
             return foo;
         }
     """,
                  debug=True)
     _, result = bootstrap_function(code)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 42)
Exemple #26
0
 def test_basic_dictionary4(self):
     code = parse("""
         function() => int {
             var foo = { 3 : 55 };
             return foo[3];
         }
     """)
     func, result = bootstrap_function(code)
     if hasattr(func, "break_types"):
         self.assertNotIn("exception", func.break_types)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 55)
Exemple #27
0
 def test_single_assignment_destructure(self):
     code = parse("""
         function() {
             int foo = 0;
             { foo } = { foo: 12, bar: 30 };
             return foo;
         }
     """,
                  debug=True)
     _, result = bootstrap_function(code)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 12)
Exemple #28
0
 def test_local_function(self):
     code = parse("""
         function() {
             var x = function() {
                 return 42;
             };
             return x();
         }
     """)
     _, result = bootstrap_function(code)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 42)
Exemple #29
0
 def test_mixed_destructure(self):
     code = parse("""
         function() {
             int foo = 0;
             { foo, int bar } = { foo: 12, bar: 30 };
             return foo + bar;
         }
     """,
                  debug=True)
     _, result = bootstrap_function(code)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 42)
Exemple #30
0
 def test_double_assignment_destructure(self):
     code = parse("""
         function() {
             int foo = 0, bar = 0;
             [ foo, bar ] = [ 12, 30 ];
             return foo + bar;
         }
     """,
                  debug=True)
     _, result = bootstrap_function(code)
     self.assertEquals(result.caught_break_mode, "value")
     self.assertEquals(result.value, 42)