Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.55 KB | None | 0 0
  1. import unittest
  2.  
  3. from clock import Clock
  4.  
  5.  
  6. class ClockTest(unittest.TestCase):
  7. # Test creating a new clock with an initial time.
  8. def test_on_the_hour(self):
  9. self.assertEqual('08:00', str(Clock(8, 0)))
  10.  
  11. def test_past_the_hour(self):
  12. self.assertEqual('11:09', str(Clock(11, 9)))
  13.  
  14. def test_midnight_is_zero_hours(self):
  15. self.assertEqual('00:00', str(Clock(24, 0)))
  16.  
  17. def test_hour_rolls_over(self):
  18. self.assertEqual('01:00', str(Clock(25, 0)))
  19.  
  20. def test_hour_rolls_over_continuously(self):
  21. self.assertEqual('04:00', str(Clock(100, 0)))
  22.  
  23. def test_sixty_minutes_is_next_hour(self):
  24. self.assertEqual('02:00', str(Clock(1, 60)))
  25.  
  26. def test_minutes_roll_over(self):
  27. self.assertEqual('02:40', str(Clock(0, 160)))
  28.  
  29. def test_minutes_roll_over_continuously(self):
  30. self.assertEqual('04:43', str(Clock(0, 1723)))
  31.  
  32. def test_hour_and_minutes_roll_over(self):
  33. self.assertEqual('03:40', str(Clock(25, 160)))
  34.  
  35. def test_hour_and_minutes_roll_over_continuously(self):
  36. self.assertEqual('11:01', str(Clock(201, 3001)))
  37.  
  38. def test_hour_and_minutes_roll_over_to_exactly_midnight(self):
  39. self.assertEqual('00:00', str(Clock(72, 8640)))
  40.  
  41. def test_negative_hour(self):
  42. self.assertEqual('23:15', str(Clock(-1, 15)))
  43.  
  44. def test_negative_hour_rolls_over(self):
  45. self.assertEqual('23:00', str(Clock(-25, 0)))
  46.  
  47. def test_negative_hour_rolls_over_continuously(self):
  48. self.assertEqual('05:00', str(Clock(-91, 0)))
  49.  
  50. def test_negative_minutes(self):
  51. self.assertEqual('00:20', str(Clock(1, -40)))
  52.  
  53. def test_negative_minutes_roll_over(self):
  54. self.assertEqual('22:20', str(Clock(1, -160)))
  55.  
  56. def test_negative_minutes_roll_over_continuously(self):
  57. self.assertEqual('16:40', str(Clock(1, -4820)))
  58.  
  59. def test_negative_hour_and_minutes_both_roll_over(self):
  60. self.assertEqual('20:20', str(Clock(-25, -160)))
  61.  
  62. def test_negative_hour_and_minutes_both_roll_over_continuously(self):
  63. self.assertEqual('22:10', str(Clock(-121, -5810)))
  64.  
  65. # Test adding and subtracting minutes.
  66. def test_add_minutes(self):
  67. self.assertEqual('10:03', str(Clock(10, 0).add(3)))
  68.  
  69. def test_add_no_minutes(self):
  70. self.assertEqual('06:41', str(Clock(6, 41).add(0)))
  71.  
  72. def test_add_to_next_hour(self):
  73. self.assertEqual('01:25', str(Clock(0, 45).add(40)))
  74.  
  75. def test_add_more_than_one_hour(self):
  76. self.assertEqual('11:01', str(Clock(10, 0).add(61)))
  77.  
  78. def test_add_more_than_two_hours_with_carry(self):
  79. self.assertEqual('03:25', str(Clock(0, 45).add(160)))
  80.  
  81. def test_add_across_midnight(self):
  82. self.assertEqual('00:01', str(Clock(23, 59).add(2)))
  83.  
  84. def test_add_more_than_one_day(self):
  85. self.assertEqual('06:32', str(Clock(5, 32).add(1500)))
  86.  
  87. def test_add_more_than_two_days(self):
  88. self.assertEqual('11:21', str(Clock(1, 1).add(3500)))
  89.  
  90. def test_subtract_minutes(self):
  91. self.assertEqual('10:00', str(Clock(10, 3).add(-3)))
  92.  
  93. def test_subtract_to_previous_hour(self):
  94. self.assertEqual('10:00', str(Clock(10, 3).add(-3)))
  95.  
  96. def test_subtract_more_than_an_hour(self):
  97. self.assertEqual('09:33', str(Clock(10, 3).add(-30)))
  98.  
  99. def test_subtract_across_midnight(self):
  100. self.assertEqual('08:53', str(Clock(10, 3).add(-70)))
  101.  
  102. def test_subtract_more_than_two_hours(self):
  103. self.assertEqual('21:20', str(Clock(0, 0).add(-160)))
  104.  
  105. def test_subtract_more_than_two_hours_with_borrow(self):
  106. self.assertEqual('03:35', str(Clock(6, 15).add(-160)))
  107.  
  108. def test_subtract_more_than_one_day(self):
  109. self.assertEqual('04:32', str(Clock(5, 32).add(-1500)))
  110.  
  111. def test_subtract_more_than_two_days(self):
  112. self.assertEqual('00:20', str(Clock(2, 20).add(-3000)))
  113.  
  114. # Construct two separate clocks, set times, test if they are equal.
  115. def test_clocks_with_same_time(self):
  116. self.assertEqual(Clock(15, 37), Clock(15, 37))
  117.  
  118. def test_clocks_a_minute_apart(self):
  119. self.assertNotEqual(Clock(15, 36), Clock(15, 37))
  120.  
  121. def test_clocks_an_hour_apart(self):
  122. self.assertNotEqual(Clock(14, 37), Clock(15, 37))
  123.  
  124. def test_clocks_with_hour_overflow(self):
  125. self.assertNotEqual(Clock(10, 37), Clock(34, 37))
  126.  
  127. def test_clocks_with_hour_overflow_by_several_days(self):
  128. self.assertEqual(Clock(3, 11), Clock(99, 11))
  129.  
  130. def test_clocks_a_minute_apart(self):
  131. self.assertNotEqual(Clock(15, 36), Clock(15, 37))
  132.  
  133. def test_clocks_an_hour_apart(self):
  134. self.assertNotEqual(Clock(14, 37), Clock(15, 37))
  135.  
  136. def test_clocks_with_hour_overflow(self):
  137. self.assertEqual(Clock(10, 37), Clock(34, 37))
  138.  
  139. def test_clocks_with_hour_overflow_by_several_days(self):
  140. self.assertEqual(Clock(3, 11), Clock(99, 11))
  141.  
  142. def test_clocks_with_negative_hour(self):
  143. self.assertEqual(Clock(22, 40), Clock(-2, 40))
  144.  
  145. def test_clocks_with_negative_hour_that_wraps(self):
  146. self.assertEqual(Clock(17, 3), Clock(-31, 3))
  147.  
  148. def test_clocks_with_negative_hour_that_wraps_multiple_times(self):
  149. self.assertEqual(Clock(13, 49), Clock(-83, 49))
  150.  
  151. def test_clocks_with_minute_overflow(self):
  152. self.assertEqual(Clock(0, 1), Clock(0, 1441))
  153.  
  154. def test_clocks_with_minute_overflow_by_several_days(self):
  155. self.assertEqual(Clock(2, 2), Clock(2, 4322))
  156.  
  157. def test_clocks_with_negative_minute(self):
  158. self.assertEqual(Clock(2, 40), Clock(3, -20))
  159.  
  160. def test_clocks_with_negative_minute_that_wraps(self):
  161. self.assertEqual(Clock(4, 10), Clock(5, -1490))
  162.  
  163. def test_clocks_with_negative_minute_that_wraps_multiple_times(self):
  164. self.assertEqual(Clock(6, 15), Clock(6, -4305))
  165.  
  166. def test_clocks_with_negative_hours_and_minutes(self):
  167. self.assertEqual(Clock(7, 32), Clock(-12, -268))
  168.  
  169. def test_clocks_with_negative_hours_and_minutes_that_wrap(self):
  170. self.assertEqual(Clock(18, 7), Clock(-54, -11513))
  171.  
  172. if __name__ == '__main__':
  173. unittest.main()
  174.  
  175. class Clock:
  176. def __init__(self, h, m):
  177. self._hour = h
  178. self._min = m
  179.  
  180. while self._min >= 60:
  181. self._min -= 60
  182. self._hour += 1
  183.  
  184. if self._hour > 23:
  185. self._hour = self._hour % 24
  186.  
  187. while self._min < 0:
  188. self._min += 60
  189. self._hour -= 1
  190.  
  191. if self._hour < 0:
  192. self._hour = self._hour % 24
  193.  
  194.  
  195.  
  196. def __str__(self):
  197. if self._hour < 10:
  198. hour_str = '0' + str(self._hour)
  199. else:
  200. hour_str = str(self._hour)
  201. if self._min < 10:
  202. min_str = '0' + str(self._min)
  203. else:
  204. min_str = str(self._min)
  205.  
  206. return hour_str + ":" + min_str
  207.  
  208. def __eq__(self, other):
  209. if self._hour != other._hour: return False
  210. if self._min != other._min: return False
  211. return True
  212.  
  213. def add(self, m):
  214. tot_min = (self._hour * 60) + self._min
  215. tot_min += m
  216. self._hour = (tot_min // 60) % 24
  217. self._min = tot_min % 60
  218.  
  219. def getHour(self):
  220. return self._hour
  221.  
  222. def getMinute(self):
  223. return self._min
  224.  
  225. ======================================================================
  226. FAIL: test_subtract_to_previous_hour (__main__.ClockTest)
  227. ----------------------------------------------------------------------
  228. Traceback (most recent call last):
  229. File "/Users/aaron/exercism/python/clock/clock_test.py", line 94, in test_subtract_to_previous_hour
  230. self.assertEqual('10:00', str(Clock(10, 3).add(-3)))
  231. AssertionError: '10:00' != 'None'
  232. - 10:00
  233. + None
  234.  
  235. Clock(0, 45).add(40)
  236.  
  237. cl = Clock(0, 45)
  238. cl.add(40)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement