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 org.junit.Test;
25 * @author Nastaran Shafiei <nastaran.shafiei@gmail.com>
27 public class LambdaTest extends TestJPF{
29 static class EnforcedException extends RuntimeException {
34 public void testFuncObjAssignment() {
35 if(verifyUnhandledException(EnforcedException.class.getName())) {
38 throw new EnforcedException(); // make sure it gets here
41 assertTrue(r != null);
43 (new Thread(r)).start();
47 public interface FI1 {
51 public interface FI2 extends FI1 {
53 public String toString();
57 public void testSyntheticFuncObjClass() {
58 if (verifyNoPropertyViolation()) {
64 assertTrue(fi != null);
66 Class cls = fi.getClass();
68 assertEquals(cls.getInterfaces().length, 1);
70 assertEquals(cls.getDeclaredMethods().length, 1);
72 assertSame(cls.getInterfaces()[0], FI2.class);
74 assertSame(cls.getSuperclass(), Object.class);
78 public interface FI3 {
83 public void testSAMReturn() {
84 if (verifyNoPropertyViolation()) {
89 assertEquals(rt.ret(),"something");
97 public interface IncX {
102 public void testLambdaArgument() {
103 if (verifyNoPropertyViolation()) {
110 assertEquals(fo.incX(o),2);
111 assertEquals(fo.incX(o),3);
115 static Integer io = new Integer(20);
118 public void testClosure() {
119 if (verifyNoPropertyViolation()) {
124 assertSame(io.intValue(), 20);
131 static void method(FI1 fi) {
136 public void testPassingToMethod() {
137 if (verifyUnhandledException(EnforcedException.class.getName())) {
142 assertSame(io.intValue(), 20);
143 throw new EnforcedException();
148 // When invokedynamic executes for the first time, it creates a new function object.
149 // Re-executing the same bytecode returns the existing function object.
151 public void testRepeatInvokedynamic() {
152 if (verifyNoPropertyViolation()) {
156 for(int j=0; j<2; j++) {
158 System.out.println("hello world!");
162 assertTrue(f1!=null);
171 public static class C2 {
172 public static void throwException() {
173 throw new EnforcedException();
178 public void testDoubleCloneOperator() {
179 if (verifyUnhandledException(EnforcedException.class.getName())) {
180 FI1 fi = C2::throwException;
188 throw new EnforcedException();
194 public void testInitDoubleCloneOperator() {
195 if (verifyUnhandledException(EnforcedException.class.getName())) {
196 new Thread(A::new).start();
201 static final B b = new B();
205 static final D a = new D();
209 public void testClinitDeadlock() {
210 if(verifyDeadlock()) {
211 new Thread(D::new).start();
217 public void testLambdaTypeName() {
218 if(verifyNoPropertyViolation()) {
219 Runnable r1 = (A::new);
220 Runnable r2 = (B::new);
222 assertFalse(r1.getClass().getName().equals(r2.getClass().getName()));
226 public interface FI {
227 default boolean returnTrue() {
231 public String toString();
232 public String toString(int i);
236 public void testLambdaWithOverridenDefaultMethods() {
237 if(verifyNoPropertyViolation()) {
238 FI fi = (int i) -> {return "output:"+ i;};
239 assertEquals(fi.toString(10),"output:10");
243 public interface FI4 {
246 public interface FI5 extends FI {
248 public boolean equals(Object obj);
252 public void testLambdaWithMultipleSuperInterfaces() {
253 if(verifyNoPropertyViolation()) {
254 FI5 fi = (int i) -> {return "output:"+ i;};
255 assertEquals(fi.toString(10),"output:10");
259 public static class Foo {
262 static Integer j = 1;
265 public FI1 invokSam(FI1 fi) {
271 public FI1 withFreeVar() {
272 return invokSam(()->{Foo foo = this;});
275 public static FI1 withStatic(Foo foo) {
276 return foo.invokSam(()->{Foo.j = 10;});
281 public void testFreeVariables() {
282 if(verifyNoPropertyViolation()) {
286 FI1 fi1 = foo.withFreeVar();
287 FI1 fi2 = foo.withFreeVar();
289 assertFalse(fi1==fi2);
291 fi1 = Foo.withStatic(foo);
292 fi2 = Foo.withStatic(foo);