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 junit TestSuccessorValidation
28  */
29 
30 import jdk.incubator.code.Block;
31 import jdk.incubator.code.dialect.core.CoreOp;
32 import jdk.incubator.code.dialect.core.CoreType;
33 import jdk.incubator.code.dialect.java.JavaType;
34 import org.junit.jupiter.api.Assertions;
35 import org.junit.jupiter.api.Test;
36 
37 public class TestSuccessorValidation {
38     @Test
39     void testInvalid() {
40         Assertions.assertThrows(IllegalStateException.class, TestSuccessorValidation::numArgsGtNumParams);
41     }
42 
43     private static CoreOp.FuncOp numArgsGtNumParams() {
44         return CoreOp.func("invalid", CoreType.functionType(JavaType.INT, JavaType.INT)).body(eb -> {
45             Block.Builder b1 = eb.block(JavaType.INT);
46             b1.add(CoreOp.return_(b1.parameters().get(0)));
47 
48             eb.add(CoreOp.branch(b1.reference(eb.parameters().get(0), eb.parameters().get(0))));
49         });
50     }
51 
52     @Test
53     void testValid() {
54         numArgsLeqNumParams();
55     }
56 
57     private static CoreOp.FuncOp numArgsLeqNumParams() {
58         return CoreOp.func("valid", CoreType.functionType(JavaType.INT, JavaType.INT, JavaType.BOOLEAN))
59                 .body(eb -> {
60                     Block.Builder b1 = eb.block(JavaType.INT);
61                     b1.add(CoreOp.return_(b1.parameters().get(0)));
62 
63                     eb.add(CoreOp.conditionalBranch(
64                             eb.parameters().get(1),
65                             b1.reference(eb.parameters().get(0)),
66                             b1.reference()
67                     ));
68                 });
69     }
70 }