"""Pruebas de la cuenta del RTT y del RTO en rto_table.py. No necesitan la red."""
import unittest

from rto_table import first_sample, next_sample, rto, table


class Estimator(unittest.TestCase):
    def test_exercise_of_the_guide(self):
        estimated, deviation = next_sample(100, 5, 120)
        self.assertAlmostEqual(estimated, 102.5)
        self.assertAlmostEqual(deviation, 8.75)
        self.assertAlmostEqual(rto(estimated, deviation), 137.5)

    def test_deviation_uses_the_previous_estimate(self):
        # Con el EstimatedRTT ya actualizado saldría 8,125 y no 8,75.
        _, deviation = next_sample(100, 5, 120)
        self.assertNotAlmostEqual(deviation, 0.75 * 5 + 0.25 * abs(102.5 - 120))

    def test_first_sample_initializes(self):
        self.assertEqual(first_sample(80), (80, 40))
        self.assertAlmostEqual(rto(*first_sample(80)), 240)

    def test_steady_samples_bring_the_rto_down_to_the_rtt(self):
        last = table([40.0] * 60)[-1]
        self.assertAlmostEqual(last[1], 40, places=3)
        self.assertLess(last[3], 41)

    def test_one_slow_sample_moves_the_rto_more_than_the_estimate(self):
        rows = table([40.0] * 60 + [200.0])
        self.assertLess(rows[-1][1] - rows[-2][1], rows[-1][3] - rows[-2][3])


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