//! 用小布尔域的穷举结果独立核对真实规则证明器和它给出的反例。 use super::conditions; #[test] fn boolean_rule_proofs_match_enumeration_and_report_real_counterexamples() { let mut random = 0x9e3779b97f4a7c15_u64; let mut next = || { random = random .wrapping_mul(6364136223846793005) .wrapping_add(1442695040888963407); random }; for case in 0..512 { let dimensions = case % 4 + 1; let rows = (0..next() as usize % 8 + 1) .map(|_| (0..dimensions).map(|_| next() % 3).collect::>()) .collect::>(); let matches = |assignment: usize| { rows.iter() .filter(|row| { row.iter().enumerate().all(|(i, pattern)| { *pattern == 0 || (*pattern == 2) == (assignment & (1 << i) != 0) }) }) .count() }; let valid = (0..1 << dimensions).all(|assignment| matches(assignment) == 1); let inputs = (0..dimensions) .map(|i| format!("x{i}:Bool")) .collect::>() .join(","); let columns = (0..dimensions) .map(|i| format!("x{i}")) .collect::>() .join("|"); let mut text = format!("rule pick({inputs})(out:Int)\n|{columns}|out|\n"); for (index, row) in rows.iter().enumerate() { let cells = row .iter() .map(|n| ["-", "false", "true"][*n as usize]) .collect::>() .join("|"); text.push_str(&format!("|{cells}|{index}|\n")); } let checked = conditions(&text); assert_eq!(checked.is_ok(), valid, "case {case}: {text}"); if let Err(error) = checked { assert_eq!(error.code, "C009", "{error:?}"); let mut witness = 0; let mut fixed = 0; for i in 0..dimensions { let yes = error.message.contains(&format!("x{i} = true")); let no = error.message.contains(&format!("x{i} = false")); let any = error.message.contains(&format!("x{i} = ")); assert_eq!( usize::from(yes) + usize::from(no) + usize::from(any), 1, "missing witness variable: {error}" ); witness |= usize::from(yes) << i; fixed |= usize::from(!any) << i; } // 自由变量的所有补全都必须满足反例,不能只选一个值自证。 for completion in (0..1 << dimensions).filter(|value| value & fixed == witness) { if error.message.contains("overlap:") { assert!( matches(completion) > 1, "false overlap witness: {error}; {text}" ); } else { assert_eq!( matches(completion), 0, "false coverage witness: {error}; {text}" ); } } } } }