Skip to content

Commit

Permalink
Issue #5 : improve example, add on-the-fly test
Browse files Browse the repository at this point in the history
  • Loading branch information
ypo committed Nov 26, 2023
1 parent 510b750 commit e7959f9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@
//!
//! ```
//! let encoding_symbol_length = 1024;
//! let source_block_size = 4; // Number of source symbols in the source block
//! let nb_source_symbols = 4; // Number of source symbols in the source block
//! let source_block_length = encoding_symbol_length * nb_source_symbols; // Total size size of the block;
//! let mut n = 0u32;
//! let mut decoder = raptor_code::SourceBlockDecoder::new(source_block_size);
//! let mut decoder = raptor_code::SourceBlockDecoder::new(nb_source_symbols);
//!
//! while decoder.fully_specified() == false {
//! //TODO replace the following line with pkt received from network
Expand All @@ -75,8 +76,7 @@
//! n += 1;
//! }
//!
//! let source_block_size = encoding_symbol_length * source_block_size;
//! let source_block = decoder.decode(source_block_size as usize);
//! let source_block = decoder.decode(source_block_length as usize);
//!
//! ```
//!
Expand Down
70 changes: 70 additions & 0 deletions tests/raptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,70 @@ mod tests {
assert!(decoded_source_block == source_block_data);
}

fn on_the_fly_encode(
source_block: &Vec<u8>,
max_source_symbols: usize,
nb_repair_symbols: u32,
) -> Vec<Vec<u8>> {
let mut encoder = raptor_code::SourceBlockEncoder::new(&source_block, max_source_symbols);
let n = encoder.nb_source_symbols() + nb_repair_symbols;

let mut encoded_block = Vec::new();
for esi in 0..n as u32 {
let encoding_symbol = encoder.fountain(esi);
encoded_block.push(encoding_symbol);
}

encoded_block
}

fn on_the_fly_decode(
source_block_length: usize,
nb_source_symbols: usize,
encoded_block: &Vec<Option<Vec<u8>>>,
) -> Option<Vec<u8>> {
let mut decoder = raptor_code::SourceBlockDecoder::new(nb_source_symbols);
for (esi, encoding_symbol) in encoded_block.iter().enumerate() {
if decoder.fully_specified() {
break;
}
if let Some(encoding_symbol) = encoding_symbol {
decoder.push_encoding_symbol(encoding_symbol, esi as u32);
}
}

assert!(decoder.fully_specified());
decoder.decode(source_block_length as usize)
}

fn on_the_fly_encode_decode(
source_block_length: usize,
max_source_symbols: usize,
nb_repair_symbols: u32,
network_loss: u32,
) {
let source_block_data = create_source_block_data(source_block_length);

// Test block encoer
let encoding_symbols =
on_the_fly_encode(&source_block_data, max_source_symbols, nb_repair_symbols);

// Simulate packet loss
let received_encoding_symbols = network_transfer(&encoding_symbols, network_loss);

// Test block decoder
let decoded_source_block = on_the_fly_decode(
source_block_length,
max_source_symbols,
&received_encoding_symbols,
)
.unwrap();

// Check decoded block
assert!(decoded_source_block.len() == source_block_data.len());
assert!(decoded_source_block == source_block_data);
}

#[test]
pub fn test_encode_decode_100k_repair100_loss5() {
init();
Expand All @@ -83,4 +147,10 @@ mod tests {
init();
encode_decode(1024, 1024, 0, 0);
}

#[test]
pub fn test_onthefly_3k_repair3_loss10_issue5() {
init();
on_the_fly_encode_decode(3684, 4, 3, 10);
}
}

0 comments on commit e7959f9

Please sign in to comment.