1 /*
2  * Copyright (c) 2011, 2018, 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 package vm.mlvm.meth.share;
25 
26 import java.lang.invoke.MethodHandles;
27 import java.lang.invoke.MethodType;
28 
29 import vm.mlvm.share.Env;
30 import vm.mlvm.share.MlvmTest;
31 import vm.share.options.Option;
32 
33 public class SimpleUnitTest extends MlvmTest {
34 
35     @Option(name = "failOnce", default_value = "false", description = "exit after the first failure")
36     private boolean failOnce = true;
37 
SimpleUnitTest()38     public SimpleUnitTest() {}
39 
test1(int a, float b)40     public SimpleUnitTest test1(int a, float b) {
41         Env.traceNormal("test1(%d, %f) called", a, b);
42         return this;
43     }
44 
45     private static SimpleUnitTest sut = new SimpleUnitTest();
46 
test2(int a, float b)47     public static SimpleUnitTest test2(int a, float b) {
48         Env.traceNormal("test2(%d, %f) called", a, b);
49         return sut;
50     }
51 
test3(int a)52     public static int test3(int a) {
53         Env.traceNormal("test3(%d) called", a);
54         return a;
55     }
56 
test4()57     public void test4() {
58         Env.traceNormal("test4() called");
59     }
60 
test5()61     public SimpleUnitTest test5() {
62         Env.traceNormal("test5() called");
63         return this;
64     }
65 
main(String[] args)66     public static void main(String[] args) { MlvmTest.launch(args); }
67 
68     @Override
run()69     public boolean run() throws Throwable {
70         try {
71             Argument retArg;
72             retArg = new Argument(SimpleUnitTest.class, sut);
73             retArg.setPreserved(true);
74 
75             Argument intArg = new Argument(int.class, new Integer(1));
76 
77             for ( ;; ) {
78                 try {
79                     switch ( Env.getRNG().nextInt(5) ) {
80                     case 0:
81                         MHTransformationGen.createAndCallSequence(
82                             retArg,
83                             sut,
84                             MethodHandles.lookup().findVirtual(
85                                     SimpleUnitTest.class,
86                                     "test1",
87                                     MethodType.methodType(SimpleUnitTest.class, int.class, float.class)
88                             ),
89                             new Argument[] { new Argument(int.class, new Integer(1)), new Argument(float.class, new Float(1.0)) },
90                             true);
91                         break;
92 
93                     case 1:
94                         MHTransformationGen.createAndCallSequence(
95                                 retArg,
96                                 null,
97                                 MethodHandles.lookup().findStatic(
98                                         SimpleUnitTest.class,
99                                         "test2",
100                                         MethodType.methodType(SimpleUnitTest.class, int.class, float.class)
101                                 ),
102                                 new Argument[] { new Argument(int.class, new Integer(1)), new Argument(float.class, new Float(1.0)) },
103                                 true);
104                             break;
105 
106                     case 2:
107                         MHTransformationGen.createAndCallSequence(
108                                 intArg,
109                                 null,
110                                 MethodHandles.lookup().findStatic(
111                                         SimpleUnitTest.class,
112                                         "test3",
113                                         MethodType.methodType(int.class, int.class)
114                                 ),
115                                 new Argument[] { intArg },
116                                 true);
117                             break;
118 
119                     case 3:
120                         MHTransformationGen.createAndCallSequence(
121                                 new Argument(void.class, null),
122                                 sut,
123                                 MethodHandles.lookup().findVirtual(
124                                         SimpleUnitTest.class,
125                                         "test4",
126                                         MethodType.methodType(void.class)
127                                 ),
128                                 new Argument[0],
129                                 false);
130                             break;
131 
132                     default:
133                         MHTransformationGen.createAndCallSequence(
134                                 retArg,
135                                 sut,
136                                 MethodHandles.lookup().findVirtual(
137                                         SimpleUnitTest.class,
138                                         "test5",
139                                         MethodType.methodType(SimpleUnitTest.class)
140                                 ),
141                                 new Argument[0],
142                                 true);
143                             break;
144 
145                     }
146                 } catch ( Throwable e ) {
147                     Env.getLog().complain("Caught exception", e);
148                     if ( failOnce )
149                         return false;
150                 }
151             }
152         } catch ( Throwable t ) {
153             t.printStackTrace();
154             return false;
155         }
156     }
157 
158 }
159