#![allow(clippy::unwrap_used, clippy::panic)]
use dfajit::{JitDfa, TransitionTable};
use matchkit::Match;
#[test]
fn test_end_to_end_workflow() {
let mut table = TransitionTable::new(6, 256).unwrap();
for state in 0..6 {
for b in 0..=255u8 {
table.set_transition(state, b, 0);
}
}
table.set_transition(0, b'c', 1);
table.set_transition(0, b'b', 2);
table.set_transition(1, b'a', 3);
table.set_transition(2, b'a', 4);
table.set_transition(3, b't', 5); table.set_transition(3, b'r', 5); table.set_transition(4, b't', 5);
table.add_accept(5, 0); table.set_pattern_length(0, 3);
let minimized = table.minimize().unwrap_or(table.clone());
let bytes = minimized.to_bytes();
let restored = TransitionTable::from_bytes(&bytes).unwrap();
let jit = JitDfa::compile(&restored).unwrap();
let input = b"I have a cat and a bat in my car";
let mut matches = vec![Match::from_parts(0, 0, 0); 10];
let count = jit.scan(input, &mut matches);
assert_eq!(count, 3);
assert_eq!(matches[0].start, 9);
assert_eq!(matches[0].end, 12);
assert_eq!(matches[1].start, 19);
assert_eq!(matches[1].end, 22);
assert_eq!(matches[2].start, 29);
assert_eq!(matches[2].end, 32);
}
#[test]
fn test_builder_workflow() {
let patterns: &[&[u8]] = &[b"sec", b"ret"];
let jit = JitDfa::from_patterns(patterns).unwrap();
let mut matches = vec![Match::from_parts(0, 0, 0); 10];
let input = b"find the secret token";
let count = jit.scan(input, &mut matches);
assert_eq!(count, 2);
assert_eq!(matches[0].pattern_id, 0); assert_eq!(matches[1].pattern_id, 1); assert_eq!(matches[0].start, 9);
assert_eq!(matches[1].start, 12);
}