From 31de3706ddaad3d7b6b340d268ecde96b7862581 Mon Sep 17 00:00:00 2001 From: Bram Devlaminck Date: Sat, 28 Oct 2023 10:35:22 +0200 Subject: [PATCH] write test for split edge --- src/cursor.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/cursor.rs b/src/cursor.rs index 0637d6c..d8b0c2e 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -9,6 +9,7 @@ pub enum CursorIterator { InWord, } +#[derive(Debug, PartialEq)] pub struct Cursor<'a> { pub current_node_index_in_arena: usize, pub index: usize, @@ -171,4 +172,87 @@ impl<'a> Cursor<'a> { self.index = current_advance; } } +} + +#[cfg(test)] +mod tests { + use crate::cursor::Cursor; + use crate::tree::{MAX_CHILDREN, Node, NodeIndex, Nullable, Range, Tree}; + + #[test] + fn test_split_edge() { + let input = "ACAB$"; + + let mut tree = Tree { + arena: vec![ + Node::new( + Range::new(0, 0), + NodeIndex::NULL, + [NodeIndex::NULL; MAX_CHILDREN], + NodeIndex::NULL, + NodeIndex::NULL, + ), + Node::new( + Range::new(0, 5), + 0, + [NodeIndex::NULL; MAX_CHILDREN], + NodeIndex::NULL, + NodeIndex::NULL, + ), + Node::new( + Range::new(1, 5), + 0, + [NodeIndex::NULL; MAX_CHILDREN], + NodeIndex::NULL, + NodeIndex::NULL, + ), + ] + }; + + tree.arena[0].add_child(b'A', 1); + tree.arena[0].add_child(b'C', 2); + + let mut control_tree = Tree { + arena: vec![ + Node::new( + Range::new(0, 0), + NodeIndex::NULL, + [NodeIndex::NULL; MAX_CHILDREN], + NodeIndex::NULL, + NodeIndex::NULL, + ), + Node::new( + Range::new(1, 5), + 3, + [NodeIndex::NULL; MAX_CHILDREN], + NodeIndex::NULL, + NodeIndex::NULL, + ), + Node::new( + Range::new(1, 5), + 0, + [NodeIndex::NULL; MAX_CHILDREN], + NodeIndex::NULL, + NodeIndex::NULL, + ), + Node::new( + Range::new(0, 1), + 0, + [NodeIndex::NULL; MAX_CHILDREN], + NodeIndex::NULL, + NodeIndex::NULL, + ) + ] + }; + + control_tree.arena[0].add_child(b'A', 3); + control_tree.arena[0].add_child(b'C', 2); + control_tree.arena[3].add_child(b'C', 1); + + let mut cursor = Cursor {current_node_index_in_arena: 1, index : 1, tree: &mut tree}; + cursor.split_edge(input.as_bytes()); + + assert_eq!(cursor, Cursor {current_node_index_in_arena: 3 , index: 1, tree: &mut control_tree}) + } + } \ No newline at end of file