Added tests to assert space guarantee for `space::` types.

Fortune for smallmap's current commit: Future small blessing − 末小吉
master
Avril 3 years ago
parent 29d5838756
commit 8b123afd31
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -4,7 +4,7 @@ description = "Small byte-sized generic key-value map type"
keywords = ["map", "table", "small", "key", "value"] keywords = ["map", "table", "small", "key", "value"]
repository = "https://github.com/notflan/smallmap" repository = "https://github.com/notflan/smallmap"
homepage= "https://git.flanchan.moe/flanchan/smallmap" homepage= "https://git.flanchan.moe/flanchan/smallmap"
version = "1.3.2" version = "1.3.3"
authors = ["Avril <flanchan@cumallover.me>"] authors = ["Avril <flanchan@cumallover.me>"]
edition = "2018" edition = "2018"
license = "MIT" license = "MIT"

@ -212,8 +212,21 @@ where K: Collapse
/// A small hashtable-like map with byte sized key indecies. /// A small hashtable-like map with byte sized key indecies.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature="serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature="serde", derive(serde::Serialize, serde::Deserialize))]
// TODO: Replace with SmallVec<[Page<TKey, TValue>; 1]> maybe?
pub struct Map<TKey, TValue>(Vec<Page<TKey,TValue>>); pub struct Map<TKey, TValue>(Vec<Page<TKey,TValue>>);
impl<K,V> Map<K,V>
{
/// 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::<Page<K,V>>()
//self.0.iter().map(std::mem::size_of_val).sum::<usize>()
}
}
impl<K,V> Map<K,V> impl<K,V> Map<K,V>
where K: Collapse where K: Collapse
{ {

@ -12,4 +12,47 @@ use super::*;
/// A set of only non-zero bytes. /// A set of only non-zero bytes.
/// ///
/// This type is entirely space efficient and will only ever allocate `256` bytes of memory. /// This type is entirely space efficient and will only ever allocate `256` bytes of memory.
pub type NonZeroByteSet = Map<std::num::NonZeroU8, ()>; pub type NonZeroByteSet = Set<std::num::NonZeroU8>;
#[cfg(test)]
mod tests
{
use super::*;
/// Returns the currently allocated space of `map`.
pub fn space_of<K, V>(map: &Map<K,V>) -> 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<T, K, V>(map: T, expected: usize)
where T: std::borrow::Borrow<Map<K,V>>
{
let sz = space_of(map.borrow());
assert_eq!(sz, expected, "unexpected full allocated size of type {}: expected {expected}, got {sz}.", std::any::type_name::<Map<K,V>>());
}
/// 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);
}
}

Loading…
Cancel
Save