def test_branch(self): # program: convert an odd number to string 'odd', # an even number to 'even' prog = m.Program( [m.IF(odd), const_odd, m.ELSE, const_even, m.ENDIF, m.RETURN_TRUE]) self.assertEqualData(Data(id, 'even'), prog.run(Data(id, 0))) self.assertEqualData(Data(id, 'odd'), prog.run(Data(id, 1)))
def prog(self, condition=None): RETURN_MAP = { True: m.RETURN_TRUE, False: m.RETURN_FALSE, None: m.RETURN_TRUE } prog = m.Program( [Noop, RETURN_MAP[condition], Noop, Noop, m.RETURN_TRUE]) prog.runner.ensure_statistics(1) return prog
def test_multiple_use_of_instructions(self): # program: convert an odd number to string 'odd', # an even number to 'even' prog = m.Program([ odd, m.IF(odd), odd, const_even, const_odd, m.ELSE, odd, const_odd, const_even, m.ENDIF, m.RETURN_TRUE ]) self.assertEqualData(Data(id, 'even'), prog.run(Data(id, 0))) self.assertEqualData(Data(id, 'odd'), prog.run(Data(id, 1)))
def test_rule_branch(self): # program: add 2 to value if odd, else leave it prog = m.Program([ m.IF(increase_if_odd), add1, m.ENDIF, m.RETURN_TRUE, ]) self.assertEqualData(Data(id, 0), prog.run(Data(id, 0))) self.assertEqualData(Data(id, 3), prog.run(Data(id, 1)))
def test_rule(self): prog = m.Program([add1, m.RETURN_TRUE]) self.assertEqualData(Data(id, 1), prog.run(Data(id, 0))) self.assertEqualData(Data(id, 2), prog.run(Data(id, 1)))
def die_prog(self): prog = m.Program([die, m.RETURN_TRUE]) prog.runner.ensure_statistics(1) return prog
def test_statistics_created(self): prog = m.Program([Noop, m.RETURN_TRUE, Noop, Noop, m.RETURN_TRUE]) prog.run(None) self.assertEqual(2, len(prog.statistics))
def program(self): return m.Program(self.visualized_program_spec)