public void PerformanceAdd_WithOnly2ParentWith50000SplitElements() { var count = 3; var counter1 = 1; var counter2 = 25001; var hierarchy = new Hierarchy <int>(-1); Stopwatch timer = new Stopwatch(); timer.Start(); hierarchy.Add(-1, 1); hierarchy.Add(-1, 25001); for (int i = 1; i < 25000; i++) { hierarchy.Add(1, ++counter1); hierarchy.Add(25001, ++counter2); count += 2; Assert.AreEqual(count, hierarchy.Count, "Count did not increase correctly!"); } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); counter1 = 1; counter2 = 25001; for (int i = 1; i < 25000; i++) { Assert.AreEqual(1, hierarchy.GetParent(++counter1), "Parent did not match!"); Assert.AreEqual(25001, hierarchy.GetParent(++counter2), "Parent did not match!"); } CollectionAssert.AreEqual(Enumerable.Range(2, 24999).ToArray(), hierarchy.GetChildren(1).ToArray(), "Children did not match!"); CollectionAssert.AreEqual(Enumerable.Range(25002, 24999).ToArray(), hierarchy.GetChildren(25001).ToArray(), "Children did not match!"); }
public void PerformanceRemove_With2GroupsOf25000RemovalsWith50000Elements() { var count = 50001; var counter1 = 1; var counter2 = 25001; var hierarchy = new Hierarchy <int>(-1); hierarchy.Add(-1, 1); hierarchy.Add(-1, 25001); for (int i = 1; i < 25000; i++) { hierarchy.Add(counter1, ++counter1); hierarchy.Add(counter2, ++counter2); } Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 1; i < 25001; i++) { hierarchy.Remove(i); hierarchy.Remove(25000 + i); count -= 2; Assert.AreEqual(count, hierarchy.Count, "Count did not decrease correctly!"); } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); Assert.AreEqual(1, hierarchy.Count, "Incorrect count after removal!"); Assert.IsTrue(hierarchy.Contains(-1)); CollectionAssert.AreEqual(new int[0], hierarchy.GetChildren(-1).ToArray(), "Children were not deleted correcly after removal!"); }
public void PerformanceAdd_WithOnly2ParentWith50000SplitElements() { var count = 3; var counter1 = 1; var counter2 = 25001; var hierarchy = new Hierarchy<int>(-1); Stopwatch timer = new Stopwatch(); timer.Start(); hierarchy.Add(-1, 1); hierarchy.Add(-1, 25001); for (int i = 1; i < 25000; i++) { hierarchy.Add(1, ++counter1); hierarchy.Add(25001, ++counter2); count += 2; Assert.AreEqual(count, hierarchy.Count, "Count did not increase correctly!"); } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); counter1 = 1; counter2 = 25001; for (int i = 1; i < 25000; i++) { Assert.AreEqual(1, hierarchy.GetParent(++counter1), "Parent did not match!"); Assert.AreEqual(25001, hierarchy.GetParent(++counter2), "Parent did not match!"); } CollectionAssert.AreEqual(Enumerable.Range(2, 24999).ToArray(), hierarchy.GetChildren(1).ToArray(), "Children did not match!"); CollectionAssert.AreEqual(Enumerable.Range(25002, 24999).ToArray(), hierarchy.GetChildren(25001).ToArray(), "Children did not match!"); }
public void PerformanceGetParent_With50000ElementsWith5000Parents() { var counter = 5001; var hierarchy = new Hierarchy<int>(-88); for (int i = 1; i <= 5000; i++) { hierarchy.Add(-88, i); for (int j = 0; j < 10; j++) { hierarchy.Add(i, counter++); } } counter = 5001; Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 1; i <= 5000; i++) { Assert.AreEqual(-88, hierarchy.GetParent(i), "Expected parent did not match!"); for (int j = 0; j < 10; j++) { Assert.AreEqual(i, hierarchy.GetParent(counter++), "Expected parent did not match!"); } } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); }
public void PerformanceGetParent_With50000ElementsWith5000Parents() { var counter = 5001; var hierarchy = new Hierarchy <int>(-88); for (int i = 1; i <= 5000; i++) { hierarchy.Add(-88, i); for (int j = 0; j < 10; j++) { hierarchy.Add(i, counter++); } } counter = 5001; Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 1; i <= 5000; i++) { Assert.AreEqual(-88, hierarchy.GetParent(i), "Expected parent did not match!"); for (int j = 0; j < 10; j++) { Assert.AreEqual(i, hierarchy.GetParent(counter++), "Expected parent did not match!"); } } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); }
public void PerformanceRemove_With2GroupsOf25000RemovalsWith50000Elements() { var count = 50001; var counter1 = 1; var counter2 = 25001; var hierarchy = new Hierarchy<int>(-1); hierarchy.Add(-1, 1); hierarchy.Add(-1, 25001); for (int i = 1; i < 25000; i++) { hierarchy.Add(counter1, ++counter1); hierarchy.Add(counter2, ++counter2); } Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 1; i < 25001; i++) { hierarchy.Remove(i); hierarchy.Remove(25000 + i); count -= 2; Assert.AreEqual(count, hierarchy.Count, "Count did not decrease correctly!"); } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); Assert.AreEqual(1, hierarchy.Count, "Incorrect count after removal!"); Assert.IsTrue(hierarchy.Contains(-1)); CollectionAssert.AreEqual(new int[0], hierarchy.GetChildren(-1).ToArray(), "Children were not deleted correcly after removal!"); }
public void PerformanceForEach_With55500ElementsInterconnected() { var start1 = 0; var elements = new List<int>(); elements.Add(start1); var hierarchy = new Hierarchy<int>(start1); for (int i = 1; i <= 51100; i = i + 511) { hierarchy.Add(start1, i); for (int j = i + 1; j <= i + 510; j = j + 51) { hierarchy.Add(i, j); for (int k = j + 1; k <= j + 50; k++) { hierarchy.Add(j, k); } } } for (int i = 1; i <= 51100; i = i + 511) { elements.Add(i); } for (int i = 1; i <= 51100; i = i + 511) { for (int j = i + 1; j <= i + 510; j = j + 51) { elements.Add(j); } } for (int i = 1; i <= 51100; i = i + 511) { for (int j = i + 1; j <= i + 510; j = j + 51) { for (int k = j + 1; k <= j + 50; k++) { elements.Add(k); } } } var counter = 0; Stopwatch timer = new Stopwatch(); timer.Start(); foreach (var element in hierarchy) { Assert.AreEqual(elements[counter++], element, "Expected element did not match!"); } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); Assert.AreEqual(counter, hierarchy.Count, "Incorect number of elements returned!"); }
private Hierarchy GetTestHierarchy() { var hierarchy = new Hierarchy(); hierarchy.Add(this.GetTermSet(5)); hierarchy.Add(this.GetTermSet(7)); hierarchy.Add(this.GetTermSet(9)); return hierarchy; }
public void PerformanceRemove_With20000RemovalsIn4GroupsInReverseOrderFromTheMiddleWith40000Elements() { var counter1 = 10000; var counter2 = 20000; var counter3 = 30000; var counter4 = 40000; var hierarchy = new Hierarchy<int>(-2); hierarchy.Add(-2, 10000); hierarchy.Add(-2, 20000); hierarchy.Add(-2, 30000); hierarchy.Add(-2, 40000); for (int i = 1; i < 10000; i++) { hierarchy.Add(counter1, --counter1); hierarchy.Add(counter2, --counter2); hierarchy.Add(counter3, --counter3); hierarchy.Add(counter4, --counter4); } var count = 40001; counter1 = 9000; counter2 = 18000; counter3 = 27000; counter4 = 36000; Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 1; i < 5001; i++) { hierarchy.Remove(counter1--); hierarchy.Remove(counter2--); hierarchy.Remove(counter3--); hierarchy.Remove(counter4--); count -= 4; Assert.AreEqual(count, hierarchy.Count, "Count did not decrease correctly!"); } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); Assert.AreEqual(20001, hierarchy.Count, "Incorrect count after removal!"); counter1 = 9000; counter2 = 18000; counter3 = 27000; counter4 = 36000; for (int i = 0; i < 5000; i++) { Assert.IsFalse(hierarchy.Contains(counter1--)); Assert.IsFalse(hierarchy.Contains(counter2--)); Assert.IsFalse(hierarchy.Contains(counter3--)); Assert.IsFalse(hierarchy.Contains(counter4--)); } Assert.IsTrue(hierarchy.Contains(-2)); CollectionAssert.AreEqual(new[] { 4000 }, hierarchy.GetChildren(9001).ToArray(), "Children were not correctly reattached!"); CollectionAssert.AreEqual(new[] { 13000 }, hierarchy.GetChildren(18001).ToArray(), "Children were not correctly reattached!"); CollectionAssert.AreEqual(new[] { 22000 }, hierarchy.GetChildren(27001).ToArray(), "Children were not correctly reattached!"); CollectionAssert.AreEqual(new[] { 31000 }, hierarchy.GetChildren(36001).ToArray(), "Children were not correctly reattached!"); }
public void Add_ToNonExistingElement_ShouldThrowException() { Assert.Throws <ArgumentException>(() => { Hierarchy.Add(DefaultRootValue + 1, 2); }); }
public void Add_WithSingleElement_ShouldCorrectlySetElementsParent() { Hierarchy.Add(DefaultRootValue, 111); Hierarchy.Add(111, 222); Assert.AreEqual(111, Hierarchy.GetParent(222), "Element's parent wasn't correct!"); }
public void PerformanceGetCommonElements_WithHalfCommonElementsInBothCollections() { var start1 = 0; var start2 = 75000; var hierarchy = new Hierarchy <int>(start1); var hierarchy2 = new Hierarchy <int>(start2); for (int i = 1; i <= 50000; i++) { hierarchy.Add(start1, i); } for (int i = start2 - 1; i > 25000; i--) { hierarchy2.Add(start2, i); } var common = Enumerable.Range(25001, 25000).ToArray(); Stopwatch timer = new Stopwatch(); timer.Start(); CollectionAssert.AreEqual(common, hierarchy.GetCommonElements(hierarchy2).ToArray(), "GetCommonElements method returned incorrect collection!"); timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); }
public void GetCommonElements_WithHierarchyWithMultipleCommonElements_ShouldReturnACorrectCollection() { var otherHierarchy = new Hierarchy<int>(10); otherHierarchy.Add(10, -22); otherHierarchy.Add(-22, 56); otherHierarchy.Add(10, 108); otherHierarchy.Add(-22, 34); this.Hierarchy.Add(DefaultRootValue, 100); this.Hierarchy.Add(DefaultRootValue, -22); this.Hierarchy.Add(100, 34); this.Hierarchy.Add(100, 10); var result = this.Hierarchy.GetCommonElements(otherHierarchy).ToArray(); CollectionAssert.AreEquivalent(result, new[] { -22,34,10 }, "GetCommonElements returned an incorrect collection!"); }
public void Add_WithSingleElement_ShouldAddCorrectElement() { Assert.IsFalse(Hierarchy.Contains(2)); Hierarchy.Add(DefaultRootValue, 2); Assert.IsTrue(Hierarchy.Contains(2), "Element wasn't added!"); }
public void GetCommonElements_WithHierarchyWithMultipleCommonElements_ShouldReturnACorrectCollection() { var otherHierarchy = new Hierarchy <int>(10); otherHierarchy.Add(10, -22); otherHierarchy.Add(-22, 56); otherHierarchy.Add(10, 108); otherHierarchy.Add(-22, 34); this.Hierarchy.Add(DefaultRootValue, 100); this.Hierarchy.Add(DefaultRootValue, -22); this.Hierarchy.Add(100, 34); this.Hierarchy.Add(100, 10); var result = this.Hierarchy.GetCommonElements(otherHierarchy).ToArray(); CollectionAssert.AreEquivalent(result, new[] { -22, 34, 10 }, "GetCommonElements returned an incorrect collection!"); }
static void Main() { var hierarchy = new Hierarchy <string>("Leonidas"); hierarchy.Add("Leonidas", "Xena The Princess Warrior"); hierarchy.Add("Leonidas", "General Protos"); hierarchy.Add("Xena The Princess Warrior", "Gorok"); hierarchy.Add("Xena The Princess Warrior", "Bozot"); hierarchy.Add("General Protos", "Subotli"); hierarchy.Add("General Protos", "Kira"); hierarchy.Add("General Protos", "Zaler"); var children = hierarchy.GetChildren("Leonidas"); Console.WriteLine(string.Join(", ", children)); var parent = hierarchy.GetParent("Kira"); Console.WriteLine(parent); hierarchy.Remove("General Protos"); children = hierarchy.GetChildren("Leonidas"); Console.WriteLine(string.Join(", ", children)); foreach (var item in hierarchy) { Console.WriteLine(item); } }
static void Main() { var hierarchy = new Hierarchy<string>("Leonidas"); hierarchy.Add("Leonidas", "Xena The Princess Warrior"); hierarchy.Add("Leonidas", "General Protos"); hierarchy.Add("Xena The Princess Warrior", "Gorok"); hierarchy.Add("Xena The Princess Warrior", "Bozot"); hierarchy.Add("General Protos", "Subotli"); hierarchy.Add("General Protos", "Kira"); hierarchy.Add("General Protos", "Zaler"); var children = hierarchy.GetChildren("Leonidas"); Console.WriteLine(string.Join(", ", children)); var parent = hierarchy.GetParent("Kira"); Console.WriteLine(parent); hierarchy.Remove("General Protos"); children = hierarchy.GetChildren("Leonidas"); Console.WriteLine(string.Join(", ", children)); //foreach (var item in hierarchy) //{ // Console.WriteLine(item); //} }
public void Add_WithDuplicateElement_ShouldThrowException() { Assert.Throws <ArgumentException>(() => { Hierarchy.Add(DefaultRootValue, 2); Hierarchy.Add(DefaultRootValue, 3); Hierarchy.Add(3, 2); }); }
public void PerformanceForEach_With55500Elements() { var start1 = 0; var start2 = 5000; var start3 = 100000; var elements = new List <int>(); elements.Add(start1); var hierarchy = new Hierarchy <int>(start1); for (int i = 1; i <= 500; i++) { elements.Add(i); hierarchy.Add(start1, i); for (int j = 0; j < 10; j++) { elements.Add(start2); hierarchy.Add(i, start2); for (int k = 0; k < 10; k++) { elements.Add(start3); hierarchy.Add(start2, start3++); } start2++; } } elements.Sort(); var counter = 0; Stopwatch timer = new Stopwatch(); timer.Start(); foreach (var element in hierarchy) { Assert.AreEqual(elements[counter++], element, "Expected element did not match!"); } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); Assert.AreEqual(counter, hierarchy.Count, "Incorect number of elements returned!"); }
public void Add_WithAddAfterRemoving_ShouldAddCorrectly() { Hierarchy.Add(DefaultRootValue, 2); Hierarchy.Remove(2); Assert.AreEqual(1, Hierarchy.Count); Hierarchy.Add(DefaultRootValue, 2); Assert.AreEqual(2, Hierarchy.Count); }
public void Add_WithMultipleElements_ShouldIncrementCountCorrectly() { var elementsToAdd = new[] { 3, 2, 7 }; Hierarchy.Add(DefaultRootValue, elementsToAdd[0]); Hierarchy.Add(DefaultRootValue, elementsToAdd[1]); Hierarchy.Add(elementsToAdd[1], elementsToAdd[2]); Assert.AreEqual(1 + elementsToAdd.Length, Hierarchy.Count, "Count did not increase correctly!"); }
public void GetCommonElements_WithHierarchyWithOneCommonElement_ShouldReturnACollectionOfCorrectElement() { var otherHierarchy = new Hierarchy<int>(1); otherHierarchy.Add(1,13); this.Hierarchy.Add(DefaultRootValue,13); var result = this.Hierarchy.GetCommonElements(otherHierarchy).ToArray(); CollectionAssert.AreEquivalent(result, new [] {13}, "GetCommonElements returned an incorrect collection!"); }
public void PerformanceForEach_With55500Elements() { var start1 = 0; var start2 = 5000; var start3 = 100000; var elements = new List<int>(); elements.Add(start1); var hierarchy = new Hierarchy<int>(start1); for (int i = 1; i <= 500; i++) { elements.Add(i); hierarchy.Add(start1, i); for (int j = 0; j < 10; j++) { elements.Add(start2); hierarchy.Add(i, start2); for (int k = 0; k < 10; k++) { elements.Add(start3); hierarchy.Add(start2, start3++); } start2++; } } elements.Sort(); var counter = 0; Stopwatch timer = new Stopwatch(); timer.Start(); foreach (var element in hierarchy) { Assert.AreEqual(elements[counter++], element, "Expected element did not match!"); } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); Assert.AreEqual(counter, hierarchy.Count, "Incorect number of elements returned!"); }
public void GetCommonElements_WithHierarchyWithOneCommonElement_ShouldReturnACollectionOfCorrectElement() { var otherHierarchy = new Hierarchy <int>(1); otherHierarchy.Add(1, 13); this.Hierarchy.Add(DefaultRootValue, 13); var result = this.Hierarchy.GetCommonElements(otherHierarchy).ToArray(); CollectionAssert.AreEquivalent(result, new [] { 13 }, "GetCommonElements returned an incorrect collection!"); }
public void Add_WithMultipleElements_ShouldAddElementAtCorrectPlace() { Hierarchy.Add(DefaultRootValue, 6); Hierarchy.Add(DefaultRootValue, 2); Hierarchy.Add(6, 13); Hierarchy.Add(2, 7); Hierarchy.Add(7, 22); Hierarchy.Add(7, 25); Assert.AreEqual(25, Hierarchy.GetChildren(7).ToList()[1], "Element wasn't added at correct place!"); }
static void Main() { var tree = new Hierarchy <string>("jayZ"); tree.Add("jayZ", "Beyonce"); tree.Add("jayZ", "Rihana"); tree.Add("jayZ", "Shakira"); tree.Add("Shakira", "Pique"); tree.Add("jayZ", "Shakira"); tree.Add("jayZ", "Beyonce"); // var hierarchy = new Hierarchy<string>("Leonidas"); // hierarchy.Add("Leonidas", "Xena The Princess Warrior"); // hierarchy.Add("Leonidas", "General Protos"); // hierarchy.Add("Xena The Princess Warrior", "Gorok"); // hierarchy.Add("Xena The Princess Warrior", "Bozot"); // hierarchy.Add("General Protos", "Subotli"); // hierarchy.Add("General Protos", "Kira"); // hierarchy.Add("General Protos", "Zaler"); // var children = hierarchy.GetChildren("Leonidas"); // Console.WriteLine(string.Join(", ", children)); // var parent = hierarchy.GetParent("Kira"); // Console.WriteLine(parent); // hierarchy.Remove("General Protos"); // children = hierarchy.GetChildren("Leonidas"); // Console.WriteLine(string.Join(", ", children)); // foreach (var item in hierarchy) // { // Console.WriteLine(item); // } }
public void PerformanceGetChildren_With50000ElementsWith5000Parents() { var counter = 5001; var hierarchy = new Hierarchy <int>(-88); for (int i = 1; i <= 5000; i++) { hierarchy.Add(-88, i); for (int j = 0; j < 10; j++) { hierarchy.Add(i, counter++); } } var baseChildren = Enumerable.Range(1, 5000).ToArray(); counter = 5001; Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 1; i <= 5000; i++) { var children = hierarchy.GetChildren(i); var count = 0; foreach (var child in children) { count++; Assert.AreEqual(counter++, child, "Children collection did not match!"); } Assert.AreEqual(10, count, "Incorrect children collection count!"); } CollectionAssert.AreEqual(baseChildren, hierarchy.GetChildren(-88).ToArray(), "Expected children did not match!"); timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 50); }
static void Main() { Hierarchy <string> hierarchy = new Hierarchy <string>("Leonidas"); hierarchy.Add("Leonidas", "Xena The Princess Warrior"); hierarchy.Add("Leonidas", "General Protos"); hierarchy.Add("Xena The Princess Warrior", "Gorok"); hierarchy.Add("Xena The Princess Warrior", "Bozot"); hierarchy.Add("General Protos", "Subotli"); hierarchy.Add("General Protos", "Kira"); hierarchy.Add("General Protos", "Zaler"); /*Hierarchy<string> other = new Hierarchy<string>("Leonidas"); * other.Add("Leonidas", "Xena The Princess Warrior"); * other.Add("Leonidas", "General Protos"); * other.Add("Xena The Princess Warrior", "Gorok"); * other.Add("Xena The Princess Warrior", "Bozot"); * /*hierarchy.Add("General Protos", "Subotli"); * hierarchy.Add("General Protos", "Kira"); * hierarchy.Add("General Protos", "Zaler");*/ /*var children = hierarchy.GetChildren("Zaler"); * //Console.WriteLine(string.Join(", ", children)); * * //var parent = hierarchy.GetParent("Subotli"); * //Console.WriteLine(parent); * Console.WriteLine("******"); * * hierarchy.Remove("Zaler"); * children = hierarchy.GetChildren("General Protos"); * //Console.WriteLine("Children: " + string.Join(", ", children)); * * Console.WriteLine("----------"); * foreach (var item in hierarchy) * { * Console.WriteLine(item); * } * * // hierarchy.Remove("Leonidas"); * //Console.WriteLine(hierarchy.Contains("Leonidas")); * Console.WriteLine("Count: " + hierarchy.Count); * Console.WriteLine("****************");*/ //hierarchy.ForEach(Console.WriteLine); /* var result = hierarchy.GetCommonElements(other); * Console.WriteLine("--------"); * * foreach (var i in result) * { * Console.WriteLine(i); * }*/ }
public void PerformanceAdd_With40000ElementsInReverseOrderIn4Groups() { var counter1 = 10000; var counter2 = 30000; var counter3 = 50000; var counter4 = 70000; var count = 5; var hierarchy = new Hierarchy <int>(100000); Stopwatch timer = new Stopwatch(); timer.Start(); hierarchy.Add(100000, 10000); hierarchy.Add(100000, 30000); hierarchy.Add(100000, 50000); hierarchy.Add(100000, 70000); for (int i = 1; i < 10000; i++) { hierarchy.Add(counter1, --counter1); hierarchy.Add(counter2, --counter2); hierarchy.Add(counter3, --counter3); hierarchy.Add(counter4, --counter4); count += 4; Assert.AreEqual(count, hierarchy.Count, "Count did not increase correctly!"); } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); counter1 = 1; counter2 = 20001; counter3 = 40001; counter4 = 60001; for (int i = 1; i < 10000; i++) { Assert.AreEqual(counter1 + 1, hierarchy.GetParent(counter1), "Parent did not match!"); Assert.AreEqual(counter2 + 1, hierarchy.GetParent(counter2), "Parent did not match!"); Assert.AreEqual(counter3 + 1, hierarchy.GetParent(counter3), "Parent did not match!"); Assert.AreEqual(counter4 + 1, hierarchy.GetParent(counter4), "Parent did not match!"); Assert.AreEqual(counter1, hierarchy.GetChildren(counter1 + 1).FirstOrDefault(), "Children did not match!"); Assert.AreEqual(counter2, hierarchy.GetChildren(counter2 + 1).FirstOrDefault(), "Children did not match!"); Assert.AreEqual(counter3, hierarchy.GetChildren(counter3 + 1).FirstOrDefault(), "Children did not match!"); Assert.AreEqual(counter4, hierarchy.GetChildren(counter4 + 1).FirstOrDefault(), "Children did not match!"); counter1++; counter2++; counter3++; counter4++; } }
public void PerformanceGetChildren_With50000ElementsWith5000Parents() { var counter = 5001; var hierarchy = new Hierarchy<int>(-88); for (int i = 1; i <= 5000; i++) { hierarchy.Add(-88, i); for (int j = 0; j < 10; j++) { hierarchy.Add(i, counter++); } } var baseChildren = Enumerable.Range(1, 5000).ToArray(); counter = 5001; Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 1; i <= 5000; i++) { var children = hierarchy.GetChildren(i); var count = 0; foreach (var child in children) { count++; Assert.AreEqual(counter++, child, "Children collection did not match!"); } Assert.AreEqual(10, count, "Incorrect children collection count!"); } CollectionAssert.AreEqual(baseChildren, hierarchy.GetChildren(-88).ToArray(), "Expected children did not match!"); timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 50); }
public void PerformanceAdd_With40000ElementsInReverseOrderIn4Groups() { var counter1 = 10000; var counter2 = 30000; var counter3 = 50000; var counter4 = 70000; var count = 5; var hierarchy = new Hierarchy<int>(100000); Stopwatch timer = new Stopwatch(); timer.Start(); hierarchy.Add(100000, 10000); hierarchy.Add(100000, 30000); hierarchy.Add(100000, 50000); hierarchy.Add(100000, 70000); for (int i = 1; i < 10000; i++) { hierarchy.Add(counter1, --counter1); hierarchy.Add(counter2, --counter2); hierarchy.Add(counter3, --counter3); hierarchy.Add(counter4, --counter4); count += 4; Assert.AreEqual(count, hierarchy.Count, "Count did not increase correctly!"); } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); counter1 = 1; counter2 = 20001; counter3 = 40001; counter4 = 60001; for (int i = 1; i < 10000; i++) { Assert.AreEqual(counter1 + 1, hierarchy.GetParent(counter1), "Parent did not match!"); Assert.AreEqual(counter2 + 1, hierarchy.GetParent(counter2), "Parent did not match!"); Assert.AreEqual(counter3 + 1, hierarchy.GetParent(counter3), "Parent did not match!"); Assert.AreEqual(counter4 + 1, hierarchy.GetParent(counter4), "Parent did not match!"); Assert.AreEqual(counter1, hierarchy.GetChildren(counter1 + 1).FirstOrDefault(), "Children did not match!"); Assert.AreEqual(counter2, hierarchy.GetChildren(counter2 + 1).FirstOrDefault(), "Children did not match!"); Assert.AreEqual(counter3, hierarchy.GetChildren(counter3 + 1).FirstOrDefault(), "Children did not match!"); Assert.AreEqual(counter4, hierarchy.GetChildren(counter4 + 1).FirstOrDefault(), "Children did not match!"); counter1++; counter2++; counter3++; counter4++; } }
public Hierarchy ToHierarchy() { var hierarchy = new Hierarchy(); foreach (var level in this.Levels) { var labelSet = new LabelSet(level.SetName); foreach (var bindableLabel in level.Labels) { var label = new Label(bindableLabel.Index, bindableLabel.Name, bindableLabel.Lower, bindableLabel.Medium, bindableLabel.Upper); labelSet.Add(label); } hierarchy.Add(labelSet); } return hierarchy; }
public static TElement Add <TElement>(this Hierarchy parent, string name = null, string className = null) where TElement : VisualElement, new() { var element = new TElement(); if (name != null) { element.name = name; } if (className != null) { element.AddToClassList(className); } parent.Add(element); return(element); }
public void PerformanceGetChildren_With1ElementWith50000ChildrenInReversedOrder() { var hierarchy = new Hierarchy<int>(-17); for (int i = 50000; i > 0; i--) { hierarchy.Add(-17, i); } var children = Enumerable.Range(1, 50000).Reverse().ToArray(); Stopwatch timer = new Stopwatch(); timer.Start(); CollectionAssert.AreEqual(children, hierarchy.GetChildren(-17).ToArray(), "Children collection did not match!"); timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); }
public void PerformanceContains_With50000LookUpsInReversedOrderWith50000ElementsWith1Parent() { var hierarchy = new Hierarchy <int>(-3); for (int i = 1; i < 50001; i++) { hierarchy.Add(-3, i); } Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 50000; i > 0; i--) { Assert.IsTrue(hierarchy.Contains(i), "Contains method returned wrong value!"); } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 50); }
public void PerformanceGetParent_With50000ElementsWith1ParentInReversedOrder() { var hierarchy = new Hierarchy<int>(0); for (int i = 1; i < 50001; i++) { hierarchy.Add(0, i); } Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 50000; i > 0; i--) { Assert.AreEqual(0, hierarchy.GetParent(i), "Expected parent did not match!"); } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); }
public void PerformanceGetChildren_With1ElementWith50000ChildrenInReversedOrder() { var hierarchy = new Hierarchy <int>(-17); for (int i = 50000; i > 0; i--) { hierarchy.Add(-17, i); } var children = Enumerable.Range(1, 50000).Reverse().ToArray(); Stopwatch timer = new Stopwatch(); timer.Start(); CollectionAssert.AreEqual(children, hierarchy.GetChildren(-17).ToArray(), "Children collection did not match!"); timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); }
public void PerformanceGetParent_With50000ElementsWith1ParentInReversedOrder() { var hierarchy = new Hierarchy <int>(0); for (int i = 1; i < 50001; i++) { hierarchy.Add(0, i); } Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 50000; i > 0; i--) { Assert.AreEqual(0, hierarchy.GetParent(i), "Expected parent did not match!"); } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); }
public void PerformanceGetCommonElements_WithNoCommonElements() { var start1 = 0; var start2 = 100000; var hierarchy = new Hierarchy <int>(start1); var hierarchy2 = new Hierarchy <int>(start2); for (int i = 1; i <= 50000; i++) { hierarchy.Add(start1, i); hierarchy2.Add(start2, start2 + i); } Stopwatch timer = new Stopwatch(); timer.Start(); CollectionAssert.AreEqual(new int[0], hierarchy.GetCommonElements(hierarchy2).ToArray(), "GetCommonElements method returned incorrect collection!"); timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); }
public void PerformanceContains_With25000ExistingAnd25000NonexistantElements() { var hierarchy = new Hierarchy <int>(-1); for (int i = 1; i < 50001; i = i + 2) { hierarchy.Add(i - 2, i); } Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 1; i < 50001; i = i + 2) { Assert.IsTrue(hierarchy.Contains(i), "Contains method returned wrong value!"); Assert.IsFalse(hierarchy.Contains(i + 1), "Contains method returned wrong value!"); } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 50); }
public void PerformanceForEach_With55500ElementsInterconnected() { var start1 = 0; var elements = new List <int>(); elements.Add(start1); var hierarchy = new Hierarchy <int>(start1); for (int i = 1; i <= 51100; i = i + 511) { hierarchy.Add(start1, i); for (int j = i + 1; j <= i + 510; j = j + 51) { hierarchy.Add(i, j); for (int k = j + 1; k <= j + 50; k++) { hierarchy.Add(j, k); } } } for (int i = 1; i <= 51100; i = i + 511) { elements.Add(i); } for (int i = 1; i <= 51100; i = i + 511) { for (int j = i + 1; j <= i + 510; j = j + 51) { elements.Add(j); } } for (int i = 1; i <= 51100; i = i + 511) { for (int j = i + 1; j <= i + 510; j = j + 51) { for (int k = j + 1; k <= j + 50; k++) { elements.Add(k); } } } var counter = 0; Stopwatch timer = new Stopwatch(); timer.Start(); foreach (var element in hierarchy) { Assert.AreEqual(elements[counter++], element, "Expected element did not match!"); } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); Assert.AreEqual(counter, hierarchy.Count, "Incorect number of elements returned!"); }
public void PerformanceRemove_With20000RemovalsIn4GroupsInReverseOrderFromTheMiddleWith40000Elements() { var counter1 = 10000; var counter2 = 20000; var counter3 = 30000; var counter4 = 40000; var hierarchy = new Hierarchy <int>(-2); hierarchy.Add(-2, 10000); hierarchy.Add(-2, 20000); hierarchy.Add(-2, 30000); hierarchy.Add(-2, 40000); for (int i = 1; i < 10000; i++) { hierarchy.Add(counter1, --counter1); hierarchy.Add(counter2, --counter2); hierarchy.Add(counter3, --counter3); hierarchy.Add(counter4, --counter4); } var count = 40001; counter1 = 9000; counter2 = 18000; counter3 = 27000; counter4 = 36000; Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 1; i < 5001; i++) { hierarchy.Remove(counter1--); hierarchy.Remove(counter2--); hierarchy.Remove(counter3--); hierarchy.Remove(counter4--); count -= 4; Assert.AreEqual(count, hierarchy.Count, "Count did not decrease correctly!"); } timer.Stop(); Assert.IsTrue(timer.ElapsedMilliseconds < 200); Assert.AreEqual(20001, hierarchy.Count, "Incorrect count after removal!"); counter1 = 9000; counter2 = 18000; counter3 = 27000; counter4 = 36000; for (int i = 0; i < 5000; i++) { Assert.IsFalse(hierarchy.Contains(counter1--)); Assert.IsFalse(hierarchy.Contains(counter2--)); Assert.IsFalse(hierarchy.Contains(counter3--)); Assert.IsFalse(hierarchy.Contains(counter4--)); } Assert.IsTrue(hierarchy.Contains(-2)); CollectionAssert.AreEqual(new[] { 4000 }, hierarchy.GetChildren(9001).ToArray(), "Children were not correctly reattached!"); CollectionAssert.AreEqual(new[] { 13000 }, hierarchy.GetChildren(18001).ToArray(), "Children were not correctly reattached!"); CollectionAssert.AreEqual(new[] { 22000 }, hierarchy.GetChildren(27001).ToArray(), "Children were not correctly reattached!"); CollectionAssert.AreEqual(new[] { 31000 }, hierarchy.GetChildren(36001).ToArray(), "Children were not correctly reattached!"); }
public void Add_WithSingleElement_ShouldIncrementCountByOne() { Hierarchy.Add(DefaultRootValue, 2); Assert.AreEqual(2, Hierarchy.Count, "Count did not increase correctly!"); }
/// <summary> /// Converts the XML hierarchy of an <see cref="XmlReader"/> into an <see cref="IHierarchy{T}"/>. /// </summary> /// <param name="reader">The reader to convert.</param> /// <returns>An <see cref="IHierarchy{T}"/> implementation that uses <see cref="DataPair"/>.</returns> public static IHierarchy <DataPair> ToHierarchy(this XmlReader reader) { var index = 0; var depthIndexes = new Dictionary <int, Dictionary <int, int> >(); var dimension = 0; IHierarchy <DataPair> hierarchy = new Hierarchy <DataPair>(); List <DataPair> attributes = null; while (reader.Read()) { object typeStrongValue; switch (reader.NodeType) { case XmlNodeType.Attribute: typeStrongValue = ObjectConverter.FromString(reader.Value); attributes.Add(new DataPair(reader.Name, typeStrongValue, typeStrongValue.GetType())); while (reader.MoveToNextAttribute()) { goto case XmlNodeType.Attribute; } var elementIndex = index; foreach (var attribute in attributes) { hierarchy[index].Add(attribute).Data.Add("parent", elementIndex); index++; } reader.MoveToElement(); break; case XmlNodeType.Element: attributes = new List <DataPair>(); if (reader.Depth == 0) { hierarchy.Add(new DataPair(reader.Name, null, typeof(string))).Data.Add(XmlReaderKey, reader.Name); continue; } hierarchy[depthIndexes.GetDepthIndex(reader, index, dimension)].Add(new DataPair(reader.Name, null, typeof(string))).Data.Add(XmlReaderKey, reader.Name); index++; if (reader.HasAttributes) { if (reader.MoveToFirstAttribute()) { goto case XmlNodeType.Attribute; } } break; case XmlNodeType.EndElement: if (reader.Depth == 1) { dimension++; } break; case XmlNodeType.CDATA: case XmlNodeType.Text: var indexToApplyText = hierarchy[index].Data.ContainsKey(XmlReaderKey) ? index : hierarchy[index].Data["parent"].As <int>(); typeStrongValue = ObjectConverter.FromString(reader.Value); hierarchy[indexToApplyText].Replace(new DataPair(hierarchy[indexToApplyText].Data[XmlReaderKey]?.ToString(), typeStrongValue, typeStrongValue.GetType())); hierarchy[indexToApplyText].Data.Remove(XmlReaderKey); break; } } return(hierarchy); }
public void AddHierarchy(TemporalChainOfCommand temporalChainOfCommand) { Hierarchy.Add(temporalChainOfCommand); }