|
|
|
@ -1,9 +1,10 @@
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::io::BDFWriter;
|
|
|
|
|
use std::io::{BufWriter, Error};
|
|
|
|
|
use std::io::{BufWriter, Error, BufReader};
|
|
|
|
|
use std::fs::File;
|
|
|
|
|
use crate::chunks::{HashEntry, DataEntry};
|
|
|
|
|
use crate::io::BDFReader;
|
|
|
|
|
|
|
|
|
|
const FOO: &str = "foo";
|
|
|
|
|
const BAR: &str = "bar";
|
|
|
|
@ -13,16 +14,20 @@ mod tests {
|
|
|
|
|
let file = File::create("tmp.bdf")?;
|
|
|
|
|
let f = BufWriter::new(file);
|
|
|
|
|
let mut writer = BDFWriter::new(f, 2, false);
|
|
|
|
|
|
|
|
|
|
writer.add_lookup_entry(HashEntry::new(BAR.to_string(), 5))?;
|
|
|
|
|
writer.add_lookup_entry(HashEntry::new(FOO.to_string(), 4))?;
|
|
|
|
|
|
|
|
|
|
let mut entry_1 = DataEntry::new("lol".to_string());
|
|
|
|
|
entry_1.add_hash_value(FOO.to_string(), vec![0, 1, 0, 2]);
|
|
|
|
|
entry_1.add_hash_value(BAR.to_string(), vec![0, 2, 3, 4, 5]);
|
|
|
|
|
writer.add_data_entry(entry_1)?;
|
|
|
|
|
|
|
|
|
|
let mut entry_2 = DataEntry::new("lel".to_string());
|
|
|
|
|
entry_2.add_hash_value(BAR.to_string(), vec![0, 3, 2, 1, 5]);
|
|
|
|
|
entry_2.add_hash_value(FOO.to_string(), vec![4, 5, 2, 3]);
|
|
|
|
|
writer.add_data_entry(entry_2)?;
|
|
|
|
|
|
|
|
|
|
writer.flush()?;
|
|
|
|
|
writer.flush_writer()?;
|
|
|
|
|
|
|
|
|
@ -34,16 +39,66 @@ mod tests {
|
|
|
|
|
let file = File::create("tmp-compressed.bdf")?;
|
|
|
|
|
let f = BufWriter::new(file);
|
|
|
|
|
let mut writer = BDFWriter::new(f, 2, true);
|
|
|
|
|
|
|
|
|
|
writer.add_lookup_entry(HashEntry::new(FOO.to_string(), 4))?;
|
|
|
|
|
writer.add_lookup_entry(HashEntry::new(BAR.to_string(), 5))?;
|
|
|
|
|
|
|
|
|
|
let mut entry_1 = DataEntry::new("lol".to_string());
|
|
|
|
|
entry_1.add_hash_value(FOO.to_string(), vec![2, 4, 0, 2]);
|
|
|
|
|
entry_1.add_hash_value(BAR.to_string(), vec![5, 2, 1, 4, 5]);
|
|
|
|
|
writer.add_data_entry(entry_1)?;
|
|
|
|
|
|
|
|
|
|
let mut entry_2 = DataEntry::new("lel".to_string());
|
|
|
|
|
entry_2.add_hash_value(BAR.to_string(), vec![0, 3, 2, 1, 5]);
|
|
|
|
|
entry_2.add_hash_value(FOO.to_string(), vec![4, 5, 2, 3]);
|
|
|
|
|
writer.add_data_entry(entry_2)?;
|
|
|
|
|
|
|
|
|
|
writer.flush()?;
|
|
|
|
|
writer.flush_writer()?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn it_reads() -> Result<(), Error> {
|
|
|
|
|
create_simple_file(false)?;
|
|
|
|
|
let file = File::open("tmp2.bdf")?;
|
|
|
|
|
let f = BufReader::new(file);
|
|
|
|
|
let mut reader = BDFReader::new(f);
|
|
|
|
|
reader.read_metadata()?;
|
|
|
|
|
let lookup_table = &reader.read_lookup_table()?.clone();
|
|
|
|
|
let mut next_chunk = reader.next_chunk()?;
|
|
|
|
|
let data_entries = next_chunk.data_entries(lookup_table)?;
|
|
|
|
|
assert_eq!(data_entries[0].plain, "lol".to_string());
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn it_reads_compressed() -> Result<(), Error> {
|
|
|
|
|
create_simple_file(true)?;
|
|
|
|
|
let file = File::open("tmp2.bdf")?;
|
|
|
|
|
let f = BufReader::new(file);
|
|
|
|
|
let mut reader = BDFReader::new(f);
|
|
|
|
|
reader.read_metadata()?;
|
|
|
|
|
let lookup_table = &reader.read_lookup_table()?.clone();
|
|
|
|
|
let mut next_chunk = reader.next_chunk()?;
|
|
|
|
|
let data_entries = next_chunk.data_entries(lookup_table)?;
|
|
|
|
|
assert_eq!(data_entries[0].plain, "lol".to_string());
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn create_simple_file(compressed: bool) -> Result<(), Error>{
|
|
|
|
|
let file = File::create("tmp2.bdf")?;
|
|
|
|
|
let f = BufWriter::new(file);
|
|
|
|
|
let mut writer = BDFWriter::new(f, 1, compressed);
|
|
|
|
|
|
|
|
|
|
writer.add_lookup_entry(HashEntry::new(FOO.to_string(), 4))?;
|
|
|
|
|
let mut entry_1 = DataEntry::new("lol".to_string());
|
|
|
|
|
entry_1.add_hash_value(FOO.to_string(), vec![2, 4, 0, 2]);
|
|
|
|
|
writer.add_data_entry(entry_1)?;
|
|
|
|
|
|
|
|
|
|
writer.flush()?;
|
|
|
|
|
writer.flush_writer()?;
|
|
|
|
|
|
|
|
|
|