1 import org.testng.Assert;
 2 import org.testng.annotations.Test;
 3 
 4 import java.lang.reflect.code.Quotable;
 5 import java.util.function.IntUnaryOperator;
 6 import java.util.stream.IntStream;
 7 
 8 /*
 9  * @test
10  * @summary test that invoking Quotable#quoted returns the same instance
11  * @run testng QuotedSameInstanceTest
12  */
13 
14 public class QuotedSameInstanceTest {
15 
16     private static final Quotable q1 = (Quotable & Runnable) () -> {
17     };
18 
19     @Test
20     void testWithOneThread() {
21         Assert.assertSame(q1.quoted(), q1.quoted());
22     }
23 
24     interface QuotableIntUnaryOperator extends IntUnaryOperator, Quotable { }
25     private static final QuotableIntUnaryOperator q2 = x -> x;
26 
27     @Test
28     void testWithMultiThreads() {
29         Object[] quotedObjects = IntStream.range(0, 1024).parallel().mapToObj(__ -> q2.quoted()).toArray();
30         for (int i = 1; i < quotedObjects.length; i++) {
31             Assert.assertSame(quotedObjects[i], quotedObjects[i - 1]);
32         }
33     }
34 }