extra test and method encode fix

This commit is contained in:
NikVolf 2016-03-30 02:20:01 +03:00
parent 198613a854
commit be40553674
2 changed files with 16 additions and 3 deletions

View File

@ -45,4 +45,16 @@ mod tests {
assert_eq!(vec![0, 0, 0, 0, 0, 5], service_proxy.socket().borrow().write_buffer.clone());
assert_eq!(10, result);
}
#[test]
fn call_service_proxy_optional() {
let mut socket = TestSocket::new();
socket.read_buffer = vec![0, 0, 0, 10];
let service_proxy = ServiceProxy::new(socket);
let result = service_proxy.rollback(Some(5), 10);
assert_eq!(vec![0, 1, 1, 0, 0, 0, 5, 0, 0, 0, 10], service_proxy.socket().borrow().write_buffer.clone());
assert_eq!(10, result);
}
}

View File

@ -29,10 +29,11 @@ impl Service {
*lock = *lock + f as usize;
f
}
pub fn rollback(&self, a: u32, b: u32) -> i32 {
pub fn rollback(&self, a: Option<u32>, b: u32) -> i32 {
let a_0 = a.unwrap_or_else(|| 0);
let mut lock = self.rollbacks.write().unwrap();
*lock = *lock + a as usize - b as usize;
(a - b) as i32
*lock = *lock + a_0 as usize - b as usize;
(a_0 - b) as i32
}
}