public void Able_accumulate_all_paths() { var factory = new ChainFactory(); var treeHead = factory.Create("0"); var subTreeA = factory.Create("123"); var subTreeB = factory.Create("453"); treeHead.AppendSub(subTreeA); treeHead.AppendSub(subTreeB); var accumulator = new PathAccumulator(); var enumerable = new EnumeratorAccumulatingBranches(treeHead, accumulator); while (enumerable.MoveNext()) { } var expected = new List <string> { "0123", "0453" }; Assert.Equal(2, accumulator.Paths.Count); Assert.Contains(expected[0], accumulator.Paths); Assert.Contains(expected[1], accumulator.Paths); }
public void Should_travesed_path_that_present_all_tree() { var factory = new ChainFactory(); var treeHead = factory.Create("0"); var subTreeA = factory.Create("123"); var subTreeB = factory.Create("453"); treeHead.AppendSub(subTreeA); treeHead.AppendSub(subTreeB); var enumerable = new EnumerableTreeByBFS(treeHead); var buffer = new StringBuilder(); foreach (Node node in enumerable) { buffer.Append(node.Data); } var expectedPath1 = "0142533"; var expectedPath2 = "0415233"; var expected = new List <string> { expectedPath1, expectedPath2 }; Assert.Contains(expected, x => x == buffer.ToString()); }
public void Should_find_node_when_tree_with_branches() { var factory = new ChainFactory(); var treeHead = factory.Create("0"); var subTreeA = factory.Create("123"); var subTreeB = factory.Create("456"); treeHead.AppendSub(subTreeA); treeHead.AppendSub(subTreeB); var traversePathHead = factory.Create("123"); var enumerable = new EnumerableTreeBySpecifiedPath(treeHead, traversePathHead); var enumerator = enumerable.GetEnumerator(); while (enumerator.MoveNext()) { } var expected = subTreeA // 1 .SubNodes.FirstOrDefault() //2 .SubNodes.FirstOrDefault(); //3 var expectedEnumerable = new EnumerableTreeByDFS(expected); Assert.Equal(expected, enumerator.Current); }
public void Add(string pattern) { if (pattern == string.Empty) { return; } var chainFactory = new ChainFactory(); var newChainHead = chainFactory.Create(pattern); if (_heads.Contains(newChainHead)) { var head = _heads.Single(x => x.Data == pattern.First()); var traversePathHead = newChainHead.SubNodes.Single(); var enumerator = new EnumeratorTraversingSpecifiedPath(head, traversePathHead); while (enumerator.MoveNext()) { } if (!enumerator.IsDestinationReached && !enumerator.LastTraversedNode.SubNodes.Contains(enumerator.TraverseRemainder)) { enumerator.LastTraversedNode.AppendSub(enumerator.TraverseRemainder); } } else { _heads.Add(newChainHead); } }
public IEnumerable <string> Find(string searchPattern) { var factory = new ChainFactory(); var targetNode = factory.Create(searchPattern); if (!_heads.Contains(targetNode)) { return(Enumerable.Empty <string>()); } var searchHead = _heads.SingleOrDefault(x => x.Data.Equals(searchPattern.First())); var accumulator = new PathAccumulator(); var enumerator = new EnumeratorAccumulatingBranches(searchHead, accumulator); while (enumerator.MoveNext()) { } var pathEnumerator = new EnumeratorTraversingSpecifiedPath(searchHead, targetNode.SubNodes.LastOrDefault()); while (pathEnumerator.MoveNext()) { } IEnumerable <string> result = accumulator.Paths; if (pathEnumerator.IsDestinationReached) { result = result.Concat(new string[] { searchPattern }); } return(result); }
public void EnterChatMode() { sessionEndFlag = false; chain = ChainFactory.Create <string>(); chatClient.MessageReceived += ChatClient_MessageReceived; Con.WriteLine(Resources.EnterChatMessage(chatClient.ChattingWith)); var message = ""; do { readCancellationSource = new CancellationTokenSource(); message = consoleHelper.CancellableReadLine(readCancellationSource.Token); if (!string.IsNullOrEmpty(message)) { if (message.ToLower() == "show chain") { ShowChain(); } else { SendMessage(message); } } } while (message.ToLower() != "exit chat" && !sessionEndFlag); ExitChatMode(); }
public void Should_return_correct_subnodes_count() { var factory = new ChainFactory(); var patternA = "0123"; var patternB = "456789"; var headA = factory.Create(patternA); var headB = factory.Create(patternB); headA.AppendSub(headB); var treeHeadA = new EnumerableTreeByDFS(headA); var result = treeHeadA.Count(); var expected = 10; Assert.Equal(expected, result); }
public void Should_traverse_all_node_success() { var factory = new ChainFactory(); var headNode = factory.Create("12345"); var subNode = factory.Create("7890"); headNode.AppendSub(subNode); var visited = new List <Node>(); var enumerable = new EnumerableTreeByBFS(headNode); foreach (Node node in enumerable) { visited.Add(node); } Assert.Equal(visited.Count, enumerable.Count()); }
public void Should_traverse_concrete_node_success() { var factory = new ChainFactory(); var startTraversePath = factory.Create("7890"); var headNode = factory.Create("12345"); var subNode1 = factory.Create("7890"); var subNode2 = factory.Create("0543"); headNode.AppendSub(subNode1); headNode.AppendSub(subNode2); var visited = new List <Node>(); var enumerable = new EnumerableTreeBySpecifiedPath(headNode, startTraversePath); var enumerator = enumerable.GetEnumerator(); while (enumerator.MoveNext()) { visited.Add(enumerator.Current); } Assert.True(enumerator.IsDestinationReached); }
public void BasinSingleChainAddAndRestore() { var chain = ChainFactory.Create <string>(); var sampleBlock = new List <string>() { "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "B10", "B11" }; foreach (var item in sampleBlock) { chain.AddBlock(item, chain.CreateBlock(item)); } var res = chain.ViewChain().Select((obj, i) => new { content = obj, index = i }); foreach (var item in res) { Assert.AreEqual(item.content, sampleBlock[item.index]); } }
public void Should_create_tree() { var factory = new ChainFactory(); var treeHead = factory.Create("123678"); var treeTail = treeHead //1 .SubNodes[0] //2 .SubNodes[0] //3 .SubNodes[0] //6 .SubNodes[0] //7 .SubNodes[0]; //8 var expectedHead = '1'; var expectedTail = '8'; Assert.NotNull(treeHead); Assert.Equal(expectedHead, treeHead.Data); Assert.Equal(expectedTail, treeTail.Data); }