chunkable-ringbuf/src/test.rs

52 lines
1.5 KiB
Rust
Raw Normal View History

2022-09-30 22:54:57 +00:00
use crate::*;
use rand::Rng;
#[test]
fn correctness_random() {
const MAX_APPEND_LEN: usize = 256;
const CHUNK_SIZE: usize = 32;
let mut rng = rand::thread_rng();
let mut ringbuf = RingBuf::new(1024, 0_u32);
let mut current_value = 0;
for _ in 0_u32..1024 {
let append_len = rng.gen_range(0..MAX_APPEND_LEN);
2022-10-01 08:10:23 +00:00
assert_eq!(
ringbuf.push_from_iter(current_value as u32..current_value as u32 + append_len as u32),
append_len
);
2022-09-30 22:54:57 +00:00
for (j, chunk) in ringbuf.chunks_exact_mut(CHUNK_SIZE).enumerate() {
assert_eq!(chunk.len(), CHUNK_SIZE);
for (k, v) in chunk.iter().enumerate() {
assert_eq!(
*v as usize,
current_value - current_value % CHUNK_SIZE + j * CHUNK_SIZE + k
);
}
}
current_value += append_len;
}
}
#[test]
fn correctness_manual() {
let mut ringbuf = RingBuf::new(8, 0_u32);
2022-10-01 08:10:23 +00:00
assert_eq!(ringbuf.push_from_iter(1..4), 3);
2022-09-30 22:54:57 +00:00
let mut iter = ringbuf.chunks_exact_mut(4);
assert_eq!(iter.next(), None);
2022-10-01 08:10:23 +00:00
assert!(ringbuf.push(4));
2022-09-30 22:54:57 +00:00
let mut iter = ringbuf.chunks_exact_mut(4);
assert_eq!(iter.next(), Some([1, 2, 3, 4].as_mut_slice()));
assert_eq!(iter.next(), None);
2022-10-01 08:10:23 +00:00
assert_eq!(ringbuf.push_from_iter(5..8), 3);
2022-09-30 22:54:57 +00:00
let mut iter = ringbuf.chunks_exact_mut(4);
assert_eq!(iter.next(), None);
2022-10-01 08:10:23 +00:00
assert_eq!(ringbuf.push_from_iter_unchecked(8..14), 6);
2022-09-30 22:54:57 +00:00
let mut iter = ringbuf.chunks_exact_mut(4);
assert_eq!(iter.next(), Some([13, 6, 7, 8].as_mut_slice()));
assert_eq!(iter.next(), Some([9, 10, 11, 12].as_mut_slice()));
assert_eq!(iter.next(), None);
}