From 70c730a2d3c233a692b21f3d0b09ecd84df8d5c7 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Sat, 30 Mar 2024 14:23:55 +0000 Subject: [PATCH] add tests for compute_chunk_status --- crates/librqbit/src/chunk_tracker.rs | 68 ++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/crates/librqbit/src/chunk_tracker.rs b/crates/librqbit/src/chunk_tracker.rs index dbaba95..d5bccbd 100644 --- a/crates/librqbit/src/chunk_tracker.rs +++ b/crates/librqbit/src/chunk_tracker.rs @@ -220,3 +220,71 @@ impl ChunkTracker { Some(ChunkMarkingResult::NotCompleted) } } + +#[cfg(test)] +mod tests { + use librqbit_core::{constants::CHUNK_SIZE, lengths::Lengths}; + + use crate::type_aliases::BF; + + use super::compute_chunk_status; + + #[test] + fn test_compute_chunk_status() { + // Create the most obnoxious lenghts, and ensure it doesn't break in that case. + let piece_length = CHUNK_SIZE * 2 + 1; + let l = Lengths::new(piece_length as u64 * 2 + 1, piece_length).unwrap(); + + assert_eq!(l.total_pieces(), 3); + assert_eq!(l.default_chunks_per_piece(), 3); + assert_eq!(l.total_chunks(), 7); + + { + let mut needed_pieces = + BF::from_boxed_slice(vec![0u8; l.piece_bitfield_bytes()].into_boxed_slice()); + needed_pieces.set(0, true); + + let chunks = compute_chunk_status(&l, &needed_pieces).unwrap(); + dbg!(&chunks); + assert_eq!(chunks[0], false); + assert_eq!(chunks[1], false); + assert_eq!(chunks[2], false); + assert_eq!(chunks[3], true); + assert_eq!(chunks[4], true); + assert_eq!(chunks[5], true); + assert_eq!(chunks[6], true); + } + + { + let mut needed_pieces = + BF::from_boxed_slice(vec![0u8; l.piece_bitfield_bytes()].into_boxed_slice()); + needed_pieces.set(1, true); + + let chunks = compute_chunk_status(&l, &needed_pieces).unwrap(); + dbg!(&chunks); + assert_eq!(chunks[0], true); + assert_eq!(chunks[1], true); + assert_eq!(chunks[2], true); + assert_eq!(chunks[3], false); + assert_eq!(chunks[4], false); + assert_eq!(chunks[5], false); + assert_eq!(chunks[6], true); + } + + { + let mut needed_pieces = + BF::from_boxed_slice(vec![0u8; l.piece_bitfield_bytes()].into_boxed_slice()); + needed_pieces.set(2, true); + + let chunks = compute_chunk_status(&l, &needed_pieces).unwrap(); + dbg!(&chunks); + assert_eq!(chunks[0], true); + assert_eq!(chunks[1], true); + assert_eq!(chunks[2], true); + assert_eq!(chunks[3], true); + assert_eq!(chunks[4], true); + assert_eq!(chunks[5], true); + assert_eq!(chunks[6], false); + } + } +}