Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Add tests for lru and lruk
Browse files Browse the repository at this point in the history
  • Loading branch information
lanlou1554 committed Mar 31, 2024
1 parent b67a81d commit 9ebb871
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
46 changes: 46 additions & 0 deletions storage-node/src/cache/policy/lru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,50 @@ mod tests {
);
assert_eq!(cache.get(&("key2".to_string())), None);
}

#[test]
fn test_pop_key() {
let mut cache =
LruReplacer::<ParpulseDataStoreCacheKey, ParpulseDataStoreCacheValue>::new(30);
cache.put("key1".to_string(), ("value1".to_string(), 1));
cache.put("key2".to_string(), ("value2".to_string(), 2));
cache.put("key3".to_string(), ("value3".to_string(), 3));
cache.put("key4".to_string(), ("value4".to_string(), 4));
cache.put("key5".to_string(), ("value5".to_string(), 5));
assert_eq!(cache.size(), 15);
assert_eq!(cache.len(), 5);
assert_eq!(
cache.pop(&"key1".to_string()),
Some(("value1".to_string(), 1))
);
assert_eq!(cache.size(), 14);
assert_eq!(cache.len(), 4);
assert_eq!(cache.pop(&"key1".to_string()), None);
assert_eq!(cache.size(), 14);
assert_eq!(cache.len(), 4);
assert_eq!(
cache.pop(&"key2".to_string()),
Some(("value2".to_string(), 2))
);
assert_eq!(cache.size(), 12);
assert_eq!(cache.len(), 3);
assert_eq!(
cache.pop(&"key3".to_string()),
Some(("value3".to_string(), 3))
);
assert_eq!(cache.size(), 9);
assert_eq!(cache.len(), 2);
assert_eq!(
cache.pop(&"key4".to_string()),
Some(("value4".to_string(), 4))
);
assert_eq!(cache.size(), 5);
assert_eq!(cache.len(), 1);
assert_eq!(
cache.pop(&"key5".to_string()),
Some(("value5".to_string(), 5))
);
assert_eq!(cache.size(), 0);
assert_eq!(cache.len(), 0);
}
}
46 changes: 46 additions & 0 deletions storage-node/src/cache/policy/lru_k.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,50 @@ mod tests {
assert_eq!(cache.get(&key3), None);
assert_eq!(cache.size(), 4); // Only key4 should be in the cache
}

#[test]
fn test_pop_key() {
let mut cache =
LruKReplacer::<ParpulseDataStoreCacheKey, ParpulseDataStoreCacheValue>::new(30, 2);
cache.put("key1".to_string(), ("value1".to_string(), 1));
cache.put("key2".to_string(), ("value2".to_string(), 2));
cache.put("key3".to_string(), ("value3".to_string(), 3));
cache.put("key4".to_string(), ("value4".to_string(), 4));
cache.put("key5".to_string(), ("value5".to_string(), 5));
assert_eq!(cache.size(), 15);
assert_eq!(cache.len(), 5);
assert_eq!(
cache.pop(&"key1".to_string()),
Some(("value1".to_string(), 1))
);
assert_eq!(cache.size(), 14);
assert_eq!(cache.len(), 4);
assert_eq!(cache.pop(&"key1".to_string()), None);
assert_eq!(cache.size(), 14);
assert_eq!(cache.len(), 4);
assert_eq!(
cache.pop(&"key2".to_string()),
Some(("value2".to_string(), 2))
);
assert_eq!(cache.size(), 12);
assert_eq!(cache.len(), 3);
assert_eq!(
cache.pop(&"key3".to_string()),
Some(("value3".to_string(), 3))
);
assert_eq!(cache.size(), 9);
assert_eq!(cache.len(), 2);
assert_eq!(
cache.pop(&"key4".to_string()),
Some(("value4".to_string(), 4))
);
assert_eq!(cache.size(), 5);
assert_eq!(cache.len(), 1);
assert_eq!(
cache.pop(&"key5".to_string()),
Some(("value5".to_string(), 5))
);
assert_eq!(cache.size(), 0);
assert_eq!(cache.len(), 0);
}
}

0 comments on commit 9ebb871

Please sign in to comment.