Removed warning of rustc 1.7.0 complaining about deprecated usage of std::thread...
[rust-101.git] / solutions / src / bigint.rs
index 7a61b4b5de9ccfac4a6dc717145055f181a88494..5869ba89401076c8998bb7414dae31e71a3d738c 100644 (file)
@@ -154,16 +154,14 @@ impl Minimum for BigInt {
             other
         } else {
             // compare back-to-front, i.e., most significant digit first
-            let mut idx = self.data.len()-1;
+            let mut idx = self.data.len();
             while idx > 0 {
+                idx = idx-1;
                 if self.data[idx] < other.data[idx] {
                     return self;
                 } else if self.data[idx] > other.data[idx] {
                     return other;
                 }
-                else {
-                    idx = idx-1;
-                }
             }
             // the two are equal
             return self;
@@ -276,7 +274,31 @@ impl ops::Sub<BigInt> for BigInt {
 #[cfg(test)]
 mod tests {
     use std::u64;
-    use super::{overflowing_add,overflowing_sub,BigInt};
+    use super::{overflowing_add,overflowing_sub,BigInt,Minimum,vec_min};
+
+    #[test]
+    fn test_min() {
+        let b1 = BigInt::new(1);
+        let b2 = BigInt::new(42);
+        let b3 = BigInt::from_vec(vec![0, 1]);
+
+        assert_eq!(b1.min(&b2), &b1);
+        assert_eq!(b2.min(&b1), &b1);
+        assert_eq!(b3.min(&b2), &b2);
+        assert_eq!(b2.min(&b3), &b2);
+    }
+
+    #[test]
+    fn test_vec_min() {
+    let b1 = BigInt::new(1);
+        let b2 = BigInt::new(42);
+        let b3 = BigInt::from_vec(vec![0, 1]);
+
+        assert_eq!(vec_min(&vec![b2.clone(), b1.clone(), b3.clone()]), Some(&b1));
+        assert_eq!(vec_min(&vec![b2.clone(), b3.clone()]), Some(&b2));
+        assert_eq!(vec_min(&vec![b3.clone()]), Some(&b3));
+        assert_eq!(vec_min::<BigInt>(&vec![]), None);
+    }
 
     #[test]
     fn test_overflowing_add() {
@@ -320,12 +342,24 @@ mod tests {
 
         assert_eq!(&b2 - &b1, BigInt::from_vec(vec![u64::max_value() - (1 << 32) + 1]));
         assert_eq!(&b3 - &b2, BigInt::from_vec(vec![0, u64::max_value(), 0]));
-        assert_eq!(&b2 - &b4 - &b4, BigInt::from_vec(vec![0]));
+        assert_eq!(&b2 - &b4 - &b4, BigInt::new(0));
         assert_eq!(&b3 - &b2 - &b4 - &b4, BigInt::from_vec(vec![0, u64::max_value() - 1]));
         assert_eq!(&b3 - &b4 - &b2 - &b4, BigInt::from_vec(vec![0, u64::max_value() - 1]));
         assert_eq!(&b3 - &b4 - &b4 - &b2, BigInt::from_vec(vec![0, u64::max_value() - 1]));
     }
 
+    #[test]
+    #[should_panic(expected = "Wrapping subtraction of BigInt")]
+    fn test_sub_panic1() {
+        let _ = BigInt::new(1) - BigInt::new(5);
+    }
+
+    #[test]
+    #[should_panic(expected = "Wrapping subtraction of BigInt")]
+    fn test_sub_panic2() {
+        let _ = BigInt::from_vec(vec![5,8,3,33,1<<13,46,1<<49, 1, 583,1<<60,2533]) - BigInt::from_vec(vec![5,8,3,33,1<<13,46,1<<49, 5, 583,1<<60,2533]);
+    }
+
     #[test]
     fn test_inc1() {
         let mut b = BigInt::new(0);