/*
 * Copyright (C) 2014 Pedro Vicente Gómez Sánchez.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.pedrovgs.problem21;

import com.github.pedrovgs.binarytree.BinaryNode;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * @author Pedro Vicente Gómez Sánchez.
 */
public class LowestCommonAncestorTest {

  private LowestCommonAncestor lca;

  @Before
  public void setUp(){
    lca = new LowestCommonAncestor();
  }

  @Test(expected = IllegalArgumentException.class)
  public void shouldNotAcceptNullElements(){
    lca.get(null,null,null);
  }
  @Test
  public void shouldFindLCAWhenTheLCAIsOneOfTheNodesPassedAsInput(){
    BinaryNode<Integer> root = new BinaryNode<Integer>(1);
    BinaryNode<Integer> n2 = new BinaryNode<Integer>(2);
    BinaryNode<Integer> n3 = new BinaryNode<Integer>(3);
    BinaryNode<Integer> n4 = new BinaryNode<Integer>(4);
    BinaryNode<Integer> n5 = new BinaryNode<Integer>(5);
    BinaryNode<Integer> n6 = new BinaryNode<Integer>(6);
    root.setLeft(n2);
    root.setRight(n3);
    n2.setLeft(n4);
    n2.setRight(n5);
    n4.setLeft(n6);

    BinaryNode<Integer> result = lca.get(root,root,n2);

    assertEquals(n4,result);
  }

  @Test
  public void shouldFindLCAWhenIsAFatherOfBothNodes(){
    BinaryNode<Integer> root = new BinaryNode<Integer>(1);
    BinaryNode<Integer> n2 = new BinaryNode<Integer>(2);
    BinaryNode<Integer> n3 = new BinaryNode<Integer>(3);
    BinaryNode<Integer> n4 = new BinaryNode<Integer>(4);
    BinaryNode<Integer> n5 = new BinaryNode<Integer>(5);
    BinaryNode<Integer> n6 = new BinaryNode<Integer>(6);
    root.setLeft(n2);
    root.setRight(n3);
    n2.setLeft(n4);
    n2.setRight(n5);
    n4.setLeft(n6);

    BinaryNode<Integer> result = lca.get(root,n5,n6);

    assertEquals(n4,result);
  }
}

@@ -925,18 +925,16 @@
  @Before
-%0A 
  public 
@@ -945,16 +945,17 @@
  setUp()
+ 
 %7B%0A    lc
@@ -1041,18 +1041,16 @@
 n.class)
-%0A 
  public 
@@ -1083,16 +1083,17 @@
 ements()
+ 
 %7B%0A    lc
@@ -1101,19 +1101,30 @@
 .get
+Recursive
 (null,
+ 
 null,
+ 
 null
@@ -1130,25 +1130,24 @@
 l);%0A  %7D%0A
+%0A
   @Test
-%0A 
  public 
@@ -1204,16 +1204,17 @@
 sInput()
+ 
 %7B%0A    Bi
@@ -1668,33 +1668,24 @@
   BinaryNode
-%3CInteger%3E
  result = lc
@@ -1693,19 +1693,30 @@
 .get
+Recursive
 (root,
+ 
 root,
+ 
 n2);
@@ -1726,35 +1726,38 @@
    assertEquals(
-n4,
+root, 
 result);%0A  %7D%0A%0A  
@@ -1761,18 +1761,16 @@
 %0A  @Test
-%0A 
  public 
@@ -1813,16 +1813,17 @@
 hNodes()
+ 
 %7B%0A    Bi
@@ -2281,25 +2281,16 @@
 naryNode
-%3CInteger%3E
  result 
@@ -2302,22 +2302,37 @@
 .get
+Recursive
 (root,
+ 
 n5,
+ 
 n6);%0A
+    
 %0A   
@@ -2350,10 +2350,11 @@
 ls(n
-4,
+2, 
 resu

Fix tests for problem 21