use regexp::Regexp;
use regexp::ParsingError;
#[test]
fn test_eval_basic_nfa() {
let r = Regexp::new(&String::from("abc")).unwrap();
assert!(r.eval(String::from("abc")));
assert!(!r.eval(String::from("ab")));
assert!(!r.eval(String::from("abcd")));
}
#[test]
fn test_eval_basic_dfa() {
let r = Regexp::new(&String::from("abc")).unwrap().determinize();
assert!(r.eval(String::from("abc")));
assert!(!r.eval(String::from("ab")));
assert!(!r.eval(String::from("abcd")));
}
#[test]
fn test_eval_empty_nfa() {
assert!(Regexp::new(&String::from("a*")).unwrap().eval(String::from("")));
assert!(Regexp::new(&String::from("")).unwrap().eval(String::from("")));
assert!(!Regexp::new(&String::from("")).unwrap().eval(String::from("a")));
assert!(!Regexp::new(&String::from("a")).unwrap().eval(String::from("")));
}
#[test]
fn test_eval_empty_dfa() {
assert!(Regexp::new(&String::from("a*")).unwrap().determinize().eval(String::from("")));
assert!(Regexp::new(&String::from("")).unwrap().determinize().eval(String::from("")));
assert!(!Regexp::new(&String::from("")).unwrap().determinize().eval(String::from("a")));
}
#[test]
fn test_eval_asterisk_nfa() {
let r = Regexp::new(&String::from("a*b*")).unwrap();
assert!(r.eval(String::from("a")));
assert!(r.eval(String::from("ab")));
assert!(r.eval(String::from("aabb")));
assert!(!r.eval(String::from("abab")));
}
#[test]
fn test_eval_asterisk_dfa() {
let r = Regexp::new(&String::from("a*b*")).unwrap().determinize();
assert!(r.eval(String::from("a")));
assert!(r.eval(String::from("ab")));
assert!(r.eval(String::from("aabb")));
assert!(!r.eval(String::from("abab")));
}
#[test]
fn test_eval_plus_nfa() {
let r = Regexp::new(&String::from("(ab)+")).unwrap();
assert!(!r.eval(String::from("a")));
assert!(r.eval(String::from("ab")));
assert!(r.eval(String::from("abab")));
assert!(!r.eval(String::from("aabb")));
}
#[test]
fn test_eval_plus_dfa() {
let r = Regexp::new(&String::from("(ab)+")).unwrap().determinize();
assert!(!r.eval(String::from("a")));
assert!(r.eval(String::from("ab")));
assert!(r.eval(String::from("abab")));
assert!(!r.eval(String::from("aabb")));
}
#[test]
fn test_invalid_asterisk() {
let x = Regexp::new(&String::from("*"));
assert!(matches!(x, Err(ParsingError::Asterisk{s: _, pos: 0})));
}
#[test]
fn test_invalid_plus() {
let x = Regexp::new(&String::from("+"));
assert!(matches!(x, Err(ParsingError::Plus{s: _, pos: 0})));
}
#[test]
fn test_invalid_closing_parenthesis() {
let x = Regexp::new(&String::from("(a"));
assert!(matches!(x, Err(ParsingError::ClosingParenthesis{s: _, pos: 0})));
}
#[test]
fn test_invalid_opening_parenthesis() {
let x = Regexp::new(&String::from("a)"));
assert!(matches!(x, Err(ParsingError::OpeningParenthesis{s: _, pos: 1})));
}