package interviewquestions;

import java.util.Iterator;

/**
 * A layman version of junit (just because I don't want to include a dependency)
 */
public abstract class TestCase {
  public static void assertTrue(boolean condition) {
    if (!condition) {
      throw new AssertionError("invalid test");
    }
  }

  public static void assertFalse(boolean condition) {
    if (condition) {
      throw new AssertionError("invalid test");
    }
  }

  public static void assertNull(Object o) {
    if (o != null) {
      throw new AssertionError(String.format("%s is not null", o));
    }
  }

  public static void assertNotNull(Object o) {
    if (o == null) {
      throw new AssertionError("object is null");
    }
  }

  public static void assertEquals(int i1, int i2) {
    if (i1 != i2) {
      throw new AssertionError(String.format("%d != %d", i1, i2));
    }
  }

  public static void assertEquals(long i1, long i2) {
    if (i1 != i2) {
      throw new AssertionError(String.format("%d != %d", i1, i2));
    }
  }

  public static void assertEquals(float i1, float i2) {
    if (i1 != i2) {
      throw new AssertionError(String.format("%.5f != %.5f", i1, i2));
    }
  }

  public static void assertEquals(double i1, double i2) {
    if (i1 != i2) {
      throw new AssertionError(String.format("%.5f != %.5f", i1, i2));
    }
  }

  public static <E> void assertEquals(Iterable<E> i1, Iterable<E> i2) {
    Iterator<E> it1 = i1.iterator();
    Iterator<E> it2 = i2.iterator();

    while (it1.hasNext() && it2.hasNext()) {
      E e1 = it1.next();
      E e2 = it2.next();

      assertEquals(e1, e2);
    }

    assertTrue(!it1.hasNext());
    assertTrue(!it2.hasNext());
  }

  public static void assertEquals(String s1, String s2) {
    if (!s1.equals(s2)) {
      throw new AssertionError(String.format("%s != %s", s1, s2));
    }
  }

  public static void assertEquals(int[] o1, int[] o2) {
    if (o1.length == o2.length) {
      for (int i = 0; i < o1.length; i++) {
        if (o1[i] != o2[i]) {
          throw new AssertionError(String.format(
              "different objects at item %d (%d != %d)", i, o1[i], o2[i]));
        }
      }
    } else {
      throw new AssertionError("different objects");
    }
  }

  public static void assertEquals(long[] o1, long[] o2) {
    if (o1.length == o2.length) {
      for (int i = 0; i < o1.length; i++) {
        if (o1[i] != o2[i]) {
          throw new AssertionError("different objects at item " + i);
        }
      }
    } else {
      throw new AssertionError("different objects");
    }
  }

  public static void assertEquals(double[] o1, double[] o2) {
    if (o1.length == o2.length) {
      for (int i = 0; i < o1.length; i++) {
        if (o1[i] != o2[i]) {
          throw new AssertionError("different objects at item " + i);
        }
      }
    } else {
      throw new AssertionError("different objects");
    }
  }

  public static <E> void assertEquals(E[] o1, E[] o2) {
    if (o1.length == o2.length) {
      for (int i = 0; i < o1.length; i++) {
        if (!o1[i].equals(o2[i])) {
          throw new AssertionError(String.format(
              "different objects at item %d; expected=%s found=%s",
              i, o1[i], o2[i]
          ));
        }
      }
    } else {
      throw new AssertionError("different objects");
    }
  }

  public static void assertEquals(Object o1, Object o2) {
    if (!o1.equals(o2)) {
      throw new AssertionError("different objects: " + o1.toString() + " != " + o2.toString());
    }
  }
}

@@ -2219,32 +2219,419 @@
 s%22);%0A    %7D%0A  %7D%0A%0A
+  public static void assertEquals(char%5B%5D o1, char%5B%5D o2) %7B%0A    if (o1.length == o2.length) %7B%0A      for (int i = 0; i %3C o1.length; i++) %7B%0A        if (o1%5Bi%5D != o2%5Bi%5D) %7B%0A          throw new AssertionError(String.format(%0A              %22different objects at item %25d (%25c != %25c)%22, i, o1%5Bi%5D, o2%5Bi%5D));%0A        %7D%0A      %7D%0A    %7D else %7B%0A      throw new AssertionError(%22different objects%22);%0A    %7D%0A  %7D%0A%0A
   public static 

Allow assertEquals on char arrays too