Debian Codegraph @

src/tests/skip.rs

raw symbols SCIP
1 use crate::{HeapRb, Rb};
2 use alloc::rc::Rc;
3
4 #[test]
5 fn skip() {
6 // Initialize ringbuffer, prod and cons
7 let rb = HeapRb::<i8>::new(10);
8 let (mut prod, mut cons) = rb.split();
9 let mut i = 0;
10
11 // Fill the buffer
12 for _ in 0..10 {
13 prod.push(i).unwrap();
14 i += 1;
15 }
16
17 // Pop in the middle of the buffer
18 assert_eq!(cons.skip(5), 5);
19
20 // Make sure changes are taken into account
21 assert_eq!(cons.pop().unwrap(), 5);
22
23 // Fill the buffer again
24 for _ in 0..5 {
25 prod.push(i).unwrap();
26 i += 1;
27 }
28
29 assert_eq!(cons.skip(6), 6);
30 assert_eq!(cons.pop().unwrap(), 12);
31
32 // Fill the buffer again
33 for _ in 0..7 {
34 prod.push(i).unwrap();
35 i += 1;
36 }
37
38 // Ask too much, delete the max number of items
39 assert_eq!(cons.skip(10), 9);
40
41 // Try to remove more than possible
42 assert_eq!(cons.skip(1), 0);
43
44 // Make sure it is still usable
45 assert_eq!(cons.pop(), None);
46 assert_eq!(prod.push(0), Ok(()));
47 assert_eq!(cons.pop(), Some(0));
48 }
49
50 #[test]
51 fn skip_drop() {
52 let rc = Rc::<()>::new(());
53
54 static N: usize = 10;
55
56 let rb = HeapRb::<Rc<()>>::new(N);
57 let (mut prod, mut cons) = rb.split();
58
59 for _ in 0..N {
60 prod.push(rc.clone()).unwrap();
61 }
62
63 assert_eq!(cons.len(), N);
64 assert_eq!(Rc::strong_count(&rc), N + 1);
65
66 assert_eq!(cons.skip(N), N);
67
68 // Check ring buffer is empty
69 assert_eq!(cons.len(), 0);
70 // Check that items are dropped
71 assert_eq!(Rc::strong_count(&rc), 1);
72 }
73
74 #[test]
75 #[should_panic]
76 fn skip_panic() {
77 let mut rb = HeapRb::<i32>::new(2);
78 rb.push(1).unwrap();
79 rb.skip(2);
80 }