Skip to content

Commit

Permalink
Added tests for CompressedStream
Browse files Browse the repository at this point in the history
  • Loading branch information
cjrolo committed Oct 9, 2023
1 parent 5750e59 commit cd3a4c7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion brro-compressor/src/compressor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Compressor {
Compressor::Noop => noop(data),
Compressor::FFT => fft(data, 8, 0.0, 10.0), // TODO: Remove the placeholders
Compressor::Constant => constant(data),
_ => todo!(),
_ => noop(data),
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions brro-compressor/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl CompressedStream {

/// Transforms the whole CompressedStream into bytes to be written to a file
pub fn to_bytes(self) -> Vec<u8> {
// Will this chain encode??
let config = BinConfig::get();
bincode::encode_to_vec(self, config).unwrap()
}
Expand All @@ -50,3 +51,44 @@ impl CompressedStream {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_compress_chunk() {
let vector1 = vec![1.0, 1.0, 1.0, 1.0, 1.0];
let mut cs = CompressedStream::new();
cs.compress_chunk(&vector1);
assert_eq!(cs.data_frames.len(), 1);
}

#[test]
fn test_compress_chunk_with() {
let vector1 = vec![1.0, 1.0, 1.0, 1.0, 1.0];
let mut cs = CompressedStream::new();
cs.compress_chunk_with(&vector1, Compressor::Constant);
assert_eq!(cs.data_frames.len(), 1);
}

#[test]
fn test_to_bytes() {
let vector1 = vec![1.0; 1024];
let mut cs = CompressedStream::new();
cs.compress_chunk_with(&vector1, Compressor::Constant);
let b = cs.to_bytes();
assert_eq!(b, [66, 82, 82, 79, 0, 1, 37, 0, 3, 3, 0, 2, 0]);
}

#[test]
fn test_from_bytes() {
let vector1 = vec![1.0; 1024];
let mut cs = CompressedStream::new();
cs.compress_chunk_with(&vector1, Compressor::Constant);
let len = cs.data_frames.len();
let b = cs.to_bytes();
let cs2 = CompressedStream::from_bytes(&b);
assert_eq!(len, cs2.data_frames.len());
}
}
1 change: 1 addition & 0 deletions brro-compressor/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl CompressorFrame {
}

/// Calculates the size of the Frame and "closes it"
// TODO this is probably wrong, so we have to use the write stream to dump the bytes writen
pub fn close(&mut self) {
let size = size_of_val(&self.samples)
+ size_of_val(&self.compressor)
Expand Down
3 changes: 2 additions & 1 deletion brro-compressor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ fn process_args(input_path: &str, arguments: &Args) {
}
} else {
// TODO: Make this do something...
compress_file(None, Vec::new());
let cs = compress_file(None, Vec::new());
cs.to_bytes();
}
}

Expand Down

0 comments on commit cd3a4c7

Please sign in to comment.