Responses for exercises in Exercism.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

67 lines
1.5 KiB

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 test_empty_input_returns_none() {
let input = calculator_input("");
assert_eq!(evaluate(&input), None);
}
#[test]
fn test_simple_value() {
let input = calculator_input("10");
assert_eq!(evaluate(&input), Some(10));
}
#[test]
fn test_simple_addition() {
let input = calculator_input("2 2 +");
assert_eq!(evaluate(&input), Some(4));
}
#[test]
fn test_simple_subtraction() {
let input = calculator_input("7 11 -");
assert_eq!(evaluate(&input), Some(-4));
}
#[test]
fn test_simple_multiplication() {
let input = calculator_input("6 9 *");
assert_eq!(evaluate(&input), Some(54));
}
#[test]
fn test_simple_division() {
let input = calculator_input("57 19 /");
assert_eq!(evaluate(&input), Some(3));
}
#[test]
fn test_complex_operation() {
let input = calculator_input("4 8 + 7 5 - /");
assert_eq!(evaluate(&input), Some(6));
}
#[test]
fn test_too_few_operands_returns_none() {
let input = calculator_input("2 +");
assert_eq!(evaluate(&input), None);
}
#[test]
fn test_too_many_operands_returns_none() {
let input = calculator_input("2 2");
assert_eq!(evaluate(&input), None);
}