Skip to content

Commit a81539d

Browse files
committed
tests: Add further tests for mpz code.
1 parent 2f4e851 commit a81539d

File tree

9 files changed

+63
-0
lines changed

9 files changed

+63
-0
lines changed

tests/basics/builtin_hash.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
print(hash(True))
55
print({():1}) # hash tuple
66
print({1 << 66:1}) # hash big int
7+
print({-(1 << 66):2}) # hash negative big int
78
print(hash in {hash:1}) # hash function
89

910
try:

tests/basics/int_big_error.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,16 @@
1616
1 in i
1717
except TypeError:
1818
print("TypeError")
19+
20+
# overflow because rhs of >> is being converted to machine int
21+
try:
22+
1 >> i
23+
except OverflowError:
24+
print('OverflowError')
25+
26+
# to test conversion of negative mpz to machine int
27+
# (we know << will convert to machine int, even though it fails to do the shift)
28+
try:
29+
i << (-(i >> 40))
30+
except ValueError:
31+
print('ValueError')

tests/basics/int_big_lshift.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@
1515
print(-100000000000000000000000000002 >> i)
1616
print(-100000000000000000000000000003 >> i)
1717
print(-100000000000000000000000000004 >> i)
18+
19+
# shl by zero
20+
print((1<<70) << 0)

tests/basics/int_big_pow.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# test bignum power
2+
3+
i = 1 << 65
4+
5+
print(0 ** i)
6+
print(i ** 0)
7+
print(i ** 1)
8+
print(i ** 2)

tests/basics/int_big_rshift.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
i = 123456789012345678901234567890
22
print(i >> 1)
33
print(i >> 1000)
4+
5+
# result needs rounding up
6+
print(-(1<<70) >> 80)

tests/basics/struct1.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@
3535

3636
# long long ints
3737
print(struct.pack("<Q", 2**64 - 1))
38+
print(struct.pack(">Q", 2**64 - 1))
3839
print(struct.pack("<Q", 0xffffffffffffffff))
40+
print(struct.pack(">Q", 0xffffffffffffffff))
3941
print(struct.pack("<q", -1))
42+
print(struct.pack(">q", -1))
4043
print(struct.pack("<Q", 1234567890123456789))
4144
print(struct.pack("<q", -1234567890123456789))
4245
print(struct.pack(">Q", 1234567890123456789))

tests/float/int_big_float.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
# convert bignum to float on rhs
66
print("%.5g" % (2.0 * i))
77

8+
# negative bignum as float
9+
print("%.5g" % float(-i))
10+
811
# this should convert to float
912
print("%.5g" % (i / 5))
1013

tests/unix/extra_coverage.py.exp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ ementation
2727
(start=1, stop=2, step=3)
2828
# str
2929
1
30+
# mpz
31+
1
32+
12345678
33+
0
34+
0

unix/coverage.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "py/obj.h"
44
#include "py/runtime.h"
55
#include "py/repl.h"
6+
#include "py/mpz.h"
67

78
#if defined(MICROPY_UNIX_COVERAGE)
89

@@ -83,6 +84,29 @@ STATIC mp_obj_t extra_coverage(void) {
8384
printf("%d\n", MP_OBJ_IS_QSTR(mp_obj_str_intern(mp_obj_new_str("intern me", 9, false))));
8485
}
8586

87+
// mpz
88+
{
89+
printf("# mpz\n");
90+
91+
mp_uint_t value;
92+
mpz_t mpz;
93+
mpz_init_zero(&mpz);
94+
95+
// mpz_as_uint_checked, with success
96+
mpz_set_from_int(&mpz, 12345678);
97+
printf("%d\n", mpz_as_uint_checked(&mpz, &value));
98+
printf("%d\n", (int)value);
99+
100+
// mpz_as_uint_checked, with negative arg
101+
mpz_set_from_int(&mpz, -1);
102+
printf("%d\n", mpz_as_uint_checked(&mpz, &value));
103+
104+
// mpz_as_uint_checked, with overflowing arg
105+
mpz_set_from_int(&mpz, 1);
106+
mpz_shl_inpl(&mpz, &mpz, 70);
107+
printf("%d\n", mpz_as_uint_checked(&mpz, &value));
108+
}
109+
86110
return mp_const_none;
87111
}
88112
MP_DEFINE_CONST_FUN_OBJ_0(extra_coverage_obj, extra_coverage);

0 commit comments

Comments
 (0)