diff options
Diffstat (limited to 'rust/clock/tests/clock.rs')
| -rw-r--r-- | rust/clock/tests/clock.rs | 294 |
1 files changed, 294 insertions, 0 deletions
diff --git a/rust/clock/tests/clock.rs b/rust/clock/tests/clock.rs new file mode 100644 index 0000000..04f5241 --- /dev/null +++ b/rust/clock/tests/clock.rs | |||
| @@ -0,0 +1,294 @@ | |||
| 1 | use clock::Clock; | ||
| 2 | |||
| 3 | // | ||
| 4 | // Clock Creation | ||
| 5 | // | ||
| 6 | |||
| 7 | #[test] | ||
| 8 | fn test_on_the_hour() { | ||
| 9 | assert_eq!(Clock::new(8, 0).to_string(), "08:00"); | ||
| 10 | } | ||
| 11 | |||
| 12 | #[test] | ||
| 13 | fn test_past_the_hour() { | ||
| 14 | assert_eq!(Clock::new(11, 9).to_string(), "11:09"); | ||
| 15 | } | ||
| 16 | |||
| 17 | #[test] | ||
| 18 | fn test_midnight_is_zero_hours() { | ||
| 19 | assert_eq!(Clock::new(24, 0).to_string(), "00:00"); | ||
| 20 | } | ||
| 21 | |||
| 22 | #[test] | ||
| 23 | fn test_hour_rolls_over() { | ||
| 24 | assert_eq!(Clock::new(25, 0).to_string(), "01:00"); | ||
| 25 | } | ||
| 26 | |||
| 27 | #[test] | ||
| 28 | fn test_hour_rolls_over_continuously() { | ||
| 29 | assert_eq!(Clock::new(100, 0).to_string(), "04:00"); | ||
| 30 | } | ||
| 31 | |||
| 32 | #[test] | ||
| 33 | fn test_sixty_minutes_is_next_hour() { | ||
| 34 | assert_eq!(Clock::new(1, 60).to_string(), "02:00"); | ||
| 35 | } | ||
| 36 | |||
| 37 | #[test] | ||
| 38 | fn test_minutes_roll_over() { | ||
| 39 | assert_eq!(Clock::new(0, 160).to_string(), "02:40"); | ||
| 40 | } | ||
| 41 | |||
| 42 | #[test] | ||
| 43 | fn test_minutes_roll_over_continuously() { | ||
| 44 | assert_eq!(Clock::new(0, 1723).to_string(), "04:43"); | ||
| 45 | } | ||
| 46 | |||
| 47 | #[test] | ||
| 48 | fn test_hours_and_minutes_roll_over() { | ||
| 49 | assert_eq!(Clock::new(25, 160).to_string(), "03:40"); | ||
| 50 | } | ||
| 51 | |||
| 52 | #[test] | ||
| 53 | fn test_hours_and_minutes_roll_over_continuously() { | ||
| 54 | assert_eq!(Clock::new(201, 3001).to_string(), "11:01"); | ||
| 55 | } | ||
| 56 | |||
| 57 | #[test] | ||
| 58 | fn test_hours_and_minutes_roll_over_to_exactly_midnight() { | ||
| 59 | assert_eq!(Clock::new(72, 8640).to_string(), "00:00"); | ||
| 60 | } | ||
| 61 | |||
| 62 | #[test] | ||
| 63 | fn test_negative_hour() { | ||
| 64 | assert_eq!(Clock::new(-1, 15).to_string(), "23:15"); | ||
| 65 | } | ||
| 66 | |||
| 67 | #[test] | ||
| 68 | fn test_negative_hour_roll_over() { | ||
| 69 | assert_eq!(Clock::new(-25, 00).to_string(), "23:00"); | ||
| 70 | } | ||
| 71 | |||
| 72 | #[test] | ||
| 73 | fn test_negative_hour_roll_over_continuously() { | ||
| 74 | assert_eq!(Clock::new(-91, 00).to_string(), "05:00"); | ||
| 75 | } | ||
| 76 | |||
| 77 | #[test] | ||
| 78 | fn test_negative_minutes() { | ||
| 79 | assert_eq!(Clock::new(1, -40).to_string(), "00:20"); | ||
| 80 | } | ||
| 81 | |||
| 82 | #[test] | ||
| 83 | fn test_negative_minutes_roll_over() { | ||
| 84 | assert_eq!(Clock::new(1, -160).to_string(), "22:20"); | ||
| 85 | } | ||
| 86 | |||
| 87 | #[test] | ||
| 88 | fn test_negative_minutes_roll_over_continuously() { | ||
| 89 | assert_eq!(Clock::new(1, -4820).to_string(), "16:40"); | ||
| 90 | } | ||
| 91 | |||
| 92 | #[test] | ||
| 93 | fn test_negative_sixty_minutes_is_prev_hour() { | ||
| 94 | assert_eq!(Clock::new(2, -60).to_string(), "01:00"); | ||
| 95 | } | ||
| 96 | |||
| 97 | #[test] | ||
| 98 | fn test_negative_hour_and_minutes_both_roll_over() { | ||
| 99 | assert_eq!(Clock::new(-25, -160).to_string(), "20:20"); | ||
| 100 | } | ||
| 101 | |||
| 102 | #[test] | ||
| 103 | fn test_negative_hour_and_minutes_both_roll_over_continuously() { | ||
| 104 | assert_eq!(Clock::new(-121, -5810).to_string(), "22:10"); | ||
| 105 | } | ||
| 106 | |||
| 107 | #[test] | ||
| 108 | fn test_zero_hour_and_negative_minutes() { | ||
| 109 | assert_eq!(Clock::new(0, -22).to_string(), "23:38"); | ||
| 110 | } | ||
| 111 | |||
| 112 | // | ||
| 113 | // Clock Math | ||
| 114 | // | ||
| 115 | |||
| 116 | #[test] | ||
| 117 | fn test_add_minutes() { | ||
| 118 | let clock = Clock::new(10, 0).add_minutes(3); | ||
| 119 | assert_eq!(clock.to_string(), "10:03"); | ||
| 120 | } | ||
| 121 | |||
| 122 | #[test] | ||
| 123 | fn test_add_no_minutes() { | ||
| 124 | let clock = Clock::new(6, 41).add_minutes(0); | ||
| 125 | assert_eq!(clock.to_string(), "06:41"); | ||
| 126 | } | ||
| 127 | |||
| 128 | #[test] | ||
| 129 | fn test_add_to_next_hour() { | ||
| 130 | let clock = Clock::new(0, 45).add_minutes(40); | ||
| 131 | assert_eq!(clock.to_string(), "01:25"); | ||
| 132 | } | ||
| 133 | |||
| 134 | #[test] | ||
| 135 | fn test_add_more_than_one_hour() { | ||
| 136 | let clock = Clock::new(10, 0).add_minutes(61); | ||
| 137 | assert_eq!(clock.to_string(), "11:01"); | ||
| 138 | } | ||
| 139 | |||
| 140 | #[test] | ||
| 141 | fn test_add_more_than_two_hours_with_carry() { | ||
| 142 | let clock = Clock::new(0, 45).add_minutes(160); | ||
| 143 | assert_eq!(clock.to_string(), "03:25"); | ||
| 144 | } | ||
| 145 | |||
| 146 | #[test] | ||
| 147 | fn test_add_across_midnight() { | ||
| 148 | let clock = Clock::new(23, 59).add_minutes(2); | ||
| 149 | assert_eq!(clock.to_string(), "00:01"); | ||
| 150 | } | ||
| 151 | |||
| 152 | #[test] | ||
| 153 | fn test_add_more_than_one_day() { | ||
| 154 | let clock = Clock::new(5, 32).add_minutes(1500); | ||
| 155 | assert_eq!(clock.to_string(), "06:32"); | ||
| 156 | } | ||
| 157 | |||
| 158 | #[test] | ||
| 159 | fn test_add_more_than_two_days() { | ||
| 160 | let clock = Clock::new(1, 1).add_minutes(3500); | ||
| 161 | assert_eq!(clock.to_string(), "11:21"); | ||
| 162 | } | ||
| 163 | |||
| 164 | #[test] | ||
| 165 | fn test_subtract_minutes() { | ||
| 166 | let clock = Clock::new(10, 3).add_minutes(-3); | ||
| 167 | assert_eq!(clock.to_string(), "10:00"); | ||
| 168 | } | ||
| 169 | |||
| 170 | #[test] | ||
| 171 | fn test_subtract_to_previous_hour() { | ||
| 172 | let clock = Clock::new(10, 3).add_minutes(-30); | ||
| 173 | assert_eq!(clock.to_string(), "09:33"); | ||
| 174 | } | ||
| 175 | |||
| 176 | #[test] | ||
| 177 | fn test_subtract_more_than_an_hour() { | ||
| 178 | let clock = Clock::new(10, 3).add_minutes(-70); | ||
| 179 | assert_eq!(clock.to_string(), "08:53"); | ||
| 180 | } | ||
| 181 | |||
| 182 | #[test] | ||
| 183 | fn test_subtract_across_midnight() { | ||
| 184 | let clock = Clock::new(0, 3).add_minutes(-4); | ||
| 185 | assert_eq!(clock.to_string(), "23:59"); | ||
| 186 | } | ||
| 187 | |||
| 188 | #[test] | ||
| 189 | fn test_subtract_more_than_two_hours() { | ||
| 190 | let clock = Clock::new(0, 0).add_minutes(-160); | ||
| 191 | assert_eq!(clock.to_string(), "21:20"); | ||
| 192 | } | ||
| 193 | |||
| 194 | #[test] | ||
| 195 | fn test_subtract_more_than_two_hours_with_borrow() { | ||
| 196 | let clock = Clock::new(6, 15).add_minutes(-160); | ||
| 197 | assert_eq!(clock.to_string(), "03:35"); | ||
| 198 | } | ||
| 199 | |||
| 200 | #[test] | ||
| 201 | fn test_subtract_more_than_one_day() { | ||
| 202 | let clock = Clock::new(5, 32).add_minutes(-1500); | ||
| 203 | assert_eq!(clock.to_string(), "04:32"); | ||
| 204 | } | ||
| 205 | |||
| 206 | #[test] | ||
| 207 | fn test_subtract_mores_than_two_days() { | ||
| 208 | let clock = Clock::new(2, 20).add_minutes(-3000); | ||
| 209 | assert_eq!(clock.to_string(), "00:20"); | ||
| 210 | } | ||
| 211 | |||
| 212 | // | ||
| 213 | // Test Equality | ||
| 214 | // | ||
| 215 | |||
| 216 | #[test] | ||
| 217 | fn test_compare_clocks_for_equality() { | ||
| 218 | assert_eq!(Clock::new(15, 37), Clock::new(15, 37)); | ||
| 219 | } | ||
| 220 | |||
| 221 | #[test] | ||
| 222 | fn test_compare_clocks_a_minute_apart() { | ||
| 223 | assert_ne!(Clock::new(15, 36), Clock::new(15, 37)); | ||
| 224 | } | ||
| 225 | |||
| 226 | #[test] | ||
| 227 | fn test_compare_clocks_an_hour_apart() { | ||
| 228 | assert_ne!(Clock::new(14, 37), Clock::new(15, 37)); | ||
| 229 | } | ||
| 230 | |||
| 231 | #[test] | ||
| 232 | fn test_compare_clocks_with_hour_overflow() { | ||
| 233 | assert_eq!(Clock::new(10, 37), Clock::new(34, 37)); | ||
| 234 | } | ||
| 235 | |||
| 236 | #[test] | ||
| 237 | fn test_compare_clocks_with_hour_overflow_by_several_days() { | ||
| 238 | assert_eq!(Clock::new(3, 11), Clock::new(99, 11)); | ||
| 239 | } | ||
| 240 | |||
| 241 | #[test] | ||
| 242 | fn test_compare_clocks_with_negative_hour() { | ||
| 243 | assert_eq!(Clock::new(22, 40), Clock::new(-2, 40)); | ||
| 244 | } | ||
| 245 | |||
| 246 | #[test] | ||
| 247 | fn test_compare_clocks_with_negative_hour_that_wraps() { | ||
| 248 | assert_eq!(Clock::new(17, 3), Clock::new(-31, 3)); | ||
| 249 | } | ||
| 250 | |||
| 251 | #[test] | ||
| 252 | fn test_compare_clocks_with_negative_hour_that_wraps_multiple_times() { | ||
| 253 | assert_eq!(Clock::new(13, 49), Clock::new(-83, 49)); | ||
| 254 | } | ||
| 255 | |||
| 256 | #[test] | ||
| 257 | fn test_compare_clocks_with_minutes_overflow() { | ||
| 258 | assert_eq!(Clock::new(0, 1), Clock::new(0, 1441)); | ||
| 259 | } | ||
| 260 | |||
| 261 | #[test] | ||
| 262 | fn test_compare_clocks_with_minutes_overflow_by_several_days() { | ||
| 263 | assert_eq!(Clock::new(2, 2), Clock::new(2, 4322)); | ||
| 264 | } | ||
| 265 | |||
| 266 | #[test] | ||
| 267 | fn test_compare_clocks_with_negative_minute() { | ||
| 268 | assert_eq!(Clock::new(2, 40), Clock::new(3, -20)) | ||
| 269 | } | ||
| 270 | |||
| 271 | #[test] | ||
| 272 | fn test_compare_clocks_with_negative_minute_that_wraps() { | ||
| 273 | assert_eq!(Clock::new(4, 10), Clock::new(5, -1490)) | ||
| 274 | } | ||
| 275 | |||
| 276 | #[test] | ||
| 277 | fn test_compare_clocks_with_negative_minute_that_wraps_multiple() { | ||
| 278 | assert_eq!(Clock::new(6, 15), Clock::new(6, -4305)) | ||
| 279 | } | ||
| 280 | |||
| 281 | #[test] | ||
| 282 | fn test_compare_clocks_with_negative_hours_and_minutes() { | ||
| 283 | assert_eq!(Clock::new(7, 32), Clock::new(-12, -268)) | ||
| 284 | } | ||
| 285 | |||
| 286 | #[test] | ||
| 287 | fn test_compare_clocks_with_negative_hours_and_minutes_that_wrap() { | ||
| 288 | assert_eq!(Clock::new(18, 7), Clock::new(-54, -11_513)) | ||
| 289 | } | ||
| 290 | |||
| 291 | #[test] | ||
| 292 | fn test_compare_full_clock_and_zeroed_clock() { | ||
| 293 | assert_eq!(Clock::new(24, 0), Clock::new(0, 0)) | ||
| 294 | } | ||
