Files
@ bac2e7f972b5
Branch filter:
Location: Regular-Expresso/tests/test_regexp.rs
bac2e7f972b5
4.0 KiB
application/rls-services+xml
added a license
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | 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().reduce().normalize();
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().reduce().normalize().eval(String::from("")));
assert!(Regexp::new(&String::from("")).unwrap().determinize().reduce().normalize().eval(String::from("")));
assert!(!Regexp::new(&String::from("")).unwrap().determinize().reduce().normalize().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().normalize().reduce();
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 = Regexp::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 = Regexp::new(&String::from("a|b|c")).unwrap().determinize().reduce().normalize();
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 = Regexp::new(&String::from("a_")).unwrap();
assert!(r.eval(String::from("a")));
assert!(!r.eval(String::from("")));
assert!(!r.eval(String::from("ab")));
let r = Regexp::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 = Regexp::new(&String::from("a_")).unwrap().determinize().reduce().normalize();
assert!(r.eval(String::from("a")));
assert!(!r.eval(String::from("")));
assert!(!r.eval(String::from("ab")));
let r = Regexp::new(&String::from("a|_")).unwrap().determinize().reduce().normalize();
assert!(r.eval(String::from("a")));
assert!(r.eval(String::from("")));
assert!(!r.eval(String::from("b")));
}
#[test]
fn test_invalid_asterisk() {
let x = Regexp::new(&String::from("*"));
assert!(matches!(x, Err(ParsingError::Asterisk{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})));
}
#[test]
fn test_invalid_empty_variant_start() {
let x = Regexp::new(&String::from("a(|b)"));
assert!(matches!(x, Err(ParsingError::EmptyAlternativeVariant)));
}
#[test]
fn test_invalid_empty_variant_end() {
let x = Regexp::new(&String::from("a|"));
assert!(matches!(x, Err(ParsingError::EmptyAlternativeVariant)));
}
#[test]
fn test_invalid_empty_variant_middle() {
let x = Regexp::new(&String::from("a||b"));
assert!(matches!(x, Err(ParsingError::EmptyAlternativeVariant)));
}
|