From 0c2869d5420c3f1273d5203633bad931371cbaa2 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 16 Jan 2016 16:52:59 +0100 Subject: [PATCH] Tesys for U256 assign ops. --- src/uint.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/uint.rs b/src/uint.rs index 89834a85a..00865387d 100644 --- a/src/uint.rs +++ b/src/uint.rs @@ -506,6 +506,36 @@ macro_rules! construct_uint { } } + impl AddAssign<$name> for $name { + fn add_assign(&mut self, other: Self) { + *self = self.add(other); + } + } + + impl SubAssign<$name> for $name { + fn sub_assign(&mut self, other: Self) { + *self = self.sub(other); + } + } + + impl MulAssign<$name> for $name { + fn mul_assign(&mut self, other: Self) { + *self = self.mul(other); + } + } + + impl DivAssign<$name> for $name { + fn div_assign(&mut self, other: Self) { + *self = self.div(other); + } + } + + impl RemAssign<$name> for $name { + fn rem_assign(&mut self, other: Self) { + *self = self.rem(other); + } + } + impl BitAnd<$name> for $name { type Output = $name; @@ -776,6 +806,37 @@ mod tests { use std::str::FromStr; use std::num::wrapping::OverflowingOps; + #[test] + pub fn assign_ops() { + let x: U256 = x!(69); + let y: U256 = x!(42); + { + let mut z = x; + z += y; + assert_eq!(z, x + y); + } + { + let mut z = x; + z -= y; + assert_eq!(z, x - y); + } + { + let mut z = x; + z *= y; + assert_eq!(z, x * y); + } + { + let mut z = x; + z /= y; + assert_eq!(z, x / y); + } + { + let mut z = x; + z %= y; + assert_eq!(z, x % y); + } + } + #[test] pub fn uint256_from() { let e = U256([10, 0, 0, 0]);