2016-06-21 20:06:14 -07:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
from clock import Clock
|
|
|
|
|
|
2017-10-25 23:40:22 -07:00
|
|
|
|
2018-08-29 09:40:04 -04:00
|
|
|
# Tests adapted from `problem-specifications//canonical-data.json` @ v2.3.0
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
class ClockTest(unittest.TestCase):
|
|
|
|
|
# Test creating a new clock with an initial time.
|
|
|
|
|
def test_on_the_hour(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(8, 0)), '08:00')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_past_the_hour(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(11, 9)), '11:09')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_midnight_is_zero_hours(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(24, 0)), '00:00')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_hour_rolls_over(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(25, 0)), '01:00')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_hour_rolls_over_continuously(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(100, 0)), '04:00')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_sixty_minutes_is_next_hour(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(1, 60)), '02:00')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_minutes_roll_over(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(0, 160)), '02:40')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_minutes_roll_over_continuously(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(0, 1723)), '04:43')
|
2016-06-21 20:29:06 -07:00
|
|
|
|
2016-06-21 20:06:14 -07:00
|
|
|
def test_hour_and_minutes_roll_over(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(25, 160)), '03:40')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_hour_and_minutes_roll_over_continuously(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(201, 3001)), '11:01')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_hour_and_minutes_roll_over_to_exactly_midnight(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(72, 8640)), '00:00')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_negative_hour(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(-1, 15)), '23:15')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_negative_hour_rolls_over(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(-25, 0)), '23:00')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_negative_hour_rolls_over_continuously(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(-91, 0)), '05:00')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_negative_minutes(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(1, -40)), '00:20')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_negative_minutes_roll_over(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(1, -160)), '22:20')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_negative_minutes_roll_over_continuously(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(1, -4820)), '16:40')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_negative_hour_and_minutes_both_roll_over(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(-25, -160)), '20:20')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_negative_hour_and_minutes_both_roll_over_continuously(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(str(Clock(-121, -5810)), '22:10')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
# Test adding and subtracting minutes.
|
|
|
|
|
def test_add_minutes(self):
|
2017-10-21 11:29:33 -05:00
|
|
|
self.assertEqual(str(Clock(10, 0) + 3), '10:03')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_add_no_minutes(self):
|
2017-10-21 11:29:33 -05:00
|
|
|
self.assertEqual(str(Clock(6, 41) + 0), '06:41')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_add_to_next_hour(self):
|
2017-10-21 11:29:33 -05:00
|
|
|
self.assertEqual(str(Clock(0, 45) + 40), '01:25')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_add_more_than_one_hour(self):
|
2017-10-21 11:29:33 -05:00
|
|
|
self.assertEqual(str(Clock(10, 0) + 61), '11:01')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_add_more_than_two_hours_with_carry(self):
|
2017-10-21 11:29:33 -05:00
|
|
|
self.assertEqual(str(Clock(0, 45) + 160), '03:25')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_add_across_midnight(self):
|
2017-10-21 11:29:33 -05:00
|
|
|
self.assertEqual(str(Clock(23, 59) + 2), '00:01')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_add_more_than_one_day(self):
|
2017-10-21 11:29:33 -05:00
|
|
|
self.assertEqual(str(Clock(5, 32) + 1500), '06:32')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_add_more_than_two_days(self):
|
2017-10-21 11:29:33 -05:00
|
|
|
self.assertEqual(str(Clock(1, 1) + 3500), '11:21')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_subtract_minutes(self):
|
2018-02-21 22:41:42 +08:00
|
|
|
self.assertEqual(str(Clock(10, 3) - 3), '10:00')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_subtract_to_previous_hour(self):
|
2018-02-21 22:41:42 +08:00
|
|
|
self.assertEqual(str(Clock(10, 3) - 30), '09:33')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_subtract_more_than_an_hour(self):
|
2018-02-21 22:41:42 +08:00
|
|
|
self.assertEqual(str(Clock(10, 3) - 70), '08:53')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_subtract_across_midnight(self):
|
2018-02-21 22:41:42 +08:00
|
|
|
self.assertEqual(str(Clock(0, 3) - 4), '23:59')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_subtract_more_than_two_hours(self):
|
2018-02-21 22:41:42 +08:00
|
|
|
self.assertEqual(str(Clock(0, 0) - 160), '21:20')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_subtract_more_than_two_hours_with_borrow(self):
|
2018-02-21 22:41:42 +08:00
|
|
|
self.assertEqual(str(Clock(6, 15) - 160), '03:35')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_subtract_more_than_one_day(self):
|
2018-02-21 22:41:42 +08:00
|
|
|
self.assertEqual(str(Clock(5, 32) - 1500), '04:32')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
def test_subtract_more_than_two_days(self):
|
2018-02-21 22:41:42 +08:00
|
|
|
self.assertEqual(str(Clock(2, 20) - 3000), '00:20')
|
2016-06-21 20:06:14 -07:00
|
|
|
|
|
|
|
|
# Construct two separate clocks, set times, test if they are equal.
|
|
|
|
|
def test_clocks_with_same_time(self):
|
|
|
|
|
self.assertEqual(Clock(15, 37), Clock(15, 37))
|
|
|
|
|
|
|
|
|
|
def test_clocks_a_minute_apart(self):
|
|
|
|
|
self.assertNotEqual(Clock(15, 36), Clock(15, 37))
|
|
|
|
|
|
|
|
|
|
def test_clocks_an_hour_apart(self):
|
|
|
|
|
self.assertNotEqual(Clock(14, 37), Clock(15, 37))
|
|
|
|
|
|
|
|
|
|
def test_clocks_with_hour_overflow(self):
|
|
|
|
|
self.assertEqual(Clock(10, 37), Clock(34, 37))
|
|
|
|
|
|
|
|
|
|
def test_clocks_with_hour_overflow_by_several_days(self):
|
|
|
|
|
self.assertEqual(Clock(3, 11), Clock(99, 11))
|
|
|
|
|
|
|
|
|
|
def test_clocks_with_negative_hour(self):
|
|
|
|
|
self.assertEqual(Clock(22, 40), Clock(-2, 40))
|
|
|
|
|
|
|
|
|
|
def test_clocks_with_negative_hour_that_wraps(self):
|
|
|
|
|
self.assertEqual(Clock(17, 3), Clock(-31, 3))
|
|
|
|
|
|
|
|
|
|
def test_clocks_with_negative_hour_that_wraps_multiple_times(self):
|
|
|
|
|
self.assertEqual(Clock(13, 49), Clock(-83, 49))
|
|
|
|
|
|
|
|
|
|
def test_clocks_with_minute_overflow(self):
|
|
|
|
|
self.assertEqual(Clock(0, 1), Clock(0, 1441))
|
|
|
|
|
|
|
|
|
|
def test_clocks_with_minute_overflow_by_several_days(self):
|
|
|
|
|
self.assertEqual(Clock(2, 2), Clock(2, 4322))
|
|
|
|
|
|
|
|
|
|
def test_clocks_with_negative_minute(self):
|
|
|
|
|
self.assertEqual(Clock(2, 40), Clock(3, -20))
|
|
|
|
|
|
|
|
|
|
def test_clocks_with_negative_minute_that_wraps(self):
|
|
|
|
|
self.assertEqual(Clock(4, 10), Clock(5, -1490))
|
|
|
|
|
|
|
|
|
|
def test_clocks_with_negative_minute_that_wraps_multiple_times(self):
|
|
|
|
|
self.assertEqual(Clock(6, 15), Clock(6, -4305))
|
|
|
|
|
|
|
|
|
|
def test_clocks_with_negative_hours_and_minutes(self):
|
|
|
|
|
self.assertEqual(Clock(7, 32), Clock(-12, -268))
|
|
|
|
|
|
|
|
|
|
def test_clocks_with_negative_hours_and_minutes_that_wrap(self):
|
|
|
|
|
self.assertEqual(Clock(18, 7), Clock(-54, -11513))
|
|
|
|
|
|
2018-08-29 09:40:04 -04:00
|
|
|
def test_full_clock_and_zeroed_clock(self):
|
|
|
|
|
self.assertEqual(Clock(24, 0), Clock(0, 0))
|
|
|
|
|
|
2016-11-29 09:44:47 +01:00
|
|
|
|
2016-06-21 20:06:14 -07:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|