+ #[test]
+ fn test_overflowing_sub() {
+ assert_eq!(overflowing_sub(100, 10, false), (90, false));
+ assert_eq!(overflowing_sub(100, 10, true), (89, false));
+ assert_eq!(overflowing_sub(10, 1 << 63, false), ((1 << 63) + 10, true));
+ assert_eq!(overflowing_sub(10, 1 << 63, true), ((1 << 63) + 9, true));
+ assert_eq!(overflowing_sub(42, 42 , true), (u64::max_value(), true));
+ }
+
+ #[test]
+ fn test_add() {
+ let b1 = BigInt::new(1 << 32);
+ let b2 = BigInt::from_vec(vec![0, 1]);
+ let b3 = BigInt::from_vec(vec![0, 0, 1]);
+ let b4 = BigInt::new(1 << 63);
+
+ assert_eq!(&b1 + &b2, BigInt::from_vec(vec![1 << 32, 1]));
+ assert_eq!(&b2 + &b1, BigInt::from_vec(vec![1 << 32, 1]));
+ assert_eq!(&b2 + &b3, BigInt::from_vec(vec![0, 1, 1]));
+ assert_eq!(&b2 + &b3 + &b4 + &b4, BigInt::from_vec(vec![0, 2, 1]));
+ assert_eq!(&b2 + &b4 + &b3 + &b4, BigInt::from_vec(vec![0, 2, 1]));
+ assert_eq!(&b4 + &b2 + &b3 + &b4, BigInt::from_vec(vec![0, 2, 1]));
+ }
+
+ #[test]
+ fn test_sub() {
+ let b1 = BigInt::new(1 << 32);
+ let b2 = BigInt::from_vec(vec![0, 1]);
+ let b3 = BigInt::from_vec(vec![0, 0, 1]);
+ let b4 = BigInt::new(1 << 63);
+
+ assert_eq!(&b2 - &b1, BigInt::from_vec(vec![u64::max_value() - (1 << 32) + 1]));
+ assert_eq!(&b3 - &b2, BigInt::from_vec(vec![0, u64::max_value(), 0]));
+ assert_eq!(&b2 - &b4 - &b4, BigInt::new(0));
+ assert_eq!(&b3 - &b2 - &b4 - &b4, BigInt::from_vec(vec![0, u64::max_value() - 1]));
+ assert_eq!(&b3 - &b4 - &b2 - &b4, BigInt::from_vec(vec![0, u64::max_value() - 1]));
+ assert_eq!(&b3 - &b4 - &b4 - &b2, BigInt::from_vec(vec![0, u64::max_value() - 1]));
+ }
+
+ #[test]
+ #[should_panic(expected = "Wrapping subtraction of BigInt")]
+ fn test_sub_panic1() {
+ let _ = BigInt::new(1) - BigInt::new(5);
+ }
+
+ #[test]
+ #[should_panic(expected = "Wrapping subtraction of BigInt")]
+ fn test_sub_panic2() {
+ let _ = BigInt::from_vec(vec![5,8,3,33,1<<13,46,1<<49, 1, 583,1<<60,2533]) - BigInt::from_vec(vec![5,8,3,33,1<<13,46,1<<49, 5, 583,1<<60,2533]);
+ }
+
+ #[test]
+ fn test_inc1() {
+ let mut b = BigInt::new(0);
+ b.inc1();
+ assert_eq!(b, BigInt::new(1));
+ b.inc1();
+ assert_eq!(b, BigInt::new(2));
+
+ b = BigInt::new(u64::MAX);
+ b.inc1();
+ assert_eq!(b, BigInt::from_vec(vec![0, 1]));
+ b.inc1();
+ assert_eq!(b, BigInt::from_vec(vec![1, 1]));
+ }
+