37
37
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38
38
# SOFTWARE.
39
39
40
+ import math
40
41
import os
41
42
import types
42
43
import unittest
@@ -1254,6 +1255,10 @@ def test_foreign_number(self):
1254
1255
def wrap (obj ):
1255
1256
return __graalpython__ .foreign_wrapper (obj )
1256
1257
1258
+ def assertValueAndType (actual , expected ):
1259
+ self .assertEqual (expected , actual )
1260
+ self .assertEqual (type (expected ), type (actual ))
1261
+
1257
1262
n = wrap (42 )
1258
1263
self .assertEqual (type (n ).mro (), [polyglot .ForeignNumber , polyglot .ForeignObject , object ])
1259
1264
assert repr (n ) == '42' , repr (n )
@@ -1265,17 +1270,64 @@ def wrap(obj):
1265
1270
assert wrap (2 ) * wrap (3 ) == 6
1266
1271
assert wrap (7 ) / wrap (2 ) == 3.5
1267
1272
assert wrap (7 ) // wrap (2 ) == 3
1273
+ assert wrap (8 ) % wrap (3 ) == 2
1274
+ assert wrap (2 ) ** wrap (3 ) == 8
1275
+ assert wrap (1 ) << wrap (3 ) == 8
1276
+ assert wrap (8 ) >> wrap (2 ) == 2
1277
+
1278
+ # 1 and not 1.0 is unfortunate but interop does not give us a way to know if a non-primitive/wrapped 3.0 is integral or floating point
1279
+ assertValueAndType (wrap (3.0 ) // wrap (2.0 ), 1 )
1280
+ assertValueAndType (wrap (3.0 ) // 2.0 , 1.0 )
1281
+ assertValueAndType (3.0 // wrap (2.0 ), 1.0 )
1282
+
1283
+ assertValueAndType (wrap (3 ) - 2.0 , 1.0 )
1284
+ assertValueAndType (3.0 - wrap (2 ), 1.0 )
1268
1285
1269
1286
assert wrap (0b1110 ) & wrap (0b0111 ) == 0b0110
1270
1287
assert wrap (0b1110 ) | wrap (0b0111 ) == 0b1111
1271
1288
assert wrap (0b1110 ) ^ wrap (0b0111 ) == 0b1001
1272
1289
1273
- # TODO test ~invert and more
1290
+ assert wrap ((1 << 65 ) - 2 ) & wrap (0b111 ) == 0b110
1291
+ assert wrap ((1 << 65 ) - 2 ) | wrap (0b111 ) == ((1 << 65 ) - 1 )
1292
+ assert wrap ((1 << 65 ) - 2 ) ^ wrap (0b1 ) == ((1 << 65 ) - 1 )
1293
+
1294
+ assert wrap (42 ).as_integer_ratio () == (42 , 1 )
1295
+ assert wrap (0b1010 ).bit_count () == 2
1296
+ assert wrap (0b1010 ).bit_length () == 4
1297
+ assert wrap (42 ).conjugate () == 42
1298
+ assert wrap (42 ).is_integer ()
1299
+ assert wrap (42.0 ).is_integer ()
1300
+ assert not wrap (42.5 ).is_integer ()
1301
+ assert wrap (42.0 ).to_bytes () == b"*"
1302
+
1303
+ assert ~ wrap (42 ) == - 43
1304
+ assert - wrap (42 ) == - 42
1305
+ assert + wrap (42 ) == 42
1306
+
1307
+ assertValueAndType (abs (wrap (- 2 )), 2 )
1308
+ assertValueAndType (float (wrap (2 )), 2.0 )
1309
+ assertValueAndType (int (wrap (2.3 )), 2 )
1310
+ assertValueAndType (math .floor (wrap (2.3 )), 2 )
1311
+ assertValueAndType (math .ceil (wrap (2.3 )), 3 )
1312
+ assertValueAndType (math .trunc (wrap (- 2.3 )), - 2 )
1313
+ assertValueAndType (round (wrap (2.3 )), 2 )
1314
+
1315
+ missing_int_methods = set (dir (int )) - set (dir (type (wrap (1 ))))
1316
+ missing_int_methods = [m for m in missing_int_methods if m .startswith ('_' ) and m != '__getnewargs__' ]
1317
+ self .assertEqual ([], missing_int_methods )
1318
+
1319
+ missing_float_methods = set (dir (float )) - set (dir (type (wrap (1.2 ))))
1320
+ missing_float_methods = [m for m in missing_float_methods if m .startswith ('_' ) and m not in ('__getnewargs__' , '__getformat__' )]
1321
+ self .assertEqual ([], missing_float_methods )
1274
1322
1275
1323
def test_foreign_boolean (self ):
1276
1324
def wrap (obj ):
1277
1325
return __graalpython__ .foreign_wrapper (obj )
1278
1326
1327
+ def assertValueAndType (actual , expected ):
1328
+ self .assertEqual (expected , actual )
1329
+ self .assertEqual (type (expected ), type (actual ))
1330
+
1279
1331
self .assertEqual (type (wrap (True )).mro (), [polyglot .ForeignBoolean , polyglot .ForeignNumber , polyglot .ForeignObject , object ])
1280
1332
assert repr (wrap (True )) == 'True'
1281
1333
assert repr (wrap (False )) == 'False'
@@ -1287,13 +1339,13 @@ def wrap(obj):
1287
1339
assert bool (wrap (True )) is True
1288
1340
assert bool (wrap (False )) is False
1289
1341
1290
- assert wrap (True ) + wrap (2 ) == 3
1291
- assert wrap (False ) + wrap (2 ) == 2
1292
- assert wrap (2 ) + wrap (True ) == 3
1293
- assert wrap (2 ) + wrap (False ) == 2
1342
+ assertValueAndType ( wrap (True ) + wrap (2 ), 3 )
1343
+ assertValueAndType ( wrap (False ) + wrap (2 ), 2 )
1344
+ assertValueAndType ( wrap (2 ) + wrap (True ), 3 )
1345
+ assertValueAndType ( wrap (2 ) + wrap (False ), 2 )
1294
1346
1295
- assert wrap (True ) - wrap (2 ) == - 1
1296
- assert wrap (2 ) - wrap (True ) == 1
1347
+ assertValueAndType ( wrap (True ) - wrap (2 ), - 1 )
1348
+ assertValueAndType ( wrap (2 ) - wrap (True ), 1 )
1297
1349
1298
1350
assert wrap (True ) & wrap (True ) is True
1299
1351
assert wrap (True ) & wrap (False ) is False
@@ -1304,7 +1356,15 @@ def wrap(obj):
1304
1356
assert wrap (True ) ^ wrap (False ) is True
1305
1357
assert wrap (False ) ^ wrap (False ) is False
1306
1358
1307
- # TODO ~invert
1359
+ assertValueAndType (~ wrap (True ), - 2 )
1360
+ assertValueAndType (~ wrap (False ), - 1 )
1361
+
1362
+ assertValueAndType (float (wrap (True )), 1.0 )
1363
+ assertValueAndType (int (wrap (True )), 1 )
1364
+
1365
+ missing_bool_methods = set (dir (bool )) - set (dir (type (wrap (True ))))
1366
+ missing_bool_methods = [m for m in missing_bool_methods if m .startswith ('_' ) and m != '__getnewargs__' ]
1367
+ self .assertEqual ([], missing_bool_methods )
1308
1368
1309
1369
def test_foreign_repl (self ):
1310
1370
from java .util .logging import LogRecord
0 commit comments