diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8503686 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +# Generated by http://gitignore.io + +### IntelliJ ### +*.iml +*.ipr +*.iws +.idea/ + +### Java ### +*.class + +# Package Files # +*.jar +*.war +*.ear + +### Maven ### +target/ diff --git a/README b/README deleted file mode 100644 index 3a46807..0000000 --- a/README +++ /dev/null @@ -1,28 +0,0 @@ -JDK8 Lambda Samples --------------------------------------------------------------------------- - -**** DISCLAIMER: **** -**** At the present moment, Project Lambda is in heavy development. **** -**** These tests may fail to compile, fail to run, or fail the **** -**** assertions due to syntactic and semantic changes in JDK. **** - -One more time time: - THESE TESTS REFLECT THE CURRENT STATE OF LAMBDA, AND NOT THE FINAL JDK8 - -These tests were used on local JUGs to highlight different things about -upcoming Project Lambda (support for lambda expressions in Java 8). The -tests are by no means extensive or complete, and really are the collateral. - -The general discussion about the implementation of Project Lambda: - http://mail.openjdk.java.net/mailman/listinfo/lambda-dev - -JSR335 EG observers lists: - http://mail.openjdk.java.net/mailman/listinfo/lambda-libs-spec-observers - http://mail.openjdk.java.net/mailman/listinfo/lambda-spec-observers - -Binary builds for JDK8 with Lambda support: - http://jdk8.java.net/lambda - -General information about the project: - http://openjdk.java.net/projects/lambda/ - diff --git a/README.md b/README.md new file mode 100644 index 0000000..b7ed57d --- /dev/null +++ b/README.md @@ -0,0 +1,51 @@ +## JDK8 Lambda Samples +originally created by [@shipilev](https://github.com/shipilev), [repo](https://github.com/shipilev/jdk8-lambda-samples) + +#### short notes: + +| class | test | description | +|---|---|---| +| LambdaTest | testCmpLegacy/testCmpLambda0 | Comparators: lambdas and anonymous classes +||testCmpLambda1| (int x)-> +||testOneArg| ()->() +||testOneArg1 & 2| (x)-> aka x-> +||testCmpLambda2 & 3| Integer bigger than 127 +||testConsumer | Consumer in .forEach() +|UroborosTest| \* | chain call +|CaptureTest|testLegacy | final fields in anonymous class +||testLambda | *effective final* with lambda +||testPredicate1 & 2| Predicates with context +|ScopingTest|testScoping1| different **this** for anonymous classes and lambdas +|FibonacciTest| test10 | capture **this** but not field +|| test10_static| without capture, just link to static method +|MethodRefTest| testMethodRefStatic| methodRef to static method (as prev) +||testMethodRefInstance0 & 1 & 2 | *bound* MethodRef +||testMethodRefInstanceUnbound | *unbound* MethodRef (add **this**) +||testConstructor | call **new** +||testConstructor0 | constructor w/o parameters +||testConstructor1 | constructor w/ parameters +|ZamTest| \* | Replace lambdas to MethodRef +|ThreadLocalTest|threadLocalLegacy| legacy variant +||threadLocalLambda| added method withInitial(Supplier) +||threadLocalMRef | via MethodRef +| ▼ Advanced ▼ +|NoiseSampleTest| +|WeirdFunctionTest| + +###Default Methods + +| class | test | description | +|---|---|---| +|DefaultTest| \* | backward compatibility +|GuavaTest| testTreeMultiset | usage with third party libraries +|StaticInterfaceTest| test | functional interface with static method + +###Streams (Bulk Operations for Collections) + +| class | test | description | +|---|---|---| +|ExternalInternalTest| \* | +||►|highlight the difference between internal and external iteration +||►|iteration is explicitly serial +||►|libraries have more headroom optimizing with internal iteration +||►|sometimes even forEach is not enough: chaining the action within the serial again \ No newline at end of file diff --git a/pom.xml b/pom.xml index bdfb5b8..5fd761f 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,7 @@ junit junit - 4.8.1 + 4.11 test diff --git a/src/test/java/net/openjdk/defaults/DefaultTest.java b/src/test/java/net/openjdk/defaults/DefaultTest.java index bc685a7..2ce4552 100644 --- a/src/test/java/net/openjdk/defaults/DefaultTest.java +++ b/src/test/java/net/openjdk/defaults/DefaultTest.java @@ -1,25 +1,17 @@ package net.openjdk.defaults; -import junit.framework.Assert; import org.junit.Test; -public class DefaultTest { - - @Test - public void testNew() { - Assert.assertEquals("Legacy Method from the New Class", new NewClass().legacyMethod()); - Assert.assertEquals("New Method from the New Class", new NewClass().newMethod()); - } +import static org.junit.Assert.assertEquals; - @Test - public void testLegacy() { - Assert.assertEquals("Legacy Method from the Legacy Class", new LegacyClass().legacyMethod()); - Assert.assertEquals("The beauty is in the eye of the defender", new LegacyClass().newMethod()); - } +public class DefaultTest { public interface LegacyInterface { String legacyMethod(); - default String newMethod() { return "The beauty is in the eye of the defender"; } + + default String newMethod() { + return "The beauty is in the eye of the defender"; + } } public class LegacyClass implements LegacyInterface { @@ -41,4 +33,16 @@ public String newMethod() { } } + @Test + public void testLegacy() { + assertEquals("Legacy Method from the Legacy Class", new LegacyClass().legacyMethod()); + assertEquals("The beauty is in the eye of the defender", new LegacyClass().newMethod()); + } + + @Test + public void testNew() { + assertEquals("Legacy Method from the New Class", new NewClass().legacyMethod()); + assertEquals("New Method from the New Class", new NewClass().newMethod()); + } + } diff --git a/src/test/java/net/openjdk/defaults/GuavaTest.java b/src/test/java/net/openjdk/defaults/GuavaTest.java index 9be56ef..ec612b9 100644 --- a/src/test/java/net/openjdk/defaults/GuavaTest.java +++ b/src/test/java/net/openjdk/defaults/GuavaTest.java @@ -1,9 +1,10 @@ package net.openjdk.defaults; import com.google.common.collect.TreeMultiset; -import junit.framework.Assert; import org.junit.Test; +import static org.junit.Assert.*; + public class GuavaTest { @Test @@ -14,7 +15,7 @@ public void testTreeMultiset() { strings.add("Baz"); String result = strings.stream().reduce((s1, s2) -> s1 + ", " + s2).get(); - Assert.assertEquals("Bar, Baz, Foo", result); + assertEquals("Bar, Baz, Foo", result); } } diff --git a/src/test/java/net/openjdk/defaults/StaticInterfaceTest.java b/src/test/java/net/openjdk/defaults/StaticInterfaceTest.java index 972714b..9adeafd 100644 --- a/src/test/java/net/openjdk/defaults/StaticInterfaceTest.java +++ b/src/test/java/net/openjdk/defaults/StaticInterfaceTest.java @@ -1,15 +1,14 @@ package net.openjdk.defaults; -import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; +import static org.junit.Assert.*; + public class StaticInterfaceTest { @Test public void test() { - // FIXME: does not run with current public builds - Assert.assertEquals("toDublin", TicketOffice.defaultOffice().qDublin()); + assertEquals("toDublin", TicketOffice.defaultOffice().qDublin()); } public interface TicketOffice { diff --git a/src/test/java/net/openjdk/lambda/CaptureTest.java b/src/test/java/net/openjdk/lambda/CaptureTest.java index a0a866a..9ef934b 100644 --- a/src/test/java/net/openjdk/lambda/CaptureTest.java +++ b/src/test/java/net/openjdk/lambda/CaptureTest.java @@ -1,18 +1,20 @@ package net.openjdk.lambda; -import junit.framework.Assert; 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; + final int one = 1; Comparator cmp = new Comparator() { @Override @@ -22,36 +24,43 @@ public int compare(Integer x, Integer y) { }; - Assert.assertEquals(0, cmp.compare(0, 0)); - Assert.assertEquals(-1, cmp.compare(-100, 100)); - Assert.assertEquals(1, cmp.compare(100, -100)); + 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; + int one = 1; Comparator cmp = (x, y) -> (x < y) ? minus_one : ((x > y) ? one : zero); - Assert.assertEquals(0, cmp.compare(0, 0)); - Assert.assertEquals(-1, cmp.compare(-100, 100)); - Assert.assertEquals(1, cmp.compare(100, -100)); + assertEquals(0, cmp.compare(0, 0)); + assertEquals(-1, cmp.compare(-100, 100)); + assertEquals(1, cmp.compare(100, -100)); } - private Predicate makeCaseUnsensitiveMatcher(String pattern) { return s -> pattern.equalsIgnoreCase(s); } @Test - public void testLambda1() { + public void testPredicate1() { // Predicate ~ boolean test(T t); - Assert.assertTrue( makeCaseUnsensitiveMatcher("true").test("TruE") ); - Assert.assertTrue( makeCaseUnsensitiveMatcher("false").test("FalsE") ); - Assert.assertFalse(makeCaseUnsensitiveMatcher("true").test("FalsE") ); + assertTrue(makeCaseUnsensitiveMatcher("true").test("TruE")); + assertTrue(makeCaseUnsensitiveMatcher("false").test("FalsE")); + assertFalse(makeCaseUnsensitiveMatcher("true").test("FalsE")); + } + + @Test + public void testPredicate2() { + Function> matcher = s1 -> s2 -> s1.equalsIgnoreCase(s2); + assertTrue(matcher.apply("true").test("TruE")); + assertTrue(matcher.apply("false").test("FalsE")); + assertFalse(matcher.apply("true").test("FalsE")); } } diff --git a/src/test/java/net/openjdk/lambda/FibonacciTest.java b/src/test/java/net/openjdk/lambda/FibonacciTest.java index 577f838..8137b77 100644 --- a/src/test/java/net/openjdk/lambda/FibonacciTest.java +++ b/src/test/java/net/openjdk/lambda/FibonacciTest.java @@ -1,27 +1,28 @@ package net.openjdk.lambda; -import junit.framework.Assert; import org.junit.Test; import java.util.function.IntUnaryOperator; +import static org.junit.Assert.*; + public class FibonacciTest { // Фибоначчийн тоог хүн төрөлхтөн өөрсдийн ахуй (source: Wikipedia MN) - private IntUnaryOperator fib = - (n) -> (n < 2) ? n : fib.applyAsInt(n - 1) + fib.applyAsInt(n - 2); + private IntUnaryOperator fib; + {fib = (n) -> (n < 2) ? n : fib.applyAsInt(n - 1) + fib.applyAsInt(n - 2);} @Test public void test10() { - Assert.assertEquals(55, fib.applyAsInt(10)); + assertEquals(55, fib.applyAsInt(10)); } - private static IntUnaryOperator fib_static = - (n) -> (n < 2) ? n : fib_static.applyAsInt(n - 1) + fib_static.applyAsInt(n - 2); + private static IntUnaryOperator fib_static; + static {fib_static = (n) -> (n < 2) ? n : fib_static.applyAsInt(n - 1) + fib_static.applyAsInt(n - 2);} @Test public void test10_static() { - Assert.assertEquals(55, fib_static.applyAsInt(10)); + assertEquals(55, fib_static.applyAsInt(10)); } // @Test @@ -29,7 +30,7 @@ public void test10_static() { // IntUnaryOperator fib = // (n) -> (n < 2) ? n : fib.operate(n - 1) + fib.operate(n - 2); // -// Assert.assertEquals(55, fib.operateAsInt(10)); +// assertEquals(55, fib.operateAsInt(10)); // } } diff --git a/src/test/java/net/openjdk/lambda/LambdaTest.java b/src/test/java/net/openjdk/lambda/LambdaTest.java index 76626f3..04872dd 100644 --- a/src/test/java/net/openjdk/lambda/LambdaTest.java +++ b/src/test/java/net/openjdk/lambda/LambdaTest.java @@ -1,6 +1,5 @@ package net.openjdk.lambda; -import junit.framework.Assert; import org.junit.Test; import java.util.Arrays; @@ -9,6 +8,8 @@ import java.util.function.Function; import java.util.function.Supplier; +import static org.junit.Assert.*; + public class LambdaTest { @Test @@ -21,65 +22,65 @@ public int compare(Integer x, Integer y) { }; - Assert.assertEquals(0, cmp.compare(0, 0)); - Assert.assertEquals(-1, cmp.compare(-100, 100)); - Assert.assertEquals(1, cmp.compare(100, -100)); + assertEquals(0, cmp.compare(0, 0)); + assertEquals(-1, cmp.compare(-100, 100)); + assertEquals(1, cmp.compare(100, -100)); } @Test public void testCmpLambda0() { Comparator cmp = (x, y) -> (x < y) ? -1 : ((x > y) ? 1 : 0); - Assert.assertEquals(0, cmp.compare(0, 0)); - Assert.assertEquals(-1, cmp.compare(-100, 100)); - Assert.assertEquals(1, cmp.compare(100, -100)); + assertEquals(0, cmp.compare(0, 0)); + assertEquals(-1, cmp.compare(-100, 100)); + assertEquals(1, cmp.compare(100, -100)); } @Test public void testCmpLambda1() { Comparator cmp = (Integer x, Integer y) -> (x < y) ? -1 : ((x > y) ? 1 : 0); - Assert.assertEquals(0, cmp.compare(0, 0)); - Assert.assertEquals(-1, cmp.compare(-100, 100)); - Assert.assertEquals(1, cmp.compare(100, -100)); + assertEquals(0, cmp.compare(0, 0)); + assertEquals(-1, cmp.compare(-100, 100)); + assertEquals(1, cmp.compare(100, -100)); } @Test public void testNoArgs() { // Supplier ~ T get(); Supplier ultimateAnswerFactory = () -> 42; - Assert.assertTrue(42 == ultimateAnswerFactory.get()); + assertTrue(42 == ultimateAnswerFactory.get()); } @Test public void testOneArg0() { // Function ~ R apply(T t); Function f = (String s) -> Integer.parseInt(s); - Assert.assertEquals(Integer.valueOf(0), f.apply("0")); - Assert.assertEquals(Integer.valueOf(1), f.apply("1")); + assertEquals(Integer.valueOf(0), f.apply("0")); + assertEquals(Integer.valueOf(1), f.apply("1")); } @Test public void testOneArg1() { Function f = (s) -> Integer.parseInt(s); - Assert.assertEquals(Integer.valueOf(0), f.apply("0")); - Assert.assertEquals(Integer.valueOf(1), f.apply("1")); + assertEquals(Integer.valueOf(0), f.apply("0")); + assertEquals(Integer.valueOf(1), f.apply("1")); } @Test public void testOneArg2() { Function f = s -> Integer.parseInt(s); - Assert.assertEquals(Integer.valueOf(0), f.apply("0")); - Assert.assertEquals(Integer.valueOf(1), f.apply("1")); + assertEquals(Integer.valueOf(0), f.apply("0")); + assertEquals(Integer.valueOf(1), f.apply("1")); } @Test public void testCmpLambda2() { // wrong Comparator cmp = (x, y) -> (x < y) ? -1 : ((x == y) ? 0 : 1); - Assert.assertEquals(0, cmp.compare(100, 100)); - Assert.assertEquals(-1, cmp.compare(0, 100)); - Assert.assertEquals(1, cmp.compare(100, -100)); + assertEquals(0, cmp.compare(100, 100)); + assertEquals(-1, cmp.compare(0, 100)); + assertEquals(1, cmp.compare(100, -100)); } @Test @@ -90,22 +91,16 @@ public void testCmpLambda3() { // right return (x < y) ? -1 : ((x == y) ? 0 : 1); }; - Assert.assertEquals(0, cmp.compare(1000, 1000)); - Assert.assertEquals(-1, cmp.compare(0, 100)); - Assert.assertEquals(1, cmp.compare(100, -100)); + assertEquals(0, cmp.compare(1000, 1000)); + assertEquals(-1, cmp.compare(0, 100)); + assertEquals(1, cmp.compare(100, -100)); } @Test - public void testBlock0() { + public void testConsumer() { // Consumer ~ void accept(T t); Consumer b = s -> { System.out.println(s);}; Arrays.asList("Foo", "Bar", "Baz", "Baz", "Foo", "Bar").forEach(b); } - @Test - public void testBlock1() { - Consumer b = s -> System.out.println(s); - Arrays.asList("Foo", "Bar", "Baz", "Baz", "Foo", "Bar").forEach(b); - } - } diff --git a/src/test/java/net/openjdk/lambda/MethodRefTest.java b/src/test/java/net/openjdk/lambda/MethodRefTest.java index 76ca0a1..3eb2845 100644 --- a/src/test/java/net/openjdk/lambda/MethodRefTest.java +++ b/src/test/java/net/openjdk/lambda/MethodRefTest.java @@ -1,6 +1,5 @@ package net.openjdk.lambda; -import junit.framework.Assert; import org.junit.Test; import java.util.Arrays; @@ -10,15 +9,17 @@ import java.util.function.Function; import java.util.function.Predicate; +import static org.junit.Assert.*; + public class MethodRefTest { @Test public void testMethodRefStatic() { Comparator cmp = Integer::compare; - Assert.assertEquals(0, cmp.compare(0, 0)); - Assert.assertEquals(-1, cmp.compare(-100, 100)); - Assert.assertEquals(1, cmp.compare(100, -100)); + assertEquals(0, cmp.compare(0, 0)); + assertEquals(-1, cmp.compare(-100, 100)); + assertEquals(1, cmp.compare(100, -100)); } @Test @@ -35,17 +36,17 @@ private Predicate makeCaseUnsensitiveMatcher(String pattern) { @Test public void testMethodRefInstance1() { // Predicate ~ boolean test(T t); - Assert.assertTrue( makeCaseUnsensitiveMatcher("true").test("TruE") ); - Assert.assertTrue( makeCaseUnsensitiveMatcher("false").test("FalsE") ); - Assert.assertFalse(makeCaseUnsensitiveMatcher("true").test("FalsE") ); + assertTrue( makeCaseUnsensitiveMatcher("true").test("TruE") ); + assertTrue( makeCaseUnsensitiveMatcher("false").test("FalsE") ); + assertFalse(makeCaseUnsensitiveMatcher("true").test("FalsE") ); } @Test public void testMethodRefInstance2() { // Predicate ~ boolean test(T t); Predicate isTrue = "true"::equalsIgnoreCase; - Assert.assertTrue(isTrue.test("TruE")); - Assert.assertFalse(isTrue.test("FalsE")); + assertTrue(isTrue.test("TruE")); + assertFalse(isTrue.test("FalsE")); } @Test @@ -54,24 +55,16 @@ public void testMethodRefInstanceUnbound() { // Integer ~ int compareTo(Integer anotherInteger) Comparator cmp = Integer::compareTo; - Assert.assertEquals(0, cmp.compare(0, 0)); - Assert.assertEquals(-1, cmp.compare(-100, 100)); - Assert.assertEquals(1, cmp.compare(100, -100)); - } - - - @Test - public void testParseInt(){ - Function f = Integer::parseInt; - Assert.assertEquals(Integer.valueOf(0), f.apply("0")); - Assert.assertEquals(Integer.valueOf(1), f.apply("1")); + assertEquals(0, cmp.compare(0, 0)); + assertEquals(-1, cmp.compare(-100, 100)); + assertEquals(1, cmp.compare(100, -100)); } @Test public void testConstructor(){ Function f = Integer::new; - Assert.assertEquals(Integer.valueOf(0), f.apply("0")); - Assert.assertEquals(Integer.valueOf(1), f.apply("1")); + assertEquals(Integer.valueOf(0), f.apply("0")); + assertEquals(Integer.valueOf(1), f.apply("1")); } public static class Counter { @@ -86,11 +79,11 @@ public Counter(int count) { private int count = 0; - public int inc() { + public int incCount() { return ++count; } - public int get() { + public int getCount() { return count; } } @@ -98,15 +91,15 @@ public int get() { @Test public void testConstructor0(){ Supplier f = Counter::new; - Assert.assertEquals(0, f.get().get()); - Assert.assertEquals(1, f.get().inc()); + assertEquals(0, f.get().getCount()); + assertEquals(1, f.get().incCount()); } @Test public void testConstructor1(){ Function f = Counter::new; - Assert.assertEquals(1, f.apply(1).get()); - Assert.assertEquals(42, f.apply(42).get()); + assertEquals(1, f.apply(1).getCount()); + assertEquals(42, f.apply(42).getCount()); } } diff --git a/src/test/java/net/openjdk/lambda/NoiseSampleTest.java b/src/test/java/net/openjdk/lambda/NoiseSampleTest.java index de19a3c..1ad83c0 100644 --- a/src/test/java/net/openjdk/lambda/NoiseSampleTest.java +++ b/src/test/java/net/openjdk/lambda/NoiseSampleTest.java @@ -1,12 +1,13 @@ package net.openjdk.lambda; -import junit.framework.Assert; import org.junit.Test; import java.util.HashMap; import java.util.Map; import java.util.function.Supplier; +import static org.junit.Assert.*; + public class NoiseSampleTest { /** @@ -85,8 +86,8 @@ public Counter get() { } ); - Assert.assertEquals(1, map.get("foo").get("bar").inc()); - Assert.assertEquals(2, map.get("foo").get("bar").inc()); + assertEquals(1, map.get("foo").get("bar").inc()); + assertEquals(2, map.get("foo").get("bar").inc()); } @Test @@ -94,8 +95,8 @@ public void testL0() { Map> map = new ComputeMap>(() -> new ComputeMap<>(() -> new Counter())); - Assert.assertEquals(1, map.get("foo").get("bar").inc()); - Assert.assertEquals(2, map.get("foo").get("bar").inc()); + assertEquals(1, map.get("foo").get("bar").inc()); + assertEquals(2, map.get("foo").get("bar").inc()); } @Test @@ -103,8 +104,8 @@ public void testL1() { Supplier> mapFactory = () -> new ComputeMap<>(() -> new Counter()); Map> map = new ComputeMap<>(mapFactory); - Assert.assertEquals(1, map.get("foo").get("bar").inc()); - Assert.assertEquals(2, map.get("foo").get("bar").inc()); + assertEquals(1, map.get("foo").get("bar").inc()); + assertEquals(2, map.get("foo").get("bar").inc()); } @Test @@ -112,8 +113,8 @@ public void testL2() { Map> map = new ComputeMap<>((Supplier>) () -> new ComputeMap<>(() -> new Counter())); - Assert.assertEquals(1, map.get("foo").get("bar").inc()); - Assert.assertEquals(2, map.get("foo").get("bar").inc()); + assertEquals(1, map.get("foo").get("bar").inc()); + assertEquals(2, map.get("foo").get("bar").inc()); } @Test @@ -121,8 +122,8 @@ public void testR() { Map> map = new ComputeMap>(() -> new ComputeMap<>(Counter::new)); - Assert.assertEquals(1, map.get("foo").get("bar").inc()); - Assert.assertEquals(2, map.get("foo").get("bar").inc()); + assertEquals(1, map.get("foo").get("bar").inc()); + assertEquals(2, map.get("foo").get("bar").inc()); } } diff --git a/src/test/java/net/openjdk/lambda/ScopingTest.java b/src/test/java/net/openjdk/lambda/ScopingTest.java new file mode 100644 index 0000000..8912869 --- /dev/null +++ b/src/test/java/net/openjdk/lambda/ScopingTest.java @@ -0,0 +1,54 @@ +package net.openjdk.lambda; + +import org.junit.Test; + +import java.util.function.Supplier; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNot.not; +import static org.junit.Assert.assertThat; + +public class ScopingTest { + + class MasterClassAnonymous { + + Supplier sa1 = new Supplier() { + @Override + public String get() { + System.out.println("this.toString() = " + this.toString()); + return this.toString(); + } + }; + + Supplier sa2 = new Supplier() { + @Override + public String get() { + System.out.println("toString() = " + toString()); + return toString(); + } + }; + + public String toString() { + return "I am a Master!"; + } + } + + class MasterClassLambda { + Supplier sl1 = () -> this.toString(); + Supplier sl2 = () -> toString(); + + public String toString() { + return "I am a Master!"; + } + } + + @Test + public void testScoping1() throws Exception { + assertThat(new MasterClassAnonymous().sa1.get(), is(not("I am a Master!"))); + assertThat(new MasterClassAnonymous().sa2.get(), is(not("I am a Master!"))); + + assertThat(new MasterClassLambda().sl1.get(), is("I am a Master!")); + assertThat(new MasterClassLambda().sl2.get(), is("I am a Master!")); + } + +} diff --git a/src/test/java/net/openjdk/lambda/ThreadLocalTest.java b/src/test/java/net/openjdk/lambda/ThreadLocalTest.java index 0acd672..92297b8 100644 --- a/src/test/java/net/openjdk/lambda/ThreadLocalTest.java +++ b/src/test/java/net/openjdk/lambda/ThreadLocalTest.java @@ -1,39 +1,40 @@ package net.openjdk.lambda; -import junit.framework.Assert; import org.junit.Test; import java.util.concurrent.atomic.AtomicInteger; +import static org.junit.Assert.*; + public class ThreadLocalTest { @Test public void threadLocalLegacy(){ - final AtomicInteger counter = new AtomicInteger(0); + final AtomicInteger counter = new AtomicInteger(0); //final isn't necessary ThreadLocal tlNumber = new ThreadLocal() { @Override protected Integer initialValue() { return counter.incrementAndGet(); } }; - Assert.assertEquals(tlNumber.get(), (Integer)1); - Assert.assertEquals(tlNumber.get(), (Integer)1); + assertEquals(tlNumber.get(), (Integer)1); + assertEquals(tlNumber.get(), (Integer)1); } @Test public void threadLocalLambda() { AtomicInteger counter = new AtomicInteger(0); ThreadLocal tlNumber = ThreadLocal.withInitial(() -> counter.incrementAndGet()); - Assert.assertEquals(tlNumber.get(), (Integer)1); - Assert.assertEquals(tlNumber.get(), (Integer)1); + assertEquals(tlNumber.get(), (Integer)1); + assertEquals(tlNumber.get(), (Integer)1); } @Test public void threadLocalMRef() { AtomicInteger counter = new AtomicInteger(0); ThreadLocal tlNumber = ThreadLocal.withInitial(counter::incrementAndGet); - Assert.assertEquals(tlNumber.get(), (Integer)1); - Assert.assertEquals(tlNumber.get(), (Integer)1); + assertEquals(tlNumber.get(), (Integer)1); + assertEquals(tlNumber.get(), (Integer)1); } } diff --git a/src/test/java/net/openjdk/lambda/UroborosTest.java b/src/test/java/net/openjdk/lambda/UroborosTest.java index f50f93d..e0b9850 100644 --- a/src/test/java/net/openjdk/lambda/UroborosTest.java +++ b/src/test/java/net/openjdk/lambda/UroborosTest.java @@ -1,8 +1,9 @@ package net.openjdk.lambda; -import junit.framework.Assert; import org.junit.Test; +import static org.junit.Assert.*; + public class UroborosTest { interface Uroboros { @@ -19,7 +20,7 @@ public void test() { chain = chain.bite(); } - Assert.assertEquals(10, depth); + assertEquals(10, depth); } @Test @@ -38,7 +39,7 @@ public void testStructured() { chain = chain.bite(); } - Assert.assertEquals(7, depth); + assertEquals(7, depth); } } \ No newline at end of file diff --git a/src/test/java/net/openjdk/lambda/WeirdFunctionTest.java b/src/test/java/net/openjdk/lambda/WeirdFunctionTest.java index 63669f9..3d1637d 100644 --- a/src/test/java/net/openjdk/lambda/WeirdFunctionTest.java +++ b/src/test/java/net/openjdk/lambda/WeirdFunctionTest.java @@ -1,10 +1,11 @@ package net.openjdk.lambda; -import junit.framework.Assert; import org.junit.Test; import java.util.function.Function; +import static org.junit.Assert.*; + public class WeirdFunctionTest { @Test @@ -20,17 +21,17 @@ public void test1() { Function f = ((Integer)0)::compareTo; f = f.compose(f); - Assert.assertEquals(Integer.valueOf(0), f.apply(0)); - Assert.assertEquals(Integer.valueOf(1), f.apply(100)); - Assert.assertEquals(Integer.valueOf(-1), f.apply(-100)); + assertEquals(Integer.valueOf(0), f.apply(0)); + assertEquals(Integer.valueOf(1), f.apply(100)); + assertEquals(Integer.valueOf(-1), f.apply(-100)); } @Test public void test2() { Function f = ((Function)((Integer)0)::compareTo).compose(((Integer)0)::compareTo); - Assert.assertEquals(Integer.valueOf(0), f.apply(0)); - Assert.assertEquals(Integer.valueOf(1), f.apply(100)); - Assert.assertEquals(Integer.valueOf(-1), f.apply(-100)); + assertEquals(Integer.valueOf(0), f.apply(0)); + assertEquals(Integer.valueOf(1), f.apply(100)); + assertEquals(Integer.valueOf(-1), f.apply(-100)); } } diff --git a/src/test/java/net/openjdk/lambda/ZamTest.java b/src/test/java/net/openjdk/lambda/ZamTest.java index d42f440..66c32c5 100644 --- a/src/test/java/net/openjdk/lambda/ZamTest.java +++ b/src/test/java/net/openjdk/lambda/ZamTest.java @@ -1,6 +1,5 @@ package net.openjdk.lambda; -import junit.framework.Assert; import org.junit.Test; import java.io.ByteArrayInputStream; @@ -15,38 +14,40 @@ import java.util.NavigableSet; import java.util.TreeSet; +import static org.junit.Assert.*; + public class ZamTest { @Test public void testUnsignedComparator() { Comparator cmp = (x, y) -> Integer.compareUnsigned(x, y); - Assert.assertEquals(0, cmp.compare(0, 0)); - Assert.assertEquals(1, cmp.compare(-100, 100)); - Assert.assertEquals(-1, cmp.compare(100, -100)); + assertEquals(0, cmp.compare(0, 0)); + assertEquals(1, cmp.compare(-100, 100)); + assertEquals(-1, cmp.compare(100, -100)); } @Test public void testUnsignedComparatorMRef() { Comparator cmp = Integer::compareUnsigned; - Assert.assertEquals(0, cmp.compare(0, 0)); - Assert.assertEquals(1, cmp.compare(-100, 100)); - Assert.assertEquals(-1, cmp.compare(100, -100)); + assertEquals(0, cmp.compare(0, 0)); + assertEquals(1, cmp.compare(-100, 100)); + assertEquals(-1, cmp.compare(100, -100)); } @Test public void testUnsignedTreeSet() { NavigableSet set = new TreeSet<>((x, y) -> Integer.compareUnsigned(x, y)); set.addAll(Arrays.asList(-100, 0, 100)); - Assert.assertEquals(0, set.first().intValue()); - Assert.assertEquals(-100, set.last().intValue()); + assertEquals(0, set.first().intValue()); + assertEquals(-100, set.last().intValue()); } @Test public void testUnsignedTreeSetMRef() { NavigableSet set = new TreeSet<>(Integer::compareUnsigned); set.addAll(Arrays.asList(-100, 0, 100)); - Assert.assertEquals(0, set.first().intValue()); - Assert.assertEquals(-100, set.last().intValue()); + assertEquals(0, set.first().intValue()); + assertEquals(-100, set.last().intValue()); } @@ -54,10 +55,10 @@ public void testUnsignedTreeSetMRef() { public void testUnsignedTreeSetNotSerializable() { NavigableSet set = new TreeSet<>((x, y) -> Integer.compareUnsigned(x, y)); set.addAll(Arrays.asList(-100, 0, 100)); - Assert.assertEquals(0, set.first().intValue()); - Assert.assertEquals(-100, set.last().intValue()); + assertEquals(0, set.first().intValue()); + assertEquals(-100, set.last().intValue()); byte[] serializedSet = writeSetToBytes(set, false); - Assert.assertEquals(null, serializedSet ); + assertEquals(null, serializedSet ); } @@ -65,28 +66,28 @@ public void testUnsignedTreeSetNotSerializable() { public void testUnsignedTreeSetSerializable() { NavigableSet set = new TreeSet<>((Comparator & Serializable) ((x, y) -> Integer.compareUnsigned(x, y))); set.addAll(Arrays.asList(-100, 0, 100)); - Assert.assertEquals(0, set.first().intValue()); - Assert.assertEquals(-100, set.last().intValue()); + assertEquals(0, set.first().intValue()); + assertEquals(-100, set.last().intValue()); byte[] serializedSet = writeSetToBytes(set, true); NavigableSet set1 = readSetFromBytes(serializedSet); - Assert.assertEquals(0, set1.first().intValue()); - Assert.assertEquals(-100, set1.last().intValue()); - Assert.assertEquals(set, set1); + assertEquals(0, set1.first().intValue()); + assertEquals(-100, set1.last().intValue()); + assertEquals(set, set1); } @Test public void testUnsignedTreeSetSerializableMRef() { NavigableSet set = new TreeSet<>((Comparator & Serializable) (Integer::compareUnsigned)); set.addAll(Arrays.asList(-100, 0, 100)); - Assert.assertEquals(0, set.first().intValue()); - Assert.assertEquals(-100, set.last().intValue()); + assertEquals(0, set.first().intValue()); + assertEquals(-100, set.last().intValue()); byte[] serializedSet = writeSetToBytes(set, true); NavigableSet set1 = readSetFromBytes(serializedSet); - Assert.assertEquals(0, set1.first().intValue()); - Assert.assertEquals(-100, set1.last().intValue()); - Assert.assertEquals(set, set1); + assertEquals(0, set1.first().intValue()); + assertEquals(-100, set1.last().intValue()); + assertEquals(set, set1); } @@ -96,7 +97,7 @@ private NavigableSet readSetFromBytes(byte[] serializedSet) { ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedSet)); return (NavigableSet) ois.readObject(); } catch (ClassNotFoundException | IOException e) { - Assert.fail(); + fail(); } return null; } @@ -110,11 +111,11 @@ private byte[] writeSetToBytes(NavigableSet set, boolean shouldFailIfNo oos.close(); } catch (NotSerializableException e) { if(shouldFailIfNotSerializable) { - Assert.fail(); + fail(); } return null; } catch (IOException e) { - Assert.fail(); + fail(); } return baos.toByteArray(); } diff --git a/src/test/java/net/openjdk/streams/CollectorsTest.java b/src/test/java/net/openjdk/streams/CollectorsTest.java index 38c0b77..af97153 100644 --- a/src/test/java/net/openjdk/streams/CollectorsTest.java +++ b/src/test/java/net/openjdk/streams/CollectorsTest.java @@ -1,6 +1,5 @@ package net.openjdk.streams; -import junit.framework.Assert; import org.junit.Test; import java.util.ArrayList; @@ -10,18 +9,20 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; +import static org.junit.Assert.*; + public class CollectorsTest { @Test public void test1() { List result = IntStream.range(0, 1000).boxed().collect(Collectors.toCollection(ArrayList::new)); - Assert.assertEquals(1000, result.size()); + assertEquals(1000, result.size()); } @Test public void test2() { List result = IntStream.range(0, 1000).boxed().collect(Collectors.toList()); - Assert.assertEquals(1000, result.size()); + assertEquals(1000, result.size()); } @Test @@ -34,10 +35,11 @@ public void test3() { (x) -> x % 3 ) ); - Assert.assertEquals(1000, map.size()); - Assert.assertEquals(Integer.valueOf(0), map.get(111)); + assertEquals(1000, map.size()); + assertEquals(Integer.valueOf(0), map.get(111)); } + /* @Test public void test4() { Map map = @@ -50,7 +52,7 @@ public void test4() { Collectors.lastWinsMerger() ) ); - Assert.assertEquals(42, map.size()); + assertEquals(42, map.size()); } - + */ } diff --git a/src/test/java/net/openjdk/streams/ExternalInternalTest.java b/src/test/java/net/openjdk/streams/ExternalInternalTest.java index 50ed77b..864a5c0 100644 --- a/src/test/java/net/openjdk/streams/ExternalInternalTest.java +++ b/src/test/java/net/openjdk/streams/ExternalInternalTest.java @@ -1,11 +1,12 @@ package net.openjdk.streams; -import junit.framework.Assert; import org.junit.Test; import java.util.Arrays; import java.util.List; +import static org.junit.Assert.*; + public class ExternalInternalTest { @Test @@ -17,7 +18,7 @@ public void testExternal() { sb.append(s); } - Assert.assertEquals( + assertEquals( "FooBarBaz", sb.toString() ); @@ -30,7 +31,7 @@ public void testInternal() { List strings = Arrays.asList("Foo", "Bar", "Baz"); strings.forEach(sb::append); - Assert.assertEquals( + assertEquals( "FooBarBaz", sb.toString() ); diff --git a/src/test/java/net/openjdk/streams/LazyEagerTest.java b/src/test/java/net/openjdk/streams/LazyEagerTest.java index 63565b0..5f98fa9 100644 --- a/src/test/java/net/openjdk/streams/LazyEagerTest.java +++ b/src/test/java/net/openjdk/streams/LazyEagerTest.java @@ -1,16 +1,15 @@ package net.openjdk.streams; -import junit.framework.Assert; -import org.junit.Ignore; import org.junit.Test; -import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.junit.Assert.*; + public class LazyEagerTest { private int invocations = 0; @@ -23,14 +22,14 @@ public void testLazy() { Iterator i = stream.iterator(); - Assert.assertEquals("Foo", i.next()); - Assert.assertEquals(1, invocations); + assertEquals("Foo", i.next()); + assertEquals(1, invocations); - Assert.assertEquals("Bar", i.next()); - Assert.assertEquals(3, invocations); + assertEquals("Bar", i.next()); + assertEquals(3, invocations); - Assert.assertEquals("Baz", i.next()); - Assert.assertEquals(5, invocations); + assertEquals("Baz", i.next()); + assertEquals(5, invocations); } @Test @@ -40,8 +39,8 @@ public void testEager() { .filter((s) -> { invocations++; return s.length() == 3; }) .collect(Collectors.toList()); - Assert.assertEquals(3, list.size()); - Assert.assertEquals(5, invocations); + assertEquals(3, list.size()); + assertEquals(5, invocations); } } diff --git a/src/test/java/net/openjdk/streams/SeqParTest.java b/src/test/java/net/openjdk/streams/SeqParTest.java index e6faedc..9d2baba 100644 --- a/src/test/java/net/openjdk/streams/SeqParTest.java +++ b/src/test/java/net/openjdk/streams/SeqParTest.java @@ -1,13 +1,13 @@ package net.openjdk.streams; -import junit.framework.Assert; import org.junit.Before; import org.junit.Test; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Optional; + +import static org.junit.Assert.*; public class SeqParTest { @@ -26,7 +26,7 @@ public void setup() { @Test public void testSeq() { - Assert.assertEquals( + assertEquals( Integer.valueOf(COUNT), list.stream().reduce(Math::max).get() ); @@ -34,7 +34,7 @@ public void testSeq() { @Test public void testPar() { - Assert.assertEquals( + assertEquals( Integer.valueOf(COUNT), list.parallelStream().reduce(Math::max).get() ); diff --git a/src/test/java/net/openjdk/streams/ShortCircuitTest.java b/src/test/java/net/openjdk/streams/ShortCircuitTest.java index ecfe561..c003bfd 100644 --- a/src/test/java/net/openjdk/streams/ShortCircuitTest.java +++ b/src/test/java/net/openjdk/streams/ShortCircuitTest.java @@ -1,10 +1,10 @@ package net.openjdk.streams; -import org.junit.Assert; import org.junit.Test; import java.util.stream.IntStream; -import java.util.stream.Streams; + +import static org.junit.Assert.*; public class ShortCircuitTest { @@ -16,8 +16,8 @@ public void test01() { int first = IntStream.range(1, 1000) .filter(x -> { invocations++; return (x % 42) == 0; }) .findFirst().getAsInt(); - Assert.assertEquals(42, first); - Assert.assertEquals(42, invocations); + assertEquals(42, first); + assertEquals(42, invocations); } @Test @@ -27,10 +27,10 @@ public void test02() { .parallel() .filter(x -> { invocations++; return (x % 42) == 0; }) .findFirst().getAsInt(); - Assert.assertEquals(42, first); + assertEquals(42, first); // This assert will fail: parallel streams can evaluate more -// Assert.assertEquals(42, invocations); +// assertEquals(42, invocations); } @Test @@ -43,10 +43,10 @@ public void test03() { .findFirst().getAsInt(); // This assert can also fail: unordered streams are not having any particular order -// Assert.assertEquals(42, first); +// assertEquals(42, first); // This assert will fail: parallel streams can evaluate more -// Assert.assertEquals(42, invocations); +// assertEquals(42, invocations); } } diff --git a/src/test/java/net/openjdk/streams/StreamAPITest.java b/src/test/java/net/openjdk/streams/StreamAPITest.java index ef8fb8f..ce37830 100644 --- a/src/test/java/net/openjdk/streams/StreamAPITest.java +++ b/src/test/java/net/openjdk/streams/StreamAPITest.java @@ -1,6 +1,5 @@ package net.openjdk.streams; -import junit.framework.Assert; import org.junit.Test; import java.util.ArrayList; @@ -9,13 +8,15 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.junit.Assert.*; + public class StreamAPITest { @Test public void test1() { List strings = Arrays.asList("Foo", "Bar", "Baz"); Stream stream = strings.stream(); - Assert.assertEquals( + assertEquals( "Foo", stream.findFirst().get() ); @@ -23,7 +24,7 @@ public void test1() { @Test public void test2() { - Assert.assertEquals( + assertEquals( Arrays.asList("Bar", "Baz"), Arrays.asList("Foo", "Bar", "Baz") .stream() @@ -34,7 +35,7 @@ public void test2() { @Test public void test3() { - Assert.assertEquals( + assertEquals( Arrays.asList(3, 3, 3), Arrays.asList("Foo", "Bar", "Baz") .stream() @@ -45,7 +46,7 @@ public void test3() { @Test public void test4() { - Assert.assertEquals( + assertEquals( Integer.valueOf(9), Arrays.asList("Foo", "BarBar", "BazBazBaz") .stream() @@ -57,7 +58,7 @@ public void test4() { @Test public void test5() { - Assert.assertEquals( + assertEquals( Arrays.asList("Foo", "Bar", "Baz"), Arrays.asList("Foo Bar Baz") .stream() @@ -69,7 +70,7 @@ public void test5() { @SuppressWarnings("serial") @Test public void test6() { - Assert.assertEquals( + assertEquals( new ArrayList() {{ add("Foo"); add("Bar"); add("Baz"); }}, Arrays.asList("Foo", "Bar", "Baz", "Baz", "Foo", "Bar") .stream() @@ -80,7 +81,7 @@ public void test6() { @Test public void test7() { - Assert.assertEquals( + assertEquals( Arrays.asList("Bar", "Baz", "Foo"), Arrays.asList("Foo", "Bar", "Baz") .stream() diff --git a/src/test/java/net/openjdk/streams/StreamBuildersTest.java b/src/test/java/net/openjdk/streams/StreamBuildersTest.java index 180a064..a5f79f0 100644 --- a/src/test/java/net/openjdk/streams/StreamBuildersTest.java +++ b/src/test/java/net/openjdk/streams/StreamBuildersTest.java @@ -1,57 +1,57 @@ package net.openjdk.streams; -import junit.framework.Assert; import org.junit.Test; import java.util.stream.DoubleStream; import java.util.stream.IntStream; import java.util.stream.Stream; -import java.util.stream.StreamSupport; -import java.util.stream.Streams; + +import static org.junit.Assert.*; public class StreamBuildersTest { @Test public void test1() { Stream s = Stream.builder().add(1).add(2).add(3).build(); - Assert.assertEquals(3, s.count()); + assertEquals(3, s.count()); } @Test public void test2() { IntStream s = IntStream.builder().add(1).add(2).add(3).build(); - Assert.assertEquals(3, s.count()); + assertEquals(3, s.count()); } @Test public void test3() { - Stream s = Streams.concat( + Stream s = Stream.concat( Stream.builder().add(1).build(), Stream.builder().add(2).build() ); - Assert.assertEquals(2, s.count()); + assertEquals(2, s.count()); } @Test public void test4() { DoubleStream avg = DoubleStream.generate(Math::random); double d = avg.limit(1000000).average().getAsDouble(); - Assert.assertEquals(0.5D, d, 0.001); + assertEquals(0.5D, d, 0.001); } @Test public void test5() { int sum = IntStream.range(0, 1000).sum(); - Assert.assertEquals(500500 - 1000, sum); + assertEquals(500500 - 1000, sum); } @Test public void test6() { int sum = IntStream.range(0, 1000).reduce((r1, r2) -> r2 + r1).getAsInt(); - Assert.assertEquals(500500 - 1000, sum); + assertEquals(500500 - 1000, sum); } + /* @Test public void test7() { String sum = Streams.zip( @@ -60,7 +60,7 @@ public void test7() { (r1, r2) -> r1 + r2) .findFirst() .get(); - Assert.assertEquals("Prefix0", sum); + assertEquals("Prefix0", sum); } @Test @@ -71,7 +71,8 @@ public void test8() { (r1, r2) -> r1 + r2) .findFirst() .get(); - Assert.assertEquals("Prefix0", sum); + assertEquals("Prefix0", sum); } + */ }