From 0c42a527422fcc16caa1ba6a106df139e2f0dd76 Mon Sep 17 00:00:00 2001 From: Eric Woolsey Date: Thu, 25 Jul 2024 10:55:05 -0700 Subject: [PATCH] Capacity tests for mmap vec --- src/generic_storage/mmap_vec.rs | 201 +++++++++++++++++++++++++++++++- 1 file changed, 198 insertions(+), 3 deletions(-) diff --git a/src/generic_storage/mmap_vec.rs b/src/generic_storage/mmap_vec.rs index 177afbc..1087795 100644 --- a/src/generic_storage/mmap_vec.rs +++ b/src/generic_storage/mmap_vec.rs @@ -46,9 +46,8 @@ impl MmapVec { /// Notably this means that there can exist no other mutable mappings to the /// same file in this process or any other pub unsafe fn create(file: File) -> color_eyre::Result { - let initial_byte_len = META_SIZE; - - file.set_len(initial_byte_len as u64) + file.set_len(0)?; + file.set_len(META_SIZE as u64) .context("Failed to resize underlying file")?; let mut s = Self::restore(file)?; @@ -250,6 +249,202 @@ mod tests { use super::*; + #[test] + #[allow(clippy::manual_bits)] + fn test_capacity_push() { + let f = tempfile::NamedTempFile::new().unwrap(); + let file_path = f.path().to_owned(); + + let mut storage: MmapVec = unsafe { MmapVec::create(f.reopen().unwrap()).unwrap() }; + assert_eq!(storage.capacity, 0); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len(), + META_SIZE as u64 + ); + + storage.push(0); + assert_eq!(storage.capacity, 1); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + size_of::() + META_SIZE + ); + + storage.push(0); + assert_eq!(storage.capacity, 2); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + size_of::() * 2 + META_SIZE + ); + + storage.push(0); + assert_eq!(storage.capacity, 4); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + size_of::() * 4 + META_SIZE + ); + + storage.push(0); + assert_eq!(storage.capacity, 4); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + size_of::() * 4 + META_SIZE + ); + + storage.push(0); + assert_eq!(storage.capacity, 8); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + size_of::() * 8 + META_SIZE + ); + } + + #[test] + #[allow(clippy::manual_bits)] + fn test_capacity_extend() { + let f = tempfile::NamedTempFile::new().unwrap(); + let file_path = f.path().to_owned(); + + let mut storage: MmapVec = unsafe { MmapVec::create(f.reopen().unwrap()).unwrap() }; + assert_eq!(storage.capacity, 0); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len(), + META_SIZE as u64 + ); + + storage.extend_from_slice(&[0, 0]); + assert_eq!(storage.capacity, 2); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + size_of::() * 2 + META_SIZE + ); + + storage.extend_from_slice(&[0, 0, 0]); + assert_eq!(storage.capacity, 8); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + size_of::() * 8 + META_SIZE + ); + + storage.extend_from_slice(&[0]); + assert_eq!(storage.capacity, 8); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + size_of::() * 8 + META_SIZE + ); + } + + #[test] + #[allow(clippy::manual_bits)] + fn test_capacity_create() { + let f = tempfile::NamedTempFile::new().unwrap(); + let file_path = f.path().to_owned(); + + let mut storage: MmapVec = unsafe { MmapVec::create(f.reopen().unwrap()).unwrap() }; + assert_eq!(storage.capacity, 0); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + META_SIZE + ); + + storage.extend_from_slice(&[0, 0, 0, 0, 0]); + assert_eq!(storage.capacity, 8); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + size_of::() * 8 + META_SIZE + ); + + let storage: MmapVec = unsafe { MmapVec::create(f.reopen().unwrap()).unwrap() }; + assert_eq!(storage.capacity, 0); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + META_SIZE + ); + } + + #[test] + #[allow(clippy::manual_bits)] + fn test_capacity_create_from_path() { + let f = tempfile::NamedTempFile::new().unwrap(); + let file_path = f.path().to_owned(); + + let mut storage: MmapVec = unsafe { MmapVec::create(f.reopen().unwrap()).unwrap() }; + assert_eq!(storage.capacity, 0); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + META_SIZE + ); + + storage.extend_from_slice(&[0, 0, 0, 0, 0]); + assert_eq!(storage.capacity, 8); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + size_of::() * 8 + META_SIZE + ); + + let storage: MmapVec = unsafe { MmapVec::create_from_path(&file_path).unwrap() }; + assert_eq!(storage.capacity, 0); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + META_SIZE + ); + } + + #[test] + #[allow(clippy::manual_bits)] + fn test_capacity_restore() { + let f = tempfile::NamedTempFile::new().unwrap(); + let file_path = f.path().to_owned(); + + let mut storage: MmapVec = unsafe { MmapVec::create(f.reopen().unwrap()).unwrap() }; + assert_eq!(storage.capacity, 0); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + META_SIZE + ); + + storage.extend_from_slice(&[0, 0, 0, 0, 0]); + assert_eq!(storage.capacity, 8); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + size_of::() * 8 + META_SIZE + ); + + let storage: MmapVec = unsafe { MmapVec::restore(f.reopen().unwrap()).unwrap() }; + assert_eq!(storage.capacity, 8); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + size_of::() * 8 + META_SIZE + ); + } + + #[test] + #[allow(clippy::manual_bits)] + fn test_capacity_restore_from_path() { + let f = tempfile::NamedTempFile::new().unwrap(); + let file_path = f.path().to_owned(); + + let mut storage: MmapVec = unsafe { MmapVec::create(f.reopen().unwrap()).unwrap() }; + assert_eq!(storage.capacity, 0); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + META_SIZE + ); + + storage.extend_from_slice(&[0, 0, 0, 0, 0]); + assert_eq!(storage.capacity, 8); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + size_of::() * 8 + META_SIZE + ); + + let storage: MmapVec = unsafe { MmapVec::restore_from_path(&file_path).unwrap() }; + assert_eq!(storage.capacity, 8); + assert_eq!( + std::fs::metadata(&file_path).unwrap().len() as usize, + size_of::() * 8 + META_SIZE + ); + } + #[test] fn test_mmap_vec() { let f = tempfile::tempfile().unwrap();