public void TestCompositeBreadthFirstIterator() { var iterator = new CompositeIterator <int>(_composite); var expected = new int[] { 1, 2, 3, 4, 5 }; Assert.AreEqual(expected, IterateToArray(iterator)); }
private T[] IterateToArray <T>(CompositeIterator <T> iterator) { var actual = new List <T>(); while (iterator.MoveNext()) { actual.Add(iterator.Current.element); } return(actual.ToArray()); }
public void CompositeIteratorTest() { #region 准备测试数据 // Stack<T> 堆栈 Stack <ObjectWithName> stack = new Stack <ObjectWithName>(); stack.Push(new ObjectWithName("2")); stack.Push(new ObjectWithName("1")); // Queue<T> 队列 Queue <ObjectWithName> queue = new Queue <ObjectWithName>(); queue.Enqueue(new ObjectWithName("3")); queue.Enqueue(new ObjectWithName("4")); // T[] 数组 ObjectWithName[] array = new ObjectWithName[3]; array[0] = new ObjectWithName("5"); array[1] = new ObjectWithName("6"); array[2] = new ObjectWithName("7"); #region BinaryTree 二叉树 BinaryTreeNode root = new BinaryTreeNode("8"); root.Left = new BinaryTreeNode("9"); root.Right = new BinaryTreeNode("10"); root.Right.Left = new BinaryTreeNode("11"); root.Right.Left.Left = new BinaryTreeNode("12"); root.Right.Left.Right = new BinaryTreeNode("13"); root.Right.Right = new BinaryTreeNode("14"); root.Right.Right.Right = new BinaryTreeNode("15"); root.Right.Right.Right.Right = new BinaryTreeNode("16"); #endregion // 合并所有 IEnumerator 对象 CompositeIterator iterator = new CompositeIterator(); iterator.Add(stack); iterator.Add(queue); iterator.Add(array); iterator.Add(root); #endregion int count = 0; foreach (ObjectWithName obj in iterator) { Assert.AreEqual <string>((++count).ToString(), obj.ToString()); } /** * 无论您所面对的对象内部结构多么复杂,如果您的任务是封装它们,而不只是把它们全部public了, * 则尽可能地提供一个Iterator,可以的话按照需要使用的领域各提供一个Iterator。 */ }
public static void Start(MenuComponent component) { var componentIterator = component.CreateIterator(); CompositeIterator iterator = new CompositeIterator(componentIterator); while (iterator.MoveNext()) { MenuComponent innerComponent = iterator.Current as MenuComponent; } }
public void CompositeData() { Stack <ObjectWithName> stack = new Stack <ObjectWithName>(); stack.Push(new ObjectWithName("2")); stack.Push(new ObjectWithName("1")); // Queue Queue <ObjectWithName> queue = new Queue <ObjectWithName>(); queue.Enqueue(new ObjectWithName("3")); queue.Enqueue(new ObjectWithName("4")); // T ObjectWithName[] array = new ObjectWithName[3]; array[0] = new ObjectWithName("5"); array[1] = new ObjectWithName("6"); array[2] = new ObjectWithName("7"); // BinaryTree BinaryTreeNode root = new BinaryTreeNode("8"); root.Left = new BinaryTreeNode("9"); root.Right = new BinaryTreeNode("10"); root.Right.Left = new BinaryTreeNode("11"); root.Right.Left.Left = new BinaryTreeNode("12"); root.Right.Left.Right = new BinaryTreeNode("13"); root.Right.Right = new BinaryTreeNode("14"); root.Right.Right.Right = new BinaryTreeNode("15"); root.Right.Right.Left = new BinaryTreeNode("16"); // 合并所有 IEnumerator 对象 CompositeIterator iterator = new CompositeIterator(); iterator.Add(stack); iterator.Add(queue); iterator.Add(array); iterator.Add(root); List <string> logs = new List <string>(); foreach (ObjectWithName item in iterator) { var s = item.ToString(); logs.Add(s); } var length = logs.Count; }
public void Test() { #region 准备测试数据 // stack<T> Stack <ObjectWithName> stack = new Stack <ObjectWithName>(); stack.Push(new ObjectWithName("2")); stack.Push(new ObjectWithName("1")); // Queue<T> Queue <ObjectWithName> queue = new Queue <ObjectWithName>(); queue.Enqueue(new ObjectWithName("3")); queue.Enqueue(new ObjectWithName("4")); // T[] ObjectWithName[] array = new ObjectWithName[3]; array[0] = new ObjectWithName("5"); array[1] = new ObjectWithName("6"); array[2] = new ObjectWithName("7"); // BinaryTree BinaryTreeNode root = new BinaryTreeNode("8"); root.Left = new BinaryTreeNode("9"); root.Right = new BinaryTreeNode("10"); root.Right.Left = new BinaryTreeNode("11"); root.Right.Left.Left = new BinaryTreeNode("12"); root.Right.Left.Right = new BinaryTreeNode("13"); root.Right.Right = new BinaryTreeNode("14"); root.Right.Right.Right = new BinaryTreeNode("15"); root.Right.Right.Right.Right = new BinaryTreeNode("16"); // 合并所有 IEnumerator 对象 CompositeIterator iterator = new CompositeIterator(); iterator.Add(stack); iterator.Add(queue); iterator.Add(array); iterator.Add(root); #endregion int count = 0; foreach (ObjectWithName obj in iterator) { Assert.AreEqual <string>((++count).ToString(), obj.ToString()); } System.Diagnostics.Trace.WriteLine(count); }
public void DoFormatMail(Mail mail) { StringBuilder stringBuilder = new StringBuilder(); AE.Net.Mail.MailMessage mailMessage = new AE.Net.Mail.MailMessage(); AE.Net.Mail.Attachment asdf = new AE.Net.Mail.Attachment(); //MailTemplate pull CompositeIterator iterator = (CompositeIterator)mail.CreateIterator(); MailTemplate mailTemplate = (MailTemplate)iterator.First(); //AttachmentBox pull iterator = (CompositeIterator)mailTemplate.CreateIterator(); Attachments attachmentBox = (Attachments)iterator.First(); //Attachments pull iterator = (CompositeIterator)attachmentBox.CreateIterator(); while (!iterator.IsDone()) { models.components.Attachment attachment = (models.components.Attachment)iterator.CurrentItem(); mailMessage.Attachments.Add(new AE.Net.Mail.Attachment { Body = attachment.GetFileDirectory() }); } mailMessage.To.Add(new MailAddress(mail.TargetMail)); mailMessage.Subject = mailTemplate.Subject; mailMessage.Body = mailTemplate.Body; var msgStr = new StringWriter(); mailMessage.Save(msgStr); mail.FormatedMail = msgStr.ToString(); }
public void TestInitialize() { ComponentCollection collection = new ComponentCollection(); IMenuComponent allMenus = new Menu("All menus", "All menus combined!"); IMenuComponent pancakeMenu = new Menu("Pancake house menu", "Breakfast"); IMenuComponent dinerMenu = new Menu("Dinner menu", "Lunch"); IMenuComponent cafeMenu = new Menu("Cafe menu", "Dinner"); IMenuComponent desertMenu = new Menu("Dessert menu", "Dessert of course!"); allMenus.Add(pancakeMenu); allMenus.Add(dinerMenu); pancakeMenu.Add(new MenuItem("Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true, 2.99)); pancakeMenu.Add(new MenuItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false, 2.99)); pancakeMenu.Add(new MenuItem("Blueberry Pancakes", "Pancakes made with fresh blueberries", true, 3.49)); pancakeMenu.Add(new MenuItem("Waffles", "Waffles, with your choice of blueberries or strawberries", true, 3.59)); dinerMenu.Add(new MenuItem("Vegetarian BLT", "(Fakin’) Bacon with lettuce & tomato on whole wheat", true, 2.99)); dinerMenu.Add(new MenuItem("BLT", "Bacon with lettuce & tomato on whole wheat", false, 2.99)); dinerMenu.Add(new MenuItem("Soup of the day", "Soup of the day, with a side of potato salad", false, 3.29)); dinerMenu.Add(new MenuItem("Hotdog", "A hot dog, with saurkraut, relish, onions, topped with cheese", false, 3.05)); desertMenu.Add(new MenuItem("Lava cake", "it's a coffee", true, 2.00)); desertMenu.Add(new MenuItem("Ice cream", "it's a coffee", true, 1.80)); dinerMenu.Add(desertMenu); cafeMenu.Add(new MenuItem("Espresso", "it's a coffee", true, 1.40)); cafeMenu.Add(new MenuItem("Cappuccino", "it's a coffee", true, 1.80)); allMenus.Add(cafeMenu); collection.Add(allMenus); this.iterator = new CompositeIterator(collection.GetIterator()); }
public void TestCleanup() { this.iterator = null; }
public void SetUp() { _iterator = new CompositeIterator<int>( CreateImpl(1, 2), CreateImpl(3, 4, 5), CreateImpl(6, 7, 8, 9)); /* * 0: 1 3 6 * 1: 2 3 6 * 2: 1 4 6 * 3: 2 4 6 * 4: 1 5 6 * 5: 2 5 6 * 6: 1 3 7 * 7: 2 3 7 * 8: 1 4 7 * 9: 2 4 7 * 10: 1 5 7 * 11: 2 5 7 * 12: 1 3 8 * 13: 2 3 8 * 14: 1 4 8 * 15: 2 4 8 * 16: 1 5 8 * 17: 2 5 8 * 18: 1 3 9 * 19: 2 3 9 * 20: 1 4 9 * 21: 2 4 9 * 22: 1 5 9 * 23: 2 5 9 * */ }