use pgn4::{GameResult, Variant, PGN4}; use std::collections::{HashMap, HashSet}; use std::fs; #[test] fn all_files() { let mut bracket_keys = HashSet::new(); let mut success = 0; let mut count = 0; let mut variants: HashMap = Default::default(); for file in fs::read_dir("../tools/raw/").unwrap() { let f = file.unwrap(); let pgn_str = match fs::read_to_string(f.path()) { Ok(s) => s, Err(e) => { eprintln!("Error loading {:?}: {:?}", f.path(), e); panic!() } }; let pgn: PGN4 = match pgn_str.parse() { Ok(pog) => pog, Err(e) => { eprintln!("Error loading {:?}: {:?}", f.path(), e); panic!() } }; // println!("Working on {:?}",f.path()); let string = pgn.to_string(); //let mut expected = pgn_str.lines(); //let mut got = string.lines(); // TODO: allow whitespace to matter more again let a_w: Vec<&str> = pgn_str.split_whitespace().collect(); let b_w: Vec<&str> = string.split_whitespace().collect(); if a_w != b_w { eprintln!("Failure on {:?}", f.path()); assert_eq!(pgn_str, string, "Pgn not inverse"); } /*for (a, b) in pgn_str.lines().zip(string.lines()) { if a != b { let a_w: Vec<&str> = a.split_whitespace().collect(); let b_w: Vec<&str> = b.split_whitespace().collect(); if a_w != b_w { eprintln!("Failure on {:?}", f.path()); eprintln!("{}",pgn); assert_eq!(a, b, "Pgn not inverse"); } } }*/ if let Ok(v) = pgn.variant() { *variants.entry(v).or_insert(0) += 1; } else { eprintln!( "Failure on {:?} due to variant failing to parse: {:?}", f.path(), pgn.variant() ); } count += 1; match pgn.result() { GameResult::Error => eprintln!("Failure on {:?} due to result", f.path()), _ => { success += 1; } } for bracket in pgn.bracketed { if bracket.0 == "Termination" { bracket_keys.insert(bracket.1); } } } println!("{}/{}", success, count); let mut variant_count = variants.into_iter().collect::>(); variant_count.sort_by(|a, b| a.1.cmp(&b.1)); variant_count.reverse(); // println!("{:?}", bracket_keys); let mut current_total = 0; for (v, n) in &variant_count { current_total += n; println!("{} games using {}", n, v); /* if current_total > (count * 9) / 10 { break; }*/ } panic!("Show msg {:?}", variant_count[0].1); }