-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathCaptureTest.java
More file actions
66 lines (50 loc) · 1.81 KB
/
CaptureTest.java
File metadata and controls
66 lines (50 loc) · 1.81 KB
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
package net.openjdk.lambda;
import org.junit.Test;
import java.util.Comparator;
import java.util.function.Function;
import java.util.function.Predicate;
import static org.junit.Assert.*;
public class CaptureTest {
@Test
public void testLegacy() {
final int minus_one = -1;
final int zero = 0;
final int one = 1;
Comparator<Integer> cmp = new Comparator<Integer>() {
@Override
public int compare(Integer x, Integer y) {
return (x < y) ? minus_one : ((x > y) ? one : zero);
}
};
assertEquals(0, cmp.compare(0, 0));
assertEquals(-1, cmp.compare(-100, 100));
assertEquals(1, cmp.compare(100, -100));
}
@Test
public void testLambda() {
int minus_one = -1;
int zero = 0;
int one = 1;
Comparator<Integer> cmp = (x, y) -> (x < y) ? minus_one : ((x > y) ? one : zero);
assertEquals(0, cmp.compare(0, 0));
assertEquals(-1, cmp.compare(-100, 100));
assertEquals(1, cmp.compare(100, -100));
}
private Predicate<String> makeCaseUnsensitiveMatcher(String pattern) {
return s -> pattern.equalsIgnoreCase(s);
}
@Test
public void testPredicate1() {
// Predicate<T> ~ boolean test(T t);
assertTrue(makeCaseUnsensitiveMatcher("true").test("TruE"));
assertTrue(makeCaseUnsensitiveMatcher("false").test("FalsE"));
assertFalse(makeCaseUnsensitiveMatcher("true").test("FalsE"));
}
@Test
public void testPredicate2() {
Function<String, Predicate<String>> matcher = s1 -> s2 -> s1.equalsIgnoreCase(s2);
assertTrue(matcher.apply("true").test("TruE"));
assertTrue(matcher.apply("false").test("FalsE"));
assertFalse(matcher.apply("true").test("FalsE"));
}
}