public void EnableHierarchy_NodeNotInTree() { SkipTree <Item> tree = new SkipTree <Item>(); var one = new Item(null, "one"); var two = new Item(null, "two"); var three = new Item(null, "three"); var four = new Item(two, "four"); var five = new Item(two, "five"); var six = new Item(two, "six"); tree.AddItem(one); // two not in tree! tree.AddItem(three); tree.AddItem(four); tree.AddItem(five); tree.AddItem(six); tree.DisableHierarchy(five); tree.DisableHierarchy(four); string[] output = new string[5]; int i = 0; tree.EnableHierarchy(two); tree.TraversePreOrder((item) => { output[i++] = item.name; }); Assert.AreEqual(new[] { "one", "three", "four", "five", "six" }, output); Assert.AreEqual(5, tree.Size); }
public void AddNodeToRoot() { SkipTree <Item> tree = new SkipTree <Item>(); var one = new Item(null, "one"); var two = new Item(one, "two"); var three = new Item(one, "three"); tree.AddItem(two); tree.AddItem(three); string[] output = new string[2]; int i = 0; tree.TraversePreOrder((item) => { output[i++] = item.name; }); Assert.AreEqual(new[] { "two", "three" }, output); }
public void Event_AddItem() { int callCount = 0; SkipTree <Item> tree = new SkipTree <Item>(); var one = new Item(null, "one"); var two = new Item(null, "two"); tree.onTreeChanged += (changeType) => { if (changeType == SkipTree <Item> .TreeChangeType.ItemAdded) { callCount++; } }; tree.AddItem(one); tree.AddItem(two); Assert.AreEqual(2, callCount); }
public void MissingParentInTree() { SkipTree <Item> tree = new SkipTree <Item>(); var one = new Item(null, "one"); var two = new Item(one, "two"); var three = new Item(two, "three"); var four = new Item(two, "four"); tree.AddItem(three); tree.AddItem(four); tree.AddItem(one); string[] output = new string[3]; int i = 0; tree.TraversePreOrder((item) => { output[i++] = item.name; }); Assert.AreEqual(new[] { "one", "three", "four" }, output); }
public void Event_RemoveHierarchy() { int callCount = 0; SkipTree <Item> tree = new SkipTree <Item>(); var one = new Item(null, "one"); var two = new Item(null, "two"); tree.onTreeChanged += (changeType) => { if (changeType == SkipTree <Item> .TreeChangeType.HierarchyRemoved) { callCount++; } }; tree.AddItem(one); tree.AddItem(two); tree.DisableHierarchy(two); tree.RemoveHierarchy(two); Assert.AreEqual(1, callCount); }
public void OhForFuckSakesNotAgain() { SkipTree <Item> tree = new SkipTree <Item>(); var one = new Item(null, "one"); var two = new Item(null, "two"); var three = new Item(two, "three"); var four = new Item(two, "four"); var five = new Item(two, "five"); var six = new Item(null, "six"); tree.AddItem(one); tree.AddItem(two); tree.AddItem(three); tree.AddItem(four); tree.AddItem(five); tree.AddItem(six); string[] output = new string[3]; int i = 0; tree.ConditionalTraversePreOrder((item) => { output[i++] = item.name; return(item.name != "two"); }); Assert.AreEqual(new [] { "one", "two", "six" }, output); }
public void RemoveAnElementHierarchy_NodeInTree() { SkipTree <Item> tree = new SkipTree <Item>(); var one = new Item(null, "one"); var two = new Item(null, "two"); var three = new Item(null, "three"); var four = new Item(two, "four"); var five = new Item(two, "five"); var six = new Item(two, "six"); tree.AddItem(one); tree.AddItem(two); tree.AddItem(three); tree.AddItem(four); tree.AddItem(five); tree.AddItem(six); tree.RemoveHierarchy(two); string[] output = new string[2]; int i = 0; tree.TraversePreOrder((item) => { output[i++] = item.name; }); Assert.AreEqual(new[] { "one", "three" }, output); Assert.AreEqual(2, tree.Size); }
public void SetSiblingIndex() { SkipTree <Item> tree = new SkipTree <Item>(); var one = new Item(null, "one"); var two = new Item(null, "two"); var three = new Item(null, "three"); var four = new Item(null, "four"); var five = new Item(null, "five"); var six = new Item(null, "six"); tree.AddItem(one); tree.AddItem(two); tree.AddItem(three); tree.AddItem(four); tree.AddItem(five); tree.AddItem(six); Assert.AreEqual(0, tree.GetSiblingIndex(one)); Assert.AreEqual(1, tree.GetSiblingIndex(two)); Assert.AreEqual(2, tree.GetSiblingIndex(three)); Assert.AreEqual(3, tree.GetSiblingIndex(four)); Assert.AreEqual(4, tree.GetSiblingIndex(five)); Assert.AreEqual(5, tree.GetSiblingIndex(six)); tree.SetSiblingIndex(two, 0); Assert.AreEqual(1, tree.GetSiblingIndex(one)); Assert.AreEqual(0, tree.GetSiblingIndex(two)); tree.SetSiblingIndex(five, 3); Assert.AreEqual(1, tree.GetSiblingIndex(one)); Assert.AreEqual(0, tree.GetSiblingIndex(two)); Assert.AreEqual(2, tree.GetSiblingIndex(three)); Assert.AreEqual(3, tree.GetSiblingIndex(five)); Assert.AreEqual(4, tree.GetSiblingIndex(four)); Assert.AreEqual(5, tree.GetSiblingIndex(six)); tree.SetSiblingIndex(six, -1); Assert.AreEqual(0, tree.GetSiblingIndex(six)); Assert.AreEqual(1, tree.GetSiblingIndex(two)); Assert.AreEqual(2, tree.GetSiblingIndex(one)); Assert.AreEqual(3, tree.GetSiblingIndex(three)); Assert.AreEqual(4, tree.GetSiblingIndex(five)); Assert.AreEqual(5, tree.GetSiblingIndex(four)); tree.SetSiblingIndex(six, 10); Assert.AreEqual(0, tree.GetSiblingIndex(two)); Assert.AreEqual(1, tree.GetSiblingIndex(one)); Assert.AreEqual(2, tree.GetSiblingIndex(three)); Assert.AreEqual(3, tree.GetSiblingIndex(five)); Assert.AreEqual(4, tree.GetSiblingIndex(four)); Assert.AreEqual(5, tree.GetSiblingIndex(six)); }
public void GetTraversableTree_Root() { SkipTree <Item> tree = new SkipTree <Item>(); var one = new Item(null, "one"); var two = new Item(null, "two"); var three = new Item(null, "three"); var four = new Item(two, "four"); var five = new Item(two, "five"); var six = new Item(two, "six"); tree.AddItem(one); tree.AddItem(two); tree.AddItem(three); tree.AddItem(four); tree.AddItem(five); tree.AddItem(six); SkipTree <Item> .TreeNode traverseTree = tree.GetTraversableTree(); Stack <SkipTree <Item> .TreeNode> stack = new Stack <SkipTree <Item> .TreeNode>(); stack.Push(traverseTree); string[] output = new string[6]; int count = 0; while (stack.Count > 0) { var current = stack.Pop(); if (current.item != null) { output[count++] = current.item.name; } for (int i = 0; i < current.children.Length; i++) { stack.Push(current.children[i]); } } Assert.AreEqual(new string[] { "three", "two", "six", "five", "four", "one" }, output); }