bufferring 0.0.2

Ring buffers for Rust
Documentation
#![cfg(test)]

use proptest::prelude::*;

use crate::RingBuffer;
use crate::masking::prop as masking;

proptest! {
    #[test]
    fn test_iter_next(
        (mut buf, items) in masking::arb_heap_paired(any::<u32>()),
    ) {
        let mut iter = buf.iter();
        for item in items.iter() {
            prop_assert_eq!(Some(*item), iter.next().map(|x| *x));
        }
        prop_assert_eq!(None, iter.next());

        let mut iter = buf.iter_mut();
        for item in items.iter() {
            prop_assert_eq!(Some(*item), iter.next().map(|x| *x));
        }
        prop_assert_eq!(None, iter.next());
    }

    #[test]
    fn test_iter_size_hint(
        (buf, items) in masking::arb_heap_paired(any::<u32>()),
    ) {
        prop_assert_eq!(buf.iter().size_hint(), (items.len(), Some(items.len())));
    }

    #[test]
    fn test_iter_count(
        (buf, items) in masking::arb_heap_paired(any::<u32>()),
    ) {
        prop_assert_eq!(buf.iter().count(), items.len());
    }

    #[test]
    fn test_iter_len(buf in masking::arb_heap(any::<i32>())) {
        // Test that `buf.iter()` returns the same number of elements as
        // `buf.len()` - for any of the overloadable methods in `Iterator`.

        prop_assert_eq!(buf.iter().count(), buf.len());
        prop_assert_eq!(buf.iter().fold(0, |c, _| c + 1), buf.len());
        prop_assert_eq!(buf.iter().map(|_| 1).sum::<usize>(), buf.len());

        let mut len = 0;
        for _ in buf.iter() { len += 1; }
        prop_assert_eq!(len, buf.len());
    }

    #[test]
    fn test_enqueue_iter(mut buf in masking::arb_heap(any::<i32>()), item in any::<i32>()) {
        // Test that a newly-enqueued item shows up in `iter()` - and that none
        // of the other items are affected.

        let len = buf.len() + 1;
        let (mut old, mut new) = (Vec::with_capacity(len), Vec::with_capacity(len));
        old.extend(buf.iter().copied());
        old.push(item);
        new.extend(buf.enqueue(item).into_iter());
        new.extend(buf.iter().copied());
        prop_assert_eq!(old, new);
    }

    #[test]
    fn test_iter_mut(mut buf in masking::arb_heap(any::<i32>())) {
        // Test that `iter()` and `iter_mut()` are always identical.

        let by_ref: Vec<_> = buf.iter().map(|x| *x).collect();
        let by_mut: Vec<_> = buf.iter_mut().map(|x| *x).collect();
        prop_assert_eq!(by_ref, by_mut);

        prop_assert_eq!(buf.iter().count(), buf.iter_mut().count());
        prop_assert_eq!(buf.iter().fold(0, |c, _| c + 1),
                        buf.iter_mut().fold(0, |c, _| c + 1));
    }
}