Skip to content

Commit

Permalink
refactor: using div_ceil to calculate num of chunks from given file size
Browse files Browse the repository at this point in the history
  • Loading branch information
bochaco committed Jun 25, 2024
1 parent f378ded commit 4d82206
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
7 changes: 2 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 15 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4d82206

Please sign in to comment.