Skip to content

Commit

Permalink
test: Add a new test suite for TSPGenerator with comprehensive covera…
Browse files Browse the repository at this point in the history
…ge and validation checks
  • Loading branch information
knakamura13 committed Aug 13, 2024
1 parent 70f3909 commit c1bc209
Showing 1 changed file with 61 additions and 4 deletions.
65 changes: 61 additions & 4 deletions tests/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,65 @@ def test_generate_custom_size(self):

assert problem.length == size

def test_generate_maximize(self):
"""Test generate method with maximize set to True."""
problem = QueensGenerator.generate(SEED, maximize=True)
# noinspection PyTypeChecker
class TestTSPGenerator:

def test_generate_invalid_seed(self):
"""Test generate method raises ValueError when seed is not an integer."""
with pytest.raises(ValueError) as excinfo:
TSPGenerator.generate(seed="not_an_int", number_of_cities=5)
assert str(excinfo.value) == "Seed must be an integer. Got not_an_int"

def test_generate_invalid_number_of_cities(self):
"""Test generate method raises ValueError when number_of_cities is not a positive integer."""
with pytest.raises(ValueError) as excinfo:
TSPGenerator.generate(SEED, number_of_cities=0)
assert str(excinfo.value) == "Number of cities must be a positive integer. Got 0"

def test_generate_invalid_area_width(self):
"""Test generate method raises ValueError when area_width is not a positive integer."""
with pytest.raises(ValueError) as excinfo:
TSPGenerator.generate(SEED, number_of_cities=5, area_width=0)
assert str(excinfo.value) == "Area width must be a positive integer. Got 0"

assert problem.maximize == 1.0
def test_generate_invalid_area_height(self):
"""Test generate method raises ValueError when area_height is not a positive integer."""
with pytest.raises(ValueError) as excinfo:
TSPGenerator.generate(SEED, number_of_cities=5, area_height=0)
assert str(excinfo.value) == "Area height must be a positive integer. Got 0"

def test_generate_default_parameters(self):
"""Test generate method with default parameters."""
problem = TSPGenerator.generate(seed=SEED, number_of_cities=5)
assert problem.length == 5
assert problem.coords is not None
assert problem.distances is not None
assert problem.source_graph is not None

def test_generate_custom_parameters(self):
"""Test generate method with custom parameters."""
problem = TSPGenerator.generate(seed=SEED, number_of_cities=5, area_width=100, area_height=100)
assert problem.length == 5
assert problem.coords is not None
assert problem.distances is not None
assert problem.source_graph is not None

def test_generate_no_duplicate_coordinates(self):
"""Test generate method ensures no duplicate coordinates."""
problem = TSPGenerator.generate(seed=SEED, number_of_cities=5)
coords = problem.coords
assert len(coords) == len(set(coords))

def test_generate_distances(self):
"""Test generate method calculates distances correctly."""
problem = TSPGenerator.generate(seed=SEED, number_of_cities=5)
distances = problem.distances
for u, v, d in distances:
assert d == np.linalg.norm(np.subtract(problem.coords[u], problem.coords[v]))

def test_generate_graph(self):
"""Test generate method creates a valid graph."""
problem = TSPGenerator.generate(seed=SEED, number_of_cities=5)
graph = problem.source_graph
assert graph.number_of_nodes() == 5
assert graph.number_of_edges() == len(problem.distances)

0 comments on commit c1bc209

Please sign in to comment.