From 8b123afd3199d1c1b8c8e2c0a919c180c1406667 Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 11 Mar 2022 16:51:08 +0000 Subject: [PATCH] Added tests to assert space guarantee for `space::` types. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for smallmap's current commit: Future small blessing − 末小吉 --- Cargo.toml | 2 +- src/lib.rs | 13 +++++++++++++ src/space.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 96fc787..e65c3d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ description = "Small byte-sized generic key-value map type" keywords = ["map", "table", "small", "key", "value"] repository = "https://github.com/notflan/smallmap" homepage= "https://git.flanchan.moe/flanchan/smallmap" -version = "1.3.2" +version = "1.3.3" authors = ["Avril "] edition = "2018" license = "MIT" diff --git a/src/lib.rs b/src/lib.rs index 0d71f38..a66fcac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -212,8 +212,21 @@ where K: Collapse /// A small hashtable-like map with byte sized key indecies. #[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] #[cfg_attr(feature="serde", derive(serde::Serialize, serde::Deserialize))] +// TODO: Replace with SmallVec<[Page; 1]> maybe? pub struct Map(Vec>); +impl Map +{ + /// Returns the currently allocated size of the map in bytes (including currently unused reserved space.) + #[inline(always)] + #[allow(dead_code)] // Used in test cases, but compiler still warns about it + pub(crate) fn internal_size_bytes(&self) -> usize + { + self.0.capacity() * std::mem::size_of::>() + //self.0.iter().map(std::mem::size_of_val).sum::() + } +} + impl Map where K: Collapse { diff --git a/src/space.rs b/src/space.rs index 8d47fac..d84e03e 100644 --- a/src/space.rs +++ b/src/space.rs @@ -12,4 +12,47 @@ use super::*; /// A set of only non-zero bytes. /// /// This type is entirely space efficient and will only ever allocate `256` bytes of memory. -pub type NonZeroByteSet = Map; +pub type NonZeroByteSet = Set; + +#[cfg(test)] +mod tests +{ + use super::*; + + /// Returns the currently allocated space of `map`. + pub fn space_of(map: &Map) -> usize + { + map.internal_size_bytes() + } + + /// Check the allocation size of types in this module + mod allocsz { + use super::*; + + /// Assert the allocated space (in bytes) of `map` is equal to `expected`. + pub fn assert_space_of(map: T, expected: usize) + where T: std::borrow::Borrow> + { + let sz = space_of(map.borrow()); + assert_eq!(sz, expected, "unexpected full allocated size of type {}: expected {expected}, got {sz}.", std::any::type_name::>()); + } + + /// Create a test that asserts the allocation size of a type is equal to a specific number of bytes + /// + /// # Usage + /// ``` + /// # use super::*; + /// size_test!(non_zero_byte_set, NonZeroByteSet, 256); // Creates a test function, named `non_zero_byte_set`, that asserts the type `NonZeroByteSet` allocates exactly 256 bytes. + /// ``` + macro_rules! size_test { + ($name:ident, $type:ty, $num:expr) => { + #[test] + fn $name() { + assert_space_of(<$type>::new(), $num); + } + } + } + + size_test!(non_zero_byte_set, NonZeroByteSet, 256); + } +}