-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.rs
More file actions
74 lines (65 loc) · 1.86 KB
/
Copy pathmain.rs
File metadata and controls
74 lines (65 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
fn main() {
println!("Hello, i am Crabby 🦀 !");
}
#[allow(dead_code)]
fn add(a: u8, b: u8) -> u8 {
a + b
}
#[derive(Debug)]
pub enum Game {
Rock,
Paper,
Scissors,
}
#[derive(Debug, PartialEq)]
pub enum GameResult {
Win,
Draw,
Lost,
}
#[allow(dead_code)]
fn play(a: Game, b: Game) -> GameResult {
match (a, b) {
// win
(Game::Paper, Game::Rock) => GameResult::Win,
(Game::Rock, Game::Scissors) => GameResult::Win,
(Game::Scissors, Game::Paper) => GameResult::Win,
// draw
(Game::Rock, Game::Rock) => GameResult::Draw,
(Game::Scissors, Game::Scissors) => GameResult::Draw,
(Game::Paper, Game::Paper) => GameResult::Draw,
// lose
(Game::Rock, Game::Paper) => GameResult::Lost,
(Game::Paper, Game::Scissors) => GameResult::Lost,
(Game::Scissors, Game::Rock) => GameResult::Lost,
}
}
#[cfg(test)]
mod tests {
use super::add;
use super::play;
use super::Game;
use super::GameResult;
#[test]
fn test_add() {
assert_eq!(add(12, 5), 17);
}
#[test]
fn test_player_one_wins() {
assert_eq!(play(Game::Rock, Game::Scissors), GameResult::Win);
assert_eq!(play(Game::Scissors, Game::Paper), GameResult::Win);
assert_eq!(play(Game::Paper, Game::Rock), GameResult::Win);
}
#[test]
fn test_player_one_draw() {
assert_eq!(play(Game::Rock, Game::Rock), GameResult::Draw);
assert_eq!(play(Game::Paper, Game::Paper), GameResult::Draw);
assert_eq!(play(Game::Scissors, Game::Scissors), GameResult::Draw);
}
#[test]
fn test_player_one_lose() {
assert_eq!(play(Game::Rock, Game::Paper), GameResult::Lost);
assert_eq!(play(Game::Paper, Game::Scissors), GameResult::Lost);
assert_eq!(play(Game::Scissors, Game::Rock), GameResult::Lost);
}
}