Skip to content

Commit

Permalink
Merge pull request #32 from florian1345/coverage-except-error
Browse files Browse the repository at this point in the history
Achieve good coverage everywhere except error module
  • Loading branch information
florian1345 authored Jun 9, 2023
2 parents c68caa6 + 34b0dbf commit 031b8d4
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
51 changes: 48 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2164,11 +2164,28 @@ mod tests {
}

#[test]
fn reserving_fails_on_overflow() {
fn reserving_fails_on_internal_overflow() {
let mut cache = LruCache::new(1024);
cache.insert("hello", "world").unwrap();

assert!(cache.try_reserve(usize::MAX).is_err());

let try_reserve_result = cache.try_reserve(usize::MAX);

assert_eq!(Err(TryReserveError::CapacityOverflow), try_reserve_result);
}

#[test]
fn reserving_fails_on_external_overflow() {
// In this test, the number of entries does not overflow usize, but the
// number of bytes does. Hence, lru-mem does not raise an error, but
// hashbrown does.

let mut cache = LruCache::new(1024);
cache.insert("hello", "world").unwrap();
let additional = usize::MAX / mem::size_of::<Entry<&str, &str>>();

let try_reserve_result = cache.try_reserve(additional);

assert_eq!(Err(TryReserveError::CapacityOverflow), try_reserve_result);
}

#[test]
Expand Down Expand Up @@ -2313,4 +2330,32 @@ mod tests {
assert_eq!(Some(&"hello"), iter.next());
assert_eq!(None, iter.next());
}

#[test]
fn empty_cache_formats_for_debug_correctly() {
let cache = LruCache::<&str, &str>::new(1024);

assert_eq!("{}", format!("{:?}", cache));
}

#[test]
fn singleton_cache_formats_for_debug_correctly() {
let mut cache = LruCache::new(1024);
cache.insert("hello", "world").unwrap();

assert_eq!("{\"hello\": \"world\"}", format!("{:?}", cache));
}

#[test]
fn larger_cache_formats_for_debug_correctly() {
let mut cache = LruCache::new(1024);
cache.insert(2, 4).unwrap();
cache.insert(3, 9).unwrap();
cache.insert(5, 25).unwrap();
cache.insert(7, 49).unwrap();
cache.insert(11, 121).unwrap();
cache.touch(&2);

assert_eq!("{3: 9, 5: 25, 7: 49, 11: 121, 2: 4}", format!("{:?}", cache));
}
}
35 changes: 33 additions & 2 deletions src/mem_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ impl HeapSize for OsStr {

impl MemSize for OsStr {
fn mem_size(&self) -> usize {
self.len()
mem::size_of_val(self)
}
}

Expand Down Expand Up @@ -504,7 +504,7 @@ impl HeapSize for Path {

impl MemSize for Path {
fn mem_size(&self) -> usize {
self.as_os_str().mem_size()
mem::size_of_val(self)
}
}

Expand All @@ -525,6 +525,7 @@ mod test {
const HASH_SET_SIZE: usize = mem::size_of::<HashSet<u8>>();
const BINARY_HEAP_SIZE: usize = mem::size_of::<BinaryHeap<u8>>();
const STRING_RESULT_SIZE: usize = mem::size_of::<Result<String, String>>();
const PATH_BUF_SIZE: usize = mem::size_of::<PathBuf>();

#[test]
fn tuples_have_correct_size() {
Expand Down Expand Up @@ -873,4 +874,34 @@ mod test {
assert_eq!(mem::size_of::<RangeInclusive<MockRangeable>>() + 85,
range_inclusive.mem_size());
}

#[test]
fn empty_path_has_correct_size() {
let path = Path::new("");

assert_eq!(0, path.mem_size());
}

#[test]
fn non_empty_path_has_correct_size() {
let path = Path::new("hello");
let os_str = OsStr::new("hello");

assert_eq!(os_str.mem_size(), path.mem_size());
}

#[test]
fn empty_path_buf_has_correct_size() {
let path_buf = PathBuf::new();

assert_eq!(PATH_BUF_SIZE, path_buf.mem_size());
}

#[test]
fn non_empty_path_buf_has_correct_size() {
let path_buf = PathBuf::from("hello/world");
let os_str = OsStr::new("hello/world");

assert_eq!(PATH_BUF_SIZE + os_str.mem_size(), path_buf.mem_size());
}
}

0 comments on commit 031b8d4

Please sign in to comment.