mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-01-19 17:12:45 +08:00
common: port test_format
This commit is contained in:
parent
408161f4d6
commit
ffbb56c4a9
|
@ -1286,7 +1286,7 @@ pub fn format_size(mut sz: i64) -> WString {
|
|||
}
|
||||
|
||||
/// Version of format_size that does not allocate memory.
|
||||
fn format_size_safe(buff: &mut [u8; 128], mut sz: u64) {
|
||||
pub fn format_size_safe(buff: &mut [u8; 128], mut sz: u64) {
|
||||
let buff_size = 128;
|
||||
let max_len = buff_size - 1; // need to leave room for a null terminator
|
||||
buff.fill(0);
|
||||
|
@ -1339,10 +1339,12 @@ fn format_safe_impl<CharT: From<u8>>(buff: &mut [CharT], size: usize, mut val: u
|
|||
let mut idx = 0;
|
||||
if val == 0 {
|
||||
buff[idx] = CharT::from(b'0');
|
||||
idx += 1;
|
||||
} else {
|
||||
// Generate the string backwards, then reverse it.
|
||||
while val != 0 {
|
||||
buff[idx] = CharT::from((val % 10) as u8 + b'0');
|
||||
idx += 1;
|
||||
val /= 10;
|
||||
}
|
||||
buff[..idx].reverse();
|
||||
|
@ -1363,10 +1365,11 @@ fn append_ull(buff: &mut [u8], val: &mut u64, inout_idx: &mut usize, max_len: us
|
|||
|
||||
fn append_str(buff: &mut [u8], s: &str, inout_idx: &mut usize, max_len: usize) {
|
||||
let mut idx = *inout_idx;
|
||||
let bytes = s.as_bytes();
|
||||
while idx < bytes.len().min(max_len) {
|
||||
buff[idx] = bytes[idx];
|
||||
let mut bytes = s.as_bytes();
|
||||
while !bytes.is_empty() && idx < max_len {
|
||||
buff[idx] = bytes[0];
|
||||
idx += 1;
|
||||
bytes = &bytes[1..];
|
||||
}
|
||||
*inout_idx = idx;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
use crate::common::{scoped_push, ScopeGuard, ScopeGuarding};
|
||||
use crate::common::{
|
||||
cstr2wcstring, format_llong_safe, format_size_safe, scoped_push, ScopeGuard, ScopeGuarding,
|
||||
};
|
||||
use crate::wchar::prelude::*;
|
||||
|
||||
#[test]
|
||||
fn test_scoped_push() {
|
||||
|
@ -73,3 +76,59 @@ fn test_assert_is_locked() {
|
|||
let _guard = lock.lock().unwrap();
|
||||
assert_is_locked!(&lock);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format() {
|
||||
// Testing formatting functions
|
||||
struct Test {
|
||||
val: u64,
|
||||
expected: &'static str,
|
||||
}
|
||||
let tests = [
|
||||
Test {
|
||||
val: 0,
|
||||
expected: "empty",
|
||||
},
|
||||
Test {
|
||||
val: 1,
|
||||
expected: "1B",
|
||||
},
|
||||
Test {
|
||||
val: 2,
|
||||
expected: "2B",
|
||||
},
|
||||
Test {
|
||||
val: 1024,
|
||||
expected: "1kB",
|
||||
},
|
||||
Test {
|
||||
val: 1870,
|
||||
expected: "1.8kB",
|
||||
},
|
||||
Test {
|
||||
val: 4322911,
|
||||
expected: "4.1MB",
|
||||
},
|
||||
];
|
||||
|
||||
for test in tests {
|
||||
let mut buff = [0_u8; 128];
|
||||
format_size_safe(&mut buff, test.val);
|
||||
assert_eq!(cstr2wcstring(&buff), WString::from_str(test.expected));
|
||||
}
|
||||
|
||||
for j in -129..=129 {
|
||||
let mut buff1 = [0_u8; 64];
|
||||
let mut buff2 = [0_u8; 64];
|
||||
format_llong_safe(&mut buff1, j);
|
||||
unsafe { libc::snprintf(buff2.as_mut_ptr().cast(), 64, "%d\0".as_ptr().cast(), j) };
|
||||
assert_eq!(cstr2wcstring(&buff1), cstr2wcstring(&buff2));
|
||||
}
|
||||
|
||||
let q = i64::MIN;
|
||||
let mut buff1 = [0_u8; 64];
|
||||
let mut buff2 = [0_u8; 64];
|
||||
format_llong_safe(&mut buff1, q);
|
||||
unsafe { libc::snprintf(buff2.as_mut_ptr().cast(), 128, "%ld\0".as_ptr().cast(), q) };
|
||||
assert_eq!(cstr2wcstring(&buff1), cstr2wcstring(&buff2));
|
||||
}
|
||||
|
|
|
@ -367,38 +367,6 @@ static void test_enum_array() {
|
|||
do_test(es.at(test_enum::gamma) == "def");
|
||||
}
|
||||
|
||||
static void test_format() {
|
||||
say(L"Testing formatting functions");
|
||||
struct {
|
||||
unsigned long long val;
|
||||
const char *expected;
|
||||
} tests[] = {{0, "empty"}, {1, "1B"}, {2, "2B"},
|
||||
{1024, "1kB"}, {1870, "1.8kB"}, {4322911, "4.1MB"}};
|
||||
for (const auto &test : tests) {
|
||||
char buff[128];
|
||||
format_size_safe(buff, test.val);
|
||||
do_test(!std::strcmp(buff, test.expected));
|
||||
}
|
||||
|
||||
for (int j = -129; j <= 129; j++) {
|
||||
char buff1[128], buff2[128];
|
||||
format_long_safe(buff1, j);
|
||||
snprintf(buff2, 128, "%d", j);
|
||||
do_test(!std::strcmp(buff1, buff2));
|
||||
|
||||
wchar_t wbuf1[128], wbuf2[128];
|
||||
format_long_safe(wbuf1, j);
|
||||
std::swprintf(wbuf2, 128, L"%d", j);
|
||||
do_test(!std::wcscmp(wbuf1, wbuf2));
|
||||
}
|
||||
|
||||
long q = LONG_MIN;
|
||||
char buff1[128], buff2[128];
|
||||
format_long_safe(buff1, q);
|
||||
snprintf(buff2, 128, "%ld", q);
|
||||
do_test(!std::strcmp(buff1, buff2));
|
||||
}
|
||||
|
||||
/// Helper to convert a narrow string to a sequence of hex digits.
|
||||
static std::string str2hex(const std::string &input) {
|
||||
std::string output;
|
||||
|
@ -5383,7 +5351,6 @@ static const test_t s_tests[]{
|
|||
{TEST_GROUP("new_parser_ad_hoc"), test_new_parser_ad_hoc},
|
||||
{TEST_GROUP("new_parser_errors"), test_new_parser_errors},
|
||||
{TEST_GROUP("error_messages"), test_error_messages},
|
||||
{TEST_GROUP("format"), test_format},
|
||||
{TEST_GROUP("convert"), test_convert},
|
||||
{TEST_GROUP("convert"), test_convert_private_use},
|
||||
{TEST_GROUP("convert_ascii"), test_convert_ascii},
|
||||
|
|
Loading…
Reference in New Issue
Block a user