def test_string_append_char(self): i = '_string_append_char' # 1 before = {'_char': [Character('C')], '_string': ['']} after = {'_string': ['C']} self.assertTrue(t_u.run_test(before, after, i)) # 2 before = {'_char': [Character('C')], '_string': ['AB']} after = {'_string': ['ABC']} self.assertTrue(t_u.run_test(before, after, i))
def test_string_nth(self): i = '_string_nth' # 1 before = {'_string': [''], '_integer': [1]} after = {'_string': [''], '_integer': [1]} self.assertTrue(t_u.run_test(before, after, i)) # 2 before = {'_string': ['A'], '_integer': [1]} after = {'_char': [Character('A')]} self.assertTrue(t_u.run_test(before, after, i)) # 3 before = {'_string': ['AB'], '_integer': [3]} after = {'_char': [Character('B')]} self.assertTrue(t_u.run_test(before, after, i))
def test_string_last(self): i = '_string_last' # 1 before = {'_string': ['']} after = {'_string': ['']} self.assertTrue(t_u.run_test(before, after, i)) # 2 before = {'_string': ['A']} after = {'_char': [Character('A')]} self.assertTrue(t_u.run_test(before, after, i)) # 3 before = {'_string': ['AB']} after = {'_char': [Character('B')]} self.assertTrue(t_u.run_test(before, after, i))
def test_string_split_at_char(self): i = '_string_split_at_char' # 1 before = {'_string': [''], '_char': [Character('o')]} after = {'_string': ['']} self.assertTrue(t_u.run_test(before, after, i)) # 2 before = {'_string': ['HelloWorld'], '_char': [Character('o')]} after = {'_string': ['Hell', 'W', 'rld']} self.assertTrue(t_u.run_test(before, after, i)) # 3 before = {'_string': ['HelloWorld'], '_char': [Character('a')]} after = {'_string': ['HelloWorld']} self.assertTrue(t_u.run_test(before, after, i))
def test_float_from_char(self): i = '_float_from_char' # 1 before = {'_char': [Character('1')]} after = {'_float': [49.0]} self.assertTrue(t_u.run_test(before, after, i)) # 2 before = {'_char': [Character('0')]} after = {'_float': [48.0]} self.assertTrue(t_u.run_test(before, after, i)) # 3 before = {'_char': [Character(' ')]} after = {'_float': [32.0]} self.assertTrue(t_u.run_test(before, after, i)) # 4 before = {'_char': [Character('\n')]} after = {'_float': [10.0]} self.assertTrue(t_u.run_test(before, after, i))
def test_integer_from_char(self): i = '_integer_from_char' # 1 before = {'_char': [Character('1')]} after = {'_integer': [49]} self.assertTrue(t_u.run_test(before, after, i)) # 2 before = {'_char': [Character('0')]} after = {'_integer': [48]} self.assertTrue(t_u.run_test(before, after, i)) # 3 before = {'_char': [Character(' ')]} after = {'_integer': [32]} self.assertTrue(t_u.run_test(before, after, i)) # 4 before = {'_char': [Character('\n')]} after = {'_integer': [10]} self.assertTrue(t_u.run_test(before, after, i))
def test_char_from_float(self): i = '_char_from_float' # 1 before = {'_float': [97.1]} after = {'_char': [Character('a')]} self.assertTrue(t_u.run_test(before, after, i)) # 2 before = {'_float': [33.2]} after = {'_char': [Character('!')]} self.assertTrue(t_u.run_test(before, after, i)) # 3 before = {'_float': [10.3]} after = {'_char': [Character('\n')]} self.assertTrue(t_u.run_test(before, after, i)) # 4 before = {'_float': [32.9]} after = {'_char': [Character(' ')]} self.assertTrue(t_u.run_test(before, after, i))
def test_char_from_integer(self): i = '_char_from_integer' # 1 before = {'_integer': [97]} after = {'_char': [Character('a')]} self.assertTrue(t_u.run_test(before, after, i)) # 2 before = {'_integer': [33]} after = {'_char': [Character('!')]} self.assertTrue(t_u.run_test(before, after, i)) # 3 before = {'_integer': [10]} after = {'_char': [Character('\n')]} self.assertTrue(t_u.run_test(before, after, i)) # 4 before = {'_integer': [32]} after = {'_char': [Character(' ')]} self.assertTrue(t_u.run_test(before, after, i))
def test_string_char_at(self): i = '_string_char_at' # 1 before = {'_string': [''], '_integer': [2]} after = {'_string': [''], '_integer': [2]} self.assertTrue(t_u.run_test(before, after, i)) # 2 before = {'_string': ['Hello'], '_integer': [1]} after = {'_char': [Character('e')]} self.assertTrue(t_u.run_test(before, after, i))
def test_char_is_digit(self): i = '_char_is_digit' # 1 before = {'_char': [Character('a')]} after = {'_boolean': [False]} self.assertTrue(t_u.run_test(before, after, i)) # 2 before = {'_char': [Character('7')]} after = {'_boolean': [True]} self.assertTrue(t_u.run_test(before, after, i)) # 3 before = {'_char': [Character('!')]} after = {'_boolean': [False]} self.assertTrue(t_u.run_test(before, after, i)) # 4 before = {'_char': [Character('\n')]} after = {'_boolean': [False]} self.assertTrue(t_u.run_test(before, after, i)) # 5 before = {'_char': [Character('\n')]} after = {'_boolean': [False]} self.assertTrue(t_u.run_test(before, after, i))
def test_string_remove_char(self): i = '_string_remove_char' # 1 before = {'_string': [''], '_char': [Character('A')]} after = {'_string': ['']} self.assertTrue(t_u.run_test(before, after, i)) # 2 before = {'_string': ['A'], '_char': [Character('A')]} after = {'_string': ['']} self.assertTrue(t_u.run_test(before, after, i)) # 3 before = {'_string': ['B'], '_char': [Character('A')]} after = {'_string': ['B']} self.assertTrue(t_u.run_test(before, after, i)) # 4 before = {'_string': ['AA'], '_char': [Character('A')]} after = {'_string': ['']} self.assertTrue(t_u.run_test(before, after, i)) # 5 before = {'_string': ['AB'], '_char': [Character('A')]} after = {'_string': ['B']} self.assertTrue(t_u.run_test(before, after, i))
def test_char_all_from_string(self): i = '_char_all_from_string' # 1 before = {'_string': ['abc']} after = {'_char': [Character('c'), Character('b'), Character('a')]} self.assertTrue(t_u.run_test(before, after, i)) # 2 before = {'_string': ['a \n']} after = {'_char': [Character('\n'), Character(' '), Character('a')]} self.assertTrue(t_u.run_test(before, after, i)) # 3 before = {'_string': ['']} after = {'_char': []} self.assertTrue(t_u.run_test(before, after, i))
def error_function(program, debug=False): errors = [] for case in training_set: interpreter = PushInterpreter() interpreter.run(program, [case[0]], [], debug) result = interpreter.state.stdout if result is None: errors.append(1e5) else: errors.append(levenshtein_distance(case[1], result)) return errors atom_generators = list(merge_sets( get_instructions_by_pysh_type("_integer"), get_instructions_by_pysh_type("_boolean"), get_instructions_by_pysh_type("_string"), get_instructions_by_pysh_type("_char"), get_instructions_by_pysh_type("_exec"), get_instructions_by_pysh_type("_print"), [lambda: Character('!')] )) if __name__ == "__main__": evo = SimplePushGPEvolver(n_jobs=-1, verbose=1, atom_generators=atom_generators, initial_max_genome_size=400, max_generations=300) evo.fit(error_function, 1, [])
def test_string_from_char(self): i = '_string_from_char' # 1 before = {'_char': [Character('e')]} after = {'_string': ['e']} self.assertTrue(t_u.run_test(before, after, i))