| 1 | |
| 2 | |
| 3 | class AppTestIntOp: |
| 4 | spaceconfig = dict(usemodules=['__pypy__']) |
| 5 | |
| 6 | def w_intmask(self, n): |
| 7 | import sys |
| 8 | n &= (sys.maxsize*2+1) |
| 9 | if n > sys.maxsize: |
| 10 | n -= 2*(sys.maxsize+1) |
| 11 | return int(n) |
| 12 | |
| 13 | def test_intmask(self): |
| 14 | import sys |
| 15 | assert self.intmask(sys.maxsize) == sys.maxsize |
| 16 | assert self.intmask(sys.maxsize+1) == -sys.maxsize-1 |
| 17 | assert self.intmask(-sys.maxsize-2) == sys.maxsize |
| 18 | N = 2 ** 128 |
| 19 | assert self.intmask(N+sys.maxsize) == sys.maxsize |
| 20 | assert self.intmask(N+sys.maxsize+1) == -sys.maxsize-1 |
| 21 | assert self.intmask(N-sys.maxsize-2) == sys.maxsize |
| 22 | |
| 23 | def test_int_add(self): |
| 24 | import sys |
| 25 | from __pypy__ import intop |
| 26 | assert intop.int_add(40, 2) == 42 |
| 27 | assert intop.int_add(sys.maxsize, 1) == -sys.maxsize-1 |
| 28 | assert intop.int_add(-2, -sys.maxsize) == sys.maxsize |
| 29 | |
| 30 | def test_int_sub(self): |
| 31 | import sys |
| 32 | from __pypy__ import intop |
| 33 | assert intop.int_sub(40, -2) == 42 |
| 34 | assert intop.int_sub(sys.maxsize, -1) == -sys.maxsize-1 |
| 35 | assert intop.int_sub(-2, sys.maxsize) == sys.maxsize |
| 36 | |
| 37 | def test_int_mul(self): |
| 38 | import sys |
| 39 | from __pypy__ import intop |
| 40 | assert intop.int_mul(40, -2) == -80 |
| 41 | assert intop.int_mul(-sys.maxsize, -sys.maxsize) == ( |
| 42 | self.intmask(sys.maxsize ** 2)) |
| 43 | |
| 44 | def test_int_floordiv(self): |
| 45 | import sys |
| 46 | from __pypy__ import intop |
| 47 | assert intop.int_floordiv(41, 3) == 13 |
| 48 | assert intop.int_floordiv(41, -3) == -13 |
| 49 | assert intop.int_floordiv(-41, 3) == -13 |
| 50 | assert intop.int_floordiv(-41, -3) == 13 |
| 51 | assert intop.int_floordiv(-sys.maxsize, -1) == sys.maxsize |
| 52 | assert intop.int_floordiv(sys.maxsize, -1) == -sys.maxsize |
| 53 | |
| 54 | def test_int_mod(self): |
| 55 | import sys |
| 56 | from __pypy__ import intop |
| 57 | assert intop.int_mod(41, 3) == 2 |
| 58 | assert intop.int_mod(41, -3) == 2 |
| 59 | assert intop.int_mod(-41, 3) == -2 |
| 60 | assert intop.int_mod(-41, -3) == -2 |
| 61 | assert intop.int_mod(-sys.maxsize, -1) == 0 |
| 62 | assert intop.int_mod(sys.maxsize, -1) == 0 |
| 63 | |
| 64 | def test_int_lshift(self): |
| 65 | import sys |
| 66 | from __pypy__ import intop |
| 67 | if sys.maxsize == 2**31-1: |
| 68 | bits = 32 |
| 69 | else: |
| 70 | bits = 64 |
| 71 | assert intop.int_lshift(42, 3) == 42 << 3 |
| 72 | assert intop.int_lshift(0, 3333) == 0 |
| 73 | assert intop.int_lshift(1, bits-2) == 1 << (bits-2) |
| 74 | assert intop.int_lshift(1, bits-1) == -sys.maxsize-1 == (-1) << (bits-1) |
| 75 | assert intop.int_lshift(-1, bits-2) == (-1) << (bits-2) |
| 76 | assert intop.int_lshift(-1, bits-1) == -sys.maxsize-1 |
| 77 | assert intop.int_lshift(sys.maxsize // 3, 2) == ( |
| 78 | self.intmask((sys.maxsize // 3) << 2)) |
| 79 | assert intop.int_lshift(-sys.maxsize // 3, 2) == ( |
| 80 | self.intmask((-sys.maxsize // 3) << 2)) |
| 81 | |
| 82 | def test_int_rshift(self): |
| 83 | from __pypy__ import intop |
| 84 | assert intop.int_rshift(42, 3) == 42 >> 3 |
| 85 | assert intop.int_rshift(-42, 3) == (-42) >> 3 |
| 86 | assert intop.int_rshift(0, 3333) == 0 |
| 87 | assert intop.int_rshift(-1, 0) == -1 |
| 88 | assert intop.int_rshift(-1, 1) == -1 |
| 89 | |
| 90 | def test_uint_rshift(self): |
| 91 | import sys |
| 92 | from __pypy__ import intop |
| 93 | if sys.maxsize == 2**31-1: |
| 94 | bits = 32 |
| 95 | else: |
| 96 | bits = 64 |
| 97 | N = 1 << bits |
| 98 | assert intop.uint_rshift(42, 3) == 42 >> 3 |
| 99 | assert intop.uint_rshift(-42, 3) == (N-42) >> 3 |
| 100 | assert intop.uint_rshift(0, 3333) == 0 |
| 101 | assert intop.uint_rshift(-1, 0) == -1 |
| 102 | assert intop.uint_rshift(-1, 1) == sys.maxsize |
| 103 | assert intop.uint_rshift(-1, bits-2) == 3 |
| 104 | assert intop.uint_rshift(-1, bits-1) == 1 |
| 105 | |
| 106 | def test_mulmod(self): |
| 107 | from __pypy__ import intop |
| 108 | assert intop.int_mulmod(9373891, 9832739, 2**31-1) == 1025488209 |
| 109 | |
| 110 | def test_invalid(self): |
| 111 | from __pypy__ import intop |
| 112 | with raises(ZeroDivisionError): |
| 113 | intop.int_mod(1, 0) |
| 114 | with raises(ZeroDivisionError): |
| 115 | intop.int_floordiv(1, 0) |
| 116 | with raises(ValueError): |
| 117 | intop.int_mulmod(1, 0, 0) |
| 118 |