-// Rust-101, Part 06: Abstract Datastructure, Testing
-// ==================================================
+// Rust-101, Part 06: Lifetimes, Testing
+// =====================================
use std::cmp;
use std::ops;
+use std::fmt;
+use part05::BigInt;
-pub struct BigInt {
- data: Vec<u64>, // least significant digits first. The last block will *not* be 0.
-}
-impl BigInt {
- pub fn new(x: u64) -> Self {
- if x == 0 {
- BigInt { data: vec![] }
- } else {
- BigInt { data: vec![x] }
- }
+impl PartialEq for BigInt {
+ fn eq(&self, other: &BigInt) -> bool {
+ debug_assert!(self.test_invariant() && other.test_invariant());
+ self.data == other.data
}
}
-/// Add with carry, returning the sum and the carry
-fn overflowing_add(a: u64, b: u64, carry: bool) -> (u64, bool) {
- match u64::checked_add(a, b) {
- Some(sum) if !carry => (sum, false),
- Some(sum) => { // we have to increment the sum by 1, where it may overflow again
- match u64::checked_add(sum, 1) {
- Some(total_sum) => (total_sum, false),
- None => (0, true) // we overflowed incrementing by 1, so we are just "at the edge"
- }
- },
- None => {
- // Get the remainder, i.e., the wrapping sum. This cannot overflow again by adding just 1, so it is safe
- // to add the carry here.
- let rem = u64::wrapping_add(a, b) + if carry { 1 } else { 0 };
- (rem, true)
- }
- }
+fn call_eq() {
+ let b1 = BigInt::new(13);
+ let b2 = BigInt::new(37);
+ println!("b1 == b1: {} ; b1 == b2: {}; b1 != b2: {}", b1 == b1, b1 == b2, b1 != b2);
}
-#[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 fmt::Debug for BigInt {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ self.data.fmt(f)
+ }
}
-impl ops::Add<BigInt> 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()));
- let mut carry:bool = false; // the carry bit
- for (i, val) in self.data.into_iter().enumerate() {
- // compute next digit and carry
- let rhs_val = if i < rhs.data.len() { rhs.data[i] } else { 0 };
- let (sum, new_carry) = overflowing_add(val, rhs_val, carry);
- // store them
- result_vec.push(sum);
- carry = new_carry;
- }
- BigInt { data: result_vec }
+
+
+impl BigInt {
+ pub fn inc(&mut self, mut by: u64) {
+ panic!("Not yet implemented.");
}
}
+#[test]
+fn test_inc() {
+ let mut b = BigInt::new(1337);
+ b.inc(1337);
+ assert!(b == BigInt::new(1337 + 1337));
+
+ b = BigInt::new(0);
+ assert_eq!(b, BigInt::from_vec(vec![0]));
+ b.inc(1 << 63);
+ assert_eq!(b, BigInt::from_vec(vec![1 << 63]));
+ b.inc(1 << 63);
+ assert_eq!(b, BigInt::from_vec(vec![0, 1]));
+ b.inc(1 << 63);
+ assert_eq!(b, BigInt::from_vec(vec![1 << 63, 1]));
+ b.inc(1 << 63);
+ assert_eq!(b, BigInt::from_vec(vec![0, 2]));
+}