-
Notifications
You must be signed in to change notification settings - Fork 543
Expand file tree
/
Copy pathrpn-calculator.rs
More file actions
89 lines (77 loc) · 1.83 KB
/
rpn-calculator.rs
File metadata and controls
89 lines (77 loc) · 1.83 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use rpn_calculator::*;
fn calculator_input(s: &str) -> Vec<CalculatorInput> {
s.split_whitespace()
.map(|s| match s {
"+" => CalculatorInput::Add,
"-" => CalculatorInput::Subtract,
"*" => CalculatorInput::Multiply,
"/" => CalculatorInput::Divide,
n => CalculatorInput::Value(n.parse().unwrap()),
})
.collect()
}
#[test]
fn empty_input_returns_none() {
let input = calculator_input("");
assert_eq!(evaluate(&input), None);
}
#[test]
#[ignore]
fn simple_value() {
let input = calculator_input("10");
assert_eq!(evaluate(&input), Some(10));
}
#[test]
#[ignore]
fn simple_addition() {
let input = calculator_input("2 2 +");
assert_eq!(evaluate(&input), Some(4));
}
#[test]
#[ignore]
fn simple_subtraction() {
let input = calculator_input("7 11 -");
assert_eq!(evaluate(&input), Some(-4));
}
#[test]
#[ignore]
fn simple_multiplication() {
let input = calculator_input("6 9 *");
assert_eq!(evaluate(&input), Some(54));
}
#[test]
#[ignore]
fn simple_division() {
let input = calculator_input("57 19 /");
assert_eq!(evaluate(&input), Some(3));
}
#[test]
#[ignore]
fn complex_operation() {
let input = calculator_input("4 8 + 7 5 - /");
assert_eq!(evaluate(&input), Some(6));
}
#[test]
#[ignore]
fn too_few_operands_returns_none() {
let input = calculator_input("2 +");
assert_eq!(evaluate(&input), None);
}
#[test]
#[ignore]
fn too_many_operands_returns_none() {
let input = calculator_input("2 2");
assert_eq!(evaluate(&input), None);
}
#[test]
#[ignore]
fn zero_operands_returns_none() {
let input = calculator_input("+");
assert_eq!(evaluate(&input), None);
}
#[test]
#[ignore]
fn intermediate_error_returns_none() {
let input = calculator_input("+ 2 2 *");
assert_eq!(evaluate(&input), None);
}