Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aeyakovenko committed Apr 12, 2018
1 parent bc5256f commit c350adb
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/accountant_skel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct AccountantSkel<W: Write + Send + 'static> {
}

#[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Request {
Transaction(Transaction),
GetBalance { key: PublicKey },
Expand Down Expand Up @@ -273,10 +273,11 @@ impl<W: Write + Send + 'static> AccountantSkel<W> {
}

#[cfg(test)]
pub fn to_packets(r: packet::PacketRecycler, reqs: Vec<Request>) -> Vec<SharedPackets> {
pub fn to_packets(r: &packet::PacketRecycler, reqs: Vec<Request>) -> Vec<SharedPackets> {
let mut out = vec![];
for rrs in reqs.chunks(packet::NUM_PACKETS) {
let p = r.allocate();
p.write().unwrap().packets.resize(rrs.len(), Default::default());
for (i,o) in rrs.iter().zip(p.write().unwrap().packets.iter_mut()) {
let v = serialize(&i).expect("serialize request");
let len = v.len();
Expand All @@ -290,15 +291,34 @@ pub fn to_packets(r: packet::PacketRecycler, reqs: Vec<Request>) -> Vec<SharedPa

#[cfg(test)]
mod tests {
use accountant_skel::Request;
use accountant_skel::{to_packets, Request};
use bincode::serialize;
use ecdsa;
use transaction::{memfind, test_tx};
use packet::{NUM_PACKETS, PacketRecycler};
#[test]
fn test_layout() {
let tr = test_tx();
let tx = serialize(&tr).unwrap();
let packet = serialize(&Request::Transaction(tr)).unwrap();
assert_matches!(memfind(&packet, &tx), Some(ecdsa::TX_OFFSET));
assert_matches!(memfind(&packet, &[0,1,2,3,4,5,6,7,8,9]), None);
}
#[test]
fn test_to_packets() {
let tr = Request::Transaction(test_tx());
let re = PacketRecycler::default();
let rv = to_packets(&re, vec![tr.clone(); 1]);
assert_eq!(rv.len(), 1);
assert_eq!(rv[0].read().unwrap().packets.len(), 1);

let rv = to_packets(&re, vec![tr.clone(); NUM_PACKETS]);
assert_eq!(rv.len(), 1);
assert_eq!(rv[0].read().unwrap().packets.len(), NUM_PACKETS);

let rv = to_packets(&re, vec![tr.clone(); NUM_PACKETS + 1]);
assert_eq!(rv.len(), 2);
assert_eq!(rv[0].read().unwrap().packets.len(), NUM_PACKETS);
assert_eq!(rv[1].read().unwrap().packets.len(), 1);
}
}

0 comments on commit c350adb

Please sign in to comment.