To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit 4f2d1adc authored by Stefan Schindler's avatar Stefan Schindler
Browse files

Add + and += to ByteTypes

parent af7324ff
......@@ -168,6 +168,33 @@ impl std::cmp::PartialOrd<Self> for ByteUnit {
})
}
}
impl std::ops::Add for ByteUnit {
type Output = ByteUnit;
fn add(self, other: Self) -> Self {
match self {
Byte(l) => match other {
Byte(r) => Byte(l + r),
something_else => self + something_else.to_byte(),
},
something_else => something_else.to_byte() + other,
}
}
}
impl std::ops::AddAssign for ByteUnit {
fn add_assign(&mut self, other: Self) {
match self {
&mut Byte(ref mut me) => match other {
Byte(r) => { *me += r; },
KiloByte(r) => { *me += r * 1024; },
},
&mut KiloByte(ref mut me) => match other {
Byte(r) => *me += r / 1024, // FIXME: Rundungsfehler
KiloByte(r) => *me += r,
},
}
}
}
#[derive(Debug,PartialEq,Eq)]
......@@ -342,4 +369,47 @@ mod test {
assert!(two_kilobytes > one_k_bytes);
assert!(one_k_bytes < two_kilobytes);
}
#[test]
fn add_bytes() {
assert_eq!(Byte(4), Byte(2) + Byte(2));
}
#[test]
fn add_kilobytes() {
assert_eq!(KiloByte(4), KiloByte(2) + KiloByte(2));
}
#[test]
fn add_bytes_mixed_up() {
assert_eq!(Byte(2 + 2048), Byte(2) + KiloByte(2));
}
#[test]
fn add_bytes_mixed_down() {
assert_eq!(Byte(2 + 2048), KiloByte(2) + Byte(2));
}
#[test]
fn add_assign_bytes() {
let mut four = Byte(2);
four += Byte(2);
assert_eq!(Byte(4), four);
}
#[test]
fn add_assign_kilobytes() {
let mut four = KiloByte(2);
four += KiloByte(2);
assert_eq!(KiloByte(4), four);
}
#[test]
fn add_assign_mixed_up() {
let mut four = Byte(2);
four += KiloByte(2);
assert_eq!(Byte(2 + 2048), four);
}
#[test]
fn add_assign_mixed_down() {
let mut four = KiloByte(2);
four += Byte(2);
assert_eq!(Byte(2 + 2048), four);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment