Skip to content

Commit 920e057

Browse files
committed
add round_chunk_item_size test case
1 parent bbdb6d6 commit 920e057

File tree

1 file changed

+41
-8
lines changed
  • turbopack/crates/turbopack-core/src/chunk

1 file changed

+41
-8
lines changed

turbopack/crates/turbopack-core/src/chunk/mod.rs

+41-8
Original file line numberDiff line numberDiff line change
@@ -714,15 +714,8 @@ pub trait ChunkType: ValueToString {
714714
}
715715

716716
pub fn round_chunk_item_size(size: usize) -> usize {
717-
if size == 0 {
718-
return 0;
719-
}
720717
let a = size.next_power_of_two();
721-
if a == size {
722-
size
723-
} else {
724-
size & (a | (a >> 1) | (a >> 2))
725-
}
718+
size & (a | (a >> 1) | (a >> 2))
726719
}
727720

728721
#[turbo_tasks::value(transparent)]
@@ -764,3 +757,43 @@ where
764757
chunk_item.chunking_context().chunk_item_id(chunk_item)
765758
}
766759
}
760+
761+
#[cfg(test)]
762+
mod tests {
763+
use super::*;
764+
765+
#[test]
766+
fn test_round_chunk_item_size() {
767+
assert_eq!(round_chunk_item_size(0), 0);
768+
assert_eq!(round_chunk_item_size(1), 1);
769+
assert_eq!(round_chunk_item_size(2), 2);
770+
assert_eq!(round_chunk_item_size(3), 3);
771+
assert_eq!(round_chunk_item_size(4), 4);
772+
assert_eq!(round_chunk_item_size(5), 4);
773+
assert_eq!(round_chunk_item_size(6), 6);
774+
assert_eq!(round_chunk_item_size(7), 6);
775+
assert_eq!(round_chunk_item_size(8), 8);
776+
777+
assert_eq!(changes_in_range(0..1000), 19);
778+
assert_eq!(changes_in_range(1000..2000), 2);
779+
assert_eq!(changes_in_range(2000..3000), 1);
780+
781+
assert_eq!(changes_in_range(3000..10000), 4);
782+
783+
fn changes_in_range(range: std::ops::Range<usize>) -> usize {
784+
let len = range.len();
785+
let mut count = 0;
786+
for i in range {
787+
let a = round_chunk_item_size(i);
788+
assert!(a >= i * 2 / 3);
789+
assert!(a <= i);
790+
let b = round_chunk_item_size(i + 1);
791+
792+
if a == b {
793+
count += 1;
794+
}
795+
}
796+
len - count
797+
}
798+
}
799+
}

0 commit comments

Comments
 (0)