#![warn(missing_docs)]
pub const PAYLOAD_SIZES: &[usize] = &[
32, 128, 1_024, 4_096, 16_384, 65_536, 262_144, 1_048_576, 4_194_304, ];
pub const MAX_PAYLOAD: usize = 4_194_304;
#[must_use]
pub fn make_payload(size: usize) -> Vec<u8> {
let mut out = Vec::with_capacity(size);
let mut state: u64 = 0xDEAD_BEEF_CAFE_BABE;
for _ in 0..size {
state = state
.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1);
out.push((state >> 33) as u8);
}
out
}
#[must_use]
pub fn size_label(size: usize) -> String {
if size < 1024 {
format!("{size}B")
} else if size < 1024 * 1024 {
format!("{}KiB", size / 1024)
} else {
format!("{}MiB", size / (1024 * 1024))
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn payload_sizes_are_sorted_ascending() {
for pair in PAYLOAD_SIZES.windows(2) {
assert!(pair[0] < pair[1]);
}
}
#[test]
fn max_payload_matches_last_entry() {
assert_eq!(MAX_PAYLOAD, *PAYLOAD_SIZES.last().unwrap());
}
#[test]
fn make_payload_is_deterministic() {
let a = make_payload(128);
let b = make_payload(128);
assert_eq!(a, b);
}
#[test]
fn make_payload_is_not_all_zero() {
let p = make_payload(1024);
assert!(p.iter().any(|&b| b != 0));
}
#[test]
fn size_label_formats() {
assert_eq!(size_label(32), "32B");
assert_eq!(size_label(1024), "1KiB");
assert_eq!(size_label(1_048_576), "1MiB");
}
}