pheon/tests/numeric_packing.rs
Jan Petykiewicz aa2132dc6e fix(packing): validate unsigned and floating point inputs
Compare inputs against integer bounds in f64 so unsigned minima and rounded f32 maxima cannot bypass validation. Reject nonfinite floats and preserve packed bytes in native source slots.
2026-11-14 14:36:25 -08:00

103 lines
3.6 KiB
Rust

use klamath_rs_ext::*;
use std::{mem::size_of, slice};
// Read the packed prefix of each source slot, as the Python wrapper does.
fn packed_bytes<T>(values: &[T], width: usize) -> Vec<u8> {
let bytes = unsafe { slice::from_raw_parts(values.as_ptr().cast::<u8>(), size_of_val(values)) };
bytes
.chunks_exact(size_of::<T>())
.flat_map(|slot| slot[..width].iter().copied())
.collect()
}
macro_rules! check_int2 {
($ty:ty, $function:ident) => {{
let mut values = [0 as $ty, 1 as $ty, 256 as $ty, 32767 as $ty];
assert_eq!(
unsafe { $function(values.as_mut_ptr(), values.len()) },
0 as $ty
);
assert_eq!(packed_bytes(&values, 2), [0, 0, 0, 1, 1, 0, 0x7f, 0xff]);
}};
}
macro_rules! check_int4 {
($ty:ty, $function:ident) => {{
let mut values = [0 as $ty, 1 as $ty, 65536 as $ty, 2147483520_i64 as $ty];
assert_eq!(
unsafe { $function(values.as_mut_ptr(), values.len()) },
0 as $ty
);
assert_eq!(
packed_bytes(&values, 4),
[0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0x7f, 0xff, 0xff, 0x80]
);
}};
}
#[test]
fn all_source_types_pack_to_big_endian_integer_bytes() {
check_int2!(u16, u16_to_i16);
check_int2!(u32, u32_to_i16);
check_int2!(u64, u64_to_i16);
check_int2!(i16, i16_to_i16);
check_int2!(i32, i32_to_i16);
check_int2!(i64, i64_to_i16);
check_int2!(f32, f32_to_i16);
check_int2!(f64, f64_to_i16);
check_int4!(u32, u32_to_i32);
check_int4!(u64, u64_to_i32);
check_int4!(i32, i32_to_i32);
check_int4!(i64, i64_to_i32);
check_int4!(f32, f32_to_i32);
check_int4!(f64, f64_to_i32);
}
#[test]
fn signed_limits_and_unsigned_overflows_are_checked() {
let mut signed = [i64::from(i16::MIN), -1];
assert_eq!(unsafe { i64_to_i16(signed.as_mut_ptr(), signed.len()) }, 0);
assert_eq!(packed_bytes(&signed, 2), [0x80, 0, 0xff, 0xff]);
let mut signed = [i64::from(i32::MIN), i64::from(i32::MAX)];
assert_eq!(unsafe { i64_to_i32(signed.as_mut_ptr(), signed.len()) }, 0);
assert_eq!(
packed_bytes(&signed, 4),
[0x80, 0, 0, 0, 0x7f, 0xff, 0xff, 0xff]
);
for value in [32768_u64, u64::MAX] {
let mut input = [value];
assert_eq!(unsafe { u64_to_i16(input.as_mut_ptr(), 1) }, value);
assert_eq!(input, [value]);
}
for value in [2147483648_u64, u64::MAX] {
let mut input = [value];
assert_eq!(unsafe { u64_to_i32(input.as_mut_ptr(), 1) }, value);
assert_eq!(input, [value]);
}
}
#[test]
fn nonfinite_floats_and_rounded_up_f32_limit_are_rejected() {
for convert in [f32_to_i16, f32_to_i32] {
for value in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
let mut input = [value];
let result = unsafe { convert(input.as_mut_ptr(), 1) };
assert_ne!(result, 0.0);
assert_eq!(input[0].to_bits(), value.to_bits());
}
}
for convert in [f64_to_i16, f64_to_i32] {
for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
let mut input = [value];
assert_ne!(unsafe { convert(input.as_mut_ptr(), 1) }, 0.0);
assert_eq!(input[0].to_bits(), value.to_bits());
}
}
let mut input = [2147483648_f32];
assert_eq!(unsafe { f32_to_i32(input.as_mut_ptr(), 1) }, 2147483648_f32);
assert_eq!(input, [2147483648_f32]);
let mut input = [-2147483648_f32];
assert_eq!(unsafe { f32_to_i32(input.as_mut_ptr(), 1) }, 0.0);
assert_eq!(packed_bytes(&input, 4), [0x80, 0, 0, 0]);
}