38
38
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39
39
* SOFTWARE.
40
40
*/
41
-
42
41
package com .oracle .graal .python .nodes .util ;
43
42
44
43
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
47
46
import com .oracle .graal .python .runtime .exception .PException ;
48
47
import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
49
48
import com .oracle .graal .python .test .PythonTests ;
49
+ import com .oracle .truffle .api .Truffle ;
50
+ import com .oracle .truffle .api .frame .VirtualFrame ;
51
+ import com .oracle .truffle .api .nodes .RootNode ;
50
52
import org .junit .Assert ;
51
53
import org .junit .Before ;
52
54
import org .junit .Test ;
57
59
import static com .oracle .graal .python .runtime .exception .PythonErrorType .TypeError ;
58
60
59
61
public class CastToJavaUnsignedLongNodeTests {
62
+ private static PythonObjectFactory factory = PythonObjectFactory .getUncached ();
60
63
61
64
@ Before
62
65
public void setUp () {
63
66
PythonTests .enterContext ();
64
67
}
65
68
66
- private static CastToJavaUnsignedLongNode castNode = CastToJavaUnsignedLongNode .getUncached ();
67
- private static PythonObjectFactory factory = PythonObjectFactory .getUncached ();
68
-
69
69
@ Test
70
70
public void positiveInt () {
71
- Assert .assertEquals (0 , castNode . execute (0 ));
72
- Assert .assertEquals (16 , castNode . execute (16 ));
73
- Assert .assertEquals (Integer .MAX_VALUE , castNode . execute (Integer .MAX_VALUE ));
74
- Assert .assertEquals (42 , castNode . execute ( Integer . valueOf ( 42 ) ));
71
+ Assert .assertEquals (0 , castInt (0 ));
72
+ Assert .assertEquals (16 , castInt (16 ));
73
+ Assert .assertEquals (Integer .MAX_VALUE , castInt (Integer .MAX_VALUE ));
74
+ Assert .assertEquals (42 , castObject ( 42 ));
75
75
}
76
76
77
77
@ Test
78
78
public void negativeInt () {
79
- expect (OverflowError , () -> castNode . execute (-1 ));
80
- expect (OverflowError , () -> castNode . execute (Integer .MIN_VALUE ));
79
+ expect (OverflowError , () -> castInt (-1 ));
80
+ expect (OverflowError , () -> castInt (Integer .MIN_VALUE ));
81
81
}
82
82
83
83
@ Test
84
84
public void positiveLong () {
85
- Assert .assertEquals (0 , castNode . execute (0L ));
86
- Assert .assertEquals (0x80000000L , castNode . execute (0x80000000L ));
87
- Assert .assertEquals (Long .MAX_VALUE , castNode . execute (Long .MAX_VALUE ));
88
- Assert .assertEquals (1234567890123L , castNode . execute ( Long . valueOf ( 1234567890123L ) ));
85
+ Assert .assertEquals (0 , castLong (0L ));
86
+ Assert .assertEquals (0x80000000L , castLong (0x80000000L ));
87
+ Assert .assertEquals (Long .MAX_VALUE , castLong (Long .MAX_VALUE ));
88
+ Assert .assertEquals (1234567890123L , castLong ( 1234567890123L ));
89
89
}
90
90
91
91
@ Test
92
92
public void negativeLong () {
93
- expect (OverflowError , () -> castNode . execute (-1L ));
94
- expect (OverflowError , () -> castNode . execute (Long .MIN_VALUE ));
93
+ expect (OverflowError , () -> castLong (-1L ));
94
+ expect (OverflowError , () -> castLong (Long .MIN_VALUE ));
95
95
}
96
96
97
97
@ Test
98
98
public void positiveBigInt () {
99
- Assert .assertEquals (0 , castNode . execute (makePInt (0 )));
100
- Assert .assertEquals (1234567890123L , castNode . execute (makePInt (1234567890123L )));
101
- Assert .assertEquals (Long .MAX_VALUE , castNode . execute (makePInt (Long .MAX_VALUE )));
102
- Assert .assertEquals (0x8000000000000000L , castNode . execute (makePInt ("8000000000000000" )));
103
- Assert .assertEquals (0xFFFFFFFFFFFFFFFFL , castNode . execute (makePInt ("FFFFFFFFFFFFFFFF" )));
99
+ Assert .assertEquals (0 , castObject (makePInt (0 )));
100
+ Assert .assertEquals (1234567890123L , castObject (makePInt (1234567890123L )));
101
+ Assert .assertEquals (Long .MAX_VALUE , castObject (makePInt (Long .MAX_VALUE )));
102
+ Assert .assertEquals (0x8000000000000000L , castObject (makePInt ("8000000000000000" )));
103
+ Assert .assertEquals (0xFFFFFFFFFFFFFFFFL , castObject (makePInt ("FFFFFFFFFFFFFFFF" )));
104
104
}
105
105
106
106
@ Test
107
107
public void negativeBigInt () {
108
- expect (OverflowError , () -> castNode . execute (makePInt (-1 )));
109
- expect (OverflowError , () -> castNode . execute (makePInt ("-10000000000000000" )));
108
+ expect (OverflowError , () -> castObject (makePInt (-1 )));
109
+ expect (OverflowError , () -> castObject (makePInt ("-10000000000000000" )));
110
110
}
111
111
112
112
@ Test
113
113
public void largeBigInt () {
114
- expect (OverflowError , () -> castNode . execute (makePInt ("10000000000000000" )));
115
- expect (OverflowError , () -> castNode . execute (makePInt ("10000000000000001" )));
114
+ expect (OverflowError , () -> castObject (makePInt ("10000000000000000" )));
115
+ expect (OverflowError , () -> castObject (makePInt ("10000000000000001" )));
116
116
}
117
117
118
118
@ Test
119
119
public void nonInteger () {
120
- expect (TypeError , () -> castNode . execute ("123" ));
121
- expect (TypeError , () -> castNode . execute (2.7 ));
120
+ expect (TypeError , () -> castObject ("123" ));
121
+ expect (TypeError , () -> castObject (2.7 ));
122
122
}
123
123
124
124
private static PInt makePInt (long l ) {
@@ -137,4 +137,37 @@ private static void expect(PythonBuiltinClassType errorType, Runnable test) {
137
137
e .expect (errorType , IsBuiltinClassProfile .getUncached ());
138
138
}
139
139
}
140
+
141
+ private static long castInt (int arg ) {
142
+ return (Long ) Truffle .getRuntime ().createCallTarget (new RootNode (null ) {
143
+ @ Child private CastToJavaUnsignedLongNode castNode = CastToJavaUnsignedLongNode .create ();
144
+
145
+ @ Override
146
+ public Object execute (VirtualFrame frame ) {
147
+ return castNode .execute (arg );
148
+ }
149
+ }).call ();
150
+ }
151
+
152
+ private static long castLong (long arg ) {
153
+ return (Long ) Truffle .getRuntime ().createCallTarget (new RootNode (null ) {
154
+ @ Child private CastToJavaUnsignedLongNode castNode = CastToJavaUnsignedLongNode .create ();
155
+
156
+ @ Override
157
+ public Object execute (VirtualFrame frame ) {
158
+ return castNode .execute (arg );
159
+ }
160
+ }).call ();
161
+ }
162
+
163
+ private static long castObject (Object arg ) {
164
+ return (Long ) Truffle .getRuntime ().createCallTarget (new RootNode (null ) {
165
+ @ Child private CastToJavaUnsignedLongNode castNode = CastToJavaUnsignedLongNode .create ();
166
+
167
+ @ Override
168
+ public Object execute (VirtualFrame frame ) {
169
+ return castNode .execute (arg );
170
+ }
171
+ }).call ();
172
+ }
140
173
}
0 commit comments