public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Wagner <l.wagner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH v2 proxmox 02/42] human-byte: move tests to their own sub-module
Date: Wed, 24 May 2023 15:56:09 +0200	[thread overview]
Message-ID: <20230524135649.934881-3-l.wagner@proxmox.com> (raw)
In-Reply-To: <20230524135649.934881-1-l.wagner@proxmox.com>

The `#[cfg(test)]` directive ensures that the tests are not compiled
for non-test builds.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---

Notes:
    Already posted to pbs-devel, but included here as well since we
    depend on it and it has not yet been applied.
    Changes from the version posted there: Fixed typo in commit message.
    
    https://lists.proxmox.com/pipermail/pbs-devel/2023-May/006127.html

 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 7be16a51..ef680b4e 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::<HumanByte>().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::<HumanByte>().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





  parent reply	other threads:[~2023-05-24 13:57 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-24 13:56 [pve-devel] [PATCH v2 cluster/guest-common/manager/ha-manager/proxmox{, -perl-rs} 00/42] fix #4156: introduce new notification module Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox 01/42] add `proxmox-human-byte` crate Lukas Wagner
2023-06-26 11:58   ` Wolfgang Bumiller
2023-05-24 13:56 ` Lukas Wagner [this message]
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox 03/42] add proxmox-notify crate Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox 04/42] notify: add debian packaging Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox 05/42] notify: preparation for the first endpoint plugin Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox 06/42] notify: preparation for the API Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox 07/42] notify: api: add API for sending notifications/testing endpoints Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox 08/42] notify: add notification channels Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox 09/42] notify: api: add API for channels Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox 10/42] notify: add sendmail plugin Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox 11/42] notify: api: add API for sendmail endpoints Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox 12/42] notify: add gotify endpoint Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox 13/42] notify: api: add API for gotify endpoints Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox 14/42] notify: add notification filter mechanism Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox 15/42] notify: api: add API for filters Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox 16/42] notify: add template rendering Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox 17/42] notify: add example for " Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox-perl-rs 18/42] log: set default log level to 'info', add product specific logging env var Lukas Wagner
2023-06-05  7:27   ` Wolfgang Bumiller
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox-perl-rs 19/42] add PVE::RS::Notify module Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox-perl-rs 20/42] notify: add api for sending notifications/testing endpoints Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox-perl-rs 21/42] notify: add api for notification channels Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox-perl-rs 22/42] notify: add api for sendmail endpoints Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox-perl-rs 23/42] notify: add api for gotify endpoints Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 proxmox-perl-rs 24/42] notify: add api for notification filters Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-cluster 25/42] cluster files: add notifications.cfg Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-guest-common 26/42] vzdump: add config options for new notification backend Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-manager 27/42] test: fix names of .PHONY targets Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-manager 28/42] add PVE::Notify module Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-manager 29/42] vzdump: send notifications via new notification module Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-manager 30/42] test: rename mail_test.pl to vzdump_notification_test.pl Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-manager 31/42] api: apt: send notification via new notification module Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-manager 32/42] api: replication: send notifications " Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-manager 33/42] ui: backup: allow to select notification channel for notifications Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-manager 34/42] ui: backup: adapt backup job details to new notification params Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-manager 35/42] ui: backup: allow to set notification-{channel, mode} for one-off backups Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-manager 36/42] api: prepare api handler module for notification config Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-manager 37/42] api: add api routes for notification channels Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-manager 38/42] api: add api routes for sendmail endpoints Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-manager 39/42] api: add api routes for gotify endpoints Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-manager 40/42] api: add api routes for notification filters Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-manager 41/42] ui: backup: disable notification mode selector for now Lukas Wagner
2023-05-24 13:56 ` [pve-devel] [PATCH v2 pve-ha-manager 42/42] manager: send notifications via new notification module Lukas Wagner
2023-05-26  8:31 ` [pve-devel] [PATCH v2 cluster/guest-common/manager/ha-manager/proxmox{, -perl-rs} 00/42] fix #4156: introduce " Lukas Wagner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230524135649.934881-3-l.wagner@proxmox.com \
    --to=l.wagner@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal