work on parts 6-8
[rust-101.git] / src / part08.rs
1 // Rust-101, Part 08: Associated Types, Modules
2 // ============================================
3
4 use std::cmp;
5 use std::ops;
6 use std::fmt;
7 use part05::BigInt;
8
9 // Add with carry, returning the sum and the carry
10 fn overflowing_add(a: u64, b: u64, carry: bool) -> (u64, bool) {
11     let sum = u64::wrapping_add(a, b);
12     if sum >= a { // first addition did not overflow
13         unimplemented!()
14     } else { // first addition *did* overflow
15         unimplemented!()
16     }
17 }
18
19 /*#[test]*/
20 fn test_overflowing_add() {
21     assert_eq!(overflowing_add(10, 100, false), (110, false));
22     assert_eq!(overflowing_add(10, 100, true), (111, false));
23     assert_eq!(overflowing_add(1 << 63, 1 << 63, false), (0, true));
24     assert_eq!(overflowing_add(1 << 63, 1 << 63, true), (1, true));
25     assert_eq!(overflowing_add(1 << 63, (1 << 63) -1 , true), (0, true));
26 }
27
28 impl ops::Add for BigInt {
29     type Output = BigInt;
30     fn add(self, rhs: BigInt) -> Self::Output {
31         let mut result_vec:Vec<u64> = Vec::with_capacity(cmp::max(self.data.len(), rhs.data.len()));
32         unimplemented!()
33     }
34 }
35
36 // [index](main.html) | [previous](part07.html) | [next](main.html)