summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2017-03-06 16:23:09 +0100
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2017-03-06 16:23:09 +0100
commit121fb88988fd4b4511476231943e999cf38b2866 (patch)
tree62ef99450f270331cb2932be7574c6800a00e91a
parent325c4473a5b760a2251a6e0e418abf218741fb1f (diff)
float/float2int*: Make actually be parsable for MICROPY_LONGINT_IMPL_NONE.
The use of large literal numbers is a big no-no when it comes to writing programs which work with different int representations. Also, some checks are pretty adhoc (e.g using struct module to check for 64-bitness). This change bases entire detection on sys.maxsize and integer operarions, and thus more correct, even if longer. Note that this change doesn't mean that any of these tests can pass with anything but MPZ - even despite checking for various int representations, the tests aren't written to be portable among them.
-rw-r--r--tests/float/float2int.py34
-rw-r--r--tests/float/float2int_doubleprec.py32
-rw-r--r--tests/float/float2int_fp30.py32
3 files changed, 62 insertions, 36 deletions
diff --git a/tests/float/float2int.py b/tests/float/float2int.py
index da530cee6..3596d2f73 100644
--- a/tests/float/float2int.py
+++ b/tests/float/float2int.py
@@ -5,21 +5,31 @@ try:
except:
import struct
+import sys
+
+maxsize_bits = 0
+maxsize = sys.maxsize
+while maxsize:
+ maxsize >>= 1
+ maxsize_bits += 1
+
# work out configuration values
-is_64bit = struct.calcsize("P") == 8
+is_64bit = maxsize_bits > 32
# 0 = none, 1 = long long, 2 = mpz
-try:
- dummy = 0x7fffffffffffffff
- try:
- if (0xffffffffffffffff + 1) > 0:
- ll_type = 2
- else:
- ll_type = 1
- except:
- # in case the sum in the if statement above changes to raising an exception on overflow
+ll_type = None
+if is_64bit:
+ if maxsize_bits < 63:
+ ll_type = 0
+else:
+ if maxsize_bits < 31:
+ ll_type = 0
+if ll_type is None:
+ one = 1
+ if one << 65 < one << 62:
ll_type = 1
-except:
- ll_type = 0
+ else:
+ ll_type = 2
+
# basic conversion
print(int(14187745.))
diff --git a/tests/float/float2int_doubleprec.py b/tests/float/float2int_doubleprec.py
index acdc8c69c..de2137d66 100644
--- a/tests/float/float2int_doubleprec.py
+++ b/tests/float/float2int_doubleprec.py
@@ -5,21 +5,29 @@ try:
except:
import struct
+import sys
+maxsize_bits = 0
+maxsize = sys.maxsize
+while maxsize:
+ maxsize >>= 1
+ maxsize_bits += 1
+
# work out configuration values
-is_64bit = struct.calcsize("P") == 8
+is_64bit = maxsize_bits > 32
# 0 = none, 1 = long long, 2 = mpz
-try:
- dummy = 0x7fffffffffffffff
- try:
- if (0xffffffffffffffff + 1) > 0:
- ll_type = 2
- else:
- ll_type = 1
- except:
- # in case the sum in the if statement above changes to raising an exception on overflow
+ll_type = None
+if is_64bit:
+ if maxsize_bits < 63:
+ ll_type = 0
+else:
+ if maxsize_bits < 31:
+ ll_type = 0
+if ll_type is None:
+ one = 1
+ if one << 65 < one << 62:
ll_type = 1
-except:
- ll_type = 0
+ else:
+ ll_type = 2
# This case occurs with time.time() values
if ll_type != 0:
diff --git a/tests/float/float2int_fp30.py b/tests/float/float2int_fp30.py
index bad9c31e9..fbb94a4cc 100644
--- a/tests/float/float2int_fp30.py
+++ b/tests/float/float2int_fp30.py
@@ -5,21 +5,29 @@ try:
except:
import struct
+import sys
+maxsize_bits = 0
+maxsize = sys.maxsize
+while maxsize:
+ maxsize >>= 1
+ maxsize_bits += 1
+
# work out configuration values
-is_64bit = struct.calcsize("P") == 8
+is_64bit = maxsize_bits > 32
# 0 = none, 1 = long long, 2 = mpz
-try:
- dummy = 0x7fffffffffffffff
- try:
- if (0xffffffffffffffff + 1) > 0:
- ll_type = 2
- else:
- ll_type = 1
- except:
- # in case the sum in the if statement above changes to raising an exception on overflow
+ll_type = None
+if is_64bit:
+ if maxsize_bits < 63:
+ ll_type = 0
+else:
+ if maxsize_bits < 31:
+ ll_type = 0
+if ll_type is None:
+ one = 1
+ if one << 65 < one << 62:
ll_type = 1
-except:
- ll_type = 0
+ else:
+ ll_type = 2
# basic conversion
print(int(14187744.))