#![allow(clippy::unreadable_literal)]
#[cfg(feature = "alloc")]
use {
crate::format::TimeFormatter,
crate::Error,
alloc::{format, vec},
};
use super::{check_all, MockTime};
#[test]
fn test_larger_than_int_max_formats_are_returned_verbatim() {
let times = [
MockTime::new(1970, 1, 1, 0, 0, 0, 0, 4, 1, 0, false, 0, ""),
MockTime::new(-1970, 1, 1, 0, 0, 0, 0, 4, 1, 0, false, 0, ""),
];
for format in [
"%100000000000000000000c",
"%1000000000000c",
"%10000000000c",
"%2147483648c", ] {
check_all(×, format, &[format, format]);
}
}
#[test]
#[cfg(feature = "alloc")]
fn test_format_specifiers_large_width_success() {
let specifiers = [
"Y", "C", "y", "m", "B", "b", "d", "e", "j", "H", "k", "I", "l", "P", "p", "M", "S", "L",
"N", "z", ":z", "::z", ":::z", "Z", "A", "a", "u", "w", "G", "g", "V", "U", "W", "s", "n",
"t", "c", "D", "F", "v", "r", "R", "T", "X",
];
let width = 2 * usize::from(u16::MAX);
let time = MockTime::new(
2021, 12, 31, 23, 59, 60, 987654321, 5, 365, 1640995200, false, 3600, "CET", );
for spec in specifiers {
let fmt_str = format!("|%{width}{spec}|");
let mut buf = vec![0u8; width + 2];
let result = TimeFormatter::new(&time, fmt_str.as_bytes()).fmt(&mut buf.as_mut_slice());
result.unwrap_or_else(|_| panic!("Failed for specifier '{spec}' with width {width}"));
let output = core::str::from_utf8(&buf).expect("Output not valid UTF-8");
match &buf[..] {
[b'|', inner @ .., b'|'] => {
assert_eq!(
inner.len(),
width,
"bad len for '{spec}': expected {width}, got {got}",
got = inner.len()
);
}
_ => panic!("Output not properly quoted for specifier '{spec}': {output}"),
};
}
}
#[test]
#[cfg(feature = "alloc")]
fn test_format_specifiers_int_max_fail() {
let specifiers = [
"Y", "C", "y", "m", "B", "b", "d", "e", "j", "H", "k", "I", "l", "P", "p", "M", "S", "L",
"N", "z", ":z", "::z", ":::z", "Z", "A", "a", "u", "w", "G", "g", "V", "U", "W", "s", "n",
"t", "c", "D", "F", "v", "r", "R", "T", "X",
];
let width = usize::try_from(i32::MAX).unwrap();
let time = MockTime::new(
2021, 12, 31, 23, 59, 60, 987654321, 5, 365, 1640995200, false, 3600, "CET", );
for spec in specifiers {
let fmt_str = format!("'%{width}{spec}'");
let mut buf = [0u8; 100];
let err = TimeFormatter::new(&time, fmt_str.as_bytes())
.fmt(&mut &mut buf[..])
.unwrap_err();
assert!(
matches!(err, Error::FormattedStringTooLarge),
"Expected write failure for specifier '{spec}' with width {width} but got unexpected error: {err:?}",
);
}
}