import unittest import writer class TestLineChooser(unittest.TestCase): def test_simulated_annealing(self): chooser = writer.LineChooserSimulatedAnnealing() chooser.now = lambda *args: 100 for name, c in ({ "fresh something": [ writer.LineChooserSimulatedAnnealing.initial_health, chooser.now(), ], "old something": [ writer.LineChooserSimulatedAnnealing.initial_health - writer.LineChooserSimulatedAnnealing.decay_rate * 5, chooser.now() - 5, ], "fresh nothing": [ writer.LineChooserSimulatedAnnealing.initial_health - writer.LineChooserSimulatedAnnealing.nothing_penalty, chooser.now(), ], }).items(): self.assertEqual( c[0], chooser.health(self.new_line(True, c[1], not "nothing" in name)), name, ) def test_latest_sticky(self): stale_something = self.new_line(False, 1, 1) stale_nothing = self.new_line(False, 1, None) old_something = self.new_line(True, 2, 2) old_nothing = self.new_line(True, 2, None) new_something = self.new_line(True, 3, 3) new_nothing = self.new_line(True, 3, None) chooser = writer.LineChooserLatestSticky() for name, c in ({ "nothing over stale": [new_nothing, stale_something], "slightly old over nothing": [old_something, new_nothing], "new and nonzero": [new_something, old_something], "new over nothing": [new_something, old_nothing], "new over stale nothing": [new_something, stale_nothing], }).items(): self.assertEqual( c[0], chooser.choose(c[0], c[1]), name, ) def new_line(self, is_recent, t, v): result = writer.Line(v) result.t = t result.is_recent = lambda *args: is_recent return result if __name__ == "__main__": unittest.main()