From ac44ece745618efee0f0b3387a23ecb403aa7e15 Mon Sep 17 00:00:00 2001 From: Peter Tseng Date: Fri, 12 May 2017 17:59:43 -0700 Subject: [PATCH 1/3] tournament: print header without newline if no teams Needed for tournament 1.3.0 new cases --- exercises/tournament/example.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exercises/tournament/example.rs b/exercises/tournament/example.rs index 4a5a5bc1a..38b11f50f 100644 --- a/exercises/tournament/example.rs +++ b/exercises/tournament/example.rs @@ -65,12 +65,12 @@ fn write_tally(results: &HashMap) -> String { Equal => a.1.cmp(&(b.1)).reverse(), other => other }); - let header = format!("{:30} | MP | W | D | L | P\n", "Team"); - let lines: Vec<_> = v.iter().map(|&(ref team, games, r, points)| { + let mut lines = vec![format!("{:30} | MP | W | D | L | P", "Team")]; + lines.extend(v.iter().map(|&(ref team, games, r, points)| { format!("{:30} | {:2} | {:2} | {:2} | {:2} | {:2}", team, games, r.wins, r.draws, r.losses, points) - }).collect(); - header + &lines.join("\n") + })); + lines.join("\n") } fn add_game_result(results: &mut HashMap, team: String, result: GameResult) { From 65c9479911afafe7255ab93738b5275bf10fde1d Mon Sep 17 00:00:00 2001 From: Peter Tseng Date: Fri, 12 May 2017 18:01:02 -0700 Subject: [PATCH 2/3] tournament: Sort by name A-Z, not games played "games played" has never been a tiebreaker, never in the history of tournament: * https://github.com/exercism/x-common/pull/22 * https://github.com/exercism/x-common/pull/254 There was a tie in favour of the team with more wins, but that was removed in https://github.com/exercism/x-common/pull/300 since it was hard to trigger. Needed for tournament 1.3.0 new cases. --- exercises/tournament/example.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/tournament/example.rs b/exercises/tournament/example.rs index 38b11f50f..f7aae6c8a 100644 --- a/exercises/tournament/example.rs +++ b/exercises/tournament/example.rs @@ -59,10 +59,10 @@ fn write_tally(results: &HashMap) -> String { let points = r.wins * 3 + r.draws; (team, games, r, points) }).collect(); - // Sort by points, then games played, in reverse order. + // Sort by points descending, then name A-Z. v.sort_by(|a, b| match a.3.cmp(&(b.3)).reverse() { - Equal => a.1.cmp(&(b.1)).reverse(), + Equal => a.0.cmp(&(b.0)), other => other }); let mut lines = vec![format!("{:30} | MP | W | D | L | P", "Team")]; From 97fa5f3cad42f0d4b40a04acc3f3071d2a215cdc Mon Sep 17 00:00:00 2001 From: Peter Tseng Date: Fri, 12 May 2017 17:43:14 -0700 Subject: [PATCH 3/3] tournament 1.3.0: Add simple start cases, remove invalid lines https://github.com/exercism/x-common/pull/773 --- exercises/tournament/Cargo.lock | 2 +- exercises/tournament/Cargo.toml | 2 +- exercises/tournament/tests/tournament.rs | 146 ++++++++++++++++++----- 3 files changed, 118 insertions(+), 32 deletions(-) diff --git a/exercises/tournament/Cargo.lock b/exercises/tournament/Cargo.lock index c8b806751..3eefcf651 100644 --- a/exercises/tournament/Cargo.lock +++ b/exercises/tournament/Cargo.lock @@ -1,4 +1,4 @@ [root] name = "tournament" -version = "0.0.0" +version = "1.3.0" diff --git a/exercises/tournament/Cargo.toml b/exercises/tournament/Cargo.toml index 4b19bce2b..7b45dcf86 100644 --- a/exercises/tournament/Cargo.toml +++ b/exercises/tournament/Cargo.toml @@ -1,3 +1,3 @@ [package] name = "tournament" -version = "0.0.0" +version = "1.3.0" diff --git a/exercises/tournament/tests/tournament.rs b/exercises/tournament/tests/tournament.rs index c34ced5c1..7fd7e2bcd 100644 --- a/exercises/tournament/tests/tournament.rs +++ b/exercises/tournament/tests/tournament.rs @@ -1,16 +1,107 @@ extern crate tournament; #[test] -fn test_good() { - let input = "Allegoric Alaskians;Blithering Badgers;win\n".to_string() + +fn just_the_header_if_no_input() { + let input = ""; + let expected = "Team | MP | W | D | L | P"; + + assert_eq!(tournament::tally(&input), expected); +} + +#[test] +#[ignore] +fn a_win_is_three_points_a_loss_is_zero_points() { + let input = "Allegoric Alaskans;Blithering Badgers;win"; + let expected = "Team | MP | W | D | L | P\n".to_string() + + "Allegoric Alaskans | 1 | 1 | 0 | 0 | 3\n" + + "Blithering Badgers | 1 | 0 | 0 | 1 | 0"; + + assert_eq!(tournament::tally(&input), expected); +} + +#[test] +#[ignore] +fn a_win_can_also_be_expressed_as_a_loss() { + let input = "Blithering Badgers;Allegoric Alaskans;loss"; + let expected = "Team | MP | W | D | L | P\n".to_string() + + "Allegoric Alaskans | 1 | 1 | 0 | 0 | 3\n" + + "Blithering Badgers | 1 | 0 | 0 | 1 | 0"; + + assert_eq!(tournament::tally(&input), expected); +} + +#[test] +#[ignore] +fn a_different_team_can_win() { + let input = "Blithering Badgers;Allegoric Alaskans;win"; + let expected = "Team | MP | W | D | L | P\n".to_string() + + "Blithering Badgers | 1 | 1 | 0 | 0 | 3\n" + + "Allegoric Alaskans | 1 | 0 | 0 | 1 | 0"; + + assert_eq!(tournament::tally(&input), expected); +} + +#[test] +#[ignore] +fn a_draw_is_one_point_each() { + let input = "Allegoric Alaskans;Blithering Badgers;draw"; + let expected = "Team | MP | W | D | L | P\n".to_string() + + "Allegoric Alaskans | 1 | 0 | 1 | 0 | 1\n" + + "Blithering Badgers | 1 | 0 | 1 | 0 | 1"; + + assert_eq!(tournament::tally(&input), expected); +} + +#[test] +#[ignore] +fn there_can_be_more_than_one_match() { + let input = "Allegoric Alaskans;Blithering Badgers;win\n".to_string() + + "Allegoric Alaskans;Blithering Badgers;win"; + let expected = "Team | MP | W | D | L | P\n".to_string() + + "Allegoric Alaskans | 2 | 2 | 0 | 0 | 6\n" + + "Blithering Badgers | 2 | 0 | 0 | 2 | 0"; + + assert_eq!(tournament::tally(&input), expected); +} + +#[test] +#[ignore] +fn there_can_be_more_than_one_winner() { + let input = "Allegoric Alaskans;Blithering Badgers;loss\n".to_string() + + "Allegoric Alaskans;Blithering Badgers;win"; + let expected = "Team | MP | W | D | L | P\n".to_string() + + "Allegoric Alaskans | 2 | 1 | 0 | 1 | 3\n" + + "Blithering Badgers | 2 | 1 | 0 | 1 | 3"; + + assert_eq!(tournament::tally(&input), expected); +} + +#[test] +#[ignore] +fn there_can_be_more_than_two_teams() { + let input = "Allegoric Alaskans;Blithering Badgers;win\n".to_string() + + "Blithering Badgers;Courageous Californians;win\n" + + "Courageous Californians;Allegoric Alaskans;loss"; + let expected = "Team | MP | W | D | L | P\n".to_string() + + "Allegoric Alaskans | 2 | 2 | 0 | 0 | 6\n" + + "Blithering Badgers | 2 | 1 | 0 | 1 | 3\n" + + "Courageous Californians | 2 | 0 | 0 | 2 | 0"; + + assert_eq!(tournament::tally(&input), expected); +} + +#[test] +#[ignore] +fn typical_input() { + let input = "Allegoric Alaskans;Blithering Badgers;win\n".to_string() + "Devastating Donkeys;Courageous Californians;draw\n" + - "Devastating Donkeys;Allegoric Alaskians;win\n" + + "Devastating Donkeys;Allegoric Alaskans;win\n" + "Courageous Californians;Blithering Badgers;loss\n" + "Blithering Badgers;Devastating Donkeys;loss\n" + - "Allegoric Alaskians;Courageous Californians;win"; + "Allegoric Alaskans;Courageous Californians;win"; let expected = "Team | MP | W | D | L | P\n".to_string() + "Devastating Donkeys | 3 | 2 | 1 | 0 | 7\n" + - "Allegoric Alaskians | 3 | 2 | 0 | 1 | 6\n" + + "Allegoric Alaskans | 3 | 2 | 0 | 1 | 6\n" + "Blithering Badgers | 3 | 1 | 0 | 2 | 3\n" + "Courageous Californians | 3 | 0 | 1 | 2 | 1"; @@ -19,39 +110,34 @@ fn test_good() { #[test] #[ignore] -fn test_ignore_bad_lines() { - let input = "Allegoric Alaskians;Blithering Badgers;win\n".to_string() + - "Devastating Donkeys_Courageous Californians;draw\n" + - "Devastating Donkeys;Allegoric Alaskians;win\n" + - "\n" + - "Courageous Californians;Blithering Badgers;loss\n" + - "Bla;Bla;Bla\n" + - "Blithering Badgers;Devastating Donkeys;loss\n" + - "# Yackity yackity yack\n" + - "Allegoric Alaskians;Courageous Californians;win\n" + - "Devastating Donkeys;Courageous Californians;draw\n" + - "Devastating Donkeys@Courageous Californians;draw"; +fn incomplete_competition_not_all_pairs_have_played() { + let input = "Allegoric Alaskans;Blithering Badgers;loss\n".to_string() + + "Devastating Donkeys;Allegoric Alaskans;loss\n" + + "Courageous Californians;Blithering Badgers;draw\n" + + "Allegoric Alaskans;Courageous Californians;win"; let expected = "Team | MP | W | D | L | P\n".to_string() + - "Devastating Donkeys | 3 | 2 | 1 | 0 | 7\n" + - "Allegoric Alaskians | 3 | 2 | 0 | 1 | 6\n" + - "Blithering Badgers | 3 | 1 | 0 | 2 | 3\n" + - "Courageous Californians | 3 | 0 | 1 | 2 | 1"; + "Allegoric Alaskans | 3 | 2 | 0 | 1 | 6\n" + + "Blithering Badgers | 2 | 1 | 1 | 0 | 4\n" + + "Courageous Californians | 2 | 0 | 1 | 1 | 1\n" + + "Devastating Donkeys | 1 | 0 | 0 | 1 | 0"; assert_eq!(tournament::tally(&input), expected); } #[test] #[ignore] -fn test_incomplete_competition() { - let input = "Allegoric Alaskians;Blithering Badgers;win\n".to_string() + - "Devastating Donkeys;Allegoric Alaskians;win\n" + - "Courageous Californians;Blithering Badgers;loss\n" + - "Allegoric Alaskians;Courageous Californians;win"; +fn ties_broken_alphabetically() { + let input = "Courageous Californians;Devastating Donkeys;win\n".to_string() + + "Allegoric Alaskans;Blithering Badgers;win\n" + + "Devastating Donkeys;Allegoric Alaskans;loss\n" + + "Courageous Californians;Blithering Badgers;win\n" + + "Blithering Badgers;Devastating Donkeys;draw\n" + + "Allegoric Alaskans;Courageous Californians;draw"; let expected = "Team | MP | W | D | L | P\n".to_string() + - "Allegoric Alaskians | 3 | 2 | 0 | 1 | 6\n" + - "Blithering Badgers | 2 | 1 | 0 | 1 | 3\n" + - "Devastating Donkeys | 1 | 1 | 0 | 0 | 3\n" + - "Courageous Californians | 2 | 0 | 0 | 2 | 0"; + "Allegoric Alaskans | 3 | 2 | 1 | 0 | 7\n" + + "Courageous Californians | 3 | 2 | 1 | 0 | 7\n" + + "Blithering Badgers | 3 | 0 | 1 | 2 | 1\n" + + "Devastating Donkeys | 3 | 0 | 1 | 2 | 1"; assert_eq!(tournament::tally(&input), expected); }