def test_add_with_tuple(self): expected_result = State(3, 1, 4) a = State(1, 0, 1) b = (2, 1, 3) result = a + b self.assertEqual(expected_result, result)
def test_subtract(self): expected_result = State(2, 2, 0) a = State(3, 3, 1) b = State(1, 1, 1) result = a - b self.assertEqual(expected_result, result)
def test_result_on_second_transition(self): expected_result = State(3, 2, 1) state = State(2, 2, 0) action = (1, 0, 1) result = self.problem.result(state, action) self.assertEqual(expected_result, result)
def test_result_on_initial_transition(self): expected_result = State(2, 2, 0) state = State(3, 3, 1) action = (1, 1, 1) result = self.problem.result(state, action) self.assertEqual(expected_result, result)
def test_expand(self): next_nodes = self.root_node.expand(self.problem) exp_nodes = [ Node(State((3, 2, 0)), self.root_node, self.actions[0]), Node(State((3, 1, 0)), self.root_node, self.actions[1]), Node(State((2, 2, 0)), self.root_node, self.actions[2]) ] self.assertEqual(3, len(next_nodes)) for i, nxt_node in enumerate(next_nodes): self.assertEqual(exp_nodes[i], nxt_node)
def test_goal_is(self): node = Node(State((0, 0, 0))) node_is_goal = self.problem.goal_is(node.state) self.assertTrue(node_is_goal)
def test_result(self): actions = self.problem.actions(self.root_node) result = self.problem.result(self.root_node, actions[0]) exp_node = Node(State((3, 2, 0)), self.root_node, actions[0]) self.assertEqual(exp_node, result)
def test_equal(self): a = State(3, 3, 1) b = State(3, 3, 1) self.assertEqual(a, b)
def test_less_than_raises_value_error_with_invalid_operand(self): a = State(3, 3, 0) b = (3, 3, 1) with self.assertRaises(ValueError): a < b
def test_actions_from_second_state(self): expected_actions = {(1, 0, 1), (0, 2, 1), (1, 1, 1)} actions = self.problem.actions(State(2, 2, 0)) self.assertEqual(expected_actions, actions)
def test_missionaries_are_safe(self): exp_valid = [ State((3, 0, 1)), State((3, 0, 0)), State((0, 3, 1)), State((0, 3, 0)), State((2, 2, 1)), State((2, 2, 0)), State((1, 1, 1)), State((1, 1, 0)), State((3, 2, 1)), State((3, 2, 0)), State((0, 0, 0)) ] exp_invalid = [ State((-1, -1, -1)), State((2, -3, 2)), State((2, 3, 2)), State((2, 3, 1)), State((1, 3, 0)), State((1, 2, 1)), State((4, 2, 1)), State((1, 4, 1)), State((1, 2, -1)), State((1, 2, -1)), State((2, 1, 1)) # other (right) side is invalid ] for state in exp_valid: self.assertTrue(state.is_valid, 'state should be valid. state: {}'.format(state)) for state in exp_invalid: self.assertFalse( state.is_valid, 'state should be invalid. state: {}'.format(state))
def test_is_state_valid_with_valid_states(self): more_missionaries_than_cannibals = State(3, 2, 0) equal_number_of_missionaries_and_cannibals = State(2, 2, 0) self.assertTrue(more_missionaries_than_cannibals.is_valid()) self.assertTrue(equal_number_of_missionaries_and_cannibals.is_valid())
def test_add_raises_value_error_with_invalid_operand(self): a = State(1, 0, 1) b = "invalid operand" with self.assertRaises(ValueError): a + b
def test_is_state_valid_with_invalid_states(self): contains_negative_number = State(0, -1, 0) more_cannibals_than_missionaries_on_wrong_side1 = State(1, 3, 0) more_cannibals_than_missionaries_on_wrong_side2 = State(2, 3, 0) more_cannibals_than_missionaries_on_wrong_side3 = State(1, 2, 0) more_cannibals_than_missionaries_on_right_side1 = State(2, 1, 0) more_cannibals_than_missionaries_on_right_side2 = State(1, 0, 0) more_than_one_boat = State(3, 2, 2) more_cannibals_than_initial_state = State(4, 3, 1) more_missionaries_than_initial_state = State(3, 4, 1) self.assertFalse(contains_negative_number.is_valid()) self.assertFalse( more_cannibals_than_missionaries_on_wrong_side1.is_valid()) self.assertFalse( more_cannibals_than_missionaries_on_wrong_side2.is_valid()) self.assertFalse( more_cannibals_than_missionaries_on_wrong_side3.is_valid()) self.assertFalse( more_cannibals_than_missionaries_on_right_side1.is_valid()) self.assertFalse( more_cannibals_than_missionaries_on_right_side2.is_valid()) self.assertFalse(more_than_one_boat.is_valid()) self.assertFalse(more_cannibals_than_initial_state.is_valid()) self.assertFalse(more_missionaries_than_initial_state.is_valid())
def test_child_node(self): child = self.root_node.child_node(self.problem, self.actions[0]) exp_child = Node(State((3, 2, 0)), self.root_node, self.actions[0]) self.assertEqual(exp_child, child)
def test_subtract_raises_value_error_with_invalid_operand(self): a = State(3, 3, 1) b = "invalid operand" with self.assertRaises(ValueError): a - b
def test_missionaries_are_safe(self): exp_safe = [ State((3, 0, 1)), State((3, 0, 0)), State((0, 3, 1)), State((0, 3, 0)), State((2, 2, 1)), State((2, 2, 0)), State((1, 1, 1)), State((1, 1, 0)), State((3, 2, 1)), State((3, 2, 0)) ] exp_unsafe = [ State((1, 2, 1)), State((1, 2, 0)), State((2, 3, 1)), State((2, 3, 0)) ] for state in exp_safe: self.assertTrue( state.missionaries_are_safe, 'missionaries should be safe. state: {}'.format(state)) for state in exp_unsafe: self.assertFalse( state.missionaries_are_safe, 'missionaries should be unsafe. state: {}'.format(state))
def test_less_than(self): a = State(3, 3, 0) b = State(3, 3, 1) self.assertTrue(a < b)
def setUp(self): self.state = State((3, 3, 1)) self.actions = [(1, 0, 1), (2, 0, 1), (0, 1, 1), (0, 2, 1), (1, 1, 1)]
def test_actions_from_initial_state(self): expected_actions = {(0, 1, 1), (1, 1, 1), (0, 2, 1)} actions = self.problem.actions(State(3, 3, 1)) self.assertEqual(expected_actions, actions)