def test_happy_path(self): f = UpdateInput() entry = f( Environment( {"interpreter_state": BatchedState({}, {}, [], ["xxx\nyyy"])})) assert entry["code"] == "xxx\nyyy" state = BatchedState({}, {Diff([]): ["foo"]}, [Diff([])], ["foo"]) entry = f(Environment({"interpreter_state": state})) assert entry["code"] == "foo"
def create_state(self, inputs: List[str]) -> BatchedState[AST, str, str, str]: return BatchedState( type_environment={}, environment={}, history=[], context=inputs, )
def test_ast_set_sample(self): asts = ["c0", "c1", "c2"] sampler = SequentialProgramSampler(MockSynthesizer(asts), transform_input, Collate(), MockEncoder(), MockExpander(), MockInterpreter()) zero = SamplerState(0, sampler.initialize([(None, None)])) samples = list(sampler.batch_k_samples([zero], [3])) samples.sort(key=lambda x: -x.state.score) assert 3 == len(samples) assert samples[0] == DuplicatedSamplerState( SamplerState( 1, Environment({ "test_cases": [(None, None)], "reference": [Token(None, str(asts[0]), str(asts[0]))], "variables": [["#" + str(asts[0])]], "interpreter_state": BatchedState({str(asts[0]): None}, {str(asts[0]): ["#" + str(asts[0])]}, [str(asts[0])], ["#" + str(asts[0])]) })), 1) assert DuplicatedSamplerState( SamplerState( 0.5, Environment({ "test_cases": [(None, None)], "reference": [Token(None, str(asts[1]), str(asts[1]))], "variables": [["#" + str(asts[1])]], "interpreter_state": BatchedState({str(asts[1]): None}, {str(asts[1]): ["#" + str(asts[1])]}, [str(asts[1])], ["#" + str(asts[1])]) })), 1) == samples[1] assert DuplicatedSamplerState( SamplerState( 1.0 / 3, Environment({ "test_cases": [(None, None)], "reference": [Token(None, str(asts[2]), str(asts[2]))], "variables": [["#" + str(asts[2])]], "interpreter_state": BatchedState({str(asts[2]): None}, {str(asts[2]): ["#" + str(asts[2])]}, [str(asts[2])], ["#" + str(asts[2])]) })), 1) == samples[2]
def create_state(self, inputs: List[None]) \ -> BatchedState[AST, np.ndarray, str, None]: return BatchedState( type_environment={}, environment={}, history=[], context=inputs, )
def test_create_output(self): sampler = SequentialProgramSampler(MockSynthesizer([]), transform_input, Collate(), MockEncoder(), MockExpander(), MockInterpreter()) assert sampler.create_output( None, Environment({ "interpreter_state": BatchedState({}, {}, [], [None]) })) is None assert sampler.create_output( None, Environment( {"interpreter_state": BatchedState({}, {}, ["tmp"], [None])})) == ("tmp", False) assert sampler.create_output( None, Environment({ "interpreter_state": BatchedState({}, {}, ["line0", "line1"], [None]) })) == ("line0\nline1", False)
def test_execute(self): ref0 = Insert(0, "foo") ref1 = Replace(1, "test") state = BatchedState({}, {}, [], ["bar\nhoge"]) interpreter = Interpreter() state = interpreter.execute(ref0, state) assert state.history == [ref0] assert set(state.environment.keys()) == set([ref0]) assert state.type_environment[ref0] == "Insert" assert state.environment[ref0][0] == "foo\nbar\nhoge" assert state.context[0] == "foo\nbar\nhoge" state = interpreter.execute(ref1, state) assert state.history == [ref0, ref1] assert set(state.environment.keys()) == set([ref1]) assert state.type_environment[ref1] == "Replace" assert state.environment[ref1][0] == "foo\ntest\nhoge" assert state.context[0] == "foo\ntest\nhoge"
def create_state(self, inputs): return BatchedState({}, {}, [], inputs)