use crate::*; use rand::Rng; #[test] fn chunks_exact_mut_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_overflowing( 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 chunks_exact_mut_manual() { let mut ringbuf = RingBuf::new(8, 0_u32); assert_eq!(ringbuf.push_from_iter(1..4), 3); assert_eq!(ringbuf.available(), 3); let mut iter = ringbuf.chunks_exact_mut(4); assert_eq!(iter.next(), None); assert_eq!(ringbuf.available(), 3); assert!(ringbuf.push(4)); assert_eq!(ringbuf.available(), 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.available(), 0); assert_eq!(ringbuf.push_from_iter(5..8), 3); assert_eq!(ringbuf.available(), 3); let mut iter = ringbuf.chunks_exact_mut(4); assert_eq!(iter.next(), None); assert_eq!(ringbuf.push_from_iter_overflowing(8..14), 6); assert_eq!(ringbuf.available(), 8); dbg!("{:?}", ringbuf.raw_slice()); assert_eq!(ringbuf.align_skip(4), 3); let mut iter = ringbuf.chunks_exact_mut(4); assert_eq!(iter.next(), Some([9, 10, 11, 12].as_mut_slice())); assert_eq!(iter.next(), None); assert_eq!(ringbuf.read_first_one(), Some(&13)); assert_eq!(ringbuf.available(), 0); } #[test] fn push_from_slice_overflowing() { let mut ringbuf = RingBuf::new(8, 0_u32); ringbuf.push_from_slice_overflowing(&[1, 2, 3, 4, 5]); dbg!("{:?}", ringbuf.raw_slice()); ringbuf.push_from_slice_overflowing(&[6, 7, 8, 9, 10, 11]); dbg!("{:?}", ringbuf.raw_slice()); assert_eq!( ringbuf.read_all_slices(), ([4, 5, 6, 7, 8].as_slice(), [9, 10, 11].as_slice()) ); }