| 1 |
use std::io; |
| 2 |
|
| 3 |
use crate::RingBuffer; |
| 4 |
|
| 5 |
#[test] |
| 6 |
fn from() { |
| 7 |
let buf0 = RingBuffer::<u8>::new(4); |
| 8 |
let buf1 = RingBuffer::<u8>::new(4); |
| 9 |
let (mut prod0, mut cons0) = buf0.split(); |
| 10 |
let (mut prod1, mut cons1) = buf1.split(); |
| 11 |
|
| 12 |
let mut tmp = [0; 5]; |
| 13 |
|
| 14 |
assert_eq!(prod0.push_slice(&[0, 1, 2]), 3); |
| 15 |
|
| 16 |
match prod1.read_from(&mut cons0, None) { |
| 17 |
Ok(n) => assert_eq!(n, 3), |
| 18 |
other => panic!("{:?}", other), |
| 19 |
} |
| 20 |
match prod1.read_from(&mut cons0, None) { |
| 21 |
Err(e) => { |
| 22 |
assert_eq!(e.kind(), io::ErrorKind::WouldBlock); |
| 23 |
} |
| 24 |
other => panic!("{:?}", other), |
| 25 |
} |
| 26 |
|
| 27 |
assert_eq!(cons1.pop_slice(&mut tmp), 3); |
| 28 |
assert_eq!(tmp[0..3], [0, 1, 2]); |
| 29 |
|
| 30 |
assert_eq!(prod0.push_slice(&[3, 4, 5]), 3); |
| 31 |
|
| 32 |
match prod1.read_from(&mut cons0, None) { |
| 33 |
Ok(n) => assert_eq!(n, 2), |
| 34 |
other => panic!("{:?}", other), |
| 35 |
} |
| 36 |
assert_eq!(cons1.pop_slice(&mut tmp), 2); |
| 37 |
assert_eq!(tmp[0..2], [3, 4]); |
| 38 |
|
| 39 |
match prod1.read_from(&mut cons0, None) { |
| 40 |
Ok(n) => assert_eq!(n, 1), |
| 41 |
other => panic!("{:?}", other), |
| 42 |
} |
| 43 |
assert_eq!(cons1.pop_slice(&mut tmp), 1); |
| 44 |
assert_eq!(tmp[0..1], [5]); |
| 45 |
|
| 46 |
assert_eq!(prod1.push_slice(&[6, 7, 8]), 3); |
| 47 |
assert_eq!(prod0.push_slice(&[9, 10]), 2); |
| 48 |
|
| 49 |
match prod1.read_from(&mut cons0, None) { |
| 50 |
Ok(n) => assert_eq!(n, 1), |
| 51 |
other => panic!("{:?}", other), |
| 52 |
} |
| 53 |
match prod1.read_from(&mut cons0, None) { |
| 54 |
Ok(n) => assert_eq!(n, 0), |
| 55 |
other => panic!("{:?}", other), |
| 56 |
} |
| 57 |
|
| 58 |
assert_eq!(cons1.pop_slice(&mut tmp), 4); |
| 59 |
assert_eq!(tmp[0..4], [6, 7, 8, 9]); |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn into() { |
| 64 |
let buf0 = RingBuffer::<u8>::new(4); |
| 65 |
let buf1 = RingBuffer::<u8>::new(4); |
| 66 |
let (mut prod0, mut cons0) = buf0.split(); |
| 67 |
let (mut prod1, mut cons1) = buf1.split(); |
| 68 |
|
| 69 |
let mut tmp = [0; 5]; |
| 70 |
|
| 71 |
assert_eq!(prod0.push_slice(&[0, 1, 2]), 3); |
| 72 |
|
| 73 |
match cons0.write_into(&mut prod1, None) { |
| 74 |
Ok(n) => assert_eq!(n, 3), |
| 75 |
other => panic!("{:?}", other), |
| 76 |
} |
| 77 |
match cons0.write_into(&mut prod1, None) { |
| 78 |
Ok(n) => assert_eq!(n, 0), |
| 79 |
other => panic!("{:?}", other), |
| 80 |
} |
| 81 |
|
| 82 |
assert_eq!(cons1.pop_slice(&mut tmp), 3); |
| 83 |
assert_eq!(tmp[0..3], [0, 1, 2]); |
| 84 |
|
| 85 |
assert_eq!(prod0.push_slice(&[3, 4, 5]), 3); |
| 86 |
|
| 87 |
match cons0.write_into(&mut prod1, None) { |
| 88 |
Ok(n) => assert_eq!(n, 2), |
| 89 |
other => panic!("{:?}", other), |
| 90 |
} |
| 91 |
assert_eq!(cons1.pop_slice(&mut tmp), 2); |
| 92 |
assert_eq!(tmp[0..2], [3, 4]); |
| 93 |
|
| 94 |
match cons0.write_into(&mut prod1, None) { |
| 95 |
Ok(n) => assert_eq!(n, 1), |
| 96 |
other => panic!("{:?}", other), |
| 97 |
} |
| 98 |
assert_eq!(cons1.pop_slice(&mut tmp), 1); |
| 99 |
assert_eq!(tmp[0..1], [5]); |
| 100 |
|
| 101 |
assert_eq!(prod1.push_slice(&[6, 7, 8]), 3); |
| 102 |
assert_eq!(prod0.push_slice(&[9, 10]), 2); |
| 103 |
|
| 104 |
match cons0.write_into(&mut prod1, None) { |
| 105 |
Ok(n) => assert_eq!(n, 1), |
| 106 |
other => panic!("{:?}", other), |
| 107 |
} |
| 108 |
match cons0.write_into(&mut prod1, None) { |
| 109 |
Err(e) => { |
| 110 |
assert_eq!(e.kind(), io::ErrorKind::WouldBlock); |
| 111 |
} |
| 112 |
other => panic!("{:?}", other), |
| 113 |
} |
| 114 |
|
| 115 |
assert_eq!(cons1.pop_slice(&mut tmp), 4); |
| 116 |
assert_eq!(tmp[0..4], [6, 7, 8, 9]); |
| 117 |
} |
| 118 |
|
| 119 |
#[test] |
| 120 |
fn count() { |
| 121 |
let buf0 = RingBuffer::<u8>::new(4); |
| 122 |
let buf1 = RingBuffer::<u8>::new(4); |
| 123 |
let (mut prod0, mut cons0) = buf0.split(); |
| 124 |
let (mut prod1, mut cons1) = buf1.split(); |
| 125 |
|
| 126 |
let mut tmp = [0; 5]; |
| 127 |
|
| 128 |
assert_eq!(prod0.push_slice(&[0, 1, 2, 3]), 4); |
| 129 |
|
| 130 |
match prod1.read_from(&mut cons0, Some(3)) { |
| 131 |
Ok(n) => assert_eq!(n, 3), |
| 132 |
other => panic!("{:?}", other), |
| 133 |
} |
| 134 |
match prod1.read_from(&mut cons0, Some(2)) { |
| 135 |
Ok(n) => assert_eq!(n, 1), |
| 136 |
other => panic!("{:?}", other), |
| 137 |
} |
| 138 |
|
| 139 |
assert_eq!(cons1.pop_slice(&mut tmp), 4); |
| 140 |
assert_eq!(tmp[0..4], [0, 1, 2, 3]); |
| 141 |
|
| 142 |
assert_eq!(prod0.push_slice(&[4, 5, 6, 7]), 4); |
| 143 |
|
| 144 |
match cons0.write_into(&mut prod1, Some(3)) { |
| 145 |
Ok(n) => assert_eq!(n, 1), |
| 146 |
other => panic!("{:?}", other), |
| 147 |
} |
| 148 |
match cons0.write_into(&mut prod1, Some(2)) { |
| 149 |
Ok(n) => assert_eq!(n, 2), |
| 150 |
other => panic!("{:?}", other), |
| 151 |
} |
| 152 |
match cons0.write_into(&mut prod1, Some(2)) { |
| 153 |
Ok(n) => assert_eq!(n, 1), |
| 154 |
other => panic!("{:?}", other), |
| 155 |
} |
| 156 |
|
| 157 |
assert_eq!(cons1.pop_slice(&mut tmp), 4); |
| 158 |
assert_eq!(tmp[0..4], [4, 5, 6, 7]); |
| 159 |
} |