| 1 | use crate::{Consumer, HeapRb}; |
| 2 | use alloc::collections::BTreeSet; |
| 3 | use core::cell::RefCell; |
| 4 | |
| 5 | #[derive(Debug)] |
| 6 | struct Dropper<'a> { |
| 7 | id: i32, |
| 8 | set: &'a RefCell<BTreeSet<i32>>, |
| 9 | } |
| 10 | |
| 11 | impl<'a> Dropper<'a> { |
| 12 | fn new(set: &'a RefCell<BTreeSet<i32>>, id: i32) -> Self { |
| 13 | if !set.borrow_mut().insert(id) { |
| 14 | panic!("value {} already exists", id); |
| 15 | } |
| 16 | Self { set, id } |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | impl<'a> Drop for Dropper<'a> { |
| 21 | fn drop(&mut self) { |
| 22 | if !self.set.borrow_mut().remove(&self.id) { |
| 23 | panic!("value {} already removed", self.id); |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | #[test] |
| 29 | fn single() { |
| 30 | let set = RefCell::new(BTreeSet::new()); |
| 31 | |
| 32 | let cap = 3; |
| 33 | let buf = HeapRb::new(cap); |
| 34 | |
| 35 | assert_eq!(set.borrow().len(), 0); |
| 36 | |
| 37 | { |
| 38 | let (mut prod, mut cons) = buf.split(); |
| 39 | |
| 40 | prod.push(Dropper::new(&set, 1)).unwrap(); |
| 41 | assert_eq!(set.borrow().len(), 1); |
| 42 | prod.push(Dropper::new(&set, 2)).unwrap(); |
| 43 | assert_eq!(set.borrow().len(), 2); |
| 44 | prod.push(Dropper::new(&set, 3)).unwrap(); |
| 45 | assert_eq!(set.borrow().len(), 3); |
| 46 | |
| 47 | cons.pop().unwrap(); |
| 48 | assert_eq!(set.borrow().len(), 2); |
| 49 | cons.pop().unwrap(); |
| 50 | assert_eq!(set.borrow().len(), 1); |
| 51 | |
| 52 | prod.push(Dropper::new(&set, 4)).unwrap(); |
| 53 | assert_eq!(set.borrow().len(), 2); |
| 54 | } |
| 55 | |
| 56 | assert_eq!(set.borrow().len(), 0); |
| 57 | } |
| 58 | |
| 59 | // TODO: Use transactions. |
| 60 | #[test] |
| 61 | fn transaction() { |
| 62 | let set = RefCell::new(BTreeSet::new()); |
| 63 | |
| 64 | let cap = 5; |
| 65 | let buf = HeapRb::new(cap); |
| 66 | |
| 67 | assert_eq!(set.borrow().len(), 0); |
| 68 | { |
| 69 | let (mut prod, mut cons) = buf.split(); |
| 70 | let mut id = 0; |
| 71 | let mut cnt = 0; |
| 72 | let assert_cnt = |cnt, n, cons: &Consumer<_, _>, set: &RefCell<BTreeSet<_>>| { |
| 73 | assert_eq!(cnt, n); |
| 74 | assert_eq!(cnt, cons.len()); |
| 75 | assert_eq!(cnt, set.borrow().len()); |
| 76 | }; |
| 77 | |
| 78 | for _ in 0..4 { |
| 79 | id += 1; |
| 80 | cnt += 1; |
| 81 | prod.push(Dropper::new(&set, id)).unwrap(); |
| 82 | } |
| 83 | assert_cnt(cnt, 4, &cons, &set); |
| 84 | |
| 85 | for _ in cons.pop_iter().take(2) { |
| 86 | cnt -= 1; |
| 87 | } |
| 88 | assert_cnt(cnt, 2, &cons, &set); |
| 89 | |
| 90 | while !prod.is_full() { |
| 91 | id += 1; |
| 92 | cnt += 1; |
| 93 | prod.push(Dropper::new(&set, id)).unwrap(); |
| 94 | } |
| 95 | assert_cnt(cnt, 5, &cons, &set); |
| 96 | |
| 97 | for _ in cons.pop_iter() { |
| 98 | cnt -= 1; |
| 99 | } |
| 100 | assert_cnt(cnt, 0, &cons, &set); |
| 101 | |
| 102 | while !prod.is_full() { |
| 103 | id += 1; |
| 104 | cnt += 1; |
| 105 | prod.push(Dropper::new(&set, id)).unwrap(); |
| 106 | } |
| 107 | assert_cnt(cnt, 5, &cons, &set); |
| 108 | } |
| 109 | |
| 110 | assert_eq!(set.borrow().len(), 0); |
| 111 | } |