use polars::df; use polars::prelude::*; use std::fs::File; use temex::{Temex, TemexTrace}; #[test] fn test_misc() { let trace = TemexTrace::try_from(&File::open("tests/traces/one_var_length_1.csv").unwrap()).unwrap(); let te = Temex::new("[p]").unwrap(); assert!(te.is_match(&trace).unwrap()); let te_b = Temex::new("[not p]").unwrap(); assert!(!te_b.is_match(&trace).unwrap()); } #[test] fn test_bad_trace() { let bad_trace_file = File::open("tests/traces/bad_trace.csv").unwrap(); let bad_trace = TemexTrace::try_from(&bad_trace_file); assert!(bad_trace.is_err()); } #[test] fn test_fizzbuzz() { let fizzbuzz_trace = TemexTrace::try_from(File::open("tests/traces/fizzbuzz.csv").unwrap()).unwrap(); let te2 = Temex::new("([true][true][Fizz or (Buzz and false)]){33}").unwrap(); assert!(te2.is_match(&fizzbuzz_trace).unwrap()); let te3 = Temex::new("([true]{4}[Buzz or (Fizz and false)]){20}").unwrap(); assert!(te3.is_match(&fizzbuzz_trace).unwrap()); let te4 = Temex::new("([true]{14}[Fizz and Buzz]){6}").unwrap(); assert!(te4.is_match(&fizzbuzz_trace).unwrap()); let te5 = Temex::new("[Buzz and Fizz]").unwrap(); let te5_matches: Vec<(usize, usize)> = te5 .find_iter(&fizzbuzz_trace) .map(|m| (m.start(), m.end())) .collect(); println!("{:?}", te5_matches); assert_eq!( te5_matches, vec![(14, 15), (29, 30), (44, 45), (59, 60), (74, 75), (89, 90)] ); } #[test] fn test_polars() { let df = df! [ "p1" => [true, true, true], "p2" => [false, false, false], "p3" => [true, false, true] ] .unwrap(); let trace = TemexTrace::try_from(df).unwrap(); let te = Temex::new("^[p1 and (p2 or p3)][p1][p1 and (p2 or p3)]$").unwrap(); assert!(te.is_match(&trace).unwrap()); } #[test] fn test_string() { let s = "p1,p2,p3,p4,p5\n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n\ 0, 0, 0, 0, 0 \n\ 1, 1, 1, 1, 1 \n"; let trace = &s.parse::().unwrap(); let trace_ = TemexTrace::try_from(s).unwrap(); assert_eq!(*trace, trace_); let te = Temex::new( "^([not (p1 or p2 or p3 or p5) and not not not p4][(p1 or p3) and not not not not not not p4]){21}$", ) .unwrap(); assert!(te.is_match(&trace).unwrap()); let s = "CPU_core1_GT_80, CPU_core2_GT_80, mem_usage_GT_40\n\ 0, 0, 0\n\ 1, 0, 0\n\ 0, 1, 0\n\ 0, 0, 1\n\ 1, 0, 1\n\ 0, 1, 1\n\ 1, 1, 0\n\ 1, 1, 0\n\ 1, 1, 0\n"; let trace2 = &s.parse::().unwrap(); let te = Temex::new("[CPU_core1_GT_80 and CPU_core2_GT_80 and not mem_usage_GT_40]*").unwrap(); assert!(te.is_match(trace2).unwrap()); }