diff options
| author | Federico Igne <git@federicoigne.com> | 2021-11-03 18:45:47 +0000 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2021-11-03 18:55:08 +0000 |
| commit | 921a9ff1bd17c991739a8d3b5b25e65235b49bf5 (patch) | |
| tree | 935e89dee398706a01ff1c3d56bdbf3d8e2d13f6 /rust/bowling/tests/bowling.rs | |
| parent | fc1d4fbb98323b34829da9393f366fb07d2f1605 (diff) | |
| download | exercism-921a9ff1bd17c991739a8d3b5b25e65235b49bf5.tar.gz exercism-921a9ff1bd17c991739a8d3b5b25e65235b49bf5.zip | |
[rust] Bowling
Diffstat (limited to 'rust/bowling/tests/bowling.rs')
| -rw-r--r-- | rust/bowling/tests/bowling.rs | 418 |
1 files changed, 418 insertions, 0 deletions
diff --git a/rust/bowling/tests/bowling.rs b/rust/bowling/tests/bowling.rs new file mode 100644 index 0000000..92f646f --- /dev/null +++ b/rust/bowling/tests/bowling.rs | |||
| @@ -0,0 +1,418 @@ | |||
| 1 | use bowling::*; | ||
| 2 | |||
| 3 | #[test] | ||
| 4 | fn roll_returns_a_result() { | ||
| 5 | let mut game = BowlingGame::new(); | ||
| 6 | assert!(game.roll(0).is_ok()); | ||
| 7 | } | ||
| 8 | |||
| 9 | #[test] | ||
| 10 | fn you_cannot_roll_more_than_ten_pins_in_a_single_roll() { | ||
| 11 | let mut game = BowlingGame::new(); | ||
| 12 | |||
| 13 | assert_eq!(game.roll(11), Err(Error::NotEnoughPinsLeft)); | ||
| 14 | } | ||
| 15 | |||
| 16 | #[test] | ||
| 17 | fn a_game_score_is_some_if_ten_frames_have_been_rolled() { | ||
| 18 | let mut game = BowlingGame::new(); | ||
| 19 | |||
| 20 | for _ in 0..10 { | ||
| 21 | let _ = game.roll(0); | ||
| 22 | let _ = game.roll(0); | ||
| 23 | } | ||
| 24 | |||
| 25 | assert!(game.score().is_some()); | ||
| 26 | } | ||
| 27 | |||
| 28 | #[test] | ||
| 29 | fn you_cannot_score_a_game_with_no_rolls() { | ||
| 30 | let game = BowlingGame::new(); | ||
| 31 | |||
| 32 | assert_eq!(game.score(), None); | ||
| 33 | } | ||
| 34 | |||
| 35 | #[test] | ||
| 36 | fn a_game_score_is_none_if_fewer_than_ten_frames_have_been_rolled() { | ||
| 37 | let mut game = BowlingGame::new(); | ||
| 38 | |||
| 39 | for _ in 0..9 { | ||
| 40 | let _ = game.roll(0); | ||
| 41 | let _ = game.roll(0); | ||
| 42 | } | ||
| 43 | |||
| 44 | assert_eq!(game.score(), None); | ||
| 45 | } | ||
| 46 | |||
| 47 | #[test] | ||
| 48 | fn a_roll_is_err_if_the_game_is_done() { | ||
| 49 | let mut game = BowlingGame::new(); | ||
| 50 | |||
| 51 | for _ in 0..10 { | ||
| 52 | let _ = game.roll(0); | ||
| 53 | let _ = game.roll(0); | ||
| 54 | } | ||
| 55 | |||
| 56 | assert_eq!(game.roll(0), Err(Error::GameComplete)); | ||
| 57 | } | ||
| 58 | |||
| 59 | #[test] | ||
| 60 | fn twenty_zero_pin_rolls_scores_zero() { | ||
| 61 | let mut game = BowlingGame::new(); | ||
| 62 | |||
| 63 | for _ in 0..20 { | ||
| 64 | let _ = game.roll(0); | ||
| 65 | } | ||
| 66 | |||
| 67 | assert_eq!(game.score(), Some(0)); | ||
| 68 | } | ||
| 69 | |||
| 70 | #[test] | ||
| 71 | fn ten_frames_without_a_strike_or_spare() { | ||
| 72 | let mut game = BowlingGame::new(); | ||
| 73 | |||
| 74 | for _ in 0..10 { | ||
| 75 | let _ = game.roll(3); | ||
| 76 | let _ = game.roll(6); | ||
| 77 | } | ||
| 78 | |||
| 79 | assert_eq!(game.score(), Some(90)); | ||
| 80 | } | ||
| 81 | |||
| 82 | #[test] | ||
| 83 | fn spare_in_the_first_frame_followed_by_zeros() { | ||
| 84 | let mut game = BowlingGame::new(); | ||
| 85 | |||
| 86 | let _ = game.roll(6); | ||
| 87 | let _ = game.roll(4); | ||
| 88 | |||
| 89 | for _ in 0..18 { | ||
| 90 | let _ = game.roll(0); | ||
| 91 | } | ||
| 92 | |||
| 93 | assert_eq!(game.score(), Some(10)); | ||
| 94 | } | ||
| 95 | |||
| 96 | #[test] | ||
| 97 | fn points_scored_in_the_roll_after_a_spare_are_counted_twice_as_a_bonus() { | ||
| 98 | let mut game = BowlingGame::new(); | ||
| 99 | |||
| 100 | let _ = game.roll(6); | ||
| 101 | let _ = game.roll(4); | ||
| 102 | let _ = game.roll(3); | ||
| 103 | |||
| 104 | for _ in 0..17 { | ||
| 105 | let _ = game.roll(0); | ||
| 106 | } | ||
| 107 | |||
| 108 | assert_eq!(game.score(), Some(16)); | ||
| 109 | } | ||
| 110 | |||
| 111 | #[test] | ||
| 112 | fn consecutive_spares_each_get_a_one_roll_bonus() { | ||
| 113 | let mut game = BowlingGame::new(); | ||
| 114 | |||
| 115 | let _ = game.roll(5); | ||
| 116 | let _ = game.roll(5); | ||
| 117 | let _ = game.roll(3); | ||
| 118 | let _ = game.roll(7); | ||
| 119 | let _ = game.roll(4); | ||
| 120 | |||
| 121 | for _ in 0..15 { | ||
| 122 | let _ = game.roll(0); | ||
| 123 | } | ||
| 124 | |||
| 125 | assert_eq!(game.score(), Some(31)); | ||
| 126 | } | ||
| 127 | |||
| 128 | #[test] | ||
| 129 | fn if_the_last_frame_is_a_spare_you_get_one_extra_roll_that_is_scored_once() { | ||
| 130 | let mut game = BowlingGame::new(); | ||
| 131 | |||
| 132 | for _ in 0..18 { | ||
| 133 | let _ = game.roll(0); | ||
| 134 | } | ||
| 135 | |||
| 136 | let _ = game.roll(5); | ||
| 137 | let _ = game.roll(5); | ||
| 138 | let _ = game.roll(7); | ||
| 139 | |||
| 140 | assert_eq!(game.score(), Some(17)); | ||
| 141 | } | ||
| 142 | |||
| 143 | #[test] | ||
| 144 | fn a_strike_earns_ten_points_in_a_frame_with_a_single_roll() { | ||
| 145 | let mut game = BowlingGame::new(); | ||
| 146 | |||
| 147 | let _ = game.roll(10); | ||
| 148 | |||
| 149 | for _ in 0..18 { | ||
| 150 | let _ = game.roll(0); | ||
| 151 | } | ||
| 152 | |||
| 153 | assert_eq!(game.score(), Some(10)); | ||
| 154 | } | ||
| 155 | |||
| 156 | #[test] | ||
| 157 | fn points_scored_in_the_two_rolls_after_a_strike_are_counted_twice_as_a_bonus() { | ||
| 158 | let mut game = BowlingGame::new(); | ||
| 159 | |||
| 160 | let _ = game.roll(10); | ||
| 161 | let _ = game.roll(5); | ||
| 162 | let _ = game.roll(3); | ||
| 163 | |||
| 164 | for _ in 0..16 { | ||
| 165 | let _ = game.roll(0); | ||
| 166 | } | ||
| 167 | |||
| 168 | assert_eq!(game.score(), Some(26)); | ||
| 169 | } | ||
| 170 | |||
| 171 | #[test] | ||
| 172 | fn consecutive_strikes_each_get_the_two_roll_bonus() { | ||
| 173 | let mut game = BowlingGame::new(); | ||
| 174 | |||
| 175 | let _ = game.roll(10); | ||
| 176 | let _ = game.roll(10); | ||
| 177 | let _ = game.roll(10); | ||
| 178 | let _ = game.roll(5); | ||
| 179 | let _ = game.roll(3); | ||
| 180 | |||
| 181 | for _ in 0..12 { | ||
| 182 | let _ = game.roll(0); | ||
| 183 | } | ||
| 184 | |||
| 185 | assert_eq!(game.score(), Some(81)); | ||
| 186 | } | ||
| 187 | |||
| 188 | #[test] | ||
| 189 | fn a_strike_in_the_last_frame_earns_a_two_roll_bonus_that_is_counted_once() { | ||
| 190 | let mut game = BowlingGame::new(); | ||
| 191 | |||
| 192 | for _ in 0..18 { | ||
| 193 | let _ = game.roll(0); | ||
| 194 | } | ||
| 195 | |||
| 196 | let _ = game.roll(10); | ||
| 197 | let _ = game.roll(7); | ||
| 198 | let _ = game.roll(1); | ||
| 199 | |||
| 200 | assert_eq!(game.score(), Some(18)); | ||
| 201 | } | ||
| 202 | |||
| 203 | #[test] | ||
| 204 | fn a_spare_with_the_two_roll_bonus_does_not_get_a_bonus_roll() { | ||
| 205 | let mut game = BowlingGame::new(); | ||
| 206 | |||
| 207 | for _ in 0..18 { | ||
| 208 | let _ = game.roll(0); | ||
| 209 | } | ||
| 210 | |||
| 211 | let _ = game.roll(10); | ||
| 212 | let _ = game.roll(7); | ||
| 213 | let _ = game.roll(3); | ||
| 214 | |||
| 215 | assert_eq!(game.score(), Some(20)); | ||
| 216 | } | ||
| 217 | |||
| 218 | #[test] | ||
| 219 | fn strikes_with_the_two_roll_bonus_do_not_get_a_bonus_roll() { | ||
| 220 | let mut game = BowlingGame::new(); | ||
| 221 | |||
| 222 | for _ in 0..18 { | ||
| 223 | let _ = game.roll(0); | ||
| 224 | } | ||
| 225 | |||
| 226 | let _ = game.roll(10); | ||
| 227 | let _ = game.roll(10); | ||
| 228 | let _ = game.roll(10); | ||
| 229 | |||
| 230 | assert_eq!(game.score(), Some(30)); | ||
| 231 | } | ||
| 232 | |||
| 233 | #[test] | ||
| 234 | fn a_strike_with_the_one_roll_bonus_after_a_spare_in_the_last_frame_does_not_get_a_bonus() { | ||
| 235 | let mut game = BowlingGame::new(); | ||
| 236 | |||
| 237 | for _ in 0..18 { | ||
| 238 | let _ = game.roll(0); | ||
| 239 | } | ||
| 240 | |||
| 241 | let _ = game.roll(7); | ||
| 242 | let _ = game.roll(3); | ||
| 243 | let _ = game.roll(10); | ||
| 244 | |||
| 245 | assert_eq!(game.score(), Some(20)); | ||
| 246 | } | ||
| 247 | |||
| 248 | #[test] | ||
| 249 | fn all_strikes_is_a_perfect_score_of_300() { | ||
| 250 | let mut game = BowlingGame::new(); | ||
| 251 | |||
| 252 | for _ in 0..12 { | ||
| 253 | let _ = game.roll(10); | ||
| 254 | } | ||
| 255 | |||
| 256 | assert_eq!(game.score(), Some(300)); | ||
| 257 | } | ||
| 258 | |||
| 259 | #[test] | ||
| 260 | fn you_cannot_roll_more_than_ten_pins_in_a_single_frame() { | ||
| 261 | let mut game = BowlingGame::new(); | ||
| 262 | |||
| 263 | assert!(game.roll(5).is_ok()); | ||
| 264 | assert_eq!(game.roll(6), Err(Error::NotEnoughPinsLeft)); | ||
| 265 | } | ||
| 266 | |||
| 267 | #[test] | ||
| 268 | fn first_bonus_ball_after_a_final_strike_cannot_score_an_invalid_number_of_pins() { | ||
| 269 | let mut game = BowlingGame::new(); | ||
| 270 | |||
| 271 | for _ in 0..18 { | ||
| 272 | let _ = game.roll(0); | ||
| 273 | } | ||
| 274 | |||
| 275 | let _ = game.roll(10); | ||
| 276 | |||
| 277 | assert_eq!(game.roll(11), Err(Error::NotEnoughPinsLeft)); | ||
| 278 | } | ||
| 279 | |||
| 280 | #[test] | ||
| 281 | fn the_two_balls_after_a_final_strike_cannot_score_an_invalid_number_of_pins() { | ||
| 282 | let mut game = BowlingGame::new(); | ||
| 283 | |||
| 284 | for _ in 0..18 { | ||
| 285 | let _ = game.roll(0); | ||
| 286 | } | ||
| 287 | |||
| 288 | let _ = game.roll(10); | ||
| 289 | |||
| 290 | assert!(game.roll(5).is_ok()); | ||
| 291 | assert_eq!(game.roll(6), Err(Error::NotEnoughPinsLeft)); | ||
| 292 | } | ||
| 293 | |||
| 294 | #[test] | ||
| 295 | fn the_two_balls_after_a_final_strike_can_be_a_strike_and_non_strike() { | ||
| 296 | let mut game = BowlingGame::new(); | ||
| 297 | |||
| 298 | for _ in 0..18 { | ||
| 299 | let _ = game.roll(0); | ||
| 300 | } | ||
| 301 | |||
| 302 | let _ = game.roll(10); | ||
| 303 | |||
| 304 | assert!(game.roll(10).is_ok()); | ||
| 305 | assert!(game.roll(6).is_ok()); | ||
| 306 | } | ||
| 307 | |||
| 308 | #[test] | ||
| 309 | fn the_two_balls_after_a_final_strike_cannot_be_a_non_strike_followed_by_a_strike() { | ||
| 310 | let mut game = BowlingGame::new(); | ||
| 311 | |||
| 312 | for _ in 0..18 { | ||
| 313 | let _ = game.roll(0); | ||
| 314 | } | ||
| 315 | |||
| 316 | let _ = game.roll(10); | ||
| 317 | |||
| 318 | assert!(game.roll(6).is_ok()); | ||
| 319 | assert_eq!(game.roll(10), Err(Error::NotEnoughPinsLeft)); | ||
| 320 | } | ||
| 321 | |||
| 322 | #[test] | ||
| 323 | fn second_bonus_ball_after_a_final_strike_cannot_score_an_invalid_number_of_pins_even_if_first_is_strike( | ||
| 324 | ) { | ||
| 325 | let mut game = BowlingGame::new(); | ||
| 326 | |||
| 327 | for _ in 0..18 { | ||
| 328 | let _ = game.roll(0); | ||
| 329 | } | ||
| 330 | |||
| 331 | let _ = game.roll(10); | ||
| 332 | |||
| 333 | assert!(game.roll(10).is_ok()); | ||
| 334 | assert_eq!(game.roll(11), Err(Error::NotEnoughPinsLeft)); | ||
| 335 | } | ||
| 336 | |||
| 337 | #[test] | ||
| 338 | fn if_the_last_frame_is_a_strike_you_cannot_score_before_the_extra_rolls_are_taken() { | ||
| 339 | let mut game = BowlingGame::new(); | ||
| 340 | |||
| 341 | for _ in 0..18 { | ||
| 342 | let _ = game.roll(0); | ||
| 343 | } | ||
| 344 | |||
| 345 | let _ = game.roll(10); | ||
| 346 | |||
| 347 | assert_eq!(game.score(), None); | ||
| 348 | |||
| 349 | let _ = game.roll(10); | ||
| 350 | |||
| 351 | assert_eq!(game.score(), None); | ||
| 352 | |||
| 353 | let _ = game.roll(10); | ||
| 354 | |||
| 355 | assert!(game.score().is_some()); | ||
| 356 | } | ||
| 357 | |||
| 358 | #[test] | ||
| 359 | fn if_the_last_frame_is_a_spare_you_cannot_create_a_score_before_extra_roll_is_taken() { | ||
| 360 | let mut game = BowlingGame::new(); | ||
| 361 | |||
| 362 | for _ in 0..18 { | ||
| 363 | let _ = game.roll(0); | ||
| 364 | } | ||
| 365 | |||
| 366 | let _ = game.roll(5); | ||
| 367 | let _ = game.roll(5); | ||
| 368 | |||
| 369 | assert_eq!(game.score(), None); | ||
| 370 | |||
| 371 | let _ = game.roll(10); | ||
| 372 | |||
| 373 | assert!(game.score().is_some()); | ||
| 374 | } | ||
| 375 | |||
| 376 | #[test] | ||
| 377 | fn cannot_roll_after_bonus_roll_for_spare() { | ||
| 378 | let mut game = BowlingGame::new(); | ||
| 379 | |||
| 380 | for _ in 0..9 { | ||
| 381 | let _ = game.roll(0); | ||
| 382 | let _ = game.roll(0); | ||
| 383 | } | ||
| 384 | let _ = game.roll(7); | ||
| 385 | let _ = game.roll(3); | ||
| 386 | assert!(game.roll(2).is_ok()); | ||
| 387 | |||
| 388 | assert_eq!(game.roll(2), Err(Error::GameComplete)); | ||
| 389 | } | ||
| 390 | |||
| 391 | #[test] | ||
| 392 | fn cannot_roll_after_bonus_roll_for_strike() { | ||
| 393 | let mut game = BowlingGame::new(); | ||
| 394 | |||
| 395 | for _ in 0..9 { | ||
| 396 | let _ = game.roll(0); | ||
| 397 | let _ = game.roll(0); | ||
| 398 | } | ||
| 399 | let _ = game.roll(10); | ||
| 400 | let _ = game.roll(3); | ||
| 401 | assert!(game.roll(2).is_ok()); | ||
| 402 | |||
| 403 | assert_eq!(game.roll(2), Err(Error::GameComplete)); | ||
| 404 | } | ||
| 405 | |||
| 406 | #[test] | ||
| 407 | fn last_two_strikes_followed_by_only_last_bonus_with_non_strike_points() { | ||
| 408 | let mut game = BowlingGame::new(); | ||
| 409 | for _ in 0..16 { | ||
| 410 | let _ = game.roll(0); | ||
| 411 | } | ||
| 412 | let _ = game.roll(10); | ||
| 413 | let _ = game.roll(10); | ||
| 414 | let _ = game.roll(0); | ||
| 415 | let _ = game.roll(1); | ||
| 416 | |||
| 417 | assert_eq!(game.score(), Some(31)); | ||
| 418 | } | ||
