package com.janosgyerik.tools.algorithm.tree.binarytree;

import org.junit.Test;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import static org.junit.Assert.*;

public class IteratorsTest {

    // example tree from http://en.wikipedia.org/wiki/Tree_traversal
    /*
            F
        B       G
      A   D       I
         C E     H
          */
    private final TreeNode<Character> root;

    public IteratorsTest() {
        root = new TreeNode<>('F');
        root.left = new TreeNode<>('B');
        root.left.left = new TreeNode<>('A');
        root.left.right = new TreeNode<>('D');
        root.left.right.left = new TreeNode<>('C');
        root.left.right.right = new TreeNode<>('E');
        root.right = new TreeNode<>('G');
        root.right.right = new TreeNode<>('I');
        root.right.right.left = new TreeNode<>('H');
    }

    static <T> List<T> iterateToList(Iterator<T> iterator) {
        List<T> list = new LinkedList<>();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }

    @Test
    public void testPreOrderIterator() {
        Iterator<Character> iterator = Iterators.preOrderIterator(root);
        assertEquals(Arrays.asList('F', 'B', 'A', 'D', 'C', 'E', 'G', 'I', 'H'), iterateToList(iterator));
    }

    @Test
    public void testInOrderIterator() {
        Iterator<Character> iterator = Iterators.inOrderIterator(root);
        assertEquals(Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'), iterateToList(iterator));
    }

    @Test
    public void testPostOrderIterator() {
        Iterator<Character> iterator = Iterators.postOrderIterator(root);
        assertEquals(Arrays.asList('A', 'C', 'E', 'D', 'B', 'H', 'I', 'G', 'F'), iterateToList(iterator));
    }

    @Test
    public void testLevelOrderIterator() {
        Iterator<Character> iterator = Iterators.levelOrderIterator(root);
        assertEquals(Arrays.asList('F', 'B', 'G', 'A', 'D', 'I', 'C', 'E', 'H'), iterateToList(iterator));
    }
}

@@ -96,23 +96,19 @@
 til.
-Arrays;
+*;%0A
 %0Aimport 
 java
@@ -107,80 +107,75 @@
 ort 
-java.util.Iterator;%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0A
+static com.janosgyerik.tools.algorithm.tree.binarytree.Iterators.*;
 %0Aimp
@@ -1143,17 +1143,18 @@
 oid test
-P
+_p
 reOrderI
@@ -1204,26 +1204,16 @@
 rator = 
-Iterators.
 preOrder
@@ -1343,32 +1343,73 @@
     %7D%0A%0A    @Test
+(expected = NoSuchElementException.class)
 %0A    public void
@@ -1413,14 +1413,11 @@
 oid 
-testIn
+pre
 Orde
@@ -1417,32 +1417,64 @@
 preOrderIterator
+_should_throw_if_iterated_beyond
 () %7B%0A        Ite
@@ -1501,26 +1501,202 @@
 rator = 
-Iterators.
+preOrderIterator(new TreeNode%3C%3E('A'));%0A        iterator.next();%0A        iterator.next();%0A    %7D%0A%0A    @Test%0A    public void test_inOrderIterator() %7B%0A        Iterator%3CCharacter%3E iterator = 
 inOrderI
@@ -1825,32 +1825,73 @@
     %7D%0A%0A    @Test
+(expected = NoSuchElementException.class)
 %0A    public void
@@ -1895,16 +1895,10 @@
 oid 
-testPost
+in
 Orde
@@ -1898,32 +1898,64 @@
  inOrderIterator
+_should_throw_if_iterated_beyond
 () %7B%0A        Ite
@@ -1982,24 +1982,31 @@
 rator = 
+inOrder
 Iterator
 s.postOr
@@ -1997,18 +1997,188 @@
 Iterator
-s.
+(new TreeNode%3C%3E('A'));%0A        iterator.next();%0A        iterator.next();%0A    %7D%0A%0A    @Test%0A    public void test_postOrderIterator() %7B%0A        Iterator%3CCharacter%3E iterator = 
 postOrde
@@ -2309,32 +2309,73 @@
     %7D%0A%0A    @Test
+(expected = NoSuchElementException.class)
 %0A    public void
@@ -2379,17 +2379,12 @@
 oid 
-testLevel
+post
 Orde
@@ -2384,32 +2384,64 @@
 ostOrderIterator
+_should_throw_if_iterated_beyond
 () %7B%0A        Ite
@@ -2472,148 +2472,587 @@
 r = 
-Iterators.levelOrderIterator(root);%0A        assertEquals(Arrays.asList('F', 'B', 'G', 'A', 'D', 'I', 'C', 'E', 'H'), iterateToList(iterator)
+postOrderIterator(new TreeNode%3C%3E('A'));%0A        iterator.next();%0A        iterator.next();%0A    %7D%0A%0A    @Test%0A    public void test_levelOrderIterator() %7B%0A        Iterator%3CCharacter%3E iterator = levelOrderIterator(root);%0A        assertEquals(Arrays.asList('F', 'B', 'G', 'A', 'D', 'I', 'C', 'E', 'H'), iterateToList(iterator));%0A    %7D%0A%0A    @Test(expected = NoSuchElementException.class)%0A    public void levelOrderIterator_should_throw_if_iterated_beyond() %7B%0A        Iterator%3CCharacter%3E iterator = levelOrderIterator(new TreeNode%3C%3E('A'));%0A        iterator.next();%0A        iterator.next(
 );%0A 

Improve coverage in Iterators