public void ShouldPassCrossLevelEdge() { var tree = new Dictionary <string, List <string> > { { "A", new List <string>() }, { "B", new List <string> { "A" } }, { "C", new List <string> { "B" } }, { "D", new List <string> { "A", "C" } } }; var expected = new Dictionary <string, List <string> > { { "A", new List <string> { "B", "D" } }, { "B", new List <string> { "C" } }, { "C", new List <string> { "D" } }, { "D", new List <string>() } }; Assert.That(() => CircularityRule.Test(tree), Throws.Nothing); }
/// <summary> /// Performs action on each element. /// </summary> /// <param name="source">elements on </param> /// <param name="action">action to be performed on elements</param> /// <param name="depends">all elements returned by this expresion will be finished before given element</param> /// <param name="threads">maximum threads used except main thread</param> public static void ForEach <T>(this IEnumerable <T> source, Action <T> action, Func <T, IEnumerable <T> > depends, int threads = 1) { var tree = source.ToDictionary(x => x, x => depends(x).ToList()); CircularityRule.Test(tree); tree = DependencyReductionFilter.Filter(tree); var elements = new ConcurrentDictionary <T, bool>(tree.Keys.ToDictionary(x => x, x => false)); elements.Keys.ForEach(x => { action(x); elements[x] = true; }, x => tree[x].All(y => elements[y]), threads); }
public void ShouldPassForwardEdge() { var tree = new Dictionary <string, List <string> > { { "A", new List <string> { "B", "D" } }, { "B", new List <string> { "C" } }, { "C", new List <string> { "D" } }, { "D", new List <string>() } }; Assert.That(() => CircularityRule.Test(tree), Throws.Nothing); }
public void ShouldThrowSelfReference() { var tree = new Dictionary <string, List <string> > { { "A", new List <string> { "B" } }, { "B", new List <string> { "C" } }, { "C", new List <string> { "C" } } }; Assert.That( () => CircularityRule.Test(tree), Throws.Exception.TypeOf <ReferenceException>().With.Message.EqualTo("Circular reference: C.")); }
public void ShouldThrowThatTreeHasNoLeaves() { var tree = new Dictionary <string, List <string> > { { "A", new List <string> { "B" } }, { "B", new List <string> { "C" } }, { "C", new List <string> { "A" } } }; Assert.That( () => CircularityRule.Test(tree), Throws.Exception.TypeOf <ReferenceException>().With.Message.EqualTo("All dependencies are circular.")); }