|
|
|
@ -1,8 +1,9 @@
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
|
|
use super::io::BDFWriter;
|
|
|
|
|
use std::io::{BufWriter, Error, BufReader};
|
|
|
|
|
use std::fs::File;
|
|
|
|
|
use std::fs::{File, remove_file};
|
|
|
|
|
use crate::chunks::{HashEntry, DataEntry};
|
|
|
|
|
use crate::io::BDFReader;
|
|
|
|
|
|
|
|
|
@ -11,9 +12,7 @@ mod tests {
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn it_writes_uncompressed() -> Result<(), Error> {
|
|
|
|
|
let file = File::create("tmp.bdf")?;
|
|
|
|
|
let f = BufWriter::new(file);
|
|
|
|
|
let mut writer = BDFWriter::new(f, 2, false);
|
|
|
|
|
let mut writer = new_writer("tmp1.bdf", 2, false)?;
|
|
|
|
|
|
|
|
|
|
writer.add_lookup_entry(HashEntry::new(BAR.to_string(), 5))?;
|
|
|
|
|
writer.add_lookup_entry(HashEntry::new(FOO.to_string(), 4))?;
|
|
|
|
@ -30,15 +29,14 @@ mod tests {
|
|
|
|
|
|
|
|
|
|
writer.flush()?;
|
|
|
|
|
writer.flush_writer()?;
|
|
|
|
|
remove_file("tmp1.bdf")?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn it_writes_compressed() -> Result<(), Error> {
|
|
|
|
|
let file = File::create("tmp-compressed.bdf")?;
|
|
|
|
|
let f = BufWriter::new(file);
|
|
|
|
|
let mut writer = BDFWriter::new(f, 2, true);
|
|
|
|
|
let mut writer = new_writer("tmp2.bdf", 2, true)?;
|
|
|
|
|
|
|
|
|
|
writer.add_lookup_entry(HashEntry::new(FOO.to_string(), 4))?;
|
|
|
|
|
writer.add_lookup_entry(HashEntry::new(BAR.to_string(), 5))?;
|
|
|
|
@ -56,43 +54,43 @@ mod tests {
|
|
|
|
|
writer.flush()?;
|
|
|
|
|
writer.flush_writer()?;
|
|
|
|
|
|
|
|
|
|
remove_file("tmp2.bdf")?;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
create_simple_file("tmp3.bdf", false)?;
|
|
|
|
|
let mut reader = new_reader("tmp3.bdf")?;
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
|
|
remove_file("tmp3.bdf")?;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
create_simple_file("tmp4.bdf", true)?;
|
|
|
|
|
let mut reader = new_reader("tmp4.bdf")?;
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
|
|
remove_file("tmp4.bdf")?;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
fn create_simple_file(name: &str, compressed: bool) -> Result<(), Error>{
|
|
|
|
|
let mut writer = new_writer(name, 1, compressed)?;
|
|
|
|
|
|
|
|
|
|
writer.add_lookup_entry(HashEntry::new(FOO.to_string(), 4))?;
|
|
|
|
|
let mut entry_1 = DataEntry::new("lol".to_string());
|
|
|
|
@ -104,6 +102,20 @@ mod tests {
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn new_reader(file_name: &str) -> Result<BDFReader, Error> {
|
|
|
|
|
let file = File::open(file_name)?;
|
|
|
|
|
let f = BufReader::new(file);
|
|
|
|
|
|
|
|
|
|
Ok(BDFReader::new(f))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn new_writer(file_name: &str, entries: u64, compress: bool) -> Result<BDFWriter, Error> {
|
|
|
|
|
let file = File::create(file_name)?;
|
|
|
|
|
let f = BufWriter::new(file);
|
|
|
|
|
|
|
|
|
|
Ok(BDFWriter::new(f, entries, compress))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub mod chunks;
|
|
|
|
|