Exercism: Go version of the 'Clock' exercise.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.1 KiB

  1. package clock
  2. // Source: exercism/x-common
  3. // Commit: 269f498 Merge pull request #48 from soniakeys/custom-set-json
  4. // Test creating a new clock with an initial time.
  5. var timeTests = []struct {
  6. h, m int
  7. want string
  8. }{
  9. {8, 0, "08:00"}, // on the hour
  10. {9, 0, "09:00"}, // on the hour
  11. {11, 9, "11:09"}, // past the hour
  12. {11, 19, "11:19"}, // past the hour
  13. {24, 0, "00:00"}, // midnight is zero hours
  14. {25, 0, "01:00"}, // hour rolls over
  15. {1, 60, "02:00"}, // sixty minutes is next hour
  16. {0, 160, "02:40"}, // minutes roll over
  17. {25, 160, "03:40"}, // hour and minutes roll over
  18. {-1, 15, "23:15"}, // negative hour
  19. {-25, 0, "23:00"}, // negative hour rolls over
  20. {1, -40, "00:20"}, // negative minutes
  21. {1, -160, "22:20"}, // negative minutes roll over
  22. {-25, -160, "20:20"}, // negative hour and minutes both roll over
  23. {-25, -1500, "22:00"},
  24. {-49, -3000, "21:00"},
  25. }
  26. // Test adding and subtracting minutes.
  27. var addTests = []struct {
  28. h, m, a int
  29. want string
  30. }{
  31. {10, 0, 3, "10:03"}, // add minutes
  32. {0, 45, 40, "01:25"}, // add to next hour
  33. {10, 0, 61, "11:01"}, // add more than one hour
  34. {23, 59, 2, "00:01"}, // add across midnight
  35. {5, 32, 1500, "06:32"}, // add more than one day (1500 min = 25 hrs)
  36. {0, 45, 160, "03:25"}, // add more than two hours with carry
  37. {10, 3, -3, "10:00"}, // subtract minutes
  38. {10, 3, -30, "09:33"}, // subtract to previous hour
  39. {10, 3, -70, "08:53"}, // subtract more than an hour
  40. {0, 3, -4, "23:59"}, // subtract across midnight
  41. {0, 0, -160, "21:20"}, // subtract more than two hours
  42. {5, 32, -1500, "04:32"}, // subtract more than one day (1500 min = 25 hrs)
  43. {6, 15, -160, "03:35"}, // subtract more than two hours with borrow
  44. }
  45. // Construct two separate clocks, set times, test if they are equal.
  46. type hm struct{ h, m int }
  47. var eqTests = []struct {
  48. c1, c2 hm
  49. want bool
  50. }{
  51. // clocks with same time
  52. {
  53. hm{15, 37},
  54. hm{15, 37},
  55. true,
  56. },
  57. // clocks a minute apart
  58. {
  59. hm{15, 36},
  60. hm{15, 37},
  61. false,
  62. },
  63. // clocks an hour apart
  64. {
  65. hm{14, 37},
  66. hm{15, 37},
  67. false,
  68. },
  69. // clocks set 24 hours apart
  70. {
  71. hm{10, 37},
  72. hm{34, 37},
  73. true,
  74. },
  75. }