public void ComplexOtherTest() { var names = new List <string>(); var set = new GraphSet <string>(); var three = set.Add("3"); three.AddChild("4"); var two2 = three.AddParent("2"); var two = three.AddParent("Two"); two.AddChild("Three"); two.AddParent("One"); var un = two.AddParent("Un"); un.AddChild(two2); var gtf = set.Add("greatfather"); var gtm = set.Add("greatmother"); var gf = gtf.AddChild("grandfather"); gtm.AddChild(gf); var f = gf.AddChild("father"); f.AddChild("me"); var taskGraph = set.ToTaskGraph(x => { OnNames(names, x); }); var tasks = taskGraph.GetNodes().Select(x => x.Value.Task).ToArray(); Assert.Equal(12, tasks.Length); var task = Task.Factory.ContinueWhenAll(tasks, x => { }); task.Wait(); lock (_synchro) { Assert.False(_error, "Names contains " + names.FirstOrDefault()); if (names.Count != 12) { var message = "Names: " + string.Join(", ", names); Assert.False(true, message); } Assert.True(names.IndexOf("3") < names.IndexOf("4")); Assert.True(names.IndexOf("Two") < names.IndexOf("3")); Assert.True(names.IndexOf("One") < names.IndexOf("Two"), "One: " + names.IndexOf("One") + " Two: " + names.IndexOf("Two")); Assert.True(names.IndexOf("Un") < names.IndexOf("Two")); Check.That(names).Contains("One"); Check.That(names).Contains("Two"); Check.That(names).Contains("Three"); Check.That(names).Contains("4"); Check.That(names).Contains("Un"); Check.That(names).Contains("father"); Check.That(names).Contains("me"); Check.That(names).Contains("grandfather"); Check.That(names).Contains("greatfather"); Check.That(names).Contains("greatmother"); } }
public void CompareTest() { var set = new GraphSet <int>(); var second = set.Add(2); var first = set.Add(1); second.AddParent(first); var third = set.Add(3); third.AddParent(second); var comparer = new GraphNodeComparer <int>(); Assert.Equal(-1, comparer.Compare(second, third)); Assert.Equal(1, comparer.Compare(third, first)); }
public void ComplexCase() { var set = new GraphSet <string>(); var three = set.Add("3"); var four = three.AddChild("4"); three.AddParent("2"); var two = three.AddParent("Two"); var one = two.AddParent("One"); two.AddParent("Un"); four.AddParent(one); var names = new List <string>(); var task = set.ToTask(x => { OnNames(names, x); }); task.Wait(); lock (_synchro) { Assert.Equal(6, names.Count); Assert.True(names.IndexOf("3") < names.IndexOf("4")); Assert.True(names.IndexOf("Two") < names.IndexOf("3")); Assert.True(names.IndexOf("2") < names.IndexOf("3")); Assert.True(names.IndexOf("One") < names.IndexOf("Two")); Assert.True(names.IndexOf("Un") < names.IndexOf("Two")); } }
private static List <GraphNode <string> > BuildList(int start, int end) { var list = new List <GraphNode <string> >(); var set = new GraphSet <string>(); var last = set.Add(end.ToString()); list.Add(last); for (var i = end - 1; i > start - 1; i--) { var newNode = set.Add(i.ToString()); list.Add(newNode); last.AddParent(newNode); last = newNode; } return(list); }
public void BuildGraphTest() { var set = new GraphSet <string>(); var child = set.Add("child"); Assert.Equal(0, child.Children.Count); Assert.Equal(0, child.Parents.Count); var root = set.Add("root"); child.AddParent(root); Assert.Equal(0, child.Children.Count); Assert.Equal(1, child.Parents.Count); Assert.Equal(root, child.Parents.FirstOrDefault()); Assert.Equal(1, root.Children.Count); Assert.Equal(child, root.Children.FirstOrDefault()); }
public void ContinueOnLeavesThenContinueWithTest() { var names = new List <string>(); var set = new GraphSet <string>(); var three = set.Add("3"); three.AddChild("4"); var two2 = three.AddParent("2"); var two = three.AddParent("Two"); two.AddChild("Three"); two.AddParent("One"); var un = two.AddParent("Un"); un.AddChild(two2); var gtf = set.Add("greatfather"); var gtm = set.Add("greatmother"); var gf = gtf.AddChild("grandfather"); gtm.AddChild(gf); var f = gf.AddChild("father"); f.AddChild("me"); var taskGraph = set.ToTaskGraph(x => { OnNames(names, x); }); var taskLeaves = taskGraph.ContinueOnLeavesWith(OnLeaves); taskLeaves.GetWhenAllTask().Wait(); var taskCombined = taskGraph.ContinueWith(x => { OnRemoveNames(names, x); }); var task = taskCombined.GetWhenAllTask(); task.Wait(); //Thread.Sleep(1000); lock (_synchro) { Assert.False(_error, "Names contains " + names.FirstOrDefault()); Assert.Equal(0, names.Count); Assert.True(_removeNames.IndexOf("3") < _removeNames.IndexOf("4")); Assert.True(_removeNames.IndexOf("Two") < _removeNames.IndexOf("3")); Assert.True(_removeNames.IndexOf("One") < _removeNames.IndexOf("Two")); Assert.True(_removeNames.IndexOf("Un") < _removeNames.IndexOf("Two")); } }
public void FilterTest() { var set = new GraphSet <int>(); var three = set.Add(3); var two = set.Add(2); var four = two.AddChild(4); var eight = two.AddChild(8); four.AddChild(eight); three.AddChild(9); two.AddChild(three.AddChild(6)); var set2 = set.Filter(x => x % 2 == 0); Assert.False(set2.Any(x => x.Value == 3)); Assert.False(set2.Any(x => x.Children.Any(c => c.Value == 3))); Assert.False(set2.Any(x => x.Parents.Any(p => p.Value == 3))); Assert.False(set2.Any(x => x.Value == 9)); Assert.True(set2.All(x => x.Value % 2 == 0)); Assert.Equal(4, set2.Count()); }
public void TestIsCyclic() { var set = new GraphSet <string>(); var three = set.Add("3"); var four = three.AddParent("4"); Assert.False(set.IsCyclic()); three.AddChild(four); Assert.True(set.IsCyclic()); four.AddParent("5"); Assert.True(set.IsCyclic()); }
public void TestComplexIsNotCyclic() { var set = new GraphSet <string>(); var three = set.Add("3"); var two = three.AddParent("2"); var one = three.AddParent("1"); Assert.False(set.IsCyclic()); var root = two.AddParent("root"); one.AddParent(root); Assert.False(set.IsCyclic()); }
public void Test() { var set = new GraphSet <int>(); var node = set.Add(3); Check.That(set.IsCyclic()).IsFalse(); Check.That(node).IsNotNull(); Check.That(node.Value).IsEqualTo(3); var nodes = set.GetNodes().ToList(); Check.That(nodes.Count).IsEqualTo(1); Check.That(nodes.Select(x => x.Value).FirstOrDefault()).IsEqualTo(3); }
public void UseCase() { var set = new GraphSet <int>(); var node = set.Add(3); var two = node.AddParent(2); two.AddParent(1); var task = set.ToTask(OnNode); task.Wait(); Assert.Equal(1, _values[0]); Assert.Equal(2, _values[1]); Assert.Equal(3, _values[2]); }
public void SortListTest() { var set = new GraphSet <int>(); var list = new List <GraphNode <int> >(); var last = set.Add(100); list.Add(last); for (var i = 99; i > -1; i--) { var newNode = set.Add(i); list.Add(newNode); last.AddParent(newNode); last = newNode; } list.Reverse(20, 30); list.Reverse(40, 30); var comparer = new GraphNodeComparer <int>(); list.Sort(comparer); for (var i = 0; i <= 100; i++) { Assert.Equal(i, list[i].Value); } }
public void TestToGraphSet() { var set = new GraphSet <string>(); var three = set.Add("3"); var four = three.AddChild("4"); three.AddParent("2"); var two = three.AddParent("Two"); var one = two.AddParent("One"); two.AddParent("Un"); four.AddParent(one); var nodes = set.GetNodes(); Assert.Equal(6, nodes.Count()); Assert.False(set.IsCyclic()); }
public void ContinueWithTest() { var names = new List <string>(); var set = new GraphSet <string>(); var three = set.Add("3"); three.AddChild("4"); var two2 = three.AddParent("2"); var two = three.AddParent("Two"); two.AddParent("One"); var un = two.AddParent("Un"); un.AddChild(two2); var taskGraph = set.ToTaskGraph(x => { OnNames(names, x); }); var taskCombined = taskGraph.ContinueWith(x => { OnRemoveNames(names, x); }); var task = taskCombined.GetWhenAllTask(); task.Wait(); Assert.Equal(0, names.Count); Assert.False(_error); }