automaton

An automaton library & basic programs written in Rust
git clone git://git.ethandl.dev/automaton
Log | Files | Refs | README

nfa_tests.rs (2911B)


      1 use crate::{
      2     nfa::NFA,
      3     tests::testlib::util::{rand_string, read_directory},
      4     Automaton, Encodable, FiniteStateAutomaton,
      5 };
      6 use std::fs;
      7 
      8 #[test]
      9 fn run_static_tests() {
     10     const NUM_EXTS: usize = 2;
     11     let expected_exts: [&str; NUM_EXTS] = [".input", ".expect"];
     12     let mut expected_files: [String; NUM_EXTS] = ["".to_owned(), "".to_owned()];
     13     let mut expected_paths: [String; NUM_EXTS] = ["".to_owned(), "".to_owned()];
     14 
     15     let nfas = read_directory("NFAs").expect("Unable to read items in NFAs directory");
     16     let nfa_tests =
     17         read_directory("NFAs/tests").expect("Unable to read items in the NFAs/tests directory");
     18 
     19     for (filename, file) in nfas.iter() {
     20         for i in 0..NUM_EXTS {
     21             expected_files[i] = format!("{}{}", filename, expected_exts[i]);
     22             expected_paths[i] = format!("NFAs/tests/{}{}", filename, expected_exts[i]);
     23         }
     24         if expected_files
     25             .iter()
     26             .all(|fname| nfa_tests.contains_key(fname))
     27         {
     28             let nfa = NFA::parse(file.as_str()).expect("Unable to parse NFA!");
     29             let inputs: Vec<String> = fs::read_to_string(expected_paths[0].clone())
     30                 .expect(&format!(
     31                     "Unable to read file {} to string",
     32                     expected_paths[0]
     33                 ))
     34                 .lines()
     35                 .map(String::from)
     36                 .collect();
     37             let expected: Vec<bool> = fs::read_to_string(expected_paths[1].clone())
     38                 .expect(&format!(
     39                     "Unable to read file {} to string",
     40                     expected_paths[0]
     41                 ))
     42                 .lines()
     43                 .map(|str| match str {
     44                     "t" => true,
     45                     _ => false,
     46                 })
     47                 .collect();
     48             assert_eq!(inputs.len(), expected.len());
     49             for ((index, input), expect) in inputs.iter().enumerate().zip(expected) {
     50                 assert_eq!(
     51                     nfa.accepts(input),
     52                     expect,
     53                     "Test no: {index}, input: {input}"
     54                 );
     55             }
     56             println!(
     57                 "Successfully ran {} tests in {}",
     58                 inputs.len(),
     59                 expected_paths[0]
     60             )
     61         }
     62     }
     63 }
     64 
     65 #[test]
     66 fn dfa_conversion() {
     67     let nfas: Vec<String> = read_directory("NFAs")
     68         .expect("Unable to read items in the NFAs directory")
     69         .values()
     70         .map(String::to_owned)
     71         .collect();
     72 
     73     for nfa_enc in nfas.iter() {
     74         let nfa = NFA::parse(nfa_enc.as_str()).expect("Unable to parse NFA!");
     75         let as_dfa = nfa.as_dfa().expect("Error converting NFA to DFA!");
     76 
     77         for i in 1..2000 {
     78             let input = rand_string(i, nfa.alphabet.iter().map(char::to_owned).collect());
     79             assert_eq!(nfa.accepts(&input), as_dfa.accepts(&input));
     80         }
     81     }
     82 }