Skip to content

Commit

Permalink
Merge pull request #259 from FppEpitech/feat/write-unit-tests-to-eval…
Browse files Browse the repository at this point in the history
…uate-our-ai-algorithm-zappy-ai

Write unit tests to evaluate our AI algorithm - Zappy AI
  • Loading branch information
Marius-P1 authored Jun 23, 2024
2 parents ed816f3 + c4ef9eb commit 41b2e46
Show file tree
Hide file tree
Showing 6 changed files with 913 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ai/src/Player/Inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __eq__(self, inventory):
if self.food == inventory.food and self.linemate == inventory.linemate and self.deraumere == inventory.deraumere and self.sibur == inventory.sibur and self.mendiane == inventory.mendiane and self.phiras == inventory.phiras and self.thystame == inventory.thystame and self.player == inventory.player:
return True
return False


def __add__(self, inventory):
"""
Expand Down
1 change: 1 addition & 0 deletions ai/src/Player/Player.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,7 @@ def chooseAction(self, teamName : str, myuuid : str, creationTime : int):
if self.inventory.food <= 1 and self.isLeader == Role.LEADER:
self.broadcast(random.choice(self.alliesUuid), teamName, myuuid, creationTime)
self.currentMode = Mode.DYING
return
if self.isLeader == Role.LEADER:
for msg in self.broadcastReceived:
if msg[1].message == "IsLeader?":
Expand Down
13 changes: 12 additions & 1 deletion tests/ai/tests/Network/TestAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def testAPI():
print("\tTests APIException constructor: ", end="")
testAPIExceptionConstructor()
print("✅")
print("\tTests APIException getFileName method: ", end="")
testAPIExceptionGetFileName()
print("✅")
except Exception as _:
print("❌")
raise Exception("Test failed")
Expand All @@ -33,7 +36,15 @@ def testAPIConstructor():

def testAPIExceptionConstructor():
try:
api_exception = APIException("test")
api_exception = APIException("test", "arzteryutiyuo")
assert api_exception.message == "APIException: test"
assert api_exception.fileName == "arzteryutiyuo"
except Exception as _:
raise Exception("Test failed")

def testAPIExceptionGetFileName():
try:
api_exception = APIException("test", "arzteryutiyuo")
assert api_exception.getFileName() == "arzteryutiyuo"
except Exception as _:
raise Exception("Test failed")
63 changes: 63 additions & 0 deletions tests/ai/tests/Player/TestInventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def testInventory():
print("\tTests Inventory __str__ method: ", end="")
testInventoryString()
print("✅")
print("\tTests Inventory toStr method: ", end="")
testInventoryToStr()
print("✅")
print("\tTests Inventory equals operator: ", end="")
testInventoryEquals()
print("✅")
Expand All @@ -30,6 +33,15 @@ def testInventory():
print("\tTests Inventory removeAnObject method: ", end="")
testInventoryRemoveAnObject()
print("✅")
print("\tTests Inventory __add__ method: ", end="")
testInventoryAdd()
print("✅")
print("\tTests Inventory hasMoreStones method: ", end="")
testInventoryHasMoreStones()
print("✅")
print("\tTests Inventory countStones method: ", end="")
testInventoryCountStones()
print("✅")
except Exception as _:
print("❌")
raise Exception("Test failed")
Expand All @@ -54,6 +66,13 @@ def testInventoryString():
except Exception as _:
raise Exception("Test failed")

def testInventoryToStr():
try:
inventory = Inventory()
assert inventory.toStr() == "[food 10, linemate 0, deraumere 0, sibur 0, mendiane 0, phiras 0, thystame 0, player 0]"
except Exception as _:
raise Exception("Test failed")

def testInventoryEquals():
try:
inventory1 = Inventory()
Expand All @@ -75,6 +94,10 @@ def testInventoryUpdateInventory():
assert inventory.mendiane == 8
assert inventory.phiras == 9
assert inventory.thystame == 10
assert inventory.player == 0
inventory.updateInventory("[player 1 ]")
assert inventory.player == 1
inventory.updateInventory("[azeazeaz]")
except Exception as _:
raise Exception("Test failed")

Expand Down Expand Up @@ -110,6 +133,7 @@ def testInventoryAddAnObject():
assert inventory.phiras == 1
inventory.addAnObject("thystame")
assert inventory.thystame == 1
inventory.addAnObject("player")
except Exception as _:
raise Exception("Test failed")

Expand All @@ -130,5 +154,44 @@ def testInventoryRemoveAnObject():
assert inventory.phiras == 0
inventory.removeAnObject("thystame")
assert inventory.thystame == 0
inventory.removeAnObject("player")
except Exception as _:
raise Exception("Test failed")

def testInventoryAdd():
try:
inventory1 = Inventory(1, 1, 1, 1, 1, 1, 1)
inventory2 = Inventory(1, 1, 1, 1, 1, 1, 1)
inventory3 = inventory1 + inventory2
assert inventory3.food == 2
assert inventory3.linemate == 2
assert inventory3.deraumere == 2
assert inventory3.sibur == 2
assert inventory3.mendiane == 2
assert inventory3.phiras == 2
assert inventory3.thystame == 2
except Exception as _:
raise Exception("Test failed")

def testInventoryHasMoreStones():
try:
inventory1 = Inventory(1, 1, 1, 1, 1, 1, 1)
inventory2 = Inventory(1, 1, 1, 1, 1, 1, 1)
assert inventory1.hasMoreStones(inventory2) == False
inventory3 = Inventory(10, 10, 10, 10, 10, 10, 10)
assert inventory1.hasMoreStones(inventory3) == False
assert inventory3.hasMoreStones(inventory1) == True
inventory3.linemate = 0
assert inventory1.hasMoreStones(inventory3) == False
assert inventory3.hasMoreStones(inventory1) == False
except Exception as _:
raise Exception("Test failed")

def testInventoryCountStones():
try:
inventory = Inventory(1, 1, 1, 1, 1, 1, 1)
assert inventory.countStones() == 6
inventory = Inventory(0, 0, 0, 0, 0, 0, 0)
assert inventory.countStones() == 0
except Exception as _:
raise Exception("Test failed")
Loading

0 comments on commit 41b2e46

Please sign in to comment.