Untitled
unknown
python
2 years ago
8.8 kB
5
Indexable
import pytest
from hw3 import Candy, Person, Kid, Host, FluxCapacitor, choose_candy_from_host
def test_get_kid_closest_host_same_distance():
kid = Kid((0, 0), 2)
host_1 = Host((5, 0), [])
host_2 = Host((0, 5), [(19, 1)])
host_3 = Host((-3, 0), [])
host_4 = Host((0, -3), [])
flux = FluxCapacitor({kid, host_1, host_2, host_3, host_4})
closest_host_to_kid = flux._get_next_host_to_visit(kid)
assert closest_host_to_kid is not None
assert isinstance(closest_host_to_kid, Host)
assert closest_host_to_kid.get_position() == (-3, 0)
def test_get_uranium_from_candy():
candy_mass = 10
uranium = 0.5
expected = candy_mass * uranium
assert Candy(candy_mass, uranium).get_uranium_quantity() == expected
assert Candy(0, uranium).get_uranium_quantity() == 0.0
assert Candy(candy_mass, 0).get_uranium_quantity() == 0.0
def test_get_mass():
candy_mass = 9.8
assert Candy(candy_mass, 0.3).get_mass() == candy_mass
def test_choose_candy_from_host():
last_candy = Candy(987, 0.11)
candy_bag = [Candy(1.4, 0.3), Candy(0.0, 0.9), Candy(0.0, 0.0), last_candy]
chosen_candy = choose_candy_from_host(candy_bag)
assert chosen_candy == last_candy
def test_person_get_position():
pos = (5.3, 8.9)
person = Person(pos)
assert person.get_position() == pos
def test_set_position_person():
pos = (0.0, 9.99)
person = Person(pos)
new_pos = (9.99999, 0)
person.set_position(new_pos)
assert person.get_position() == new_pos
def test_kid_parent_type():
kid = Kid((0.5, 2), 12)
assert isinstance(kid, Person)
def test_kid_get_position():
pos = (5.3, 9)
kid = Kid(pos, 10)
assert kid.get_position() == pos
def test_set_kid_position():
kid = Kid((1, 1), 1)
new_pos = (3, 7)
kid.set_position(new_pos)
assert kid.get_position() == new_pos
def test_get_initiative():
initiative = 1
kid = Kid((1, 2), initiative)
assert kid.get_initiative() == initiative
def test_kid_change_position():
kid = Kid((1, 1), 2)
new_pos = (6, 8)
kid.set_position(new_pos)
assert kid.get_position() == new_pos
def test_kid_add_candy():
kid = Kid((1, 2), 3)
kid._candy_bag = [Candy(1.4, 0.3), Candy(0.0, 0.9), Candy(0.0, 0.0), Candy(987, 0.11)]
candy = Candy(10, 1)
kid.add_candy(candy)
assert kid._candy_bag[-1] == candy and len(kid._candy_bag) == 5
def test_radiation_of_kid_after_added_candy():
kid = Kid((1, 2), 3)
assert kid._current_radiation == 0
candy = Candy(10, 1)
kid.add_candy(candy)
assert kid._current_radiation == 10
def test_is_critical():
kid = Kid((1, 2), 3)
kid._current_radiation = 21
assert kid.is_critical() is True
kid._current_radiation = 20
assert kid.is_critical() is False
kid._current_radiation = 0
kid.add_candy(Candy(21, 1))
assert kid.is_critical() is True
def test_host_get_pos():
host = Host((0, 3), [(19, 0.5), (2, 0.3)])
assert host.get_position() == (0, 3)
def test_host_candy_bag():
host = Host((0, 3), [(19, 0.5), (2, 0.3)])
assert len(host.candy_bag) == 2
assert host.candy_bag[0].get_mass() == 2 and host.candy_bag[1].get_mass() == 19
def test_host_remove_candy():
host = Host((0, 3), [(19, 0.5), (2, 0.3)])
assert len(host.candy_bag) == 2
candy_to_be_given = host.remove_candy(choose_candy_from_host)
assert candy_to_be_given is not None
assert len(host.candy_bag) == 1
assert host.candy_bag[0].get_mass() == 2
def test_flux_separate_kids_and_hosts():
kid_1 = Kid((1, 1), 2)
kid_2 = Kid((5, 5), 4)
kid_3 = Kid((10, 10), 6)
host_1 = Host((4, 4), [(19, 1), (11, 0.5)])
host_2 = Host((12, 12), [(2, 1)])
participants = set()
participants.add(kid_1)
participants.add(kid_2)
participants.add(kid_3)
participants.add(host_1)
participants.add(host_2)
flux = FluxCapacitor(participants)
assert len(flux._get_kids()) == 3
assert len(flux._get_hosts()) == 2
def test_get_kid_closest_host():
kid = Kid((1, 1), 2)
host_1 = Host((1, 2), [])
host_2 = Host((10, 12), [(19, 1)])
participants = set()
participants.add(kid)
participants.add(host_1)
participants.add(host_2)
flux = FluxCapacitor(participants)
closest_host_to_kid = flux._get_next_host_to_visit(kid)
assert closest_host_to_kid is not None
assert isinstance(closest_host_to_kid, Host)
assert closest_host_to_kid.get_position() == (1, 2)
def test_get_kids_that_will_visit_host():
kid_1 = Kid((1, 1), 2)
kid_2 = Kid((3, 3), 7)
kid_3 = Kid((2, 2), 10)
kid_4 = Kid((10, 56), 9)
host_1 = Host((2, 2), [(3, 1), (10, 0.5)])
host_2 = Host((10, 55), [(2, 1)])
flux = FluxCapacitor({kid_1, kid_2, kid_3, kid_4, host_1, host_2})
kids_to_visit_host_1 = flux._get_kids_that_will_visit(host_1)
assert kids_to_visit_host_1 is not None and isinstance(kids_to_visit_host_1, list)
assert len(kids_to_visit_host_1) == 3
def test_give_poisonous_candy():
kid_1 = Kid((1, 1), 2)
kid_2 = Kid((3, 3), 7)
kid_3 = Kid((2, 2), 10)
kid_4 = Kid((10, 56), 9)
kid_5 = Kid((11, 60), 2)
host_1 = Host((2, 2), [(3, 1), (42, 0.5)])
host_2 = Host((10, 55), [(21, 1)])
print(kid_1, kid_2, kid_3, kid_4, kid_5)
flux = FluxCapacitor({kid_1, kid_2, kid_3, kid_4, kid_5, host_1, host_2})
print(flux._get_kids_that_will_visit(host_1))
assert len(flux._victims) == 0
for host in flux._hosts:
assert len(flux._get_kids_that_will_visit(host)) > 0 and isinstance(flux._get_kids_that_will_visit(host), list)
flux._give_poisonous_candy(host)
assert len(flux._victims) >= 2
assert kid_4 in flux._victims
assert kid_3 in flux._victims
# def test_get_kid_closest_host_same_distance():
# kid = Kid((0, 0), 2)
# host_1 = Host((5, 0), [])
# host_2 = Host((0, 5), [(19, 1)])
# flux = FluxCapacitor({kid, host_2, host_1})
# closest_host_to_kid = flux._get_next_host_to_visit(kid)
# assert closest_host_to_kid is not None
# assert isinstance(closest_host_to_kid, Host)
# assert closest_host_to_kid.get_position() == (0, 5)
def test_get_victims():
kid_1 = Kid((1, 1), 2)
kid_2 = Kid((3, 3), 7)
kid_3 = Kid((2, 2), 10)
kid_4 = Kid((10, 56), 9)
kid_5 = Kid((11, 60), 2)
host_1 = Host((2, 2), [(3, 1), (42, 0.5)])
host_2 = Host((10, 55), [(21, 1)])
flux = FluxCapacitor({kid_1, kid_2, kid_3, kid_4, kid_5, host_1, host_2})
assert flux.get_victim() == {kid_4, kid_3}
def test_get_kid_closest_host_same_dist_same_x():
kid = Kid((0, 0), 2)
host_1 = Host((5, 10), [])
host_2 = Host((5, -10), [(19, 1)])
participants = set()
participants.add(kid)
participants.add(host_1)
participants.add(host_2)
flux = FluxCapacitor(participants)
closest_host_to_kid = flux._get_next_host_to_visit(kid)
assert closest_host_to_kid is not None
assert isinstance(closest_host_to_kid, Host)
assert closest_host_to_kid.get_position() == (5, -10)
def test_get_victim_two_neighbors_same_distance():
kid = Kid((1, 1), 6)
host_1 = Host((0, 3), [(19, 1)])
host_2 = Host((2, 3), [(2, 1)])
flux = FluxCapacitor({kid, host_1, host_2})
assert flux._kids == {kid}
assert flux._hosts == {host_1, host_2}
assert flux._get_kids_that_will_visit(host_1)
result_kid = flux.get_victim()
assert result_kid == {kid}
assert result_kid.pop().get_position() == (2, 3)
def test_get_victim_two_neighbors_same_distance_and_same_x_value():
kid = Kid((0, 0), 6)
host_1 = Host((2, 2), [(2, 1)])
host_2 = Host((2, -2), [(19, 1)])
flux = FluxCapacitor({kid, host_1, host_2})
result_kid = flux.get_victim()
assert result_kid == {kid}
assert result_kid.pop().get_position() == (2, 2)
def test_get_victim_multiple_victims():
kid_1 = Kid((1, 2), 6)
kid_2 = Kid((3, 4), 7)
kid_3 = Kid((5, 6), 8)
host_1 = Host((3, 0), [(21, 1)])
host_2 = Host((2, 5), [(21, 1)])
host_3 = Host((7, 7), [(21, 1)])
flux = FluxCapacitor({kid_1, kid_2, kid_3, host_1, host_2, host_3})
assert flux.get_victim() == {kid_1, kid_2, kid_3}
def test_get_victim_no_candies():
kid_1 = Kid((1, 2), 6)
kid_2 = Kid((3, 4), 7)
kid_3 = Kid((5, 6), 8)
host_1 = Host((3, 0), [])
host_2 = Host((2, 5), [])
host_3 = Host((7, 7), [])
flux = FluxCapacitor({kid_1, kid_2, kid_3, host_1, host_2, host_3})
assert flux.get_victim() is None
Editor is loading...