Skip to content

Commit

Permalink
Add assertions about the inner fields of RingBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdrz committed Jan 23, 2024
1 parent 6547452 commit d2fad50
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/exec/use_pty/pipe/ring_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ mod tests {
buf.insert(&mut [0u8; HALF_LEN].as_slice()).unwrap();
buf.remove(&mut vec![]).unwrap();

assert_eq!(buf.start, HALF_LEN);
assert_eq!(buf.len, 0);

// Then we fill the first half of the buffer with ones and the second one with twos in a
// single insertion. This tests case 1.1.
// ┌─────────┬─────────┐
Expand All @@ -160,13 +163,19 @@ mod tests {
expected.extend_from_slice(&[2; HALF_LEN]);
buf.insert(&mut expected.as_slice()).unwrap();

assert_eq!(buf.start, HALF_LEN);
assert_eq!(buf.len, RingBuffer::LEN);

// When we remove all the elements of the buffer we should find them in the same order we
// inserted them. This tests case 2.1.
let mut found = vec![];
let removed_len = buf.remove(&mut found).unwrap();
assert_eq!(removed_len, expected.len());

assert_eq!(expected, found);

assert_eq!(buf.start, HALF_LEN);
assert_eq!(buf.len, 0);
}

#[test]
Expand All @@ -185,6 +194,9 @@ mod tests {
buf.insert(&mut [0; 2 * QUARTER_LEN].as_slice()).unwrap();
buf.remove(&mut vec![]).unwrap();

assert_eq!(buf.start, 2 * QUARTER_LEN);
assert_eq!(buf.len, 0);

// Then we fill one quarter of the buffer with ones. This gives us a non-empty buffer whose
// empty section is not contiguous.
// ┌───────────┬─────┬─────┐
Expand All @@ -195,6 +207,10 @@ mod tests {
// start
let mut expected = vec![1; QUARTER_LEN];
buf.insert(&mut expected.as_slice()).unwrap();

assert_eq!(buf.start, 2 * QUARTER_LEN);
assert_eq!(buf.len, QUARTER_LEN);

// Then we fill one quarter of the buffer with twos and another quarter of the buffer with
// threes in a single insertion. This tests case 1.2.
// ┌─────┬─────┬─────┬─────┐
Expand All @@ -209,13 +225,19 @@ mod tests {

expected.extend(second_half);

assert_eq!(buf.start, 2 * QUARTER_LEN);
assert_eq!(buf.len, 3 * QUARTER_LEN);

// When we remove all the elements of the buffer we should find them in the same order we
// inserted them. This tests case 2.2.
let mut found = vec![];
let removed_len = buf.remove(&mut found).unwrap();
assert_eq!(removed_len, expected.len());

assert_eq!(expected, found);

assert_eq!(buf.start, QUARTER_LEN);
assert_eq!(buf.len, 0);
}

#[test]
Expand All @@ -234,6 +256,9 @@ mod tests {
let mut expected = vec![1; QUARTER_LEN];
buf.insert(&mut expected.as_slice()).unwrap();

assert_eq!(buf.start, 0);
assert_eq!(buf.len, QUARTER_LEN);

// Then we fill one quarter of the buffer with twos. This tests case 1.3.
// ┌─────┬─────┬──────────┐
// │ 1 │ 2 │ │
Expand All @@ -246,12 +271,18 @@ mod tests {

expected.extend(second_half);

assert_eq!(buf.start, 0);
assert_eq!(buf.len, 2 * QUARTER_LEN);

// When we remove all the elements of the buffer we should find them in the same order we
// inserted them. This tests case 2.3.
let mut found = vec![];
let removed_len = buf.remove(&mut found).unwrap();
assert_eq!(removed_len, expected.len());

assert_eq!(expected, found);

assert_eq!(buf.start, 2 * QUARTER_LEN);
assert_eq!(buf.len, 0);
}
}

0 comments on commit d2fad50

Please sign in to comment.