Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ImogenBits committed Oct 26, 2023
1 parent 590466b commit e58c078
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
"""Tests for all util functions."""
from math import inf
import unittest

from algobattle.battle import Battle, Iterated, Averaged
from algobattle.problem import InstanceModel, SolutionModel, default_score
from algobattle.util import Role


class DummyInstance(InstanceModel): # noqa: D101
@property
def size(self) -> int:
return 1


class DummySolution(SolutionModel[DummyInstance]): # noqa: D101
val: float

def score(self, instance: DummyInstance, role: Role) -> float:
return self.val


class Utiltests(unittest.TestCase):
Expand All @@ -12,6 +28,35 @@ def test_default_battle_types(self):
self.assertEqual(Battle.all()["Iterated"], Iterated)
self.assertEqual(Battle.all()["Averaged"], Averaged)

def test_default_fight_score(self):
"""Tests the default fight scoring function."""
instance = DummyInstance()
scores = [
(0, 0, 1),
(0, 2, 1),
(0, 4, 1),
(0, inf, 1),
(2, 0, 0),
(2, 2, 1),
(2, 4, 1),
(2, inf, 1),
(4, 0, 0),
(4, 2, 0.5),
(4, 4, 1),
(4, inf, 1),
(inf, 0, 0),
(inf, 2, 0),
(inf, 4, 0),
(inf, inf, 1),
]
for gen, sol, score in scores:
self.assertEqual(
default_score(
instance, generator_solution=DummySolution(val=gen), solver_solution=DummySolution(val=sol)
),
score,
)


if __name__ == "__main__":
unittest.main()

0 comments on commit e58c078

Please sign in to comment.