2020-12-06 19:30:55 -06:00
|
|
|
#![feature(maybe_uninit_slice)]
|
|
|
|
#![feature(maybe_uninit_uninit_array)]
|
|
|
|
|
2019-07-31 18:47:06 -05:00
|
|
|
use rustc_serialize::leb128::*;
|
2020-12-06 19:30:55 -06:00
|
|
|
use std::mem::MaybeUninit;
|
2023-04-20 22:11:47 -05:00
|
|
|
use rustc_serialize::Decoder;
|
2019-07-31 18:47:06 -05:00
|
|
|
|
|
|
|
macro_rules! impl_test_unsigned_leb128 {
|
2019-12-22 16:42:04 -06:00
|
|
|
($test_name:ident, $write_fn_name:ident, $read_fn_name:ident, $int_ty:ident) => {
|
2019-07-31 18:47:06 -05:00
|
|
|
#[test]
|
|
|
|
fn $test_name() {
|
2020-12-29 17:14:33 -06:00
|
|
|
// Test 256 evenly spaced values of integer range,
|
|
|
|
// integer max value, and some "random" numbers.
|
|
|
|
let mut values = Vec::new();
|
|
|
|
|
|
|
|
let increment = (1 as $int_ty) << ($int_ty::BITS - 8);
|
|
|
|
values.extend((0..256).map(|i| $int_ty::MIN + i * increment));
|
|
|
|
|
|
|
|
values.push($int_ty::MAX);
|
|
|
|
|
|
|
|
values.extend(
|
|
|
|
(-500..500).map(|i| (i as $int_ty).wrapping_mul(0x12345789ABCDEFu64 as $int_ty)),
|
|
|
|
);
|
|
|
|
|
2019-07-31 18:47:06 -05:00
|
|
|
let mut stream = Vec::new();
|
|
|
|
|
2020-12-29 17:14:33 -06:00
|
|
|
for &x in &values {
|
2020-12-06 19:30:55 -06:00
|
|
|
let mut buf = MaybeUninit::uninit_array();
|
2020-12-29 17:14:33 -06:00
|
|
|
stream.extend($write_fn_name(&mut buf, x));
|
2019-07-31 18:47:06 -05:00
|
|
|
}
|
|
|
|
|
2023-04-20 22:11:47 -05:00
|
|
|
let mut decoder = rustc_serialize::opaque::MemDecoder::new(&stream, 0);
|
2020-12-29 17:14:33 -06:00
|
|
|
for &expected in &values {
|
2023-04-20 22:11:47 -05:00
|
|
|
let actual = $read_fn_name(&mut decoder);
|
2019-07-31 18:47:06 -05:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2023-04-20 22:11:47 -05:00
|
|
|
assert_eq!(stream.len(), decoder.position());
|
2019-07-31 18:47:06 -05:00
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
};
|
2019-07-31 18:47:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl_test_unsigned_leb128!(test_u16_leb128, write_u16_leb128, read_u16_leb128, u16);
|
|
|
|
impl_test_unsigned_leb128!(test_u32_leb128, write_u32_leb128, read_u32_leb128, u32);
|
|
|
|
impl_test_unsigned_leb128!(test_u64_leb128, write_u64_leb128, read_u64_leb128, u64);
|
|
|
|
impl_test_unsigned_leb128!(test_u128_leb128, write_u128_leb128, read_u128_leb128, u128);
|
|
|
|
impl_test_unsigned_leb128!(test_usize_leb128, write_usize_leb128, read_usize_leb128, usize);
|
|
|
|
|
2020-12-06 19:30:55 -06:00
|
|
|
macro_rules! impl_test_signed_leb128 {
|
2020-12-29 17:14:33 -06:00
|
|
|
($test_name:ident, $write_fn_name:ident, $read_fn_name:ident, $int_ty:ident) => {
|
2020-12-06 19:30:55 -06:00
|
|
|
#[test]
|
|
|
|
fn $test_name() {
|
2020-12-29 17:14:33 -06:00
|
|
|
// Test 256 evenly spaced values of integer range,
|
|
|
|
// integer max value, and some "random" numbers.
|
|
|
|
let mut values = Vec::new();
|
|
|
|
|
|
|
|
let mut value = $int_ty::MIN;
|
|
|
|
let increment = (1 as $int_ty) << ($int_ty::BITS - 8);
|
|
|
|
|
|
|
|
for _ in 0..256 {
|
|
|
|
values.push(value);
|
|
|
|
// The addition in the last loop iteration overflows.
|
|
|
|
value = value.wrapping_add(increment);
|
|
|
|
}
|
|
|
|
|
|
|
|
values.push($int_ty::MAX);
|
|
|
|
|
|
|
|
values.extend(
|
|
|
|
(-500..500).map(|i| (i as $int_ty).wrapping_mul(0x12345789ABCDEFi64 as $int_ty)),
|
|
|
|
);
|
|
|
|
|
2020-12-06 19:30:55 -06:00
|
|
|
let mut stream = Vec::new();
|
|
|
|
|
|
|
|
for &x in &values {
|
|
|
|
let mut buf = MaybeUninit::uninit_array();
|
|
|
|
stream.extend($write_fn_name(&mut buf, x));
|
|
|
|
}
|
|
|
|
|
2023-04-20 22:11:47 -05:00
|
|
|
let mut decoder = rustc_serialize::opaque::MemDecoder::new(&stream, 0);
|
2020-12-29 17:14:33 -06:00
|
|
|
for &expected in &values {
|
2023-04-20 22:11:47 -05:00
|
|
|
let actual = $read_fn_name(&mut decoder);
|
2020-12-06 19:30:55 -06:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2023-04-20 22:11:47 -05:00
|
|
|
assert_eq!(stream.len(), decoder.position());
|
2020-12-06 19:30:55 -06:00
|
|
|
}
|
|
|
|
};
|
2019-07-31 18:47:06 -05:00
|
|
|
}
|
2020-12-06 19:30:55 -06:00
|
|
|
|
2020-12-29 17:14:33 -06:00
|
|
|
impl_test_signed_leb128!(test_i16_leb128, write_i16_leb128, read_i16_leb128, i16);
|
|
|
|
impl_test_signed_leb128!(test_i32_leb128, write_i32_leb128, read_i32_leb128, i32);
|
|
|
|
impl_test_signed_leb128!(test_i64_leb128, write_i64_leb128, read_i64_leb128, i64);
|
|
|
|
impl_test_signed_leb128!(test_i128_leb128, write_i128_leb128, read_i128_leb128, i128);
|
|
|
|
impl_test_signed_leb128!(test_isize_leb128, write_isize_leb128, read_isize_leb128, isize);
|