From ffbb56c4a935027bf0bf0cec43733b93832bf513 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sat, 23 Sep 2023 08:08:19 +0200 Subject: [PATCH] common: port test_format --- fish-rust/src/common.rs | 11 ++++--- fish-rust/src/tests/common.rs | 61 ++++++++++++++++++++++++++++++++++- src/fish_tests.cpp | 33 ------------------- 3 files changed, 67 insertions(+), 38 deletions(-) diff --git a/fish-rust/src/common.rs b/fish-rust/src/common.rs index d102ac41f..c4d805ed6 100644 --- a/fish-rust/src/common.rs +++ b/fish-rust/src/common.rs @@ -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>(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; } diff --git a/fish-rust/src/tests/common.rs b/fish-rust/src/tests/common.rs index dd56460c8..d8c23917e 100644 --- a/fish-rust/src/tests/common.rs +++ b/fish-rust/src/tests/common.rs @@ -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)); +} diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index 5db84ccff..01100915c 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -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},