-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestValid.java
59 lines (48 loc) · 2.49 KB
/
TestValid.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
import java.util.Arrays;
import static java.util.Collections.sort;
// reminder: use `java -ea or -enableassertions` to execute
public class TestValid {
public static void main(String [] args) {
// primitive types
//assert 0.inc().equals(inc(0)); // can't parse this: ambiguous
assert (0).inc() == inc(0);
assert 0f.incf() == incf(0f);
assert true.invert() == invert(true);
// boxed types
assert Integer.valueOf(0).incBoxed().equals(incBoxed(Integer.valueOf(0)));
assert Float.valueOf(0).incfBoxed().equals(incfBoxed(Float.valueOf(0)));
assert Boolean.TRUE.invertBoxed().equals(invertBoxed(Boolean.TRUE));
// mixed boxed and unboxed
assert true.invertReturnBoxed().equals(invertReturnBoxed(true));
// string literals
assert "hello".duplicate().equals(duplicate("hello"));
assert "hello".duplicate(true).equals(duplicate("hello", true));
assert "hello".duplicate().toUpperCase().equals(duplicate("hello").toUpperCase());
assert "hello".toUpperCase().duplicate().equals(duplicate("hello".toUpperCase()));
assert "hello".duplicate().duplicate().equals(duplicate(duplicate("hello")));
// variables
String hello = "hello";
assert hello.duplicate().equals(duplicate(hello));
assert hello.duplicate(true).equals(duplicate(hello, true));
assert hello.duplicate().toUpperCase().equals(duplicate(hello).toUpperCase());
assert hello.toUpperCase().duplicate().equals(duplicate(hello.toUpperCase()));
assert hello.duplicate().duplicate().equals(duplicate(duplicate(hello)));
// static imports
sort(Arrays.asList(3, 2, 1));
Arrays.asList(3, 2, 1).sort();
// don't transform [primitive].class
boolean.class.equals(boolean.class);
Boolean.class.equals(Boolean.class);
}
public static int inc(int i) { return i + 1; }
public static float incf(float f) { return f + 1.0f; }
public static boolean invert(boolean b) { return !b; }
public static Boolean invertReturnBoxed(boolean b) { return !b; }
public static Integer incBoxed(Integer i) { return i + 1; }
public static Float incfBoxed(Float f) { return f + 1.0f; }
public static Boolean invertBoxed(Boolean b) { return !b; }
public static String duplicate(String x) { return duplicate(x, false); }
public static String duplicate(String x, boolean spaces) {
return spaces ? (x + " " + x) : (x + x);
}
}