chunkable-ringbuf/src/test.rs

50 lines
1.4 KiB
Rust

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 buf = [0; 32];
let mut current_value = 0;
for _ in 0_u32..1024 {
let append_len = rng.gen_range(0..MAX_APPEND_LEN);
ringbuf.push_from_iter(current_value as u32..current_value as u32 + append_len as u32);
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);
ringbuf.push_from_iter(1..4);
let mut iter = ringbuf.chunks_exact_mut(4);
assert_eq!(iter.next(), None);
ringbuf.push(4);
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);
ringbuf.push_from_iter(5..8);
let mut iter = ringbuf.chunks_exact_mut(4);
assert_eq!(iter.next(), None);
ringbuf.push_from_iter(8..14);
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);
}