use gf2::*;
use std::{
io::{
self,
Write,
},
path::Path,
};
use utilities_rs::Stopwatch;
fn main() {
let data_file_path = loop {
print!("Data file name (x to exit ...): ");
io::stdout().flush().ok();
let mut input = String::new();
if io::stdin().read_line(&mut input).is_err() {
eprintln!("Failed to read input. Try again ...");
continue;
}
let trimmed = input.trim();
if trimmed.eq_ignore_ascii_case("x") {
return;
}
if Path::new(trimmed).exists() {
break trimmed.to_string();
}
else {
eprintln!("Failed to open '{}'. Please try again ...", trimmed);
}
};
let file_content =
std::fs::read_to_string(&data_file_path).unwrap_or_else(|_| panic!("Failed to open '{}'", data_file_path));
let filtered_lines: Vec<String> = file_content
.lines()
.map(str::trim)
.filter(|line| !line.is_empty() && !line.starts_with('#'))
.map(str::to_string)
.collect();
let mut num_tests: usize = 0;
for pair in filtered_lines.chunks(2) {
if pair.len() < 2 {
break; }
let matrix_string = &pair[0];
let coeffs_string = &pair[1];
let m = BitMatrix::<usize>::from_string(matrix_string).unwrap_or_else(|| {
eprintln!("Failed to parse a bit-matrix from file: '{}'", data_file_path);
std::process::exit(1);
});
let coeffs = BitVector::<usize>::from_string(coeffs_string).unwrap_or_else(|| {
eprintln!("Failed to parse a characteristic polynomial from file: '{}'", data_file_path);
std::process::exit(2);
});
let canned = BitPolynomial::<usize>::from_coefficients(coeffs);
num_tests += 1;
println!("Test {} of {}: Matrix is {} x {} ... ", num_tests, num_tests, m.rows(), m.cols());
let sw = Stopwatch::default();
let computed = m.characteristic_polynomial();
let elapsed = sw.elapsed();
println!("done in {}.", Stopwatch::format_seconds(elapsed));
if computed != canned {
println!("TEST {} FAILED! Matrix:\n{}", num_tests, m);
println!("Computed characteristic: {}", computed);
println!("Pre-canned characteristic: {}", canned);
std::process::exit(1);
}
}
println!("\n Congratulations: All {} tests passed!", num_tests);
}