Exemplo n.º 1
0
 def test_higher_function(self):
     f = Function('map', lambda f, xs: [f(x) for x in xs],
                  (FunctionType(INT, INT), LIST), LIST)
     bar = Function('3x', lambda x: 3 * x, INT, INT)
     self.assertEqual(f(bar, ListValue([1, 2, 3])), ListValue([3, 6, 9]))
     self.assertEqual(f.type,
                      FunctionType((FunctionType(INT, INT), LIST), LIST))
     self.assertEqual(str(f.type), 'F((F(INT, INT), LIST), LIST)')
Exemplo n.º 2
0
 def test_out_of_range(self):
     prefix = 'LIST|LIST|LIST|SCAN1L,*,0|SCAN1L,*,3|MAP,/2,4' 
     inputs = [
         ListValue([8, 155, -231, -115, -178, 115, -246, -93, 42, 237, -104, -92,-208, -15, -116, -144, -58, -66, -120]), 
         ListValue([-2, -16, -8, -4, 5, 6, 4, 4, 7, -5, 0, 8, 2, 10, 12, 10, -4, 14]),
         ListValue([-7, -9, 3, -11, -6, 7, -3, -12, -7, -5, 10, 11, -8, -8, 10])
     ]
     program = Program.parse(prefix)
     self.assertRaises(OutputOutOfRangeError, program, *inputs)
Exemplo n.º 3
0
    def test_dfs1(self):
        ctx = Context(dict(zip(impl.FUNCTIONS, np.ones(len(impl.FUNCTIONS)))))

        inputs_list = [
            [ListValue([1, -2, 3, -4, 5, -6, 7])],
        ]
        output_list = [ListValue([1, 3, 15, 105])]
        examples = list(zip(inputs_list, output_list))

        T = 3
        solution, nb_steps = dfs(examples, T, ctx)
        for inputs, output in examples:
            self.assertEqual(solution(*inputs), output)
        self.assertTrue(nb_steps > 10)
Exemplo n.º 4
0
    def test_impossible(self):
        """Return the first n primes which is impossible in this language."""
        ctx = Context(dict(zip(impl.FUNCTIONS, np.ones(len(impl.FUNCTIONS)))))

        inputs_list = [
            [ListValue(list(range(6)))],
        ]
        output_list = [ListValue([2,3,5,7,11,13])]

        examples = list(zip(inputs_list, output_list))

        T = 2
        solution, nb_steps = dfs(examples, T, ctx)
        self.assertFalse(solution)
        self.assertTrue(nb_steps > 2000)
Exemplo n.º 5
0
def in_range(val):
    if isinstance(val, IntValue):
        val = ListValue([val.val])
    for x in val.val:
        if x < constants.INTMIN or x > constants.INTMAX:
            return False
    return True
Exemplo n.º 6
0
    def test_dfs(self):
        ctx = Context(dict(zip(impl.FUNCTIONS, np.ones(len(impl.FUNCTIONS)))))

        inputs_list = [
            [ListValue([1, 2, 3, 4, 5])],
        ]
        output_list = [
            ListValue([2, 4, 6, 8, 10]),
        ]
        examples = list(zip(inputs_list, output_list))

        T = 2
        solution, nb_steps = dfs(examples, T, ctx)
        for inputs, output in examples:
            self.assertEqual(solution(*inputs), output)
        self.assertTrue(
            nb_steps >= 10)  # Was > before, not >= but this always failed
Exemplo n.º 7
0
    def test_basic(self):
        # takes the second highest negative number
        prefix = 'LIST|INT|FILTER,<0,0|SORT,2|REVERSE,3|ACCESS,1,4'
        program = Program.parse(prefix)

        expected = IntValue(-2)
        actual = program(ListValue([1, -5, -3, -4, -2, -1, 2, 3]), IntValue(1))

        self.assertEqual(actual, expected)
        self.assertEqual(program.toprefix(), prefix)
