summaryrefslogtreecommitdiffstats
path: root/third_party/rust/ringbuf/src/tests/iter.rs
blob: ed361f8020a60eb5f387be0f96af89079c04c32e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::RingBuffer;

#[test]
fn iter() {
    let buf = RingBuffer::<i32>::new(2);
    let (mut prod, mut cons) = buf.split();

    prod.push(10).unwrap();
    prod.push(20).unwrap();

    let sum: i32 = cons.iter().sum();

    let first = cons.pop().expect("First element not available");
    let second = cons.pop().expect("Second element not available");

    assert_eq!(sum, first + second);
}

#[test]
fn iter_mut() {
    let buf = RingBuffer::<i32>::new(2);
    let (mut prod, mut cons) = buf.split();

    prod.push(10).unwrap();
    prod.push(20).unwrap();

    for v in cons.iter_mut() {
        *v *= 2;
    }

    let sum: i32 = cons.iter().sum();

    let first = cons.pop().expect("First element not available");
    let second = cons.pop().expect("Second element not available");

    assert_eq!(sum, first + second);
}

#[test]
fn into_iter() {
    let buf = RingBuffer::<i32>::new(2);
    let (mut prod, cons) = buf.split();

    prod.push(10).unwrap();
    prod.push(20).unwrap();

    for (i, v) in cons.into_iter().enumerate() {
        assert_eq!(10 * (i + 1) as i32, v);
    }
    assert!(prod.is_empty());
}