Skip to content

Commit

Permalink
Set up passing test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Iain committed Jan 22, 2020
1 parent 13636e6 commit 08631a0
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion tests/test_anneal.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import math
import random
import sys
import time

from helper import distance, cities, distance_matrix
from simanneal import Annealer

if sys.version_info.major >= 3: # pragma: no cover
from io import StringIO
else:
from StringIO import StringIO


class TravellingSalesmanProblem(Annealer):
"""Test annealer with a travelling salesman problem.
Expand Down Expand Up @@ -99,3 +105,26 @@ def test_load_state_init(tmpdir):
tsp2 = TravellingSalesmanProblem(distance_matrix, load_state=statefile)
assert tsp.state == tsp2.state


def test_default_update_formatting():
init_state = list(cities.keys())
tsp = TravellingSalesmanProblem(distance_matrix, initial_state=init_state)

# fix the start time and patch time.time() to give predictable Elapsed and Remaining times
tsp.start = 1.0
time.time = lambda: 9.0

# for step=0, the output should be column headers followed by partial data
sys.stderr = StringIO()
tsp.default_update(0, 1, 2, 3, 4)
output = sys.stderr.getvalue().split('\n')
assert len(output) == 2
assert output[0] == ' Temperature Energy Accept Improve Elapsed Remaining'
assert output[1] == '\r 1.00000 2.00 0:00:08 \r'

# when step>0, default_update should use \r to overwrite the previous data
sys.stderr = StringIO()
tsp.default_update(10, 1, 2, 3, 4)
output = sys.stderr.getvalue().split('\n')
assert len(output) == 1
assert output[0] == '\r 1.00000 2.00 300.00% 400.00% 0:00:08 11:06:32\r\r'

0 comments on commit 08631a0

Please sign in to comment.