MCPcopy Create free account
hub / github.com/SoftbearStudios/bitcode / test_pack_bytes_less_than_n

Function test_pack_bytes_less_than_n

src/pack.rs:671–729  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

669 bench_n!(bench_unpack_arithmetic, 2, 3, 4, 6, 16);
670
671 fn test_pack_bytes_less_than_n<const N: usize, const FACTOR: usize>() {
672 for n in [1, 11, 97, 991, 10007].into_iter().flat_map(|n_prime| {
673 let divisor = if FACTOR == 256 {
674 1
675 } else {
676 super::factor_to_divisor::<FACTOR>()
677 };
678 let n_factor = crate::nightly::div_ceil_usize(n_prime, divisor) * divisor;
679 [n_factor, n_prime]
680 }) {
681 let bytes: Vec<_> = crate::random_data(n)
682 .into_iter()
683 .map(|v: usize| (v % N as usize) as u8)
684 .collect();
685 let n = bytes.len(); // random_data shrinks n on miri.
686
687 println!("n {n}, N {N}, FACTOR {FACTOR}");
688 if N != FACTOR {
689 let mut bytes = bytes.clone();
690 bytes[n - 1] = (FACTOR - 1) as u8; // Make least 1 byte is out of bounds.
691 let mut packed = vec![];
692 super::pack_bytes_less_than::<FACTOR>(&bytes, &mut packed);
693
694 assert!(super::unpack_bytes_less_than::<N, 0>(
695 &mut packed.as_slice(),
696 bytes.len(),
697 &mut crate::fast::CowSlice::default()
698 )
699 .is_err());
700 assert!(super::unpack_bytes_less_than::<N, N>(
701 &mut packed.as_slice(),
702 bytes.len(),
703 &mut crate::fast::CowSlice::default()
704 )
705 .is_err());
706 }
707
708 let mut packed = vec![];
709 super::pack_bytes_less_than::<N>(&bytes, &mut packed);
710
711 let mut input = packed.as_slice();
712 let mut unpacked = crate::fast::CowSlice::default();
713 super::unpack_bytes_less_than::<N, 0>(&mut input, bytes.len(), &mut unpacked).unwrap();
714 assert!(input.is_empty());
715 assert_eq!(unsafe { unpacked.as_slice(bytes.len()) }, bytes);
716
717 let mut input = packed.as_slice();
718 let mut unpacked = crate::fast::CowSlice::default();
719 let histogram =
720 super::unpack_bytes_less_than::<N, N>(&mut input, bytes.len(), &mut unpacked)
721 .unwrap();
722 assert!(input.is_empty());
723 assert_eq!(unsafe { unpacked.as_slice(bytes.len()) }, bytes);
724 assert_eq!(
725 histogram.as_slice(),
726 &crate::histogram::histogram(&bytes)[..N]
727 );
728 }

Callers

nothing calls this directly

Calls 4

as_sliceMethod · 0.80
random_dataFunction · 0.70
collectMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected