~inkscape.dev/inkscape-devlibs64/trunk

« back to all changes in this revision

Viewing changes to python/Lib/site-packages/numpy/core/tests/test_extint128.py

  • Committer: Eduard Braun
  • Date: 2016-10-22 16:51:19 UTC
  • Revision ID: eduard.braun2@gmx.de-20161022165119-9eosgy6lp8j1kzli
Update Python to version 2.7.12

Included modules:
  coverage 4.2
  lxml 3.6.4
  numpy 1.11.2
  scour 0.35
  six 1.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import division, absolute_import, print_function
 
2
 
 
3
import sys
 
4
import itertools
 
5
import contextlib
 
6
import operator
 
7
 
 
8
import numpy as np
 
9
import numpy.core.multiarray_tests as mt
 
10
from numpy.compat import long
 
11
 
 
12
from numpy.testing import assert_raises, assert_equal
 
13
 
 
14
 
 
15
INT64_MAX = np.iinfo(np.int64).max
 
16
INT64_MIN = np.iinfo(np.int64).min
 
17
INT64_MID = 2**32
 
18
 
 
19
# int128 is not two's complement, the sign bit is separate
 
20
INT128_MAX = 2**128 - 1
 
21
INT128_MIN = -INT128_MAX
 
22
INT128_MID = 2**64
 
