Created
February 8, 2017 16:45
-
-
Save mudge/47e5938b806f480227a87f77d0760280 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Error { | |
NoRuleApplies, | |
} | |
enum Term { | |
TmTrue, | |
TmFalse, | |
TmIf(Box<Term>, Box<Term>, Box<Term>), | |
TmZero, | |
TmSucc(Box<Term>), | |
TmPred(Box<Term>), | |
TmIsZero(Box<Term>), | |
} | |
fn is_numeric_val(t: &Term) -> bool { | |
match *t { | |
Term::TmZero => true, | |
Term::TmSucc(ref t1) => is_numeric_val(&*t1), | |
_ => false, | |
} | |
} | |
fn is_val(t: &Term) -> bool { | |
match *t { | |
Term::TmTrue => true, | |
Term::TmFalse => true, | |
ref t if is_numeric_val(t) => true, | |
_ => false, | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn is_numeric_val_returns_true_for_zero() { | |
assert!(is_numeric_val(&Term::TmZero)); | |
} | |
#[test] | |
fn is_numeric_val_returns_true_for_succ_zero() { | |
assert!(is_numeric_val(&Term::TmSucc(Box::new(Term::TmZero)))); | |
} | |
#[test] | |
fn is_numeric_val_returns_false_for_true() { | |
assert!(!is_numeric_val(&Term::TmTrue)); | |
} | |
#[test] | |
fn is_numeric_val_returns_false_for_invalid_succ() { | |
assert!(!is_numeric_val(&Term::TmSucc(Box::new(Term::TmFalse)))); | |
} | |
#[test] | |
fn is_val_returns_true_for_true() { | |
assert!(is_val(&Term::TmTrue)); | |
} | |
#[test] | |
fn is_val_returns_true_for_false() { | |
assert!(is_val(&Term::TmFalse)); | |
} | |
#[test] | |
fn is_val_returns_true_for_zero() { | |
assert!(is_val(&Term::TmZero)); | |
} | |
#[test] | |
fn is_val_returns_true_for_succ_zero() { | |
assert!(is_val(&Term::TmSucc(Box::new(Term::TmZero)))); | |
} | |
#[test] | |
fn is_val_returns_false_for_conditionals() { | |
assert!(!is_val(&Term::TmIf(Box::new(Term::TmTrue), Box::new(Term::TmTrue), Box::new(Term::TmTrue)))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment