start doing the course-part of BigInt
[rust-101.git] / src / part07.rs
diff --git a/src/part07.rs b/src/part07.rs
new file mode 100644 (file)
index 0000000..e9715e0
--- /dev/null
@@ -0,0 +1,30 @@
+use std::cmp;
+use std::ops;
+use part05::BigInt;
+
+// Add with carry, returning the sum and the carry
+fn overflowing_add(a: u64, b: u64, carry: bool) -> (u64, bool) {
+    let sum = u64::wrapping_add(a, b);
+    if sum >= a {
+        panic!("First addition did not overflow. Not implemented.");
+    } else {
+        panic!("First addition *did* overflow. Not implemented.");
+    }
+}
+
+/*#[test]*/
+fn test_overflowing_add() {
+    assert_eq!(overflowing_add(10, 100, false), (110, false));
+    assert_eq!(overflowing_add(10, 100, true), (111, false));
+    assert_eq!(overflowing_add(1 << 63, 1 << 63, false), (0, true));
+    assert_eq!(overflowing_add(1 << 63, 1 << 63, true), (1, true));
+    assert_eq!(overflowing_add(1 << 63, (1 << 63) -1 , true), (0, true));
+}
+
+impl ops::Add for BigInt {
+    type Output = BigInt;
+    fn add(self, rhs: BigInt) -> Self::Output {
+        let mut result_vec:Vec<u64> = Vec::with_capacity(cmp::max(self.data.len(), rhs.data.len()));
+        panic!("Not yet implemented.");
+    }
+}