b6ec69533c166181b2dd41333ea86787e09a9983
[jpf-core.git] / src / tests / java8 / LambdaTest.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
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
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
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.
17  */
18 package java8;
19
20 import gov.nasa.jpf.util.test.TestJPF;
21
22 import org.junit.Test;
23
24 /**
25  * @author Nastaran Shafiei <nastaran.shafiei@gmail.com>
26  */
27 public class LambdaTest extends TestJPF{
28   
29   static class EnforcedException extends RuntimeException {
30     // nothing in here
31   }
32   
33   @Test
34   public void testFuncObjAssignment() {
35     if(verifyUnhandledException(EnforcedException.class.getName())) {
36       
37       Runnable r = () -> {
38         throw new EnforcedException(); // make sure it gets here
39         };
40       
41       assertTrue(r != null);
42       
43       (new Thread(r)).start();
44     }
45   }
46   
47   public interface FI1 {
48     void sam();
49   }
50   
51   public interface FI2 extends FI1 {
52     @Override
53         public String toString();
54   }
55   
56   @Test
57   public void testSyntheticFuncObjClass() {
58     if (verifyNoPropertyViolation()) {
59       
60       FI2 fi = () -> {
61         return;
62         };
63       
64       assertTrue(fi != null);
65       
66       Class cls = fi.getClass();
67       
68       assertEquals(cls.getInterfaces().length, 1);
69       
70       assertEquals(cls.getDeclaredMethods().length, 1);
71             
72       assertSame(cls.getInterfaces()[0], FI2.class);
73       
74       assertSame(cls.getSuperclass(), Object.class);
75     }
76   }
77   
78   public interface FI3 {
79     public String ret();
80   }
81   
82   @Test
83   public void testSAMReturn() {
84     if (verifyNoPropertyViolation()) {
85       FI3 rt = () -> {
86         return "something"; 
87         };
88       
89       assertEquals(rt.ret(),"something"); 
90     }
91   }
92   
93   public class C {
94     int x = 1;
95   }
96   
97   public interface IncX {
98     public int incX(C o);
99   }
100   
101   @Test
102   public void testLambdaArgument() {
103     if (verifyNoPropertyViolation()) {
104       IncX fo = (arg) -> {
105         return ++arg.x;
106         };
107       
108       C o = new C();
109       
110       assertEquals(fo.incX(o),2);
111       assertEquals(fo.incX(o),3);
112     }
113   }
114   
115   static Integer io = new Integer(20);
116   
117   @Test
118   public void testClosure() {
119     if (verifyNoPropertyViolation()) {
120       int i = 10;
121       
122       FI1 fi = () -> {
123         assertSame(i,10);
124         assertSame(io.intValue(), 20);
125       };
126       
127       fi.sam();
128     }
129   }
130   
131   static void method(FI1 fi) {
132     fi.sam();
133   }
134   
135   @Test
136   public void testPassingToMethod() {
137     if (verifyUnhandledException(EnforcedException.class.getName())) {
138       int i = 10;
139       
140       method(() -> {
141         assertSame(i,10);
142         assertSame(io.intValue(), 20);
143         throw new EnforcedException();
144       });
145     }
146   }
147   
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.
150   @Test
151   public void testRepeatInvokedynamic() {
152     if (verifyNoPropertyViolation()) {
153       int i = 10;
154       FI1 f1, f2 = null; 
155       
156       for(int j=0; j<2; j++) {
157         f1 = () -> {
158           System.out.println("hello world!");
159         };
160         
161         if(j==1) {
162           assertTrue(f1!=null);
163           assertSame(f1,f2);
164         }
165         
166         f2 = f1;
167       }
168     }
169   }
170   
171   public static class C2 {
172     public static void throwException() {
173       throw new EnforcedException();
174     }
175   }
176   
177   @Test
178   public void testDoubleCloneOperator() {
179     if (verifyUnhandledException(EnforcedException.class.getName())) {
180       FI1 fi = C2::throwException;
181       fi.sam();
182     }
183   }
184   
185   static class A {
186     static {
187       if(true) {
188         throw new EnforcedException();
189       }
190     }
191   }
192
193   @Test
194   public void testInitDoubleCloneOperator() {
195     if (verifyUnhandledException(EnforcedException.class.getName())) {
196       new Thread(A::new).start();
197     }
198   }
199   
200   static class D {
201     static final B b = new B();
202   }
203   
204   static class B {
205     static final D a = new D();
206   }
207   
208   @Test
209   public void testClinitDeadlock() {
210     if(verifyDeadlock()) {
211       new Thread(D::new).start();
212       new B();
213     }
214   }
215   
216   @Test
217   public void testLambdaTypeName() {
218     if(verifyNoPropertyViolation()) {
219       Runnable r1 = (A::new);
220       Runnable r2 = (B::new);
221       
222       assertFalse(r1.getClass().getName().equals(r2.getClass().getName()));
223     }
224   }
225   
226   public interface FI {
227     default boolean returnTrue() {
228       return true;
229     }
230     @Override
231     public String toString();
232     public String toString(int i);
233   }
234   
235   @Test
236   public void testLambdaWithOverridenDefaultMethods() {
237     if(verifyNoPropertyViolation()) {
238       FI fi = (int i) -> {return "output:"+ i;};
239       assertEquals(fi.toString(10),"output:10");
240     }
241   }
242   
243   public interface FI4 {
244   }
245   
246   public interface FI5 extends FI {
247     @Override
248     public boolean equals(Object obj);
249   }
250   
251   @Test
252   public void testLambdaWithMultipleSuperInterfaces() {
253     if(verifyNoPropertyViolation()) {
254       FI5 fi = (int i) -> {return "output:"+ i;};
255       assertEquals(fi.toString(10),"output:10");
256     }
257   }
258   
259   public static class Foo {
260     
261     Integer i = 0;
262     static Integer j = 1;
263     
264     
265     public FI1 invokSam(FI1 fi) {
266       fi.sam();
267       return fi;
268     }
269     
270     
271     public FI1 withFreeVar() {
272       return invokSam(()->{Foo foo = this;});
273     }
274     
275     public static FI1 withStatic(Foo foo) {
276       return foo.invokSam(()->{Foo.j = 10;});
277     }
278   }
279   
280   @Test
281   public void testFreeVariables() {
282     if(verifyNoPropertyViolation()) {
283       
284       Foo foo = new Foo();
285       
286       FI1 fi1 = foo.withFreeVar();  
287       FI1 fi2 = foo.withFreeVar();
288       
289       assertFalse(fi1==fi2);
290      
291       fi1 = Foo.withStatic(foo);
292       fi2 = Foo.withStatic(foo);
293       
294       assertSame(fi1,fi2);
295     }
296   }
297 }