Skip to content

Commit

Permalink
Fix existing unit test comments; Add int&hex ambiguity check
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZhihao-723 committed Dec 16, 2024
1 parent 84e0541 commit 1b7627b
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/dfa/dfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ mod tests {
}

fn create_nfa5() -> Result<NFA> {
// Should only match "ab"
// Should only match "a*"
let mut parser = RegexParser::new();
let parsed_ast = parser.parse_into_ast("a*")?;

Expand Down Expand Up @@ -686,13 +686,29 @@ mod tests {

println!("{:?}", dfa);

// Check correctness given some examples
// Should match:
// "a" or "ab"

// assert_eq!(dfa.simulate("a"), (Some(1usize), true));
assert_eq!(dfa.simulate("aa"), (Some(0usize), true));
// assert_eq!(dfa.simulate("aaa"), (Some(1usize), false));

Ok(())
}

#[test]
fn test_int_hex_ambiguity() -> Result<()> {
let mut parser = RegexParser::new();
let int_ast = parser.parse_into_ast(r"\-{0,1}\d+")?;
let mut int_nfa = NFA::new();
int_nfa.add_ast_to_nfa(&int_ast, NFA::START_STATE, NFA::ACCEPT_STATE)?;

let mut parser = RegexParser::new();
let hex_ast = parser.parse_into_ast(r"(0x){0,1}([0-9a-f]+)|([0-9A-F]+)")?;
let mut hex_nfa = NFA::new();
hex_nfa.add_ast_to_nfa(&hex_ast, NFA::START_STATE, NFA::ACCEPT_STATE)?;

let dfa = DFA::from_multiple_nfas(vec![int_nfa, hex_nfa]);

println!("{:?}", dfa);

assert_eq!(dfa.simulate("10"), (Some(0usize), true));
assert_eq!(dfa.simulate("1b"), (Some(1usize), true));

Ok(())
}
Expand Down

0 comments on commit 1b7627b

Please sign in to comment.