Files @ ac6c57072747
Branch filter:

Location: Regular-Expresso/tests/test_regexp.rs

ac6c57072747 4.5 KiB application/rls-services+xml Show Annotation Show as Raw Download as Raw
Laman
refactoring: reduction and normalization included in the DFA constructor
use regexp::RegexpNFA;
use regexp::RegexpDFA;
use regexp::ParsingError;

#[test]
fn test_eval_basic_nfa() {
	let r = RegexpNFA::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 = RegexpDFA::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_empty_nfa() {
	assert!(RegexpNFA::new(&String::from("a*")).unwrap().eval(String::from("")));
	assert!(RegexpNFA::new(&String::from("")).unwrap().eval(String::from("")));
	assert!(!RegexpNFA::new(&String::from("")).unwrap().eval(String::from("a")));
	assert!(!RegexpNFA::new(&String::from("a")).unwrap().eval(String::from("")));
}

#[test]
fn test_eval_empty_dfa() {
	assert!(RegexpDFA::new(&String::from("a*")).unwrap().eval(String::from("")));
	assert!(RegexpDFA::new(&String::from("")).unwrap().eval(String::from("")));
	assert!(!RegexpDFA::new(&String::from("")).unwrap().eval(String::from("a")));
}

#[test]
fn test_eval_asterisk_nfa() {
	let r = RegexpNFA::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 = RegexpDFA::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_alternative_nfa() {
	let r = RegexpNFA::new(&String::from("a|b|c")).unwrap();
	assert!(r.eval(String::from("a")));
	assert!(r.eval(String::from("b")));
	assert!(r.eval(String::from("c")));
	assert!(!r.eval(String::from("")));
	assert!(!r.eval(String::from("ab")));
}

#[test]
fn test_eval_alternative_dfa() {
	let r = RegexpDFA::new(&String::from("a|b|c")).unwrap();
	assert!(r.eval(String::from("a")));
	assert!(r.eval(String::from("b")));
	assert!(r.eval(String::from("c")));
	assert!(!r.eval(String::from("")));
	assert!(!r.eval(String::from("ab")));
}

#[test]
fn test_eval_lambda_nfa() {
	let r = RegexpNFA::new(&String::from("a_")).unwrap();
	assert!(r.eval(String::from("a")));
	assert!(!r.eval(String::from("")));
	assert!(!r.eval(String::from("ab")));

	let r = RegexpNFA::new(&String::from("a|_")).unwrap();
	assert!(r.eval(String::from("a")));
	assert!(r.eval(String::from("")));
	assert!(!r.eval(String::from("b")));
}

#[test]
fn test_eval_lambda_dfa() {
	let r = RegexpDFA::new(&String::from("a_")).unwrap();
	assert!(r.eval(String::from("a")));
	assert!(!r.eval(String::from("")));
	assert!(!r.eval(String::from("ab")));

	let r = RegexpDFA::new(&String::from("a|_")).unwrap();
	assert!(r.eval(String::from("a")));
	assert!(r.eval(String::from("")));
	assert!(!r.eval(String::from("b")));
}

#[test]
fn test_invalid_asterisk() {
	let x = RegexpNFA::new(&String::from("*"));
	assert!(matches!(x, Err(ParsingError::Asterisk{s: _, pos: 0})));
	let x = RegexpDFA::new(&String::from("*"));
	assert!(matches!(x, Err(ParsingError::Asterisk{s: _, pos: 0})));
}

#[test]
fn test_invalid_closing_parenthesis() {
	let x = RegexpNFA::new(&String::from("(a"));
	assert!(matches!(x, Err(ParsingError::ClosingParenthesis{s: _, pos: 0})));
	let x = RegexpDFA::new(&String::from("(a"));
	assert!(matches!(x, Err(ParsingError::ClosingParenthesis{s: _, pos: 0})));
}

#[test]
fn test_invalid_opening_parenthesis() {
	let x = RegexpNFA::new(&String::from("a)"));
	assert!(matches!(x, Err(ParsingError::OpeningParenthesis{s: _, pos: 1})));
	let x = RegexpDFA::new(&String::from("a)"));
	assert!(matches!(x, Err(ParsingError::OpeningParenthesis{s: _, pos: 1})));
}

#[test]
fn test_invalid_empty_variant_start() {
	let x = RegexpNFA::new(&String::from("a(|b)"));
	assert!(matches!(x, Err(ParsingError::EmptyAlternativeVariant)));
	let x = RegexpDFA::new(&String::from("a(|b)"));
	assert!(matches!(x, Err(ParsingError::EmptyAlternativeVariant)));
}

#[test]
fn test_invalid_empty_variant_end() {
	let x = RegexpNFA::new(&String::from("a|"));
	assert!(matches!(x, Err(ParsingError::EmptyAlternativeVariant)));
	let x = RegexpDFA::new(&String::from("a|"));
	assert!(matches!(x, Err(ParsingError::EmptyAlternativeVariant)));
}

#[test]
fn test_invalid_empty_variant_middle() {
	let x = RegexpNFA::new(&String::from("a||b"));
	assert!(matches!(x, Err(ParsingError::EmptyAlternativeVariant)));
	let x = RegexpDFA::new(&String::from("a||b"));
	assert!(matches!(x, Err(ParsingError::EmptyAlternativeVariant)));
}