1+ #![ feature( int_bits_const) ]
12#![ feature( maybe_uninit_slice) ]
23#![ feature( maybe_uninit_uninit_array) ]
34
@@ -8,16 +9,28 @@ macro_rules! impl_test_unsigned_leb128 {
89 ( $test_name: ident, $write_fn_name: ident, $read_fn_name: ident, $int_ty: ident) => {
910 #[ test]
1011 fn $test_name( ) {
12+ // Test 256 evenly spaced values of integer range,
13+ // integer max value, and some "random" numbers.
14+ let mut values = Vec :: new( ) ;
15+
16+ let increment = ( 1 as $int_ty) << ( $int_ty:: BITS - 8 ) ;
17+ values. extend( ( 0 ..256 ) . map( |i| $int_ty:: MIN + i * increment) ) ;
18+
19+ values. push( $int_ty:: MAX ) ;
20+
21+ values. extend(
22+ ( -500 ..500 ) . map( |i| ( i as $int_ty) . wrapping_mul( 0x12345789ABCDEFu64 as $int_ty) ) ,
23+ ) ;
24+
1125 let mut stream = Vec :: new( ) ;
1226
13- for x in 0 .. 62 {
27+ for & x in & values {
1428 let mut buf = MaybeUninit :: uninit_array( ) ;
15- stream. extend( $write_fn_name( & mut buf, ( 3u64 << x ) as $int_ty ) ) ;
29+ stream. extend( $write_fn_name( & mut buf, x ) ) ;
1630 }
1731
1832 let mut position = 0 ;
19- for x in 0 ..62 {
20- let expected = ( 3u64 << x) as $int_ty;
33+ for & expected in & values {
2134 let ( actual, bytes_read) = $read_fn_name( & stream[ position..] ) ;
2235 assert_eq!( expected, actual) ;
2336 position += bytes_read;
@@ -34,12 +47,28 @@ impl_test_unsigned_leb128!(test_u128_leb128, write_u128_leb128, read_u128_leb128
3447impl_test_unsigned_leb128 ! ( test_usize_leb128, write_usize_leb128, read_usize_leb128, usize ) ;
3548
3649macro_rules! impl_test_signed_leb128 {
37- ( $test_name: ident, $write_fn_name: ident, $int_ty: ident) => {
50+ ( $test_name: ident, $write_fn_name: ident, $read_fn_name : ident , $ int_ty: ident) => {
3851 #[ test]
3952 fn $test_name( ) {
40- let values: Vec <_> = ( -500 ..500 )
41- . map( |i| ( ( i as $int_ty) . wrapping_mul( 0x12345789ABCDEF_i64 as $int_ty) ) )
42- . collect( ) ;
53+ // Test 256 evenly spaced values of integer range,
54+ // integer max value, and some "random" numbers.
55+ let mut values = Vec :: new( ) ;
56+
57+ let mut value = $int_ty:: MIN ;
58+ let increment = ( 1 as $int_ty) << ( $int_ty:: BITS - 8 ) ;
59+
60+ for _ in 0 ..256 {
61+ values. push( value) ;
62+ // The addition in the last loop iteration overflows.
63+ value = value. wrapping_add( increment) ;
64+ }
65+
66+ values. push( $int_ty:: MAX ) ;
67+
68+ values. extend(
69+ ( -500 ..500 ) . map( |i| ( i as $int_ty) . wrapping_mul( 0x12345789ABCDEFi64 as $int_ty) ) ,
70+ ) ;
71+
4372 let mut stream = Vec :: new( ) ;
4473
4574 for & x in & values {
@@ -48,9 +77,8 @@ macro_rules! impl_test_signed_leb128 {
4877 }
4978
5079 let mut position = 0 ;
51- for & x in & values {
52- let expected = x as i128 ;
53- let ( actual, bytes_read) = read_signed_leb128( & mut stream, position) ;
80+ for & expected in & values {
81+ let ( actual, bytes_read) = $read_fn_name( & stream[ position..] ) ;
5482 assert_eq!( expected, actual) ;
5583 position += bytes_read;
5684 }
@@ -59,8 +87,8 @@ macro_rules! impl_test_signed_leb128 {
5987 } ;
6088}
6189
62- impl_test_signed_leb128 ! ( test_i16_leb128, write_i16_leb128, i16 ) ;
63- impl_test_signed_leb128 ! ( test_i32_leb128, write_i32_leb128, i32 ) ;
64- impl_test_signed_leb128 ! ( test_i64_leb128, write_i64_leb128, i64 ) ;
65- impl_test_signed_leb128 ! ( test_i128_leb128, write_i128_leb128, i128 ) ;
66- impl_test_signed_leb128 ! ( test_isize_leb128, write_isize_leb128, isize ) ;
90+ impl_test_signed_leb128 ! ( test_i16_leb128, write_i16_leb128, read_i16_leb128 , i16 ) ;
91+ impl_test_signed_leb128 ! ( test_i32_leb128, write_i32_leb128, read_i32_leb128 , i32 ) ;
92+ impl_test_signed_leb128 ! ( test_i64_leb128, write_i64_leb128, read_i64_leb128 , i64 ) ;
93+ impl_test_signed_leb128 ! ( test_i128_leb128, write_i128_leb128, read_i128_leb128 , i128 ) ;
94+ impl_test_signed_leb128 ! ( test_isize_leb128, write_isize_leb128, read_isize_leb128 , isize ) ;
0 commit comments