Exemplo n.º 8
0
    def test_maxrank(self):
        # takes the second highest negative number
        prefix = 'LIST|MAXIMUM,0'
        program = Program.parse(prefix)

        expected = IntValue(3)
        actual = program(ListValue([1, -5, -3, -4, -2, -1, 2, 3]))

        self.assertEqual(actual, expected)
        self.assertEqual(program.toprefix(), prefix)
Exemplo n.º 9
0
    def test_sort_and_add(self):
        score_map = {
            impl.FILTER: .8,
            impl.LTIMES: .9,
            impl.SCAN1L: .5,
            impl.GT0: 1.
        }
        ctx = Context(score_map)

        # dfs favors more recent inputs
        actual = 'LIST|FILTER,>0,0|FILTER,>0,1|SCAN1L,*,2'
        inputs_list = [
            [ListValue([1, -2, 3, -4, 5, -6, 7])],
        ]
        output_list = [ListValue([1, 3, 15, 105])]
        examples = list(zip(inputs_list, output_list))
        T = 3
        solution, nb_steps_list = sort_and_add(examples, T, ctx, gas=np.inf)
        self.assertEqual(len(nb_steps_list), 2)
        self.assertEqual(solution.prefix, actual)
Exemplo n.º 10
0
    def test_impl(self):
        self.assertEqual(impl.MAP(impl.TIMES2, ListValue([1,2,3])), ListValue([2,4,6]))
        self.assertEqual(impl.FILTER(impl.EVEN, ListValue([1,2,3])), ListValue([2]))
        self.assertEqual(impl.COUNT(impl.EVEN, ListValue([1,3,5,7])), IntValue(0))
        self.assertEqual(impl.ZIPWITH(impl.LMINUS, ListValue([1,2,3]), ListValue([2,3,4])), ListValue([-1, -1, -1]))
        self.assertEqual(impl.SCAN1L(impl.LTIMES, ListValue([1,2,3,4])), ListValue([1, 1*2, 1*2*3, 1*2*3*4]))
        self.assertEqual(impl.MINIMUM(ListValue([])), NULLVALUE)
        self.assertEqual(impl.MAXIMUM(ListValue([])), NULLVALUE)
        self.assertEqual(impl.ACCESS(IntValue(0), ListValue([])), NULLVALUE)
        self.assertEqual(impl.ACCESS(IntValue(4), ListValue([1,2,3])), NULLVALUE)
        self.assertEqual(impl.TAKE(IntValue(1), ListValue([])), ListValue([]))

        # FH: Test maxrank
        self.assertEqual(impl.MAXIMUM(ListValue([38,45,3,1,15,18,7,47,24])), IntValue(47))
Exemplo n.º 11
0
 def test_impl(self):
     self.assertEqual(impl.MAP(impl.TIMES2, ListValue([1, 2, 3])),
                      ListValue([2, 4, 6]))
     self.assertEqual(impl.FILTER(impl.EVEN, ListValue([1, 2, 3])),
                      ListValue([2]))
     self.assertEqual(impl.COUNT(impl.EVEN, ListValue([1, 3, 5, 7])),
                      IntValue(0))
     self.assertEqual(
         impl.ZIPWITH(impl.LMINUS, ListValue([1, 2, 3]),
                      ListValue([2, 3, 4])), ListValue([-1, -1, -1]))
     self.assertEqual(impl.SCAN1L(impl.LTIMES, ListValue([1, 2, 3, 4])),
                      ListValue([1, 1 * 2, 1 * 2 * 3, 1 * 2 * 3 * 4]))
     self.assertEqual(impl.MINIMUM(ListValue([])), IntValue(NULL))
     self.assertEqual(impl.MAXIMUM(ListValue([])), IntValue(NULL))
     self.assertEqual(impl.ACCESS(IntValue(0), ListValue([])),
                      IntValue(NULL))
     self.assertEqual(impl.ACCESS(IntValue(4), ListValue([1, 2, 3])),
                      IntValue(NULL))
     self.assertEqual(impl.TAKE(IntValue(1), ListValue([])), ListValue([]))