| 1 |
use crate::RingBuffer; |
| 2 |
|
| 3 |
#[test] |
| 4 |
fn for_each() { |
| 5 |
let cap = 2; |
| 6 |
let buf = RingBuffer::<i32>::new(cap); |
| 7 |
let (mut prod, mut cons) = buf.split(); |
| 8 |
|
| 9 |
prod.push(10).unwrap(); |
| 10 |
prod.push(20).unwrap(); |
| 11 |
|
| 12 |
let mut sum_1 = 0; |
| 13 |
for v in cons.iter() { |
| 14 |
sum_1 += *v; |
| 15 |
} |
| 16 |
|
| 17 |
let first = cons.pop().expect("First element not available"); |
| 18 |
let second = cons.pop().expect("Second element not available"); |
| 19 |
|
| 20 |
assert_eq!(sum_1, first + second); |
| 21 |
} |
| 22 |
|
| 23 |
#[test] |
| 24 |
fn for_each_mut() { |
| 25 |
let cap = 2; |
| 26 |
let buf = RingBuffer::<i32>::new(cap); |
| 27 |
let (mut prod, mut cons) = buf.split(); |
| 28 |
|
| 29 |
prod.push(10).unwrap(); |
| 30 |
prod.push(20).unwrap(); |
| 31 |
|
| 32 |
for v in cons.iter_mut() { |
| 33 |
*v *= 2; |
| 34 |
} |
| 35 |
|
| 36 |
let mut sum_1 = 0; |
| 37 |
for v in cons.iter_mut() { |
| 38 |
sum_1 += *v; |
| 39 |
} |
| 40 |
|
| 41 |
let first = cons.pop().expect("First element not available"); |
| 42 |
let second = cons.pop().expect("Second element not available"); |
| 43 |
|
| 44 |
assert_eq!(sum_1, first + second); |
| 45 |
} |
| 46 |
|
| 47 |
#[test] |
| 48 |
fn pop_each() { |
| 49 |
let cap = 3; |
| 50 |
let buf = RingBuffer::<i32>::new(cap); |
| 51 |
let (mut prod, mut cons) = buf.split(); |
| 52 |
|
| 53 |
prod.push(10).unwrap(); |
| 54 |
prod.push(20).unwrap(); |
| 55 |
|
| 56 |
let mut sum_1 = 0; |
| 57 |
cons.pop_each( |
| 58 |
|v| { |
| 59 |
sum_1 += v; |
| 60 |
v != 20 |
| 61 |
}, |
| 62 |
Some(2), |
| 63 |
); |
| 64 |
|
| 65 |
prod.push(30).unwrap(); |
| 66 |
prod.push(40).unwrap(); |
| 67 |
prod.push(50).unwrap(); |
| 68 |
|
| 69 |
cons.pop_each( |
| 70 |
|v| { |
| 71 |
sum_1 += v; |
| 72 |
true |
| 73 |
}, |
| 74 |
Some(2), |
| 75 |
); |
| 76 |
|
| 77 |
prod.push(60).unwrap(); |
| 78 |
|
| 79 |
cons.pop_each( |
| 80 |
|v| { |
| 81 |
sum_1 += v; |
| 82 |
v != 50 |
| 83 |
}, |
| 84 |
None, |
| 85 |
); |
| 86 |
|
| 87 |
prod.push(70).unwrap(); |
| 88 |
|
| 89 |
cons.pop_each( |
| 90 |
|v| { |
| 91 |
sum_1 += v; |
| 92 |
true |
| 93 |
}, |
| 94 |
Some(2), |
| 95 |
); |
| 96 |
|
| 97 |
assert_eq!(sum_1, 10 + 20 + 30 + 40 + 50 + 60 + 70); |
| 98 |
} |
| 99 |
|
| 100 |
#[test] |
| 101 |
fn push_pop_slice() { |
| 102 |
let buf = RingBuffer::<i32>::new(4); |
| 103 |
let (mut prod, mut cons) = buf.split(); |
| 104 |
|
| 105 |
let mut tmp = [0; 5]; |
| 106 |
|
| 107 |
assert_eq!(prod.push_slice(&[]), 0); |
| 108 |
assert_eq!(prod.push_slice(&[0, 1, 2]), 3); |
| 109 |
|
| 110 |
assert_eq!(cons.pop_slice(&mut tmp[0..2]), 2); |
| 111 |
assert_eq!(tmp[0..2], [0, 1]); |
| 112 |
|
| 113 |
assert_eq!(prod.push_slice(&[3, 4]), 2); |
| 114 |
assert_eq!(prod.push_slice(&[5, 6]), 1); |
| 115 |
|
| 116 |
assert_eq!(cons.pop_slice(&mut tmp[0..3]), 3); |
| 117 |
assert_eq!(tmp[0..3], [2, 3, 4]); |
| 118 |
|
| 119 |
assert_eq!(prod.push_slice(&[6, 7, 8, 9]), 3); |
| 120 |
|
| 121 |
assert_eq!(cons.pop_slice(&mut tmp), 4); |
| 122 |
assert_eq!(tmp[0..4], [5, 6, 7, 8]); |
| 123 |
} |
| 124 |
|
| 125 |
#[test] |
| 126 |
fn move_slice() { |
| 127 |
let buf0 = RingBuffer::<i32>::new(4); |
| 128 |
let buf1 = RingBuffer::<i32>::new(4); |
| 129 |
let (mut prod0, mut cons0) = buf0.split(); |
| 130 |
let (mut prod1, mut cons1) = buf1.split(); |
| 131 |
|
| 132 |
let mut tmp = [0; 5]; |
| 133 |
|
| 134 |
assert_eq!(prod0.push_slice(&[0, 1, 2]), 3); |
| 135 |
|
| 136 |
assert_eq!(prod1.move_from(&mut cons0, None), 3); |
| 137 |
assert_eq!(prod1.move_from(&mut cons0, None), 0); |
| 138 |
|
| 139 |
assert_eq!(cons1.pop_slice(&mut tmp), 3); |
| 140 |
assert_eq!(tmp[0..3], [0, 1, 2]); |
| 141 |
|
| 142 |
assert_eq!(prod0.push_slice(&[3, 4, 5]), 3); |
| 143 |
|
| 144 |
assert_eq!(prod1.move_from(&mut cons0, None), 3); |
| 145 |
|
| 146 |
assert_eq!(cons1.pop_slice(&mut tmp), 3); |
| 147 |
assert_eq!(tmp[0..3], [3, 4, 5]); |
| 148 |
|
| 149 |
assert_eq!(prod1.push_slice(&[6, 7, 8]), 3); |
| 150 |
assert_eq!(prod0.push_slice(&[9, 10]), 2); |
| 151 |
|
| 152 |
assert_eq!(prod1.move_from(&mut cons0, None), 1); |
| 153 |
assert_eq!(prod1.move_from(&mut cons0, None), 0); |
| 154 |
|
| 155 |
assert_eq!(cons1.pop_slice(&mut tmp), 4); |
| 156 |
assert_eq!(tmp[0..4], [6, 7, 8, 9]); |
| 157 |
} |
| 158 |
|
| 159 |
#[test] |
| 160 |
fn move_slice_count() { |
| 161 |
let buf0 = RingBuffer::<i32>::new(4); |
| 162 |
let buf1 = RingBuffer::<i32>::new(4); |
| 163 |
let (mut prod0, mut cons0) = buf0.split(); |
| 164 |
let (mut prod1, mut cons1) = buf1.split(); |
| 165 |
|
| 166 |
let mut tmp = [0; 5]; |
| 167 |
|
| 168 |
assert_eq!(prod0.push_slice(&[0, 1, 2]), 3); |
| 169 |
|
| 170 |
assert_eq!(prod1.move_from(&mut cons0, Some(2)), 2); |
| 171 |
|
| 172 |
assert_eq!(cons1.pop_slice(&mut tmp), 2); |
| 173 |
assert_eq!(tmp[0..2], [0, 1]); |
| 174 |
|
| 175 |
assert_eq!(prod1.move_from(&mut cons0, Some(2)), 1); |
| 176 |
|
| 177 |
assert_eq!(cons1.pop_slice(&mut tmp), 1); |
| 178 |
assert_eq!(tmp[0..1], [2]); |
| 179 |
|
| 180 |
assert_eq!(prod0.push_slice(&[3, 4, 5, 6]), 4); |
| 181 |
|
| 182 |
assert_eq!(prod1.move_from(&mut cons0, Some(3)), 3); |
| 183 |
|
| 184 |
assert_eq!(cons1.pop_slice(&mut tmp), 3); |
| 185 |
assert_eq!(tmp[0..3], [3, 4, 5]); |
| 186 |
|
| 187 |
assert_eq!(prod0.push_slice(&[7, 8, 9]), 3); |
| 188 |
|
| 189 |
assert_eq!(prod1.move_from(&mut cons0, Some(5)), 4); |
| 190 |
|
| 191 |
assert_eq!(cons1.pop_slice(&mut tmp), 4); |
| 192 |
assert_eq!(tmp[0..4], [6, 7, 8, 9]); |
| 193 |
} |