23
 
 
24
INT64_VALUES = (
 
25
    [INT64_MIN + j for j in range(20)] +
 
26
    [INT64_MAX - j for j in range(20)] +
 
27
    [INT64_MID + j for j in range(-20, 20)] +
 
28
    [2*INT64_MID + j for j in range(-20, 20)] +
 
29
    [INT64_MID//2 + j for j in range(-20, 20)] +
 
30
    list(range(-70, 70))
 
31
)
 
32
 
 
33
INT128_VALUES = (
 
34
    [INT128_MIN + j for j in range(20)] +
 
35
    [INT128_MAX - j for j in range(20)] +
 
36
    [INT128_MID + j for j in range(-20, 20)] +
 
37
    [2*INT128_MID + j for j in range(-20, 20)] +
 
38
    [INT128_MID//2 + j for j in range(-20, 20)] +
 
39
    list(range(-70, 70)) +
 
40
    [False]  # negative zero
 
41
)
 
42
 
 
43
INT64_POS_VALUES = [x for x in INT64_VALUES if x > 0]
 
44
 
 
45
 
 
46
@contextlib.contextmanager
 
47
def exc_iter(*args):
 
48
    """
 
49
    Iterate over Cartesian product of *args, and if an exception is raised,
 
50
    add information of the current iterate.
 
51
    """
 
52
 
 
53
    value = [None]
 
54
 
 
55
    def iterate():
 
56
        for v in itertools.product(*args):
 
57
            value[0] = v
 
58
            yield v
 
59
 
 
60
    try:
 
61
        yield iterate()
 
62
    except:
 
63
        import traceback
 
64
        msg = "At: %r\n%s" % (repr(value[0]),
 
65
                              traceback.format_exc())
 
66
        raise AssertionError(msg)
 
67
 
 
68
 
 
69
def test_safe_binop():
 
70
    # Test checked arithmetic routines
 
71
 
 
72
    ops = [
 
73
        (operator.add, 1),
 
74
        (operator.sub, 2),
 
75
        (operator.mul, 3)
 
76
    ]
 
77
 
 
78
    with exc_iter(ops, INT64_VALUES, INT64_VALUES) as it:
 
79
        for xop, a, b in it:
 
80
            pyop, op = xop
 
81
            c = pyop(a, b)
 
82
 
 
83
            if not (INT64_MIN <= c <= INT64_MAX):
 
84
                assert_raises(OverflowError, mt.extint_safe_binop, a, b, op)
 
85
            else:
 
86
                d = mt.extint_safe_binop(a, b, op)
 
87
                if c != d:
 
88
                    # assert_equal is slow
 
89
                    assert_equal(d, c)
 
90
 
 
91
 
 
92
def test_to_128():
 
93
    with exc_iter(INT64_VALUES) as it:
 
94
        for a, in it:
 
95
            b = mt.extint_to_128(a)
 
96
            if a != b:
 
97
                assert_equal(b, a)
 
98
 
 
99
 
 
100
def test_to_64():
 
101
    with exc_iter(INT128_VALUES) as it:
 
102
        for a, in it:
 
103
            if not (INT64_MIN <= a <= INT64_MAX):
 
104
                assert_raises(OverflowError, mt.extint_to_64, a)
 
105
            else:
 
106
                b = mt.extint_to_64(a)
 
107
                if a != b:
 
108
                    assert_equal(b, a)
 
109
 
 
110
 
 
111
def test_mul_64_64():
 
112
    with exc_iter(INT64_VALUES, INT64_VALUES) as it:
 
113
        for a, b in it:
 
114
            c = a * b
 
115
            d = mt.extint_mul_64_64(a, b)
 
116
            if c != d:
 
117
                assert_equal(d, c)
 
118
 
 
119
 
 
120
def test_add_128():
 
121
    with exc_iter(INT128_VALUES, INT128_VALUES) as it:
 
122
        for a, b in it:
 
123
            c = a + b
 
124
            if not (INT128_MIN <= c <= INT128_MAX):
 
125
                assert_raises(OverflowError, mt.extint_add_128, a, b)
 
126
            else:
 
127
                d = mt.extint_add_128(a, b)
 
128
                if c != d:
 
129
                    assert_equal(d, c)
 
130
 
 
131
 
 
132
def test_sub_128():
 
133
    with exc_iter(INT128_VALUES, INT128_VALUES) as it:
 
134
        for a, b in it:
 
135
            c = a - b
 
136
            if not (INT128_MIN <= c <= INT128_MAX):
 
137
                assert_raises(OverflowError, mt.extint_sub_128, a, b)
 
138
            else:
 
139
                d = mt.extint_sub_128(a, b)
 
140
                if c != d:
 
141
                    assert_equal(d, c)
 
142
 
 
143
 
 
144
def test_neg_128():
 
145
    with exc_iter(INT128_VALUES) as it:
 
146
        for a, in it:
 
147
            b = -a
 
148
            c = mt.extint_neg_128(a)
 
149
            if b != c:
 
150
                assert_equal(c, b)
 
151
 
 
152
 
 
153
def test_shl_128():
 
154
    with exc_iter(INT128_VALUES) as it:
 
155
        for a, in it:
 
156
            if a < 0:
 
157
                b = -(((-a) << 1) & (2**128-1))
 
158
            else:
 
159
                b = (a << 1) & (2**128-1)
 
160
            c = mt.extint_shl_128(a)
 
161
            if b != c:
 
162
                assert_equal(c, b)
 
163
 
 
164
 
 
165
def test_shr_128():
 
166
    with exc_iter(INT128_VALUES) as it:
 
167
        for a, in it:
 
168
            if a < 0:
 
169
                b = -((-a) >> 1)
 
170
            else:
 
171
                b = a >> 1
 
172
            c = mt.extint_shr_128(a)
 
173
            if b != c:
 
174
                assert_equal(c, b)
 
175
 
 
176
 
 
177
def test_gt_128():
 
178
    with exc_iter(INT128_VALUES, INT128_VALUES) as it:
 
179
        for a, b in it:
 
180
            c = a > b
 
181
            d = mt.extint_gt_128(a, b)
 
182
            if c != d:
 
183
                assert_equal(d, c)
 
184
 
 
185
 
 
186
def test_divmod_128_64():
 
187
    with exc_iter(INT128_VALUES, INT64_POS_VALUES) as it:
 
188
        for a, b in it:
 
189
            if a >= 0:
 
190
                c, cr = divmod(a, b)
 
191
            else:
 
192
                c, cr = divmod(-a, b)
 
193
                c = -c
 
194
                cr = -cr
 
195
 
 
196
            d, dr = mt.extint_divmod_128_64(a, b)
 
197
 
 
198
            if c != d or d != dr or b*d + dr != a:
 
199
                assert_equal(d, c)
 
200
                assert_equal(dr, cr)
 
201
                assert_equal(b*d + dr, a)
 
202
 
 
203
 
 
204
def test_floordiv_128_64():
 
205
    with exc_iter(INT128_VALUES, INT64_POS_VALUES) as it:
 
206
        for a, b in it:
 
207
            c = a // b
 
208
            d = mt.extint_floordiv_128_64(a, b)
 
209
 
 
210
            if c != d:
 
211
                assert_equal(d, c)
 
212
 
 
213
 
 
214
def test_ceildiv_128_64():
 
215
    with exc_iter(INT128_VALUES, INT64_POS_VALUES) as it:
 
216
        for a, b in it:
 
217
            c = (a + b - 1) // b
 
218
            d = mt.extint_ceildiv_128_64(a, b)
 
219
 
 
220
            if c != d:
 
221
                assert_equal(d, c)
 
222
 
 
223
 
 
224
if __name__ == "__main__":
 
225
    run_module_suite()