| 1 | use crate::{transfer, HeapRb, Rb}; |
| 2 | |
| 3 | #[test] |
| 4 | fn push_pop_slice() { |
| 5 | let buf = HeapRb::<i32>::new(4); |
| 6 | let (mut prod, mut cons) = buf.split(); |
| 7 | |
| 8 | let mut tmp = [0; 5]; |
| 9 | |
| 10 | assert_eq!(prod.push_slice(&[]), 0); |
| 11 | assert_eq!(prod.push_slice(&[0, 1, 2]), 3); |
| 12 | |
| 13 | assert_eq!(cons.pop_slice(&mut tmp[0..2]), 2); |
| 14 | assert_eq!(tmp[0..2], [0, 1]); |
| 15 | |
| 16 | assert_eq!(prod.push_slice(&[3, 4]), 2); |
| 17 | assert_eq!(prod.push_slice(&[5, 6]), 1); |
| 18 | |
| 19 | assert_eq!(cons.pop_slice(&mut tmp[0..3]), 3); |
| 20 | assert_eq!(tmp[0..3], [2, 3, 4]); |
| 21 | |
| 22 | assert_eq!(prod.push_slice(&[6, 7, 8, 9]), 3); |
| 23 | |
| 24 | assert_eq!(cons.pop_slice(&mut tmp), 4); |
| 25 | assert_eq!(tmp[0..4], [5, 6, 7, 8]); |
| 26 | } |
| 27 | |
| 28 | #[test] |
| 29 | fn move_slice() { |
| 30 | let buf0 = HeapRb::<i32>::new(4); |
| 31 | let buf1 = HeapRb::<i32>::new(4); |
| 32 | let (mut prod0, mut cons0) = buf0.split(); |
| 33 | let (mut prod1, mut cons1) = buf1.split(); |
| 34 | |
| 35 | let mut tmp = [0; 5]; |
| 36 | |
| 37 | assert_eq!(prod0.push_slice(&[0, 1, 2]), 3); |
| 38 | |
| 39 | assert_eq!(transfer(&mut cons0, &mut prod1, None), 3); |
| 40 | assert_eq!(transfer(&mut cons0, &mut prod1, None), 0); |
| 41 | |
| 42 | assert_eq!(cons1.pop_slice(&mut tmp), 3); |
| 43 | assert_eq!(tmp[0..3], [0, 1, 2]); |
| 44 | |
| 45 | assert_eq!(prod0.push_slice(&[3, 4, 5]), 3); |
| 46 | |
| 47 | assert_eq!(transfer(&mut cons0, &mut prod1, None), 3); |
| 48 | |
| 49 | assert_eq!(cons1.pop_slice(&mut tmp), 3); |
| 50 | assert_eq!(tmp[0..3], [3, 4, 5]); |
| 51 | |
| 52 | assert_eq!(prod1.push_slice(&[6, 7, 8]), 3); |
| 53 | assert_eq!(prod0.push_slice(&[9, 10]), 2); |
| 54 | |
| 55 | assert_eq!(transfer(&mut cons0, &mut prod1, None), 1); |
| 56 | assert_eq!(transfer(&mut cons0, &mut prod1, None), 0); |
| 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 move_slice_count() { |
| 64 | let buf0 = HeapRb::<i32>::new(4); |
| 65 | let buf1 = HeapRb::<i32>::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 | assert_eq!(transfer(&mut cons0, &mut prod1, Some(2)), 2); |
| 74 | |
| 75 | assert_eq!(cons1.pop_slice(&mut tmp), 2); |
| 76 | assert_eq!(tmp[0..2], [0, 1]); |
| 77 | |
| 78 | assert_eq!(transfer(&mut cons0, &mut prod1, Some(2)), 1); |
| 79 | |
| 80 | assert_eq!(cons1.pop_slice(&mut tmp), 1); |
| 81 | assert_eq!(tmp[0..1], [2]); |
| 82 | |
| 83 | assert_eq!(prod0.push_slice(&[3, 4, 5, 6]), 4); |
| 84 | |
| 85 | assert_eq!(transfer(&mut cons0, &mut prod1, Some(3)), 3); |
| 86 | |
| 87 | assert_eq!(cons1.pop_slice(&mut tmp), 3); |
| 88 | assert_eq!(tmp[0..3], [3, 4, 5]); |
| 89 | |
| 90 | assert_eq!(prod0.push_slice(&[7, 8, 9]), 3); |
| 91 | |
| 92 | assert_eq!(transfer(&mut cons0, &mut prod1, Some(5)), 4); |
| 93 | |
| 94 | assert_eq!(cons1.pop_slice(&mut tmp), 4); |
| 95 | assert_eq!(tmp[0..4], [6, 7, 8, 9]); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | #[should_panic] |
| 100 | fn push_slice_panic() { |
| 101 | let mut rb = HeapRb::<i32>::new(2); |
| 102 | rb.push_slice(&[1, 2, 3]); |
| 103 | } |
| 104 | |
| 105 | #[test] |
| 106 | #[should_panic] |
| 107 | fn pop_slice_panic() { |
| 108 | let mut rb = HeapRb::<i32>::new(2); |
| 109 | rb.push(1).unwrap(); |
| 110 | let mut tmp = [0; 2]; |
| 111 | rb.pop_slice(&mut tmp); |
| 112 | } |