From 4d8220649ef5921511d706f6788dc95797a01ae0 Mon Sep 17 00:00:00 2001 From: Gabriel Viganotti Date: Tue, 25 Jun 2024 12:29:51 -0300 Subject: [PATCH] refactor: using div_ceil to calculate num of chunks from given file size --- src/lib.rs | 7 ++----- src/tests.rs | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5e566d589..967539d1e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -572,11 +572,8 @@ fn get_num_chunks(file_size: usize) -> usize { if file_size < (3 * MAX_CHUNK_SIZE) { return 3; } - if file_size % MAX_CHUNK_SIZE == 0 { - file_size / MAX_CHUNK_SIZE - } else { - (file_size / MAX_CHUNK_SIZE) + 1 - } + + usize::div_ceil(file_size, MAX_CHUNK_SIZE) } // Returns the size of a chunk according to file size. diff --git a/src/tests.rs b/src/tests.rs index 73bfe2d21..4ea01ae09 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -9,7 +9,7 @@ use crate::{ decrypt_full_set, decrypt_range, encrypt, get_chunk_size, get_num_chunks, overlapped_chunks, seek_info, test_helpers::random_bytes, DataMap, EncryptedChunk, Error, StreamSelfDecryptor, - StreamSelfEncryptor, MIN_ENCRYPTABLE_BYTES, + StreamSelfEncryptor, MAX_CHUNK_SIZE, MIN_CHUNK_SIZE, MIN_ENCRYPTABLE_BYTES, }; use bytes::Bytes; use itertools::Itertools; @@ -241,6 +241,20 @@ fn get_chunk_sizes() -> Result<(), Error> { Ok(()) } +#[test] +fn test_get_num_chunks() { + for i in 0..3 * MIN_CHUNK_SIZE { + assert_eq!(get_num_chunks(i), 0); + } + + for i in 3 * MIN_CHUNK_SIZE..3 * MAX_CHUNK_SIZE { + assert_eq!(get_num_chunks(i), 3); + } + + assert_eq!(get_num_chunks(3 * MAX_CHUNK_SIZE), 3); + assert_eq!(get_num_chunks(3 * MAX_CHUNK_SIZE + 1), 4); +} + #[test] fn seek_and_join() -> Result<(), Error> { for i in 1..15 {