Skip to content

Commit 326e631

Browse files
committed
std::io: Add tests and benchmarks for u64_from_be_bytes()
1 parent 423dd84 commit 326e631

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

src/libstd/io/extensions.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,4 +465,86 @@ mod test {
465465
assert!(reader.read_le_f32() == 8.1250);
466466
}
467467

468+
#[test]
469+
fn test_u64_from_be_bytes() {
470+
use super::u64_from_be_bytes;
471+
472+
let buf = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09];
473+
474+
// Aligned access
475+
assert_eq!(u64_from_be_bytes(buf, 0, 0), 0);
476+
assert_eq!(u64_from_be_bytes(buf, 0, 1), 0x01);
477+
assert_eq!(u64_from_be_bytes(buf, 0, 2), 0x0102);
478+
assert_eq!(u64_from_be_bytes(buf, 0, 3), 0x010203);
479+
assert_eq!(u64_from_be_bytes(buf, 0, 4), 0x01020304);
480+
assert_eq!(u64_from_be_bytes(buf, 0, 5), 0x0102030405);
481+
assert_eq!(u64_from_be_bytes(buf, 0, 6), 0x010203040506);
482+
assert_eq!(u64_from_be_bytes(buf, 0, 7), 0x01020304050607);
483+
assert_eq!(u64_from_be_bytes(buf, 0, 8), 0x0102030405060708);
484+
485+
// Unaligned access
486+
assert_eq!(u64_from_be_bytes(buf, 1, 0), 0);
487+
assert_eq!(u64_from_be_bytes(buf, 1, 1), 0x02);
488+
assert_eq!(u64_from_be_bytes(buf, 1, 2), 0x0203);
489+
assert_eq!(u64_from_be_bytes(buf, 1, 3), 0x020304);
490+
assert_eq!(u64_from_be_bytes(buf, 1, 4), 0x02030405);
491+
assert_eq!(u64_from_be_bytes(buf, 1, 5), 0x0203040506);
492+
assert_eq!(u64_from_be_bytes(buf, 1, 6), 0x020304050607);
493+
assert_eq!(u64_from_be_bytes(buf, 1, 7), 0x02030405060708);
494+
assert_eq!(u64_from_be_bytes(buf, 1, 8), 0x0203040506070809);
495+
}
496+
}
497+
498+
#[cfg(test)]
499+
mod bench {
500+
use extra::test::BenchHarness;
501+
use container::Container;
502+
503+
macro_rules! u64_from_be_bytes_bench_impl(
504+
($size:expr, $stride:expr, $start_index:expr) =>
505+
({
506+
use vec;
507+
use super::u64_from_be_bytes;
508+
509+
let data = vec::from_fn($stride*100+$start_index, |i| i as u8);
510+
let mut sum = 0u64;
511+
bh.iter(|| {
512+
let mut i = $start_index;
513+
while (i < data.len()) {
514+
sum += u64_from_be_bytes(data, i, $size);
515+
i += $stride;
516+
}
517+
});
518+
})
519+
)
520+
521+
#[bench]
522+
fn u64_from_be_bytes_4_aligned(bh: &mut BenchHarness) {
523+
u64_from_be_bytes_bench_impl!(4, 4, 0);
524+
}
525+
526+
#[bench]
527+
fn u64_from_be_bytes_4_unaligned(bh: &mut BenchHarness) {
528+
u64_from_be_bytes_bench_impl!(4, 4, 1);
529+
}
530+
531+
#[bench]
532+
fn u64_from_be_bytes_7_aligned(bh: &mut BenchHarness) {
533+
u64_from_be_bytes_bench_impl!(7, 8, 0);
534+
}
535+
536+
#[bench]
537+
fn u64_from_be_bytes_7_unaligned(bh: &mut BenchHarness) {
538+
u64_from_be_bytes_bench_impl!(7, 8, 1);
539+
}
540+
541+
#[bench]
542+
fn u64_from_be_bytes_8_aligned(bh: &mut BenchHarness) {
543+
u64_from_be_bytes_bench_impl!(8, 8, 0);
544+
}
545+
546+
#[bench]
547+
fn u64_from_be_bytes_8_unaligned(bh: &mut BenchHarness) {
548+
u64_from_be_bytes_bench_impl!(8, 8, 1);
549+
}
468550
}

0 commit comments

Comments
 (0)