1
+ #![ feature( int_bits_const) ]
1
2
#![ feature( maybe_uninit_slice) ]
2
3
#![ feature( maybe_uninit_uninit_array) ]
3
4
@@ -8,16 +9,28 @@ macro_rules! impl_test_unsigned_leb128 {
8
9
( $test_name: ident, $write_fn_name: ident, $read_fn_name: ident, $int_ty: ident) => {
9
10
#[ test]
10
11
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
+
11
25
let mut stream = Vec :: new( ) ;
12
26
13
- for x in 0 .. 62 {
27
+ for & x in & values {
14
28
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 ) ) ;
16
30
}
17
31
18
32
let mut position = 0 ;
19
- for x in 0 ..62 {
20
- let expected = ( 3u64 << x) as $int_ty;
33
+ for & expected in & values {
21
34
let ( actual, bytes_read) = $read_fn_name( & stream[ position..] ) ;
22
35
assert_eq!( expected, actual) ;
23
36
position += bytes_read;
@@ -34,12 +47,28 @@ impl_test_unsigned_leb128!(test_u128_leb128, write_u128_leb128, read_u128_leb128
34
47
impl_test_unsigned_leb128 ! ( test_usize_leb128, write_usize_leb128, read_usize_leb128, usize ) ;
35
48
36
49
macro_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) => {
38
51
#[ test]
39
52
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
+
43
72
let mut stream = Vec :: new( ) ;
44
73
45
74
for & x in & values {
@@ -48,9 +77,8 @@ macro_rules! impl_test_signed_leb128 {
48
77
}
49
78
50
79
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..] ) ;
54
82
assert_eq!( expected, actual) ;
55
83
position += bytes_read;
56
84
}
@@ -59,8 +87,8 @@ macro_rules! impl_test_signed_leb128 {
59
87
} ;
60
88
}
61
89
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