]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_serialize/tests/leb128.rs
Rollup merge of #80279 - Yaulendil:str-as-mut, r=m-ou-se
[rust.git] / compiler / rustc_serialize / tests / leb128.rs
index 45afcc256ed8f1b67c71b50a260b1ba59516567d..a2bcf2c251d7ab1295f4293c9628819d956bcb20 100644 (file)
@@ -1,3 +1,4 @@
+#![feature(int_bits_const)]
 #![feature(maybe_uninit_slice)]
 #![feature(maybe_uninit_uninit_array)]
 
@@ -8,16 +9,28 @@ macro_rules! impl_test_unsigned_leb128 {
     ($test_name:ident, $write_fn_name:ident, $read_fn_name:ident, $int_ty:ident) => {
         #[test]
         fn $test_name() {
+            // 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)),
+            );
+
             let mut stream = Vec::new();
 
-            for x in 0..62 {
+            for &x in &values {
                 let mut buf = MaybeUninit::uninit_array();
-                stream.extend($write_fn_name(&mut buf, (3u64 << x) as $int_ty));
+                stream.extend($write_fn_name(&mut buf, x));
             }
 
             let mut position = 0;
-            for x in 0..62 {
-                let expected = (3u64 << x) as $int_ty;
+            for &expected in &values {
                 let (actual, bytes_read) = $read_fn_name(&stream[position..]);
                 assert_eq!(expected, actual);
                 position += bytes_read;
@@ -34,12 +47,28 @@ fn $test_name() {
 impl_test_unsigned_leb128!(test_usize_leb128, write_usize_leb128, read_usize_leb128, usize);
 
 macro_rules! impl_test_signed_leb128 {
-    ($test_name:ident, $write_fn_name:ident, $int_ty:ident) => {
+    ($test_name:ident, $write_fn_name:ident, $read_fn_name:ident, $int_ty:ident) => {
         #[test]
         fn $test_name() {
-            let values: Vec<_> = (-500..500)
-                .map(|i| ((i as $int_ty).wrapping_mul(0x12345789ABCDEF_i64 as $int_ty)))
-                .collect();
+            // 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)),
+            );
+
             let mut stream = Vec::new();
 
             for &x in &values {
@@ -48,9 +77,8 @@ fn $test_name() {
             }
 
             let mut position = 0;
-            for &x in &values {
-                let expected = x as i128;
-                let (actual, bytes_read) = read_signed_leb128(&mut stream, position);
+            for &expected in &values {
+                let (actual, bytes_read) = $read_fn_name(&stream[position..]);
                 assert_eq!(expected, actual);
                 position += bytes_read;
             }
@@ -59,8 +87,8 @@ fn $test_name() {
     };
 }
 
-impl_test_signed_leb128!(test_i16_leb128, write_i16_leb128, i16);
-impl_test_signed_leb128!(test_i32_leb128, write_i32_leb128, i32);
-impl_test_signed_leb128!(test_i64_leb128, write_i64_leb128, i64);
-impl_test_signed_leb128!(test_i128_leb128, write_i128_leb128, i128);
-impl_test_signed_leb128!(test_isize_leb128, write_isize_leb128, isize);
+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);