Skip to content

Commit 3efe5f4

Browse files
authored
Enable some test_long tests (#976)
1 parent f9f7124 commit 3efe5f4

File tree

5 files changed

+78
-9
lines changed

5 files changed

+78
-9
lines changed

Src/IronPython/Runtime/Operations/IntOps.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,22 @@ public static object __new__(PythonType cls, string s, int @base) {
153153
return LiteralParser.ParseIntegerSign(s, @base, start);
154154
}
155155

156+
[StaticExtensionMethod]
157+
public static object __new__(PythonType cls, string s, object @base) {
158+
switch (PythonOps.Index(@base)) {
159+
case int i:
160+
return __new__(cls, s, i);
161+
case BigInteger bi:
162+
try {
163+
return __new__(cls, s, (int)bi);
164+
} catch (OverflowException) {
165+
return __new__(cls, s, int.MaxValue);
166+
}
167+
default:
168+
throw new InvalidOperationException();
169+
}
170+
}
171+
156172
[StaticExtensionMethod]
157173
public static object __new__(CodeContext/*!*/ context, PythonType cls, IList<byte> s, int @base=10) {
158174
object value;
@@ -516,7 +532,7 @@ public static Bytes to_bytes(Int32 value, int length, string byteorder, bool sig
516532
bool isLittle = byteorder == "little";
517533
if (!isLittle && byteorder != "big") throw PythonOps.ValueError("byteorder must be either 'little' or 'big'");
518534

519-
var reqLength = (bit_length(value) + (signed ? 1 : 0)) / 8;
535+
var reqLength = (bit_length(value) + (value > 0 && signed ? 1 : 0) + 7) / 8;
520536
if (reqLength > length) throw PythonOps.OverflowError("int too big to convert");
521537

522538
var bytes = new BigInteger(value).ToByteArray();

Src/IronPython/Runtime/Operations/LongOps.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ public static BigInteger __round__(BigInteger self, BigInteger ndigits) {
10161016

10171017
// see https://bugs.python.org/issue4707#msg78141
10181018
var i = BigInteger.Pow(10, -intNDigits);
1019-
var r = self % (2 * i);
1019+
var r = Mod(self, 2 * i);
10201020
var o = i / 2;
10211021
self -= r;
10221022

Src/IronPythonTest/Cases/CPythonCasesManifest.ini

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,9 +604,8 @@ Ignore=true
604604
Ignore=true
605605
Reason=TypeError: unsupported operand type(s) for //: 'timedelta' and 'timedelta' | StackOverflowException - https://github.com/IronLanguages/ironpython2/issues/182
606606

607-
[CPython.test_long]
607+
[CPython.test_long] # IronPython.test_long_stdlib
608608
Ignore=true
609-
Reason=AttributeError: 'module' object has no attribute 'int_info'
610609

611610
[CPython.test_lzma]
612611
Ignore=true

Src/StdLib/Lib/test/test_long.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import array
99

1010
# SHIFT should match the value in longintrepr.h for best testing.
11-
SHIFT = sys.int_info.bits_per_digit
11+
SHIFT = 30 if sys.implementation.name == "ironpython" else sys.int_info.bits_per_digit # https://github.com/IronLanguages/ironpython3/issues/974
1212
BASE = 2 ** SHIFT
1313
MASK = BASE - 1
1414
KARATSUBA_CUTOFF = 70 # from longobject.c
@@ -949,15 +949,21 @@ def test_round(self):
949949
got = round(10**k + 324678, -3)
950950
expect = 10**k + 325000
951951
self.assertEqual(got, expect)
952-
self.assertIs(type(got), int)
952+
if sys.implementation.name == "ironpython": # https://github.com/IronLanguages/ironpython3/issues/52
953+
self.assertTrue(isinstance(got, int))
954+
else:
955+
self.assertIs(type(got), int)
953956

954957
# nonnegative second argument: round(x, n) should just return x
955958
for n in range(5):
956959
for i in range(100):
957960
x = random.randrange(-10000, 10000)
958961
got = round(x, n)
959962
self.assertEqual(got, x)
960-
self.assertIs(type(got), int)
963+
if sys.implementation.name == "ironpython": # https://github.com/IronLanguages/ironpython3/issues/52
964+
self.assertTrue(isinstance(got, int))
965+
else:
966+
self.assertIs(type(got), int)
961967
for huge_n in 2**31-1, 2**31, 2**63-1, 2**63, 2**100, 10**100:
962968
self.assertEqual(round(8979323, huge_n), 8979323)
963969

@@ -966,10 +972,13 @@ def test_round(self):
966972
x = random.randrange(-10000, 10000)
967973
got = round(x)
968974
self.assertEqual(got, x)
969-
self.assertIs(type(got), int)
975+
if sys.implementation.name == "ironpython": # https://github.com/IronLanguages/ironpython3/issues/52
976+
self.assertTrue(isinstance(got, int))
977+
else:
978+
self.assertIs(type(got), int)
970979

971980
# bad second argument
972-
bad_exponents = ('brian', 2.0, 0j, None)
981+
bad_exponents = ('brian', 2.0, 0j)
973982
for e in bad_exponents:
974983
self.assertRaises(TypeError, round, 3, e)
975984

Tests/test_long_stdlib.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Licensed to the .NET Foundation under one or more agreements.
2+
# The .NET Foundation licenses this file to you under the Apache 2.0 License.
3+
# See the LICENSE file in the project root for more information.
4+
5+
##
6+
## Run selected tests from test_long from StdLib
7+
##
8+
9+
import unittest
10+
import sys
11+
12+
from iptest import run_test
13+
14+
import test.test_long
15+
16+
def load_tests(loader, standard_tests, pattern):
17+
if sys.implementation.name == 'ironpython':
18+
suite = unittest.TestSuite()
19+
suite.addTest(test.test_long.LongTest('test__format__'))
20+
suite.addTest(test.test_long.LongTest('test_access_to_nonexistent_digit_0'))
21+
suite.addTest(test.test_long.LongTest('test_bit_length'))
22+
suite.addTest(test.test_long.LongTest('test_bitop_identities'))
23+
suite.addTest(test.test_long.LongTest('test_conversion'))
24+
suite.addTest(unittest.expectedFailure(test.test_long.LongTest('test_correctly_rounded_true_division'))) # https://github.com/IronLanguages/ironpython3/issues/907
25+
suite.addTest(test.test_long.LongTest('test_division'))
26+
suite.addTest(unittest.expectedFailure(test.test_long.LongTest('test_float_conversion'))) # https://github.com/IronLanguages/ironpython3/issues/907
27+
suite.addTest(test.test_long.LongTest('test_float_overflow'))
28+
suite.addTest(test.test_long.LongTest('test_format'))
29+
suite.addTest(test.test_long.LongTest('test_from_bytes'))
30+
suite.addTest(test.test_long.LongTest('test_karatsuba'))
31+
suite.addTest(test.test_long.LongTest('test_logs'))
32+
suite.addTest(test.test_long.LongTest('test_long'))
33+
suite.addTest(test.test_long.LongTest('test_mixed_compares'))
34+
suite.addTest(test.test_long.LongTest('test_nan_inf'))
35+
suite.addTest(test.test_long.LongTest('test_round'))
36+
suite.addTest(test.test_long.LongTest('test_shift_bool'))
37+
suite.addTest(unittest.expectedFailure(test.test_long.LongTest('test_small_ints'))) # https://github.com/IronLanguages/ironpython3/issues/975
38+
suite.addTest(test.test_long.LongTest('test_to_bytes'))
39+
suite.addTest(unittest.expectedFailure(test.test_long.LongTest('test_true_division'))) # https://github.com/IronLanguages/ironpython3/issues/907
40+
return suite
41+
42+
else:
43+
return loader.loadTestsFromModule(test.test_long, pattern)
44+
45+
run_test(__name__)

0 commit comments

Comments
 (0)