tests: index testing

This commit is contained in:
Jindřich Moravec 2024-02-04 20:48:10 +01:00
parent 6db62c42d7
commit a5c7306b90

View file

@ -75,25 +75,10 @@ where
self.data.entry(k).and_modify(|values| { self.data.entry(k).and_modify(|values| {
values.remove(&v); values.remove(&v);
}); });
self.dump_to_file().await self.sync_to_disk().await
} }
pub async fn sync_to_disk(&mut self) -> Result<()> { pub async fn sync_to_disk(&mut self) -> Result<()> {
self.dump_to_file().await
}
async fn append_to_file(&mut self, key: &K, value: &V) -> Result<()> {
let mut encoded = Vec::new();
encoded.extend(encode(key)?);
encoded.extend(encode(value)?);
self.file.seek(std::io::SeekFrom::End(0)).await?;
self.file.write(&encoded).await?;
Ok(())
}
async fn dump_to_file(&mut self) -> Result<()> {
let mut writer = BufWriter::new(&mut self.file); let mut writer = BufWriter::new(&mut self.file);
writer.seek(std::io::SeekFrom::Start(0)).await?; writer.seek(std::io::SeekFrom::Start(0)).await?;
@ -114,6 +99,17 @@ where
Ok(()) Ok(())
} }
async fn append_to_file(&mut self, key: &K, value: &V) -> Result<()> {
let mut encoded = Vec::new();
encoded.extend(encode(key)?);
encoded.extend(encode(value)?);
self.file.seek(std::io::SeekFrom::End(0)).await?;
self.file.write(&encoded).await?;
Ok(())
}
async fn load_from_file(&mut self) -> Result<()> { async fn load_from_file(&mut self) -> Result<()> {
let mut bytes = vec![]; let mut bytes = vec![];
@ -141,7 +137,147 @@ mod tests {
use super::*; use super::*;
#[tokio::test] #[tokio::test]
async fn encode_decode() { async fn connect_to_new() {
todo!(); let file_name = PathBuf::from("connect_to_new");
if file_name.exists() {
tokio::fs::remove_file(&file_name).await.unwrap();
}
{
let index = Index::<u64, u64>::new(file_name.clone()).await.unwrap();
assert_eq!(index.data.len(), 0);
}
{
let index = Index::<u64, u64>::connect(file_name.clone()).await.unwrap();
assert_eq!(index.data.len(), 0);
}
tokio::fs::remove_file(&file_name).await.unwrap();
}
#[tokio::test]
async fn inserting() {
let file_name = PathBuf::from("inserting");
if file_name.exists() {
tokio::fs::remove_file(&file_name).await.unwrap();
}
{
let mut index = Index::<u64, u64>::new(file_name.clone()).await.unwrap();
index.insert(1, 2).await.unwrap();
index.insert(1, 3).await.unwrap();
index.insert(1, 4).await.unwrap();
index.insert(2, 3).await.unwrap();
index.insert(2, 4).await.unwrap();
index.insert(2, 5).await.unwrap();
assert_eq!(index.data.len(), 2);
assert_eq!(index.data.get(&1).unwrap().len(), 3);
assert_eq!(index.data.get(&2).unwrap().len(), 3);
}
{
let index = Index::<u64, u64>::connect(file_name.clone()).await.unwrap();
assert_eq!(index.data.len(), 2);
assert_eq!(index.data.get(&1).unwrap().len(), 3);
assert_eq!(index.data.get(&2).unwrap().len(), 3);
}
tokio::fs::remove_file(&file_name).await.unwrap();
}
#[tokio::test]
async fn lookuping() {
let file_name = PathBuf::from("lookuping");
if file_name.exists() {
tokio::fs::remove_file(&file_name).await.unwrap();
}
{
let mut index = Index::<u64, u64>::new(file_name.clone()).await.unwrap();
index.insert(1, 2).await.unwrap();
index.insert(1, 3).await.unwrap();
index.insert(1, 4).await.unwrap();
index.insert(2, 3).await.unwrap();
index.insert(2, 4).await.unwrap();
index.insert(2, 5).await.unwrap();
assert_eq!(index.lookup(&1).await.unwrap().unwrap().len(), 3);
assert_eq!(index.lookup(&2).await.unwrap().unwrap().len(), 3);
assert_eq!(index.lookup(&3).await.unwrap(), None);
let first = index.lookup(&1).await.unwrap().unwrap();
assert!(first.contains(&2));
assert!(first.contains(&3));
assert!(first.contains(&4));
let second = index.lookup(&2).await.unwrap().unwrap();
assert!(second.contains(&3));
assert!(second.contains(&4));
assert!(second.contains(&5));
}
{
let index = Index::<u64, u64>::connect(file_name.clone()).await.unwrap();
assert_eq!(index.lookup(&1).await.unwrap().unwrap().len(), 3);
assert_eq!(index.lookup(&2).await.unwrap().unwrap().len(), 3);
assert_eq!(index.lookup(&3).await.unwrap(), None);
let first = index.lookup(&1).await.unwrap().unwrap();
assert!(first.contains(&2));
assert!(first.contains(&3));
assert!(first.contains(&4));
let second = index.lookup(&2).await.unwrap().unwrap();
assert!(second.contains(&3));
assert!(second.contains(&4));
assert!(second.contains(&5));
}
tokio::fs::remove_file(&file_name).await.unwrap();
}
#[tokio::test]
async fn deleting() {
let file_name = PathBuf::from("deleting");
if file_name.exists() {
tokio::fs::remove_file(&file_name).await.unwrap();
}
{
let mut index = Index::<u64, u64>::new(file_name.clone()).await.unwrap();
index.insert(1, 2).await.unwrap();
index.insert(1, 3).await.unwrap();
index.insert(1, 4).await.unwrap();
index.insert(2, 3).await.unwrap();
index.insert(2, 4).await.unwrap();
index.insert(2, 5).await.unwrap();
assert!(index.lookup(&1).await.unwrap().unwrap().contains(&2));
index.delete(1, 2).await.unwrap();
assert!(!index.lookup(&1).await.unwrap().unwrap().contains(&2));
assert!(index.lookup(&2).await.unwrap().unwrap().contains(&3));
index.delete(2, 3).await.unwrap();
assert!(!index.lookup(&2).await.unwrap().unwrap().contains(&3));
}
{
let mut index = Index::<u64, u64>::connect(file_name.clone()).await.unwrap();
assert!(!index.lookup(&1).await.unwrap().unwrap().contains(&2));
assert!(!index.lookup(&2).await.unwrap().unwrap().contains(&3));
assert!(index.lookup(&1).await.unwrap().unwrap().contains(&3));
index.delete(1, 3).await.unwrap();
assert!(!index.lookup(&1).await.unwrap().unwrap().contains(&3));
assert!(index.lookup(&1).await.unwrap().unwrap().contains(&4));
assert!(index.lookup(&2).await.unwrap().unwrap().contains(&4));
assert!(index.lookup(&2).await.unwrap().unwrap().contains(&5));
}
tokio::fs::remove_file(&file_name).await.unwrap();
} }
} }