2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 import gov.nasa.jpf.util.test.TestJPF;
22 import java.util.function.Supplier;
24 import org.junit.Test;
27 * @author Nastaran Shafiei <nastaran.shafiei@gmail.com>
29 public class LambdaTest extends TestJPF{
31 static class EnforcedException extends RuntimeException {
36 public void testFuncObjAssignment() {
37 if(verifyUnhandledException(EnforcedException.class.getName())) {
40 throw new EnforcedException(); // make sure it gets here
43 assertTrue(r != null);
45 (new Thread(r)).start();
49 public interface FI1 {
53 public interface FI2 extends FI1 {
55 public String toString();
59 public void testSyntheticFuncObjClass() {
60 if (verifyNoPropertyViolation()) {
66 assertTrue(fi != null);
68 Class cls = fi.getClass();
70 assertEquals(cls.getInterfaces().length, 1);
72 assertEquals(cls.getDeclaredMethods().length, 1);
74 assertSame(cls.getInterfaces()[0], FI2.class);
76 assertSame(cls.getSuperclass(), Object.class);
80 public interface FI3 {
85 public void testSAMReturn() {
86 if (verifyNoPropertyViolation()) {
91 assertEquals(rt.ret(),"something");
99 public interface IncX {
100 public int incX(C o);
104 public void testLambdaArgument() {
105 if (verifyNoPropertyViolation()) {
112 assertEquals(fo.incX(o),2);
113 assertEquals(fo.incX(o),3);
117 static Integer io = new Integer(20);
120 public void testClosure() {
121 if (verifyNoPropertyViolation()) {
126 assertSame(io.intValue(), 20);
133 static void method(FI1 fi) {
138 public void testPassingToMethod() {
139 if (verifyUnhandledException(EnforcedException.class.getName())) {
144 assertSame(io.intValue(), 20);
145 throw new EnforcedException();
150 // When invokedynamic executes for the first time, it creates a new function object.
151 // Re-executing the same bytecode returns the existing function object.
153 public void testRepeatInvokedynamic() {
154 if (verifyNoPropertyViolation()) {
158 for(int j=0; j<2; j++) {
160 System.out.println("hello world!");
164 assertTrue(f1!=null);
173 public static class C2 {
174 public static void throwException() {
175 throw new EnforcedException();
180 public void testDoubleCloneOperator() {
181 if (verifyUnhandledException(EnforcedException.class.getName())) {
182 FI1 fi = C2::throwException;
190 throw new EnforcedException();
196 public void testInitDoubleCloneOperator() {
197 if (verifyUnhandledException(EnforcedException.class.getName())) {
198 new Thread(A::new).start();
203 static final B b = new B();
207 static final D a = new D();
211 public void testClinitDeadlock() {
212 if(verifyDeadlock()) {
213 new Thread(D::new).start();
219 public void testLambdaTypeName() {
220 if(verifyNoPropertyViolation()) {
221 Runnable r1 = (A::new);
222 Runnable r2 = (B::new);
224 assertFalse(r1.getClass().getName().equals(r2.getClass().getName()));
228 public interface FI {
229 default boolean returnTrue() {
233 public String toString();
234 public String toString(int i);
238 public void testLambdaWithOverridenDefaultMethods() {
239 if(verifyNoPropertyViolation()) {
240 FI fi = (int i) -> {return "output:"+ i;};
241 assertEquals(fi.toString(10),"output:10");
245 public interface FI4 {
248 public interface FI5 extends FI {
250 public boolean equals(Object obj);
254 public void testLambdaWithMultipleSuperInterfaces() {
255 if(verifyNoPropertyViolation()) {
256 FI5 fi = (int i) -> {return "output:"+ i;};
257 assertEquals(fi.toString(10),"output:10");
261 public static class Foo {
264 static Integer j = 1;
267 public FI1 invokSam(FI1 fi) {
273 public FI1 withFreeVar() {
274 return invokSam(()->{Foo foo = this;});
277 public static FI1 withStatic(Foo foo) {
278 return foo.invokSam(()->{Foo.j = 10;});
283 public void testFreeVariables() {
284 if(verifyNoPropertyViolation()) {
288 FI1 fi1 = foo.withFreeVar();
289 FI1 fi2 = foo.withFreeVar();
291 assertFalse(fi1==fi2);
293 fi1 = Foo.withStatic(foo);
294 fi2 = Foo.withStatic(foo);
301 public void testNullCaptureValues() {
302 if(verifyNoPropertyViolation()) {
303 Supplier<String> provider = getStringProvider(null);
304 assertEquals(provider.get(), "It was null");
308 private Supplier<String> getStringProvider(String object) {
309 return () -> object == null ? "It was null" : object;