#![allow(clippy::expect_used, clippy::panic, clippy::unwrap_used)]
use zerodds_float128::Float128;
const PATTERNS: &str = include_str!("oracle/patterns.txt");
const ORACLE: &str = include_str!("oracle/x86_64-gcc-14.2.txt");
struct Row {
bits: u128,
ours: String,
theirs: String,
reparsed: u128,
}
fn rows() -> Vec<Row> {
let mut ours = Vec::new();
for line in PATTERNS.lines() {
let mut parts = line.split_whitespace();
let (Some(pattern), Some(text)) = (parts.next(), parts.next()) else {
continue;
};
ours.push((
u128::from_str_radix(pattern, 16).expect("a 32-digit hex pattern"),
text.to_string(),
));
}
let mut out = Vec::new();
for (index, line) in ORACLE.lines().enumerate() {
let mut parts = line.split_whitespace();
let (Some(pattern), Some(printed), Some(reparsed)) =
(parts.next(), parts.next(), parts.next())
else {
continue;
};
let bits = u128::from_str_radix(pattern, 16).expect("a 32-digit hex pattern");
let (ours_bits, ours_text) = ours.get(index).expect("the two files line up").clone();
assert_eq!(
ours_bits, bits,
"row {index} of the two oracle files is not the same value"
);
out.push(Row {
bits,
ours: ours_text,
theirs: printed.to_string(),
reparsed: u128::from_str_radix(reparsed, 16).expect("a 32-digit hex pattern"),
});
}
assert_eq!(out.len(), 518, "the oracle file lost rows");
out
}
#[test]
fn our_parser_reads_what_gcc_prints() {
for row in rows() {
let parsed: Float128 = row.theirs.parse().unwrap_or_else(|error| {
panic!(
"libquadmath printed `{}`, which this parser rejects: {error}",
row.theirs
)
});
assert_eq!(
parsed.to_bits(),
row.bits,
"libquadmath printed `{}` for 0x{:032x}; this parser read it as 0x{:032x}",
row.theirs,
row.bits,
parsed.to_bits()
);
}
}
#[test]
fn gcc_reads_what_our_printer_writes() {
for row in rows() {
assert_eq!(
row.reparsed, row.bits,
"this crate printed `{}` for 0x{:032x}; libquadmath read it back as 0x{:032x}",
row.ours, row.bits, row.reparsed
);
}
}
#[test]
fn the_printer_still_writes_what_the_oracle_was_run_against() {
for row in rows() {
let value = Float128::from_bits(row.bits);
assert_eq!(
value.to_shortest_decimal(),
row.ours,
"the printer changed for 0x{:032x}; re-run the oracle rather than editing the record",
row.bits
);
}
}