From 012df92d7d92cafd784fe99e473a2ec3c594c808 Mon Sep 17 00:00:00 2001 From: Richard Bonichon Date: Wed, 1 Nov 2023 11:46:35 +0100 Subject: [PATCH] optimism: testing memory_size function And a bit of fun in the comments :-) --- optimism/src/mips/witness.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/optimism/src/mips/witness.rs b/optimism/src/mips/witness.rs index 9cb714d10d..927c9f8c96 100644 --- a/optimism/src/mips/witness.rs +++ b/optimism/src/mips/witness.rs @@ -51,7 +51,7 @@ fn fresh_scratch_state() -> [Fp; N] { } const KUNIT: usize = 1024; // a kunit of memory is 1024 things (bytes, kilobytes, ...) -const PREFIXES: &str = "KMGTPEZY"; // prefixes for memory quantities KiB, MiB, GiB, ... +const PREFIXES: &str = "KMGTPE"; // prefixes for memory quantities KiB, MiB, GiB, ... // Create a human-readable string representation of the memory size fn memory_size(total: usize) -> String { @@ -72,9 +72,14 @@ fn memory_size(total: usize) -> String { let value = total as f64 / d as f64; let prefix = - // Famous last words: 1023 yottabytes ought to be enough for anybody - // Corollary: unwrap() below shouldn't fail - PREFIXES.chars().nth(idx).unwrap(); + //////////////////////////////////////////////////////////////////////////// + // Famous last words: 1023 exabytes ought to be enough for anybody // + // // + // Corollary: unwrap() below shouldn't fail // + // // + // The maximum representation for usize corresponds to 16 exabytes anyway // + //////////////////////////////////////////////////////////////////////////// + PREFIXES.chars().nth(idx).unwrap(); format!("{:.1} {}iB", value, prefix) } @@ -175,3 +180,18 @@ impl Env { } } } + +#[cfg(test)] +mod tests { + + use super::*; + + #[test] + fn test_memory_size() { + assert_eq!(memory_size(1023_usize), "1023 B"); + assert_eq!(memory_size(1024_usize), "1.0 KiB"); + assert_eq!(memory_size(1024 * 1024_usize), "1.0 MiB"); + assert_eq!(memory_size(2100 * 1024 * 1024_usize), "2.1 GiB"); + assert_eq!(memory_size(std::usize::MAX), "16.0 EiB"); + } +}