13c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray/*
23c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray * Copyright (C) 2014 The Android Open Source Project
33c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray *
43c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License");
53c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray * you may not use this file except in compliance with the License.
63c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray * You may obtain a copy of the License at
73c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray *
83c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray *      http://www.apache.org/licenses/LICENSE-2.0
93c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray *
103c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray * Unless required by applicable law or agreed to in writing, software
113c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS,
123c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray * See the License for the specific language governing permissions and
143c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray * limitations under the License.
153c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray */
163c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
173c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray/**
183c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray * Common superclass for test cases.
193c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray */
203c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
213c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffrayimport java.util.Arrays;
223c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
233c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffraypublic abstract class TestCase {
243c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertSame(Object expected, Object value) {
253c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (expected != value) {
263c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError("Objects are not the same: expected " +
273c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray          String.valueOf(expected) + ", got " + String.valueOf(value));
283c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
293c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
303c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
313c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertNotSame(Object expected, Object value) {
323c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (expected == value) {
333c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError(
343c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray          "Objects are the same: " + String.valueOf(expected));
353c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
363c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
373c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
383c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertEquals(String message, int expected, int actual) {
393c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (expected != actual) {
403c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError(message);
413c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
423c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
433c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
443c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertEquals(int expected, int actual) {
453c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (expected != actual) {
463c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError("Expected " + expected + " got " + actual);
473c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
483c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
493c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
503c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertTrue(String message, boolean condition) {
513c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (!condition) {
523c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError(message);
533c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
543c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
553c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
563c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertTrue(boolean condition) {
573c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    assertTrue("Expected true", condition);
583c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
593c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
603c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertFalse(String message, boolean condition) {
613c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (condition) {
623c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError(message);
633c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
643c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
653c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
663c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertFalse(boolean condition) {
673c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    assertFalse("Expected false", condition);
683c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
693c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
703c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertEquals(Object expected, Object actual) {
713c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (!expected.equals(actual)) {
723c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      String msg = "Expected \"" + expected + "\" but got \"" + actual + "\"";
733c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError(msg);
743c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
753c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
763c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
773c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertNotEquals(int expected, int actual) {
783c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (expected == actual) {
793c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError("Expected " + expected + " got " + actual);
803c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
813c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
823c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
833c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertNotEquals(Object expected, Object actual) {
843c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (expected.equals(actual)) {
853c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      String msg = "Objects are the same: " + String.valueOf(expected);
863c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError(msg);
873c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
883c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
893c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
903c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static <T> void assertArrayEquals(T[] actual, T... expected) {
913c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      assertTrue(Arrays.equals(expected, actual));
923c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
933c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
943c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertEquals(
953c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      String message, Object expected, Object actual) {
963c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (!expected.equals(actual)) {
973c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError(message);
983c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
993c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1003c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
1013c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertEquals(
1023c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      String message, long expected, long actual) {
1033c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (expected != actual) {
1043c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError(message);
1053c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
1063c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1073c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
1083c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertEquals(long expected, long actual) {
1093c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (expected != actual) {
1103c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError("Expected " + expected + " got " + actual);
1113c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
1123c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1133c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
1143c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertEquals(
1153c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      String message, boolean expected, boolean actual) {
1163c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (expected != actual) {
1173c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError(message);
1183c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
1193c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1203c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
1213c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertEquals(boolean expected, boolean actual) {
1223c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (expected != actual) {
1233c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError("Expected " + expected + " got " + actual);
1243c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
1253c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1263c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
1273c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertEquals(
1283c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      String message, float expected, float actual) {
1293c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (expected != actual) {
1303c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError(message);
1313c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
1323c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1333c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
1343c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertEquals(float expected, float actual) {
1353c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (expected != actual) {
1363c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError("Expected " + expected + " got " + actual);
1373c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
1383c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1393c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
1403c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertEquals(float expected, float actual,
1413c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray                                  float tolerance) {
1423c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if ((actual < expected - tolerance) || (expected + tolerance < actual)) {
1433c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError("Expected " + expected + " got " + actual +
1443c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray          " tolerance " + tolerance);
1453c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
1463c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1473c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
1483c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertEquals(
1493c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      String message, double expected, double actual) {
1503c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (expected != actual) {
1513c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError(message);
1523c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
1533c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1543c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
1553c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertEquals(double expected, double actual) {
1563c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (expected != actual) {
1573c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError("Expected " + expected + " got " + actual);
1583c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
1593c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1603c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
1613c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertEquals(double expected, double actual,
1623c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray                                  double tolerance) {
1633c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if ((actual < expected - tolerance) || (expected + tolerance < actual)) {
1643c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError("Expected " + expected + " got " + actual +
1653c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray          " tolerance " + tolerance);
1663c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
1673c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1683c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
1693c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertSame(
1703c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      String message, Object expected, Object actual) {
1713c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (expected != actual) {
1723c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError(message);
1733c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
1743c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1753c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
1763c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertNull(String message, Object object) {
1773c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (object != null) {
1783c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError(message);
1793c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
1803c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1813c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
1823c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertNull(Object object) {
1833c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    assertNull("Expected null", object);
1843c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1853c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
1863c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertNotNull(String message, Object object) {
1873c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    if (object == null) {
1883c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray      throw new AssertionError(message);
1893c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    }
1903c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1913c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
1923c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void assertNotNull(Object object) {
1933c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    assertNotNull("Expected non-null", object);
1943c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1953c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
1963c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  public static void fail(String msg) {
1973c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray    throw new AssertionError(msg);
1983c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  }
1993c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray}
200