Skip to content

Commit

Permalink
raft: port inflight test (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
cssivision authored and BusyJay committed May 9, 2018
1 parent 8652e1c commit 1115575
Showing 1 changed file with 136 additions and 1 deletion.
137 changes: 136 additions & 1 deletion src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl Progress {
}
}

#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Inflights {
// the starting index in the buffer
start: usize,
Expand Down Expand Up @@ -370,3 +370,138 @@ impl Inflights {
self.start = 0;
}
}

#[cfg(test)]
mod test {
use progress::Inflights;

#[test]
fn test_inflight_add() {
let mut inflight = Inflights::new(10);
for i in 0..5 {
inflight.add(i);
}

let wantin = Inflights{
start: 0,
count: 5,
buffer: vec![0, 1, 2, 3, 4],
};

assert_eq!(inflight, wantin);

for i in 5..10 {
inflight.add(i);
}

let wantin2 = Inflights {
start: 0,
count: 10,
buffer: vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
};

assert_eq!(inflight, wantin2);

let mut inflight2 = Inflights {
start: 5,
buffer: Vec::with_capacity(10),
..Default::default()
};
inflight2.buffer.extend_from_slice(&vec![0, 0, 0, 0, 0]);

for i in 0..5 {
inflight2.add(i);
}

let wantin21 = Inflights {
start: 5,
count: 5,
buffer: vec![0, 0, 0, 0, 0, 0, 1, 2, 3, 4],
};

assert_eq!(inflight2, wantin21);

for i in 5..10 {
inflight2.add(i);
}

let wantin22 = Inflights {
start: 5,
count: 10,
buffer: vec![5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
};

assert_eq!(inflight2, wantin22);
}

#[test]
fn test_inflight_free_to() {
let mut inflight = Inflights::new(10);
for i in 0..10 {
inflight.add(i);
}

inflight.free_to(4);

let wantin = Inflights {
start: 5,
count: 5,
buffer: vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
};

assert_eq!(inflight, wantin);

inflight.free_to(8);

let wantin2 = Inflights {
start: 9,
count: 1,
buffer: vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
};

assert_eq!(inflight, wantin2);

for i in 10..15 {
inflight.add(i);
}

inflight.free_to(12);

let wantin3 = Inflights {
start: 3,
count: 2,
buffer: vec![10, 11, 12, 13, 14, 5, 6, 7, 8, 9],
};

assert_eq!(inflight, wantin3);

inflight.free_to(14);

let wantin4 = Inflights {
start: 5,
count: 0,
buffer: vec![10, 11, 12, 13, 14, 5, 6, 7, 8, 9],
};

assert_eq!(inflight, wantin4);
}

#[test]
fn test_inflight_free_first_one() {
let mut inflight = Inflights::new(10);
for i in 0..10 {
inflight.add(i);
}

inflight.free_first_one();

let wantin = Inflights {
start: 1,
count: 9,
buffer: vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
};

assert_eq!(inflight, wantin);
}
}

0 comments on commit 1115575

Please sign in to comment.