use rust_tree::rust_tree::utils::{bytes_to_human_readable, is_broken_pipe_error};
use std::io::{Error, ErrorKind};
#[test]
fn test_bytes_to_human_readable_small() {
assert_eq!(bytes_to_human_readable(0), "0 B");
assert_eq!(bytes_to_human_readable(1), "1 B");
assert_eq!(bytes_to_human_readable(512), "512 B");
assert_eq!(bytes_to_human_readable(1023), "1023 B");
}
#[test]
fn test_bytes_to_human_readable_kilobytes() {
assert_eq!(bytes_to_human_readable(1024), "1.0 KB");
assert_eq!(bytes_to_human_readable(1536), "1.5 KB");
assert_eq!(bytes_to_human_readable(2048), "2.0 KB");
assert_eq!(bytes_to_human_readable(10240), "10.0 KB");
assert_eq!(bytes_to_human_readable(102400), "100.0 KB");
}
#[test]
fn test_bytes_to_human_readable_megabytes() {
assert_eq!(bytes_to_human_readable(1024 * 1024), "1.0 MB");
assert_eq!(bytes_to_human_readable(1024 * 1024 + 512 * 1024), "1.5 MB");
assert_eq!(bytes_to_human_readable(2 * 1024 * 1024), "2.0 MB");
assert_eq!(bytes_to_human_readable(10 * 1024 * 1024), "10.0 MB");
assert_eq!(bytes_to_human_readable(100 * 1024 * 1024), "100.0 MB");
}
#[test]
fn test_bytes_to_human_readable_gigabytes() {
let gb = 1024_u64 * 1024 * 1024;
assert_eq!(bytes_to_human_readable(gb), "1.0 GB");
assert_eq!(bytes_to_human_readable(gb + gb / 2), "1.5 GB");
assert_eq!(bytes_to_human_readable(2 * gb), "2.0 GB");
assert_eq!(bytes_to_human_readable(10 * gb), "10.0 GB");
}
#[test]
fn test_bytes_to_human_readable_terabytes() {
let tb = 1024_u64 * 1024 * 1024 * 1024;
assert_eq!(bytes_to_human_readable(tb), "1.0 TB");
assert_eq!(bytes_to_human_readable(tb + tb / 2), "1.5 TB");
assert_eq!(bytes_to_human_readable(2 * tb), "2.0 TB");
}
#[test]
fn test_bytes_to_human_readable_very_large() {
let pb = 1024_u64 * 1024 * 1024 * 1024 * 1024;
assert!(bytes_to_human_readable(pb).contains(" B"));
assert!(bytes_to_human_readable(u64::MAX).contains(" B"));
}
#[test]
fn test_bytes_to_human_readable_edge_cases() {
assert_eq!(bytes_to_human_readable(1023), "1023 B");
assert_eq!(bytes_to_human_readable(1024), "1.0 KB");
assert_eq!(bytes_to_human_readable(1025), "1.0 KB");
assert_eq!(bytes_to_human_readable(1024 * 1024 - 1), "1024.0 KB");
assert_eq!(bytes_to_human_readable(1024 * 1024), "1.0 MB");
assert_eq!(bytes_to_human_readable(1024 * 1024 + 1), "1.0 MB");
}
#[test]
fn test_bytes_to_human_readable_precision() {
let kb = 1024_u64;
assert_eq!(bytes_to_human_readable(kb + 102), "1.1 KB"); assert_eq!(bytes_to_human_readable(kb + 256), "1.2 KB"); assert_eq!(bytes_to_human_readable(kb + 512), "1.5 KB"); assert_eq!(bytes_to_human_readable(kb + 768), "1.8 KB"); }
#[test]
fn test_is_broken_pipe_error_true_cases() {
let broken_pipe = Error::new(ErrorKind::BrokenPipe, "broken pipe");
assert!(is_broken_pipe_error(&broken_pipe));
}
#[test]
fn test_is_broken_pipe_error_false_cases() {
let not_found = Error::new(ErrorKind::NotFound, "file not found");
assert!(!is_broken_pipe_error(¬_found));
let permission_denied = Error::new(ErrorKind::PermissionDenied, "permission denied");
assert!(!is_broken_pipe_error(&permission_denied));
let already_exists = Error::new(ErrorKind::AlreadyExists, "already exists");
assert!(!is_broken_pipe_error(&already_exists));
let invalid_input = Error::new(ErrorKind::InvalidInput, "invalid input");
assert!(!is_broken_pipe_error(&invalid_input));
let interrupted = Error::new(ErrorKind::Interrupted, "interrupted");
assert!(!is_broken_pipe_error(&interrupted));
let write_zero = Error::new(ErrorKind::WriteZero, "write zero");
assert!(!is_broken_pipe_error(&write_zero));
let unexpected_eof = Error::new(ErrorKind::UnexpectedEof, "unexpected eof");
assert!(!is_broken_pipe_error(&unexpected_eof));
}
#[test]
fn test_is_broken_pipe_error_with_source() {
let source_error = Error::new(ErrorKind::BrokenPipe, "source broken pipe");
let wrapper_error = Error::new(ErrorKind::Other, "wrapper error");
assert!(is_broken_pipe_error(&source_error));
assert!(!is_broken_pipe_error(&wrapper_error));
}
#[test]
fn test_bytes_to_human_readable_consistency() {
let test_value = 1536; assert_eq!(
bytes_to_human_readable(test_value),
bytes_to_human_readable(test_value)
);
let large_value = 5 * 1024 * 1024 * 1024; assert_eq!(
bytes_to_human_readable(large_value),
bytes_to_human_readable(large_value)
);
}
#[test]
fn test_bytes_to_human_readable_monotonic() {
let sizes = vec![
1024, 2048, 3072, 4096, 5120, ];
let formatted: Vec<String> = sizes
.iter()
.map(|&size| bytes_to_human_readable(size))
.collect();
for formatted_size in &formatted {
assert!(formatted_size.ends_with(" KB"));
}
}
#[test]
fn test_is_broken_pipe_error_all_error_kinds() {
let error_kinds = vec![
ErrorKind::NotFound,
ErrorKind::PermissionDenied,
ErrorKind::ConnectionRefused,
ErrorKind::ConnectionReset,
ErrorKind::ConnectionAborted,
ErrorKind::NotConnected,
ErrorKind::AddrInUse,
ErrorKind::AddrNotAvailable,
ErrorKind::BrokenPipe,
ErrorKind::AlreadyExists,
ErrorKind::WouldBlock,
ErrorKind::InvalidInput,
ErrorKind::InvalidData,
ErrorKind::TimedOut,
ErrorKind::WriteZero,
ErrorKind::Interrupted,
ErrorKind::UnexpectedEof,
ErrorKind::Other,
];
for kind in error_kinds {
let error = Error::new(kind, "test error");
let is_broken = is_broken_pipe_error(&error);
let expected = matches!(kind, ErrorKind::BrokenPipe);
assert_eq!(
is_broken, expected,
"ErrorKind::{:?} returned unexpected result",
kind
);
}
}