#[cfg(test)]
mod tests {
use crate::top_entries::TopEntries;
use crate::ByteSize;
#[test]
fn test_descending_order_is_always_maintained() {
let mut top = TopEntries::new(5);
top.insert("d".to_string(), 40);
top.insert("a".to_string(), 100);
top.insert("c".to_string(), 60);
top.insert("e".to_string(), 20);
top.insert("b".to_string(), 80);
let entries = top.get_entries();
for i in 1..entries.len() {
assert!(entries[i-1].1 >= entries[i].1,
"Entry at position {} ({}) should be larger than or equal to entry at position {} ({})",
i-1, entries[i-1].1, i, entries[i].1);
}
assert_eq!(entries[0].1, 100);
assert_eq!(entries[1].1, 80);
assert_eq!(entries[2].1, 60);
assert_eq!(entries[3].1, 40);
assert_eq!(entries[4].1, 20);
}
#[test]
fn test_capacity_enforcement_keeps_largest() {
let mut top = TopEntries::new(3);
top.insert("d".to_string(), 40);
top.insert("a".to_string(), 100);
top.insert("c".to_string(), 60);
top.insert("e".to_string(), 20); top.insert("b".to_string(), 80);
let entries = top.get_entries();
assert_eq!(entries.len(), 3, "Should only keep top 3 entries");
assert_eq!(entries[0].1, 100, "First entry should be largest (100)");
assert_eq!(entries[1].1, 80, "Second entry should be second largest (80)");
assert_eq!(entries[2].1, 60, "Third entry should be third largest (60)");
assert!(!entries.iter().any(|(_, val)| *val == 40 || *val == 20),
"Smaller values should have been dropped");
}
#[test]
fn test_order_maintenance_with_updates() {
let mut top = TopEntries::new(3);
top.insert("a".to_string(), 100);
top.insert("b".to_string(), 80);
top.insert("c".to_string(), 60);
top.insert("d".to_string(), 90);
let entries = top.get_entries();
assert_eq!(entries[0].1, 100);
assert_eq!(entries[1].1, 90);
assert_eq!(entries[2].1, 80);
top.insert("e".to_string(), 70);
let entries = top.get_entries();
assert_eq!(entries[0].1, 100);
assert_eq!(entries[1].1, 90);
assert_eq!(entries[2].1, 80);
}
#[test]
fn test_equal_values_maintain_order() {
let mut top = TopEntries::new(4);
top.insert("a".to_string(), 100);
top.insert("b".to_string(), 100);
top.insert("c".to_string(), 80);
top.insert("d".to_string(), 80);
let entries = top.get_entries();
assert_eq!(entries.len(), 4);
assert_eq!(entries[0].1, 100);
assert_eq!(entries[1].1, 100);
assert_eq!(entries[2].1, 80);
assert_eq!(entries[3].1, 80);
}
#[test]
fn test_capacity_edge_cases() {
let mut top = TopEntries::new(1);
top.insert("a".to_string(), 50);
top.insert("b".to_string(), 100);
top.insert("c".to_string(), 75);
let entries = top.get_entries();
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].1, 100, "Should keep only the largest value");
let mut top = TopEntries::new(2);
top.insert("a".to_string(), 100);
top.insert("b".to_string(), 80);
top.insert("c".to_string(), 90); top.insert("d".to_string(), 85);
let entries = top.get_entries();
assert_eq!(entries[0].1, 100);
assert_eq!(entries[1].1, 90);
}
#[test]
fn test_large_volume_maintains_invariants() {
let mut top = TopEntries::new(5);
for i in (0..1000).rev() {
top.insert(format!("item_{}", i), i as u64);
}
let entries = top.get_entries();
assert_eq!(entries.len(), 5);
for i in 0..5 {
assert_eq!(entries[i].1, 999 - i as u64);
}
top.insert("new1".to_string(), 999); top.insert("new2".to_string(), 994);
let entries = top.get_entries();
for i in 1..entries.len() {
assert!(entries[i-1].1 >= entries[i].1,
"Values should be in descending order: {} should be >= {}",
entries[i-1].1, entries[i].1);
}
assert_eq!(entries.len(), 5);
assert!(entries.iter().all(|(_, val)| *val >= 995 && *val <= 999),
"All values should be between 995 and 999");
}
#[test]
fn test_bytes_format() {
assert_eq!(0_u64.format_size(), "0 bytes");
assert_eq!(1_u64.format_size(), "1 bytes");
assert_eq!(515_u64.format_size(), "515 bytes");
assert_eq!(1023_u64.format_size(), "1023 bytes");
}
#[test]
fn test_kilobytes_format() {
assert_eq!((1024_u64).format_size(), "1.00 KB");
assert_eq!((1024_u64 + 512).format_size(), "1.50 KB");
assert_eq!((2047_u64).format_size(), "2.00 KB");
assert_eq!((1024_u64 * 1024 - 1).format_size(), "1024.00 KB");
}
#[test]
fn test_megabytes_format() {
assert_eq!((1024_u64 * 1024).format_size(), "1.00 MB");
assert_eq!((1024_u64 * 1024 + 1024 * 512).format_size(), "1.50 MB");
assert_eq!((2_u64 * 1024 * 1024 - 1).format_size(), "2.00 MB");
assert_eq!((1024_u64 * 1024 * 1024 - 1).format_size(), "1024.00 MB");
}
#[test]
fn test_gigabytes_format() {
assert_eq!((1024_u64 * 1024 * 1024).format_size(), "1.00 GB");
assert_eq!((1024_u64 * 1024 * 1024 + 1024 * 1024 * 512).format_size(), "1.50 GB");
assert_eq!((2_u64 * 1024 * 1024 * 1024 - 1).format_size(), "2.00 GB");
assert_eq!((1024_u64 * 1024 * 1024 * 1024 - 1).format_size(), "1024.00 GB");
}
#[test]
fn test_terabytes_format() {
assert_eq!((1024_u64 * 1024 * 1024 * 1024).format_size(), "1.00 TB");
assert_eq!(
(1024_u64 * 1024 * 1024 * 1024 + 1024 * 1024 * 1024 * 512).format_size(),
"1.50 TB"
);
assert_eq!(
(15_u64 * 1024 * 1024 * 1024 * 1024).format_size(),
"15.00 TB"
);
}
#[test]
fn test_boundary_values() {
let kb = 1024_u64;
let mb = kb * 1024;
let gb = mb * 1024;
let tb = gb * 1024;
assert_eq!((kb - 1).format_size(), "1023 bytes");
assert_eq!(kb.format_size(), "1.00 KB");
assert_eq!((mb - 1).format_size(), "1024.00 KB");
assert_eq!(mb.format_size(), "1.00 MB");
assert_eq!((gb - 1).format_size(), "1024.00 MB");
assert_eq!(gb.format_size(), "1.00 GB");
assert_eq!((tb - 1).format_size(), "1024.00 GB");
assert_eq!(tb.format_size(), "1.00 TB");
}
#[test]
fn test_precise_decimal_formatting() {
let size = 1024_u64 + 1; assert_eq!(size.format_size(), "1.00 KB");
let size = 1024_u64 + 512; assert_eq!(size.format_size(), "1.50 KB");
let size = (1024_u64 * 1024) + 1024 * 51; assert_eq!(size.format_size(), "1.05 MB");
}
#[test]
fn test_zero_and_small_values() {
assert_eq!(0_u64.format_size(), "0 bytes");
assert_eq!(1_u64.format_size(), "1 bytes");
assert_eq!(10_u64.format_size(), "10 bytes");
}
}