Skip to content

Commit 5ff7d0c

Browse files
authored
Merge pull request #152 from petertseng/tournament-inline
tournament: Put inputs/expectations inline, not in files
2 parents 3597821 + 5fd2b6d commit 5ff7d0c

File tree

9 files changed

+53
-94
lines changed

9 files changed

+53
-94
lines changed

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"roman-numerals",
2323
"hexadecimal",
2424
"grade-school",
25+
"tournament",
2526
"robot-simulator",
2627
"queen-attack",
2728
"sublist",
@@ -35,7 +36,6 @@
3536
"minesweeper",
3637
"dominoes",
3738
"parallel-letter-frequency",
38-
"tournament",
3939
"rectangles",
4040
"forth",
4141
"circular-buffer"

exercises/tournament/example.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use std::cmp::Ordering::Equal;
22
use std::collections::HashMap;
3-
use std::fs::File;
4-
use std::io::{BufRead, BufReader, Write, Result};
5-
use std::path::Path;
63

74
enum GameResult {
85
Win,
@@ -29,12 +26,9 @@ impl TeamResult {
2926
}
3027
}
3128

32-
pub fn tally(input_filename: &Path, output_filename: &Path) -> Result<u32> {
33-
let reader = BufReader::with_capacity(2048, File::open(input_filename).unwrap());
34-
let mut count = 0;
29+
pub fn tally(input: &str) -> String {
3530
let mut results: HashMap<String, TeamResult> = HashMap::new();
36-
for line in reader.lines() {
37-
let line = line.unwrap();
31+
for line in input.to_string().lines() {
3832
let parts: Vec<&str> = line.trim_right().split(';').collect();
3933
if parts.len() != 3 { continue; }
4034
let team1 = parts[0];
@@ -44,26 +38,22 @@ pub fn tally(input_filename: &Path, output_filename: &Path) -> Result<u32> {
4438
"win" => {
4539
add_game_result(&mut results, team1.to_string(), GameResult::Win);
4640
add_game_result(&mut results, team2.to_string(), GameResult::Loss);
47-
count += 1;
4841
},
4942
"draw" => {
5043
add_game_result(&mut results, team1.to_string(), GameResult::Draw);
5144
add_game_result(&mut results, team2.to_string(), GameResult::Draw);
52-
count += 1;
5345
},
5446
"loss" => {
5547
add_game_result(&mut results, team1.to_string(), GameResult::Loss);
5648
add_game_result(&mut results, team2.to_string(), GameResult::Win);
57-
count += 1;
5849
},
5950
_ => () // Ignore bad lines
6051
}
6152
}
62-
try!(write_tally(&results, output_filename));
63-
Ok(count)
53+
write_tally(&results)
6454
}
6555

66-
fn write_tally(results: &HashMap<String, TeamResult>, output_filename: &Path) -> Result<()> {
56+
fn write_tally(results: &HashMap<String, TeamResult>) -> String {
6757
let mut v: Vec<_> = results.iter().map(|(team, r)| {
6858
let games = r.wins + r.draws + r.losses;
6959
let points = r.wins * 3 + r.draws;
@@ -75,13 +65,12 @@ fn write_tally(results: &HashMap<String, TeamResult>, output_filename: &Path) ->
7565
Equal => a.1.cmp(&(b.1)).reverse(),
7666
other => other
7767
});
78-
let mut f = try!(File::create(output_filename));
79-
try!(writeln!(&mut f, "{:30} | MP | W | D | L | P", "Team"));
80-
for &(ref team, games, r, points) in v.iter() {
81-
try!(writeln!(&mut f, "{:30} | {:2} | {:2} | {:2} | {:2} | {:2}",
82-
team, games, r.wins, r.draws, r.losses, points));
83-
}
84-
Ok(())
68+
let header = format!("{:30} | MP | W | D | L | P\n", "Team");
69+
let lines: Vec<_> = v.iter().map(|&(ref team, games, r, points)| {
70+
format!("{:30} | {:2} | {:2} | {:2} | {:2} | {:2}",
71+
team, games, r.wins, r.draws, r.losses, points)
72+
}).collect();
73+
header + &lines.join("\n")
8574
}
8675

8776
fn add_game_result(results: &mut HashMap<String, TeamResult>, team: String, result: GameResult) {

exercises/tournament/tests/expected1.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

exercises/tournament/tests/expected2.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

exercises/tournament/tests/expected3.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

exercises/tournament/tests/input1.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

exercises/tournament/tests/input2.txt

Lines changed: 0 additions & 11 deletions
This file was deleted.

exercises/tournament/tests/input3.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,57 @@
1-
use std::fs::File;
2-
use std::path::Path;
3-
use std::io::Read;
4-
51
extern crate tournament;
62

7-
fn file_equal(output_file: &str, expected_file: &str) {
8-
let output = match File::open(&Path::new(output_file)) {
9-
Err(e) => panic!("Couldn't open {}: {}", output_file, e),
10-
Ok(mut f) => {
11-
let mut buf = String::new();
12-
match f.read_to_string(&mut buf) {
13-
Err(e) => panic!("Couldn't read {}: {}", output_file, e),
14-
Ok(_) => buf
15-
}
16-
}
17-
};
18-
let expected = match File::open(&Path::new(expected_file)) {
19-
Err(e) => panic!("Couldn't open {}: {}", expected_file, e),
20-
Ok(mut f) => {
21-
let mut buf = String::new();
22-
match f.read_to_string(&mut buf) {
23-
Err(e) => panic!("Couldn't read {}: {}", expected_file, e),
24-
Ok(_) => buf
25-
}
26-
}
27-
};
28-
assert_eq!("\n".to_string() + output.as_ref(), "\n".to_string() + expected.as_ref());
29-
30-
}
31-
32-
333
#[test]
344
fn test_good() {
35-
assert_eq!(tournament::tally(&Path::new("tests/input1.txt"), &Path::new("tests/output1.txt")).unwrap(), 6);
36-
file_equal("tests/output1.txt", "tests/expected1.txt");
5+
let input = "Allegoric Alaskians;Blithering Badgers;win\n".to_string() +
6+
"Devastating Donkeys;Courageous Californians;draw\n" +
7+
"Devastating Donkeys;Allegoric Alaskians;win\n" +
8+
"Courageous Californians;Blithering Badgers;loss\n" +
9+
"Blithering Badgers;Devastating Donkeys;loss\n" +
10+
"Allegoric Alaskians;Courageous Californians;win";
11+
let expected = "Team | MP | W | D | L | P\n".to_string() +
12+
"Devastating Donkeys | 3 | 2 | 1 | 0 | 7\n" +
13+
"Allegoric Alaskians | 3 | 2 | 0 | 1 | 6\n" +
14+
"Blithering Badgers | 3 | 1 | 0 | 2 | 3\n" +
15+
"Courageous Californians | 3 | 0 | 1 | 2 | 1";
16+
17+
assert_eq!(tournament::tally(&input), expected);
3718
}
3819

3920
#[test]
4021
#[ignore]
4122
fn test_ignore_bad_lines() {
42-
assert_eq!(tournament::tally(&Path::new("tests/input2.txt"), &Path::new("tests/output2.txt")).unwrap(), 6);
43-
file_equal("tests/output2.txt", "tests/expected2.txt");
23+
let input = "Allegoric Alaskians;Blithering Badgers;win\n".to_string() +
24+
"Devastating Donkeys_Courageous Californians;draw\n" +
25+
"Devastating Donkeys;Allegoric Alaskians;win\n" +
26+
"\n" +
27+
"Courageous Californians;Blithering Badgers;loss\n" +
28+
"Bla;Bla;Bla\n" +
29+
"Blithering Badgers;Devastating Donkeys;loss\n" +
30+
"# Yackity yackity yack\n" +
31+
"Allegoric Alaskians;Courageous Californians;win\n" +
32+
"Devastating Donkeys;Courageous Californians;draw\n" +
33+
"Devastating Donkeys@Courageous Californians;draw";
34+
let expected = "Team | MP | W | D | L | P\n".to_string() +
35+
"Devastating Donkeys | 3 | 2 | 1 | 0 | 7\n" +
36+
"Allegoric Alaskians | 3 | 2 | 0 | 1 | 6\n" +
37+
"Blithering Badgers | 3 | 1 | 0 | 2 | 3\n" +
38+
"Courageous Californians | 3 | 0 | 1 | 2 | 1";
39+
40+
assert_eq!(tournament::tally(&input), expected);
4441
}
4542

4643
#[test]
4744
#[ignore]
4845
fn test_incomplete_competition() {
49-
assert_eq!(tournament::tally(&Path::new("tests/input3.txt"), &Path::new("tests/output3.txt")).unwrap(), 4);
50-
file_equal("tests/output3.txt", "tests/expected3.txt");
46+
let input = "Allegoric Alaskians;Blithering Badgers;win\n".to_string() +
47+
"Devastating Donkeys;Allegoric Alaskians;win\n" +
48+
"Courageous Californians;Blithering Badgers;loss\n" +
49+
"Allegoric Alaskians;Courageous Californians;win";
50+
let expected = "Team | MP | W | D | L | P\n".to_string() +
51+
"Allegoric Alaskians | 3 | 2 | 0 | 1 | 6\n" +
52+
"Blithering Badgers | 2 | 1 | 0 | 1 | 3\n" +
53+
"Devastating Donkeys | 1 | 1 | 0 | 0 | 3\n" +
54+
"Courageous Californians | 2 | 0 | 0 | 2 | 0";
55+
56+
assert_eq!(tournament::tally(&input), expected);
5157
}

0 commit comments

Comments
 (0)