1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * Common superclass for test cases.
19 */
20
21import java.util.Arrays;
22
23public abstract class TestCase {
24  public static void assertSame(Object expected, Object value) {
25    if (expected != value) {
26      throw new AssertionError("Objects are not the same: expected " +
27          String.valueOf(expected) + ", got " + String.valueOf(value));
28    }
29  }
30
31  public static void assertNotSame(Object expected, Object value) {
32    if (expected == value) {
33      throw new AssertionError(
34          "Objects are the same: " + String.valueOf(expected));
35    }
36  }
37
38  public static void assertEquals(String message, int expected, int actual) {
39    if (expected != actual) {
40      throw new AssertionError(message);
41    }
42  }
43
44  public static void assertEquals(int expected, int actual) {
45    if (expected != actual) {
46      throw new AssertionError("Expected " + expected + " got " + actual);
47    }
48  }
49
50  public static void assertTrue(String message, boolean condition) {
51    if (!condition) {
52      throw new AssertionError(message);
53    }
54  }
55
56  public static void assertTrue(boolean condition) {
57    assertTrue("Expected true", condition);
58  }
59
60  public static void assertFalse(String message, boolean condition) {
61    if (condition) {
62      throw new AssertionError(message);
63    }
64  }
65
66  public static void assertFalse(boolean condition) {
67    assertFalse("Expected false", condition);
68  }
69
70  public static void assertEquals(Object expected, Object actual) {
71    if (!expected.equals(actual)) {
72      String msg = "Expected \"" + expected + "\" but got \"" + actual + "\"";
73      throw new AssertionError(msg);
74    }
75  }
76
77  public static void assertNotEquals(int expected, int actual) {
78    if (expected == actual) {
79      throw new AssertionError("Expected " + expected + " got " + actual);
80    }
81  }
82
83  public static void assertNotEquals(Object expected, Object actual) {
84    if (expected.equals(actual)) {
85      String msg = "Objects are the same: " + String.valueOf(expected);
86      throw new AssertionError(msg);
87    }
88  }
89
90  public static <T> void assertArrayEquals(T[] actual, T... expected) {
91      assertTrue(Arrays.equals(expected, actual));
92  }
93
94  public static void assertEquals(
95      String message, Object expected, Object actual) {
96    if (!expected.equals(actual)) {
97      throw new AssertionError(message);
98    }
99  }
100
101  public static void assertEquals(
102      String message, long expected, long actual) {
103    if (expected != actual) {
104      throw new AssertionError(message);
105    }
106  }
107
108  public static void assertEquals(long expected, long actual) {
109    if (expected != actual) {
110      throw new AssertionError("Expected " + expected + " got " + actual);
111    }
112  }
113
114  public static void assertEquals(
115      String message, boolean expected, boolean actual) {
116    if (expected != actual) {
117      throw new AssertionError(message);
118    }
119  }
120
121  public static void assertEquals(boolean expected, boolean actual) {
122    if (expected != actual) {
123      throw new AssertionError("Expected " + expected + " got " + actual);
124    }
125  }
126
127  public static void assertEquals(
128      String message, float expected, float actual) {
129    if (expected != actual) {
130      throw new AssertionError(message);
131    }
132  }
133
134  public static void assertEquals(float expected, float actual) {
135    if (expected != actual) {
136      throw new AssertionError("Expected " + expected + " got " + actual);
137    }
138  }
139
140  public static void assertEquals(float expected, float actual,
141                                  float tolerance) {
142    if ((actual < expected - tolerance) || (expected + tolerance < actual)) {
143      throw new AssertionError("Expected " + expected + " got " + actual +
144          " tolerance " + tolerance);
145    }
146  }
147
148  public static void assertEquals(
149      String message, double expected, double actual) {
150    if (expected != actual) {
151      throw new AssertionError(message);
152    }
153  }
154
155  public static void assertEquals(double expected, double actual) {
156    if (expected != actual) {
157      throw new AssertionError("Expected " + expected + " got " + actual);
158    }
159  }
160
161  public static void assertEquals(double expected, double actual,
162                                  double tolerance) {
163    if ((actual < expected - tolerance) || (expected + tolerance < actual)) {
164      throw new AssertionError("Expected " + expected + " got " + actual +
165          " tolerance " + tolerance);
166    }
167  }
168
169  public static void assertSame(
170      String message, Object expected, Object actual) {
171    if (expected != actual) {
172      throw new AssertionError(message);
173    }
174  }
175
176  public static void assertNull(String message, Object object) {
177    if (object != null) {
178      throw new AssertionError(message);
179    }
180  }
181
182  public static void assertNull(Object object) {
183    assertNull("Expected null", object);
184  }
185
186  public static void assertNotNull(String message, Object object) {
187    if (object == null) {
188      throw new AssertionError(message);
189    }
190  }
191
192  public static void assertNotNull(Object object) {
193    assertNotNull("Expected non-null", object);
194  }
195
196  public static void fail(String msg) {
197    throw new AssertionError(msg);
198  }
199}
200