From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id A4B969A49F for ; Mon, 8 May 2023 12:02:10 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 86B1EB23E for ; Mon, 8 May 2023 12:01:40 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Mon, 8 May 2023 12:01:39 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 4110E47B1E for ; Mon, 8 May 2023 12:01:39 +0200 (CEST) From: Lukas Wagner To: pbs-devel@lists.proxmox.com Date: Mon, 8 May 2023 12:01:36 +0200 Message-Id: <20230508100137.263413-3-l.wagner@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230508100137.263413-1-l.wagner@proxmox.com> References: <20230508100137.263413-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.177 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pbs-devel] [PATCH proxmox 2/3] human-byte: move tests to their sub module X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 08 May 2023 10:02:10 -0000 The `#[cfg(test)]` directive ensures that the tests are not compiled for non-test builds. Signed-off-by: Lukas Wagner --- proxmox-human-byte/src/lib.rs | 241 +++++++++++++++++----------------- 1 file changed, 123 insertions(+), 118 deletions(-) diff --git a/proxmox-human-byte/src/lib.rs b/proxmox-human-byte/src/lib.rs index 7be16a5..ef680b4 100644 --- a/proxmox-human-byte/src/lib.rs +++ b/proxmox-human-byte/src/lib.rs @@ -226,133 +226,138 @@ impl std::str::FromStr for HumanByte { proxmox_serde::forward_deserialize_to_from_str!(HumanByte); proxmox_serde::forward_serialize_to_display!(HumanByte); -#[test] -fn test_human_byte_parser() -> Result<(), Error> { - assert!("-10".parse::().is_err()); // negative size +#[cfg(test)] +mod tests { + use super::*; - fn do_test(v: &str, size: f64, unit: SizeUnit, as_str: &str) -> Result<(), Error> { - let h: HumanByte = v.parse()?; + #[test] + fn test_human_byte_parser() -> Result<(), Error> { + assert!("-10".parse::().is_err()); // negative size - if h.size != size { - bail!("got unexpected size for '{}' ({} != {})", v, h.size, size); - } - if h.unit != unit { - bail!( - "got unexpected unit for '{}' ({:?} != {:?})", - v, - h.unit, - unit - ); - } + fn do_test(v: &str, size: f64, unit: SizeUnit, as_str: &str) -> Result<(), Error> { + let h: HumanByte = v.parse()?; + + if h.size != size { + bail!("got unexpected size for '{}' ({} != {})", v, h.size, size); + } + if h.unit != unit { + bail!( + "got unexpected unit for '{}' ({:?} != {:?})", + v, + h.unit, + unit + ); + } - let new = h.to_string(); - if new != *as_str { - bail!("to_string failed for '{}' ({:?} != {:?})", v, new, as_str); + let new = h.to_string(); + if new != *as_str { + bail!("to_string failed for '{}' ({:?} != {:?})", v, new, as_str); + } + Ok(()) } - Ok(()) - } - fn test(v: &str, size: f64, unit: SizeUnit, as_str: &str) -> bool { - match do_test(v, size, unit, as_str) { - Ok(_) => true, - Err(err) => { - eprintln!("{}", err); // makes debugging easier - false + fn test(v: &str, size: f64, unit: SizeUnit, as_str: &str) -> bool { + match do_test(v, size, unit, as_str) { + Ok(_) => true, + Err(err) => { + eprintln!("{}", err); // makes debugging easier + false + } } } - } - assert!(test("14", 14.0, SizeUnit::Byte, "14 B")); - assert!(test("14.4", 14.4, SizeUnit::Byte, "14.4 B")); - assert!(test("14.45", 14.45, SizeUnit::Byte, "14.45 B")); - assert!(test("14.456", 14.456, SizeUnit::Byte, "14.456 B")); - assert!(test("14.4567", 14.4567, SizeUnit::Byte, "14.457 B")); - - let h: HumanByte = "1.2345678".parse()?; - assert_eq!(&format!("{:.0}", h), "1 B"); - assert_eq!(&format!("{:.0}", h.as_f64()), "1"); // use as_f64 to get raw bytes without unit - assert_eq!(&format!("{:.1}", h), "1.2 B"); - assert_eq!(&format!("{:.2}", h), "1.23 B"); - assert_eq!(&format!("{:.3}", h), "1.235 B"); - assert_eq!(&format!("{:.4}", h), "1.2346 B"); - assert_eq!(&format!("{:.5}", h), "1.23457 B"); - assert_eq!(&format!("{:.6}", h), "1.234568 B"); - assert_eq!(&format!("{:.7}", h), "1.2345678 B"); - assert_eq!(&format!("{:.8}", h), "1.2345678 B"); - - assert!(test( - "987654321", - 987654321.0, - SizeUnit::Byte, - "987654321 B" - )); - - assert!(test("1300b", 1300.0, SizeUnit::Byte, "1300 B")); - assert!(test("1300B", 1300.0, SizeUnit::Byte, "1300 B")); - assert!(test("1300 B", 1300.0, SizeUnit::Byte, "1300 B")); - assert!(test("1300 b", 1300.0, SizeUnit::Byte, "1300 B")); - - assert!(test("1.5KB", 1.5, SizeUnit::KByte, "1.5 KB")); - assert!(test("1.5kb", 1.5, SizeUnit::KByte, "1.5 KB")); - assert!(test("1.654321MB", 1.654_321, SizeUnit::MByte, "1.654 MB")); - - assert!(test("2.0GB", 2.0, SizeUnit::GByte, "2 GB")); - - assert!(test("1.4TB", 1.4, SizeUnit::TByte, "1.4 TB")); - assert!(test("1.4tb", 1.4, SizeUnit::TByte, "1.4 TB")); - - assert!(test("2KiB", 2.0, SizeUnit::Kibi, "2 KiB")); - assert!(test("2Ki", 2.0, SizeUnit::Kibi, "2 KiB")); - assert!(test("2kib", 2.0, SizeUnit::Kibi, "2 KiB")); - - assert!(test("2.3454MiB", 2.3454, SizeUnit::Mebi, "2.345 MiB")); - assert!(test("2.3456MiB", 2.3456, SizeUnit::Mebi, "2.346 MiB")); - - assert!(test("4gib", 4.0, SizeUnit::Gibi, "4 GiB")); - - Ok(()) -} + assert!(test("14", 14.0, SizeUnit::Byte, "14 B")); + assert!(test("14.4", 14.4, SizeUnit::Byte, "14.4 B")); + assert!(test("14.45", 14.45, SizeUnit::Byte, "14.45 B")); + assert!(test("14.456", 14.456, SizeUnit::Byte, "14.456 B")); + assert!(test("14.4567", 14.4567, SizeUnit::Byte, "14.457 B")); + + let h: HumanByte = "1.2345678".parse()?; + assert_eq!(&format!("{:.0}", h), "1 B"); + assert_eq!(&format!("{:.0}", h.as_f64()), "1"); // use as_f64 to get raw bytes without unit + assert_eq!(&format!("{:.1}", h), "1.2 B"); + assert_eq!(&format!("{:.2}", h), "1.23 B"); + assert_eq!(&format!("{:.3}", h), "1.235 B"); + assert_eq!(&format!("{:.4}", h), "1.2346 B"); + assert_eq!(&format!("{:.5}", h), "1.23457 B"); + assert_eq!(&format!("{:.6}", h), "1.234568 B"); + assert_eq!(&format!("{:.7}", h), "1.2345678 B"); + assert_eq!(&format!("{:.8}", h), "1.2345678 B"); + + assert!(test( + "987654321", + 987654321.0, + SizeUnit::Byte, + "987654321 B" + )); + + assert!(test("1300b", 1300.0, SizeUnit::Byte, "1300 B")); + assert!(test("1300B", 1300.0, SizeUnit::Byte, "1300 B")); + assert!(test("1300 B", 1300.0, SizeUnit::Byte, "1300 B")); + assert!(test("1300 b", 1300.0, SizeUnit::Byte, "1300 B")); + + assert!(test("1.5KB", 1.5, SizeUnit::KByte, "1.5 KB")); + assert!(test("1.5kb", 1.5, SizeUnit::KByte, "1.5 KB")); + assert!(test("1.654321MB", 1.654_321, SizeUnit::MByte, "1.654 MB")); + + assert!(test("2.0GB", 2.0, SizeUnit::GByte, "2 GB")); + + assert!(test("1.4TB", 1.4, SizeUnit::TByte, "1.4 TB")); + assert!(test("1.4tb", 1.4, SizeUnit::TByte, "1.4 TB")); + + assert!(test("2KiB", 2.0, SizeUnit::Kibi, "2 KiB")); + assert!(test("2Ki", 2.0, SizeUnit::Kibi, "2 KiB")); + assert!(test("2kib", 2.0, SizeUnit::Kibi, "2 KiB")); + + assert!(test("2.3454MiB", 2.3454, SizeUnit::Mebi, "2.345 MiB")); + assert!(test("2.3456MiB", 2.3456, SizeUnit::Mebi, "2.346 MiB")); + + assert!(test("4gib", 4.0, SizeUnit::Gibi, "4 GiB")); -#[test] -fn test_human_byte_auto_unit_decimal() { - fn convert(b: u64) -> String { - HumanByte::new_decimal(b as f64).to_string() + Ok(()) } - assert_eq!(convert(987), "987 B"); - assert_eq!(convert(1022), "1.022 KB"); - assert_eq!(convert(9_000), "9 KB"); - assert_eq!(convert(1_000), "1 KB"); - assert_eq!(convert(1_000_000), "1 MB"); - assert_eq!(convert(1_000_000_000), "1 GB"); - assert_eq!(convert(1_000_000_000_000), "1 TB"); - assert_eq!(convert(1_000_000_000_000_000), "1 PB"); - - assert_eq!(convert((1 << 30) + 103 * (1 << 20)), "1.182 GB"); - assert_eq!(convert((1 << 30) + 128 * (1 << 20)), "1.208 GB"); - assert_eq!(convert((2 << 50) + 500 * (1 << 40)), "2.802 PB"); -} -#[test] -fn test_human_byte_auto_unit_binary() { - fn convert(b: u64) -> String { - HumanByte::from(b).to_string() + #[test] + fn test_human_byte_auto_unit_decimal() { + fn convert(b: u64) -> String { + HumanByte::new_decimal(b as f64).to_string() + } + assert_eq!(convert(987), "987 B"); + assert_eq!(convert(1022), "1.022 KB"); + assert_eq!(convert(9_000), "9 KB"); + assert_eq!(convert(1_000), "1 KB"); + assert_eq!(convert(1_000_000), "1 MB"); + assert_eq!(convert(1_000_000_000), "1 GB"); + assert_eq!(convert(1_000_000_000_000), "1 TB"); + assert_eq!(convert(1_000_000_000_000_000), "1 PB"); + + assert_eq!(convert((1 << 30) + 103 * (1 << 20)), "1.182 GB"); + assert_eq!(convert((1 << 30) + 128 * (1 << 20)), "1.208 GB"); + assert_eq!(convert((2 << 50) + 500 * (1 << 40)), "2.802 PB"); + } + + #[test] + fn test_human_byte_auto_unit_binary() { + fn convert(b: u64) -> String { + HumanByte::from(b).to_string() + } + assert_eq!(convert(0), "0 B"); + assert_eq!(convert(987), "987 B"); + assert_eq!(convert(1022), "1022 B"); + assert_eq!(convert(9_000), "8.789 KiB"); + assert_eq!(convert(10_000_000), "9.537 MiB"); + assert_eq!(convert(10_000_000_000), "9.313 GiB"); + assert_eq!(convert(10_000_000_000_000), "9.095 TiB"); + + assert_eq!(convert(1 << 10), "1 KiB"); + assert_eq!(convert((1 << 10) * 10), "10 KiB"); + assert_eq!(convert(1 << 20), "1 MiB"); + assert_eq!(convert(1 << 30), "1 GiB"); + assert_eq!(convert(1 << 40), "1 TiB"); + assert_eq!(convert(1 << 50), "1 PiB"); + + assert_eq!(convert((1 << 30) + 103 * (1 << 20)), "1.101 GiB"); + assert_eq!(convert((1 << 30) + 128 * (1 << 20)), "1.125 GiB"); + assert_eq!(convert((1 << 40) + 128 * (1 << 30)), "1.125 TiB"); + assert_eq!(convert((2 << 50) + 512 * (1 << 40)), "2.5 PiB"); } - assert_eq!(convert(0), "0 B"); - assert_eq!(convert(987), "987 B"); - assert_eq!(convert(1022), "1022 B"); - assert_eq!(convert(9_000), "8.789 KiB"); - assert_eq!(convert(10_000_000), "9.537 MiB"); - assert_eq!(convert(10_000_000_000), "9.313 GiB"); - assert_eq!(convert(10_000_000_000_000), "9.095 TiB"); - - assert_eq!(convert(1 << 10), "1 KiB"); - assert_eq!(convert((1 << 10) * 10), "10 KiB"); - assert_eq!(convert(1 << 20), "1 MiB"); - assert_eq!(convert(1 << 30), "1 GiB"); - assert_eq!(convert(1 << 40), "1 TiB"); - assert_eq!(convert(1 << 50), "1 PiB"); - - assert_eq!(convert((1 << 30) + 103 * (1 << 20)), "1.101 GiB"); - assert_eq!(convert((1 << 30) + 128 * (1 << 20)), "1.125 GiB"); - assert_eq!(convert((1 << 40) + 128 * (1 << 30)), "1.125 TiB"); - assert_eq!(convert((2 << 50) + 512 * (1 << 40)), "2.5 PiB"); } -- 2.30.2