Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raft: port inflights test #54

Merged
merged 1 commit into from
May 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}