start doing the course-part of BigInt
[rust-101.git] / src / part06.rs
1 // Rust-101, Part 06: Lifetimes, Testing
2 // =====================================
3
4 use std::cmp;
5 use std::ops;
6 use std::fmt;
7 use part05::BigInt;
8
9
10 impl PartialEq for BigInt {
11     fn eq(&self, other: &BigInt) -> bool {
12         debug_assert!(self.test_invariant() && other.test_invariant());
13         self.data == other.data
14     }
15 }
16
17 fn call_eq() {
18     let b1 = BigInt::new(13);
19     let b2 = BigInt::new(37);
20     println!("b1 == b1: {} ; b1 == b2: {}; b1 != b2: {}", b1 == b1, b1 == b2, b1 != b2);
21 }
22
23
24 impl fmt::Debug for BigInt {
25     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26         self.data.fmt(f)
27     }
28 }
29
30
31
32 impl BigInt {
33     pub fn inc(&mut self, mut by: u64) {
34         panic!("Not yet implemented.");
35     }
36 }
37
38
39 #[test]
40 fn test_inc() {
41     let mut b = BigInt::new(1337);
42     b.inc(1337);
43     assert!(b == BigInt::new(1337 + 1337));
44
45     b = BigInt::new(0);
46     assert_eq!(b, BigInt::from_vec(vec![0]));
47     b.inc(1 << 63);
48     assert_eq!(b, BigInt::from_vec(vec![1 << 63]));
49     b.inc(1 << 63);
50     assert_eq!(b, BigInt::from_vec(vec![0, 1]));
51     b.inc(1 << 63);
52     assert_eq!(b, BigInt::from_vec(vec![1 << 63, 1]));
53     b.inc(1 << 63);
54     assert_eq!(b, BigInt::from_vec(vec![0, 2]));
55 }
56