start doing the course-part of BigInt
[rust-101.git] / src / part07.rs
1 use std::cmp;
2 use std::ops;
3 use part05::BigInt;
4
5 // Add with carry, returning the sum and the carry
6 fn overflowing_add(a: u64, b: u64, carry: bool) -> (u64, bool) {
7     let sum = u64::wrapping_add(a, b);
8     if sum >= a {
9         panic!("First addition did not overflow. Not implemented.");
10     } else {
11         panic!("First addition *did* overflow. Not implemented.");
12     }
13 }
14
15 /*#[test]*/
16 fn test_overflowing_add() {
17     assert_eq!(overflowing_add(10, 100, false), (110, false));
18     assert_eq!(overflowing_add(10, 100, true), (111, false));
19     assert_eq!(overflowing_add(1 << 63, 1 << 63, false), (0, true));
20     assert_eq!(overflowing_add(1 << 63, 1 << 63, true), (1, true));
21     assert_eq!(overflowing_add(1 << 63, (1 << 63) -1 , true), (0, true));
22 }
23
24 impl ops::Add for BigInt {
25     type Output = BigInt;
26     fn add(self, rhs: BigInt) -> Self::Output {
27         let mut result_vec:Vec<u64> = Vec::with_capacity(cmp::max(self.data.len(), rhs.data.len()));
28         panic!("Not yet implemented.");
29     }
30 }