Skip to content

Instantly share code, notes, and snippets.

@gusdelact
Last active October 24, 2019 21:18
Show Gist options
  • Save gusdelact/45aa3ce2b341c68debab2525f3150aba to your computer and use it in GitHub Desktop.
Save gusdelact/45aa3ce2b341c68debab2525f3150aba to your computer and use it in GitHub Desktop.
#[derive(Debug)]
enum Expr {
Const(f64),
Sum(f64, f64),
Product(f64, f64),
Quotient(f64, f64)
}
fn interpretar(expresion: Expr) -> f64 {
match expresion {
Expr::Const(valor) => valor,
Expr::Sum(a,b) => a + b,
Expr::Product(a,b) => a*b ,
Expr::Quotient(a,b) => a/b ,
}
}
#[test]
fn test_const() {
let a = Expr::Const(10.0);
let b = Expr::Const(14.0);
assert_eq!(interpretar(a),10.0);
assert_eq!(interpretar(b),14.0);
}
#[test]
fn test_suma() {
let suma = Expr::Sum( 15.0,15.0 );
assert_eq!(interpretar(suma),30.0);
}
#[test]
fn test_prod() {
let prod = Expr::Product( 10.0,15.0 );
assert_eq!(interpretar(prod),150.0);
}
#[test]
fn test_quote() {
let quot = Expr::Quotient( 14.0,7.0 );
assert_eq!(interpretar(quot),2.0);
}
#[test]
fn test_quote_zero() {
let quot = Expr::Quotient( 14.0,0.0 );
let resultado=interpretar(quot);
println!("{:?}",resultado);
assert!(resultado.is_infinite());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment