#![allow(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::cast_possible_wrap,
clippy::float_cmp,
clippy::approx_constant
)]
use velesdb_core::{Error, Result};
fn validate_offset(offset: u64) -> Result<usize> {
usize::try_from(offset)
.map_err(|_| Error::Overflow(format!("Offset {offset} exceeds usize::MAX")))
}
#[test]
fn test_usize_try_from_u64_max_on_64bit() {
let result = validate_offset(u64::MAX);
assert!(
result.is_ok(),
"max offset should validate on 64-bit targets"
);
assert_eq!(result.unwrap(), usize::MAX);
}
#[test]
fn test_production_dimension_validation_boundary() {
assert!(velesdb_core::validate_dimension(velesdb_core::MAX_DIMENSION).is_ok());
let err = velesdb_core::validate_dimension(velesdb_core::MAX_DIMENSION + 1)
.expect_err("dimension above MAX_DIMENSION must be rejected");
assert!(
matches!(err, Error::InvalidDimension { max, .. } if max == velesdb_core::MAX_DIMENSION)
);
}
#[test]
fn test_f64_to_f32_normal_range() {
let v = serde_json::json!(3.14159);
let as_f32 = v.as_f64().map(|f| f as f32);
assert_eq!(as_f32, Some(3.14159_f64 as f32));
assert!(as_f32.unwrap().is_finite());
}
#[test]
fn test_clamped_conversion_oob() {
let value: f64 = 1.5; let clamped = (value.clamp(0.0, 1.0) * 100.0) as u32;
assert_eq!(clamped, 100, "OOB value must saturate at 100, not 150");
let unclamped = (value * 100.0) as u32;
assert_ne!(
unclamped, clamped,
"clamp must change the result for OOB input"
);
assert_eq!(unclamped, 150);
}
#[test]
fn test_vector_dimension_bounds_realistic() {
for dim in [128usize, 256, 384, 512, 768, 1024, 1536, 3072] {
assert!(
velesdb_core::validate_dimension(dim).is_ok(),
"Dimension {dim} should be valid"
);
}
assert!(velesdb_core::validate_dimension(velesdb_core::MAX_DIMENSION).is_ok());
assert!(velesdb_core::validate_dimension(velesdb_core::MAX_DIMENSION + 1).is_err());
assert!(velesdb_core::validate_dimension(0).is_err());
}