-
Notifications
You must be signed in to change notification settings - Fork 15
/
MoreAsserts.java
90 lines (73 loc) · 2.73 KB
/
MoreAsserts.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package go;
import go.LoadJNI;
import java.util.Arrays;
import java.lang.Math;
import java.lang.reflect.Method;
import java.util.regex.Pattern;
import go.SeqTest;
public class MoreAsserts {
public static void assertTrue(String msg, boolean condition) {
if (!condition) {
throw new RuntimeException(msg);
}
}
public static void assertTrue(boolean condition) {
if (!condition) {
throw new RuntimeException("assert failed");
}
}
public static void assertEquals(int expected, int actual) {
assertTrue(expected == actual);
}
public static void assertFalse(boolean condition) {
assertTrue(!condition);
}
public static void assertFalse(String msg, boolean condition) {
assertTrue(msg, !condition);
}
public static void assertEquals(String msg, int expected, int actual) {
assertTrue(msg, expected == actual);
}
public static void assertEquals(String msg, long expected, long actual) {
assertTrue(msg, expected == actual);
}
public static void assertEquals(String msg, String expected, String actual) {
assertTrue(String.format("%s expected:%s != actual:%s", msg, expected, actual), expected.equals(actual));
}
public static void assertEquals(String msg, boolean expected, boolean actual) {
assertTrue(msg, expected == actual);
}
public static void assertEquals(String msg, byte[] expected, byte[] actual) {
assertTrue(msg, Arrays.equals(expected, actual));
}
public static void assertEquals(String msg, double expected, double actual, double epsilon) {
assertTrue(msg, Math.abs(expected - actual) < epsilon);
}
public static void assertEquals(String msg, Object expected, Object actual) {
assertTrue(msg, (expected == null && actual == null) || (expected.equals(actual)));
}
public static void fail(String msg) {
throw new RuntimeException(msg);
}
public static void main(String[] args) {
SeqTest test = new SeqTest();
Class c = test.getClass();
boolean failed = false;
for (Method method : c.getDeclaredMethods()) {
if (!method.getName().startsWith("test") || !Pattern.matches(args[0], method.getName())) {
continue;
}
System.out.print(method.getName());
try {
method.invoke(test);
System.out.println(" PASS");
} catch (Exception ex) {
System.out.println(" FAIL");
ex.printStackTrace();
failed = true;
}
}
// NOTE: We need to call System.exit to force all go threads to exit.
System.exit(failed ? 1 : 0);
}
}