use super::super::{inter::*, leaf::*, node::*, *};
use super::{CounterI32, alive_count, reset_alive_count};
use core::cell::UnsafeCell;
#[test]
fn test_occupied_forward_same_leaf() {
reset_alive_count();
let mut map = BTreeMap::new();
map.insert(10i32, 100i32);
map.insert(20i32, 200i32);
map.insert(30i32, 300i32);
if let Entry::Occupied(oe) = map.entry(10) {
assert_eq!(*oe.key(), 10);
assert_eq!(*oe.peak_forward().unwrap().0, 20);
let oe = oe.move_forward().ok().unwrap();
assert_eq!(*oe.key(), 20);
assert_eq!(*oe.peak_forward().unwrap().0, 30);
let oe = oe.move_forward().ok().unwrap();
assert_eq!(*oe.key(), 30);
assert!(oe.peak_forward().is_none());
assert!(oe.move_forward().is_err());
} else {
panic!("should be occupied");
}
}
#[test]
fn test_occupied_forward_cross_leaf() {
reset_alive_count();
let mut map = BTreeMap::new();
let cap = LeafNode::<i32, i32>::cap();
for i in 0..cap + 5 {
map.insert(i as i32, i as i32 * 10);
}
let end_of_first = cap as i32 - 1;
if let Entry::Occupied(oe) = map.entry(end_of_first) {
assert_eq!(oe.idx as usize, (cap - 1) as usize);
assert_eq!(*oe.peak_forward().unwrap().0, cap as i32);
let oe = oe.move_forward().ok().unwrap();
assert_eq!(*oe.key(), cap as i32);
assert_eq!(oe.idx, 0); } else {
panic!("entry missing");
}
}
#[test]
fn test_vacant_forward_point_to_element() {
reset_alive_count();
let mut map = BTreeMap::new();
map.insert(10, 100);
map.insert(30, 300);
if let Entry::Vacant(ve) = map.entry(20) {
assert_eq!(*ve.peak_forward().unwrap().0, 30);
let oe = ve.move_forward().ok().unwrap();
assert_eq!(*oe.key(), 30);
} else {
panic!("should be vacant");
}
}
#[test]
fn test_vacant_forward_at_leaf_end() {
reset_alive_count();
let mut map = BTreeMap::new();
let cap = LeafNode::<i32, i32>::cap();
for i in 0..cap {
map.insert(i as i32 * 2, i as i32);
}
let last_key = (cap - 1) as i32 * 2;
map.insert(1000, 1000);
if let Entry::Occupied(oe) = map.entry(last_key) {
let leaf = oe.leaf.clone();
if oe.idx as usize == leaf.key_count() as usize - 1 {
let search_key = last_key + 1;
if let Entry::Vacant(ve) = map.entry(search_key) {
assert_eq!(ve.idx as usize, leaf.key_count() as usize);
let next_pair = ve.peak_forward();
assert!(next_pair.is_some());
assert!(*next_pair.unwrap().0 > search_key);
let oe_next = ve.move_forward().ok().unwrap();
assert!(*oe_next.key() > search_key);
}
}
}
}
#[test]
fn test_forward_tree_end() {
reset_alive_count();
let mut map = BTreeMap::new();
map.insert(10, 100);
if let Entry::Occupied(oe) = map.entry(10) {
assert!(oe.peak_forward().is_none());
assert!(oe.move_forward().is_err());
}
if let Entry::Vacant(ve) = map.entry(20) {
assert!(ve.peak_forward().is_none());
assert!(ve.move_forward().is_err());
}
}
#[test]
fn test_occupied_backward_same_leaf() {
reset_alive_count();
let mut map = BTreeMap::new();
map.insert(10, 100);
map.insert(20, 200);
map.insert(30, 300);
if let Entry::Occupied(oe) = map.entry(30) {
assert_eq!(*oe.key(), 30);
assert_eq!(*oe.peak_backward().unwrap().0, 20);
let oe = oe.move_backward().ok().unwrap();
assert_eq!(*oe.key(), 20);
assert_eq!(*oe.peak_backward().unwrap().0, 10);
let oe = oe.move_backward().ok().unwrap();
assert_eq!(*oe.key(), 10);
assert!(oe.peak_backward().is_none());
assert!(oe.move_backward().is_err());
} else {
panic!("should be occupied");
}
}
#[test]
fn test_occupied_backward_cross_leaf() {
reset_alive_count();
let mut map = BTreeMap::new();
let cap = LeafNode::<i32, i32>::cap();
for i in 0..cap + 5 {
map.insert(i as i32, i as i32 * 10);
}
let start_of_second = cap as i32;
if let Entry::Occupied(oe) = map.entry(start_of_second) {
assert_eq!(oe.idx, 0);
assert_eq!(*oe.peak_backward().unwrap().0, cap as i32 - 1);
let oe = oe.move_backward().ok().unwrap();
assert_eq!(*oe.key(), cap as i32 - 1);
assert_eq!(oe.idx as usize, (cap - 1) as usize); } else {
panic!("entry missing");
}
}
#[test]
fn test_vacant_backward_same_leaf() {
reset_alive_count();
let mut map = BTreeMap::new();
map.insert(10, 100);
map.insert(30, 300);
if let Entry::Vacant(ve) = map.entry(20) {
assert_eq!(*ve.peak_backward().unwrap().0, 10);
let oe = ve.move_backward().ok().unwrap();
assert_eq!(*oe.key(), 10);
} else {
panic!("should be vacant");
}
}
#[test]
fn test_vacant_backward_at_leaf_start() {
reset_alive_count();
let mut map = BTreeMap::new();
let cap = LeafNode::<i32, i32>::cap();
for i in 0..cap + 5 {
map.insert(i as i32 * 2, i as i32);
}
let start_of_second = cap as i32 * 2;
let search_key = start_of_second - 1;
if let Entry::Vacant(ve) = map.entry(search_key) {
if ve.idx == 0 {
assert_eq!(*ve.peak_backward().unwrap().0, start_of_second - 2);
let oe = ve.move_backward().ok().unwrap();
assert_eq!(*oe.key(), start_of_second - 2);
}
}
}
#[test]
fn test_backward_tree_start() {
reset_alive_count();
let mut map = BTreeMap::new();
map.insert(10, 100);
if let Entry::Occupied(oe) = map.entry(10) {
assert!(oe.peak_backward().is_none());
assert!(oe.move_backward().is_err());
}
if let Entry::Vacant(ve) = map.entry(5) {
assert!(ve.peak_backward().is_none());
assert!(ve.move_backward().is_err());
}
}
#[test]
fn test_alter_key_boundary_check() {
reset_alive_count();
let mut map = BTreeMap::new();
map.insert(10, 100);
map.insert(20, 200);
map.insert(30, 300);
if let Entry::Occupied(mut oe) = map.entry(20) {
assert!(oe.alter_key(9).is_err());
assert!(oe.alter_key(10).is_err());
assert!(oe.alter_key(30).is_err());
assert!(oe.alter_key(31).is_err());
assert!(oe.alter_key(25).is_ok());
assert_eq!(*oe.key(), 25);
}
}
#[test]
fn test_alter_key_update_sep_height_2() {
unsafe {
reset_alive_count();
let mut leaf0 = LeafNode::<CounterI32, CounterI32>::alloc();
let mut leaf1 = LeafNode::<CounterI32, CounterI32>::alloc();
leaf0.insert_no_split(CounterI32::from(10), CounterI32::from(100));
leaf1.insert_no_split(CounterI32::from(20), CounterI32::from(200));
leaf1.insert_no_split(CounterI32::from(30), CounterI32::from(300));
(*leaf0.brothers()).next = leaf1.get_ptr();
(*leaf1.brothers()).prev = leaf0.get_ptr();
let mut root = InterNode::<CounterI32, CounterI32>::alloc(1);
root.set_left_ptr(leaf0.get_ptr());
root.insert_no_split(CounterI32::from(20), leaf1.get_ptr());
let mut map = BTreeMap::<CounterI32, CounterI32> {
root: Some(Node::Inter(root.clone())),
len: 3,
cache: UnsafeCell::new(PathCache::new()),
leaf_count: 2,
triggers: 0,
};
map.validate();
if let Entry::Occupied(mut oe) = map.entry(CounterI32::from(20)) {
assert!(oe.alter_key(CounterI32::from(25)).is_ok());
assert_eq!(root.get_keys()[0], CounterI32::from(25));
}
map.validate();
drop(map);
assert_eq!(alive_count(), 0);
}
}
#[test]
fn test_alter_key_update_sep_height_3() {
unsafe {
reset_alive_count();
let mut leaf0 = LeafNode::<CounterI32, CounterI32>::alloc();
let mut leaf1 = LeafNode::<CounterI32, CounterI32>::alloc();
let mut leaf2 = LeafNode::<CounterI32, CounterI32>::alloc();
let mut leaf3 = LeafNode::<CounterI32, CounterI32>::alloc();
leaf0.insert_no_split(CounterI32::from(10), CounterI32::from(100));
leaf1.insert_no_split(CounterI32::from(20), CounterI32::from(200));
leaf2.insert_no_split(CounterI32::from(30), CounterI32::from(300));
leaf3.insert_no_split(CounterI32::from(40), CounterI32::from(400));
(*leaf0.brothers()).next = leaf1.get_ptr();
(*leaf1.brothers()).prev = leaf0.get_ptr();
(*leaf1.brothers()).next = leaf2.get_ptr();
(*leaf2.brothers()).prev = leaf1.get_ptr();
(*leaf2.brothers()).next = leaf3.get_ptr();
(*leaf3.brothers()).prev = leaf2.get_ptr();
let mut inter_l = InterNode::<CounterI32, CounterI32>::alloc(1);
inter_l.set_left_ptr(leaf0.get_ptr());
inter_l.insert_no_split(CounterI32::from(20), leaf1.get_ptr());
let mut inter_r = InterNode::<CounterI32, CounterI32>::alloc(1);
inter_r.set_left_ptr(leaf2.get_ptr());
inter_r.insert_no_split(CounterI32::from(40), leaf3.get_ptr());
let mut root = InterNode::<CounterI32, CounterI32>::alloc(2);
root.set_left_ptr(inter_l.get_ptr());
root.insert_no_split(CounterI32::from(30), inter_r.get_ptr());
let mut map = BTreeMap::<CounterI32, CounterI32> {
root: Some(Node::Inter(root.clone())),
len: 4,
cache: UnsafeCell::new(PathCache::new()),
leaf_count: 4,
triggers: 0,
};
map.validate();
if let Entry::Occupied(mut oe) = map.entry(CounterI32::from(30)) {
assert!(oe.alter_key(CounterI32::from(35)).is_ok());
assert_eq!(root.get_keys()[0], CounterI32::from(35));
}
map.validate();
drop(map);
assert_eq!(alive_count(), 0);
}
}
#[test]
fn test_rangetree_swallow_forward() {
let mut map = BTreeMap::<u32, u32>::new();
map.insert(10, 20);
map.insert(30, 10);
map.insert(50, 10);
let target_end = 65;
let mut oe = match map.entry(10) {
Entry::Occupied(o) => o,
_ => panic!("missing"),
};
while let Some((&ns, _)) = oe.peak_forward() {
if ns > target_end {
break;
}
let next_oe = oe.move_forward().ok().expect("move fail");
next_oe.remove();
oe = match map.entry(10) {
Entry::Occupied(o) => o,
_ => panic!("lost"),
};
}
*oe.get_mut() = target_end - 10;
assert_eq!(map.len(), 1);
assert_eq!(*map.get(&10).unwrap(), 55);
}