MCPcopy Create free account
hub / github.com/Distributive-Network/PythonMonkey / test_eval_numbers_bigints

Function test_eval_numbers_bigints

tests/python/test_bigints.py:6–49  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4
5
6def test_eval_numbers_bigints():
7 def test_bigint(py_number: int):
8 js_number = pm.eval(f'{repr(py_number)}n')
9 assert py_number == js_number
10
11 test_bigint(0)
12 test_bigint(1)
13 test_bigint(-1)
14
15 # CPython would reuse the objects for small ints in range [-5, 256]
16 # Making sure we don't do any changes on them
17 def test_cached_int_object(py_number):
18
19 # type is still int
20 assert type(py_number) is int
21 assert not isinstance(py_number, pm.bigint)
22 test_bigint(py_number)
23 assert type(py_number) is int
24 assert not isinstance(py_number, pm.bigint)
25 # the value doesn't change
26 # TODO (Tom Tang): Find a way to create a NEW int object with the same
27 # value, because int literals also reuse the cached int objects
28 for _ in range(2):
29 test_cached_int_object(0) # _PyLong_FromByteArray reuses the int 0 object,
30 # see https://github.com/python/cpython/blob/3.9/Objects/longobject.c#L862
31 for i in range(10):
32 test_cached_int_object(random.randint(-5, 256))
33
34 test_bigint(18014398509481984) # 2**54
35 test_bigint(-18014398509481984) # -2**54
36 test_bigint(18446744073709551615) # 2**64-1
37 test_bigint(18446744073709551616) # 2**64
38 test_bigint(-18446744073709551617) # -2**64-1
39
40 limit = 2037035976334486086268445688409378161051468393665936250636140449354381299763336706183397376
41 # = 2**300
42 for i in range(10):
43 py_number = random.randint(-limit, limit)
44 test_bigint(py_number)
45
46 # TODO (Tom Tang): test -0 (negative zero)
47 # There's no -0 in both Python int and JS BigInt,
48 # but this could be possible in JS BigInt's internal representation as it uses a sign bit flag.
49 # On the other hand, Python int uses `ob_size` 0 for 0, >0 for positive values, <0 for negative values
50
51
52def test_eval_boxed_numbers_bigints():

Callers

nothing calls this directly

Calls 2

test_bigintFunction · 0.85
test_cached_int_objectFunction · 0.85

Tested by

no test coverage detected