Skip to content

Commit

Permalink
Adds tests for get_one (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Lodato authored Jun 30, 2020
1 parent fdd702c commit 75c81ef
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 4 deletions.
96 changes: 92 additions & 4 deletions src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt;
use std::hash::{BuildHasher, Hash};
use std::mem::ManuallyDrop;

/// This value determines when a value-set is promoted from a list to a HashBag.
const BAG_THRESHOLD: usize = 32;

/// A bag of values for a given key in the evmap.
Expand Down Expand Up @@ -99,6 +100,11 @@ impl<T, S> Values<T, S> {
ValuesInner::Long(ref v) => v.contains(value) != 0,
}
}

#[cfg(test)]
fn is_short(&self) -> bool {
matches!(self.0, ValuesInner::Short(_))
}
}

impl<'a, T, S> IntoIterator for &'a Values<T, S> {
Expand Down Expand Up @@ -309,14 +315,96 @@ where
#[cfg(test)]
mod tests {
use super::*;
use std::collections::hash_map::RandomState;

macro_rules! assert_empty {
($x:expr) => {
assert_eq!($x.len(), 0);
assert!($x.is_empty());
assert_eq!($x.iter().count(), 0);
assert_eq!($x.into_iter().count(), 0);
assert_eq!($x.get_one(), None);
};
}

macro_rules! assert_len {
($x:expr, $n:expr) => {
assert_eq!($x.len(), $n);
assert!(!$x.is_empty());
assert_eq!($x.iter().count(), $n);
assert_eq!($x.into_iter().count(), $n);
};
}

#[test]
fn sensible_default() {
let v: Values<i32> = Values::default();
assert!(v.is_empty());
assert_eq!(v.len(), 0);
assert!(v.is_short());
assert_eq!(v.capacity(), 1);
assert_empty!(v);
}

#[test]
fn short_values() {
let hasher = RandomState::default();
let mut v = Values::new();

let values = 0..BAG_THRESHOLD - 1;
let len = values.clone().count();
for i in values.clone() {
v.push(i, &hasher);
}

for i in values.clone() {
assert!(v.contains(&i));
}
assert!(v.is_short());
assert_len!(v, len);
assert_eq!(v.get_one(), Some(&0));

v.clear();

assert_empty!(v);

// clear() should not affect capacity or value type!
assert!(v.capacity() > 1);
assert!(v.is_short());

v.shrink_to_fit();

assert_eq!(v.capacity(), 1);
}

#[test]
fn long_values() {
let hasher = RandomState::default();
let mut v = Values::new();

let values = 0..BAG_THRESHOLD;
let len = values.clone().count();
for i in values.clone() {
v.push(i, &hasher);
}

for i in values.clone() {
assert!(v.contains(&i));
}
assert!(!v.is_short());
assert_len!(v, len);
assert!(values.contains(v.get_one().unwrap()));

v.clear();

assert_empty!(v);

// clear() should not affect capacity or value type!
assert!(v.capacity() > 1);
assert!(!v.is_short());

v.shrink_to_fit();

// Now we have short values!
assert!(v.is_short());
assert_eq!(v.capacity(), 1);
assert_eq!(v.iter().count(), 0);
assert_eq!(v.into_iter().count(), 0);
}
}
16 changes: 16 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,3 +732,19 @@ fn retain() {
vs.sort();
assert_eq!(v, &*vs);
}

#[test]
fn get_one() {
let x = ('x', 42);

let (r, mut w) = evmap::new();

w.insert(x.0, x);
w.insert(x.0, x);

assert_match!(r.get_one(&x.0), None);

w.refresh();

assert_match!(r.get_one(&x.0).as_deref(), Some(('x', 42)));
}

0 comments on commit 75c81ef

Please sign in to comment.