Skip to content

Commit

Permalink
Merge pull request #187 from jeromefroe/jerome/fmt-and-add-test
Browse files Browse the repository at this point in the history
Add test for get_key_value
  • Loading branch information
jeromefroe authored Nov 22, 2023
2 parents 39009f1 + fcd06e3 commit 5cee09a
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,9 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
/// assert_eq!(cache.get_key_value("3"), Some((&String::from("3"), &"d")));
/// ```
pub fn get_key_value<'a, Q>(&'a mut self, k: &Q) -> Option<(&'a K, &'a V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
if let Some(node) = self.map.get_mut(KeyWrapper::from_ref(k)) {
let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
Expand Down Expand Up @@ -2243,6 +2243,22 @@ mod tests {
assert_eq!(cache.pop_lru(), Some((0, 0)));
assert_eq!(cache.pop_lru(), None);
}

#[test]
fn test_get_key_value() {
use alloc::string::String;

let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

let key = String::from("apple");
cache.put(key, "red");

assert_eq!(
cache.get_key_value("apple"),
Some((&String::from("apple"), &"red"))
);
assert_eq!(cache.get_key_value("banana"), None);
}
}

/// Doctests for what should *not* compile
Expand Down

0 comments on commit 5cee09a

Please sign in to comment.