chunkable-ringbuf/src/test.rs
2022-10-01 10:10:23 +02:00

51 lines
1.5 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 current_value = 0;
for _ in 0_u32..1024 {
let append_len = rng.gen_range(0..MAX_APPEND_LEN);
assert_eq!(
ringbuf.push_from_iter(current_value as u32..current_value as u32 + append_len as u32),
append_len
);
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);
assert_eq!(ringbuf.push_from_iter(1..4), 3);
let mut iter = ringbuf.chunks_exact_mut(4);
assert_eq!(iter.next(), None);
assert!(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);
assert_eq!(ringbuf.push_from_iter(5..8), 3);
let mut iter = ringbuf.chunks_exact_mut(4);
assert_eq!(iter.next(), None);
assert_eq!(ringbuf.push_from_iter_unchecked(8..14), 6);
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);
}