1 use rustc_serialize::leb128::*;
3 macro_rules! impl_test_unsigned_leb128 {
4 ($test_name:ident, $write_fn_name:ident, $read_fn_name:ident, $int_ty:ident) => {
7 let mut stream = Vec::new();
10 $write_fn_name(&mut stream, (3u64 << x) as $int_ty);
15 let expected = (3u64 << x) as $int_ty;
16 let (actual, bytes_read) = $read_fn_name(&stream[position..]);
17 assert_eq!(expected, actual);
18 position += bytes_read;
20 assert_eq!(stream.len(), position);
25 impl_test_unsigned_leb128!(test_u16_leb128, write_u16_leb128, read_u16_leb128, u16);
26 impl_test_unsigned_leb128!(test_u32_leb128, write_u32_leb128, read_u32_leb128, u32);
27 impl_test_unsigned_leb128!(test_u64_leb128, write_u64_leb128, read_u64_leb128, u64);
28 impl_test_unsigned_leb128!(test_u128_leb128, write_u128_leb128, read_u128_leb128, u128);
29 impl_test_unsigned_leb128!(test_usize_leb128, write_usize_leb128, read_usize_leb128, usize);
32 fn test_signed_leb128() {
33 let values: Vec<_> = (-500..500).map(|i| i * 0x12345789ABCDEF).collect();
34 let mut stream = Vec::new();
36 write_signed_leb128(&mut stream, x);
40 let (value, bytes_read) = read_signed_leb128(&mut stream, pos);
44 assert_eq!(pos, stream.len());