Commit 854ca0c0 authored by SirBlueRabbit's avatar SirBlueRabbit
Browse files

rotate basic tests

parent 1aaf69b4
......@@ -11,24 +11,15 @@ pub struct Node<V> {
//right: Option<Label<Node<V>>>,
}
impl<V> Node<V> {
pub fn new(value: V) -> Self {
Node {
impl<V> Pool<Node<V>> {
pub fn add_node(&mut self, value: V) -> Label<Node<V>> {
self.insert(Node {
value,
path_parent: None,
parent: None,
reversed: false,
children: [None, None],
//left: None,
//right: None,
}
}
}
impl<V> Pool<Node<V>> {
pub fn add_node(&mut self, value: V) -> Label<Node<V>> {
let node = Node::new(value);
self.insert(node)
})
}
/* pub fn find_root(&mut self, label: Label<Node<V>>) -> Label<Node<V>> {
......@@ -82,14 +73,14 @@ impl<V> Pool<Node<V>> {
if self[p_label].reversed
== (self.is_zero_child(label) == self.is_zero_child(p_label))
{
self.rotate_up(p_label);
self.rotate_up(label);
self.rotate(p_label);
self.rotate(label);
} else {
self.rotate_up(label);
self.rotate_up(label);
self.rotate(label);
self.rotate(label);
};
} else {
self.rotate_up(label);
self.rotate(label);
break;
}
}
......@@ -106,23 +97,28 @@ impl<V> Pool<Node<V>> {
}
}
*/
fn rotate_up(&mut self, label: Label<Node<V>>) {
fn rotate(&mut self, label: Label<Node<V>>) {
let reversed = self[label].reversed;
let p_side = self.is_zero_child(label);
let p_side = !self.is_zero_child(label);
let p_label = self[label].parent.unwrap();
let b_option = self[label].children[usize::from(reversed ^ !p_side)];
let g_option = self[p_label].parent;
self[label].parent = g_option;
if let Some(g_label) = g_option {
let g_side = self.is_zero_child(p_label);
let g_side = !self.is_zero_child(p_label);
self[g_label].children[usize::from(g_side)] = Some(label);
} else {
self[label].path_parent = self[p_label].path_parent;
self[p_label].path_parent = None;
}
self[p_label].children[usize::from(p_side)] =
self[label].children[usize::from(reversed ^ !p_side)];
self[p_label].children[usize::from(p_side)] = b_option;
self[label].children[usize::from(reversed ^ !p_side)] = Some(p_label);
if let Some(b_label) = b_option {
self[b_label].parent = Some(p_label);
}
self[p_label].parent = Some(label);
}
fn is_zero_child(&self, label: Label<Node<V>>) -> bool {
......@@ -214,6 +210,91 @@ impl<V> Pool<Node<V>> {
}
} */
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let mut lct = Pool::with_capacity(1);
let label = lct.add_node(());
lct.splay(label);
}
#[test]
fn rotate_basic() {
let mut lct = Pool::with_capacity(2);
// define nodes
let o_label = lct.add_node("node");
let p_label = lct.add_node("parent");
// link nodes
lct[p_label].children[0] = Some(o_label);
lct[o_label].parent = Some(p_label);
// rotate
lct.rotate(o_label);
dbg!(lct.clone());
// check children
assert_eq!(lct[o_label].children, [None, Some(p_label)]);
assert_eq!(lct[p_label].children, [None, None]);
// check parents
assert_eq!(lct[o_label].parent, None);
assert_eq!(lct[p_label].parent, Some(o_label));
}
#[test]
fn rotate_attachments() {
let mut lct = Pool::with_capacity(7);
// define nodes
let o_label = lct.add_node("node");
let p_label = lct.add_node("parent");
let g_label = lct.add_node("grandparent");
let a_label = lct.add_node("a");
let b_label = lct.add_node("b");
let c_label = lct.add_node("c");
let d_label = lct.add_node("d");
// link nodes
lct[o_label].children = [Some(a_label), Some(b_label)];
lct[p_label].children = [Some(o_label), Some(c_label)];
lct[g_label].children = [Some(p_label), Some(d_label)];
lct[o_label].parent = Some(p_label);
lct[p_label].parent = Some(g_label);
lct[a_label].parent = Some(o_label);
lct[b_label].parent = Some(o_label);
lct[c_label].parent = Some(p_label);
lct[d_label].parent = Some(g_label);
// rotate
lct.rotate(o_label);
dbg!(lct.clone());
// check children
assert_eq!(lct[o_label].children, [Some(a_label), Some(p_label)]);
assert_eq!(lct[p_label].children, [Some(b_label), Some(c_label)]);
assert_eq!(lct[g_label].children, [Some(o_label), Some(d_label)]);
assert_eq!(lct[a_label].children, [None, None]);
assert_eq!(lct[b_label].children, [None, None]);
assert_eq!(lct[c_label].children, [None, None]);
assert_eq!(lct[d_label].children, [None, None]);
// check parents
assert_eq!(lct[o_label].parent, Some(g_label));
assert_eq!(lct[p_label].parent, Some(o_label));
assert_eq!(lct[g_label].parent, None);
assert_eq!(lct[a_label].parent, Some(o_label));
assert_eq!(lct[b_label].parent, Some(p_label));
assert_eq!(lct[c_label].parent, Some(p_label));
assert_eq!(lct[d_label].parent, Some(g_label));
}
}
/*
#[cfg(test)]
mod tests {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment