1 /*
 2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @modules jdk.incubator.code
27  * @run testng TestOptions
28  */
29 
30 import jdk.incubator.code.Op;
31 import org.testng.Assert;
32 import org.testng.annotations.Test;
33 
34 import java.lang.reflect.Method;
35 import jdk.incubator.code.op.CoreOp;
36 import jdk.incubator.code.writer.OpWriter;
37 import jdk.incubator.code.CodeReflection;
38 import java.util.Optional;
39 import java.util.stream.Stream;
40 
41 public class TestOptions {
42 
43     @CodeReflection
44     static int f(int n) {
45         return n;
46     }
47 
48     @Test
49     public void testDropWriteVoid() {
50         CoreOp.FuncOp f = getFuncOp("f");
51 
52         Assert.assertFalse(OpWriter.toText(f).contains("void"));
53         Assert.assertFalse(OpWriter.toText(f, OpWriter.VoidOpResultOption.DROP_VOID).contains("void"));
54         Assert.assertTrue(OpWriter.toText(f, OpWriter.VoidOpResultOption.WRITE_VOID).contains("void"));
55     }
56 
57     @Test
58     public void testDropWriteDescendants() {
59         CoreOp.FuncOp f = getFuncOp("f");
60 
61         Assert.assertTrue(OpWriter.toText(f).lines().count() > 1);
62         Assert.assertTrue(OpWriter.toText(f, OpWriter.OpDescendantsOption.WRITE_DESCENDANTS).lines().count() > 1);
63         Assert.assertTrue(OpWriter.toText(f, OpWriter.OpDescendantsOption.DROP_DESCENDANTS).lines().count() == 1);
64     }
65 
66 
67     static CoreOp.FuncOp getFuncOp(String name) {
68         Optional<Method> om = Stream.of(TestOptions.class.getDeclaredMethods())
69                 .filter(m -> m.getName().equals(name))
70                 .findFirst();
71 
72         Method m = om.get();
73         return Op.ofMethod(m).get();
74     }
75 }