automaton

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

dfa_tests.rs (2776B)


      1 use crate::{
      2     dfa::DFA,
      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 dfas = read_directory("DFAs").expect("Unable to read items in DFAs directory");
     16     let dfa_tests =
     17         read_directory("DFAs/tests").expect("Unable to read items in the DFAs/tests directory");
     18 
     19     for (filename, file) in dfas.iter() {
     20         for i in 0..NUM_EXTS {
     21             expected_files[i] = format!("{}{}", filename, expected_exts[i]);
     22             expected_paths[i] = format!("DFAs/tests/{}{}", filename, expected_exts[i]);
     23         }
     24         if expected_files
     25             .iter()
     26             .all(|fname| dfa_tests.contains_key(fname))
     27         {
     28             let dfa = DFA::parse(file.as_str()).expect("Unable to parse DFA!");
     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 (input, expect) in inputs.iter().zip(expected) {
     50                 assert_eq!(dfa.accepts(input), expect);
     51             }
     52             println!(
     53                 "Successfully ran {} tests in {}",
     54                 inputs.len(),
     55                 expected_paths[0]
     56             )
     57         }
     58     }
     59 }
     60 
     61 #[test]
     62 fn dfa_conversion() {
     63     let dfas: Vec<String> = read_directory("DFAs")
     64         .expect("Unable to read items in the DFAs directory")
     65         .values()
     66         .map(String::to_owned)
     67         .collect();
     68 
     69     for dfa_enc in dfas.iter() {
     70         let dfa = DFA::parse(dfa_enc.as_str()).expect("Unable to parse DFA!");
     71         let as_dfa = dfa.as_dfa().expect("Error converting DFA to DFA!");
     72 
     73         for i in 1..2000 {
     74             let input = rand_string(i, dfa.alphabet.iter().map(char::to_owned).collect());
     75             assert_eq!(dfa.accepts(&input), as_dfa.accepts(&input));
     76         }
     77     }
     78 }