use super::{Count, NegativeCount};
#[test]
fn a_count_is_the_magnitude_it_was_minted_from() {
assert_eq!(Count::new(0).get(), 0);
assert_eq!(Count::new(3).get(), 3);
assert_eq!(u64::from(Count::new(u64::MAX)), u64::MAX);
}
#[test]
fn the_stored_form_saturates_rather_than_truncating() {
assert_eq!(Count::new(0).to_signed(), 0);
assert_eq!(Count::new(7).to_signed(), 7);
let ceiling = i64::MAX.unsigned_abs();
assert_eq!(Count::new(ceiling).to_signed(), i64::MAX);
assert_eq!(Count::new(ceiling + 1).to_signed(), i64::MAX);
assert_eq!(Count::new(u64::MAX).to_signed(), i64::MAX);
}
#[test]
fn the_two_directions_round_trip_below_the_ceiling() {
for magnitude in [0_u64, 1, 42, 5_000_000_000, i64::MAX.unsigned_abs()] {
let count = Count::new(magnitude);
assert_eq!(
Count::from_signed(count.to_signed()),
Ok(count),
"the signed form reads back as the count that wrote it"
);
}
}
#[test]
fn a_negative_column_is_refused_by_name_rather_than_clamped() {
assert_eq!(Count::from_signed(-1), Err(NegativeCount { stored: -1 }));
assert_eq!(
Count::from_signed(i64::MIN),
Err(NegativeCount { stored: i64::MIN })
);
let refused = Count::from_signed(-7).expect_err("a negative column is not a cardinality");
assert_eq!(refused.stored(), -7);
assert!(
refused.to_string().contains("-7"),
"the sentence names the value it refused: {refused}"
);
}
#[test]
fn counts_order_by_magnitude() {
let mut counts = [
Count::new(9),
Count::new(0),
Count::new(u64::MAX),
Count::new(1),
];
counts.sort_unstable();
assert_eq!(
counts,
[
Count::new(0),
Count::new(1),
Count::new(9),
Count::new(u64::MAX)
]
);
}