File: test_context.py

package info (click to toggle)
python-gmpy2 2.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,996 kB
  • sloc: ansic: 24,604; python: 4,911; makefile: 123
file content (285 lines) | stat: -rw-r--r-- 9,993 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import warnings

import pytest

import gmpy2
from gmpy2 import (context, get_context, ieee, local_context, mpc, mpfr, mpz,
                   set_context)


def test_context_abs():
    ctx = gmpy2.context()

    assert ctx.abs(-1) == mpz(1)
    assert ctx.abs(0) == mpz(0)
    assert ctx.abs(1) == mpz(1)
    assert ctx.abs(mpz(8)) == mpz(8)
    assert ctx.abs(mpz(-8)) == mpz(8)
    assert ctx.abs(-1.0) == mpfr('1.0')
    assert ctx.abs(mpfr(-2)) == mpfr('2.0')
    assert ctx.abs(2+3j) == mpfr('3.6055512754639891')
    assert ctx.abs(mpc(2+3j)) == mpfr('3.6055512754639891')


def test_context_ieee():
    ctx = ieee(32)

    assert (ctx.precision == 24 and ctx.emax == 128 and
            ctx.emin == -148 and ctx.subnormalize)

    ctx = ieee(64)

    assert (ctx.precision == 53 and ctx.emax == 1024 and
            ctx.emin == -1073 and ctx.subnormalize)

    ctx = ieee(128)

    assert (ctx.precision == 113 and ctx.emax == 16384 and
            ctx.emin == -16493 and ctx.subnormalize)

    ctx = ieee(256)

    assert (ctx.precision == 237 and ctx.emax == 262144 and
            ctx.emin == -262377 and ctx.subnormalize)

    pytest.raises(ValueError, lambda: ieee(-1))
    pytest.raises(TypeError, lambda: ieee("a"))

    set_context(ieee(32))

    assert gmpy2.const_pi().digits(2) == ('110010010000111111011011', 2, 24)

    set_context(ieee(64))

    assert gmpy2.const_pi().digits(2) == ('11001001000011111101101010100010001000010110100011000', 2, 53)

    set_context(ieee(128))

    assert gmpy2.const_pi().digits(2) == ('11001001000011111101101010100010001000010110100011000010001101001100010011000110011000101000101110000000110111000', 2, 113)


def test_context():
    ctx = context()

    assert (ctx.precision == 53 and ctx.emax == 1073741823 and
            ctx.emin == -1073741823 and not ctx.subnormalize)

    ctx = context(precision=100)

    assert (ctx.precision == 100 and ctx.emax == 1073741823 and
            ctx.emin == -1073741823 and not ctx.subnormalize)

    ctx = context(real_prec=100)

    assert (ctx.precision == 53 and ctx.emax == 1073741823 and
            ctx.emin == -1073741823 and not ctx.subnormalize and
            ctx.real_prec == 100)

    ctx = context(real_prec=100,imag_prec=200)

    assert (ctx.precision == 53 and ctx.emax == 1073741823 and
            ctx.emin == -1073741823 and not ctx.subnormalize and
            ctx.real_prec == 100 and ctx.imag_prec == 200)

    pytest.raises(ValueError, lambda: context(1, 2))
    pytest.raises(ValueError, lambda: context(spam=123))


def test_get_context():
    set_context(context())

    ctx = get_context()

    assert (ctx.precision == 53 and ctx.emax == 1073741823 and
            ctx.emin == -1073741823 and not ctx.subnormalize)

    ctx = get_context()
    ctx.precision = 100

    assert (ctx.precision == 100 and ctx.emax == 1073741823 and
            ctx.emin == -1073741823 and not ctx.subnormalize)

    ctx = get_context()

    assert (ctx.precision == 100 and ctx.emax == 1073741823 and
            ctx.emin == -1073741823 and not ctx.subnormalize)

    b = ctx.copy()
    b.precision = 200

    assert (b.precision == 200 and b.emax == 1073741823 and
            b.emin == -1073741823 and not b.subnormalize)
    assert (ctx.precision == 100 and ctx.emax == 1073741823 and
            ctx.emin == -1073741823 and not ctx.subnormalize)

    ctx = get_context()

    assert (ctx.precision == 100 and ctx.emax == 1073741823 and
            ctx.emin == -1073741823 and not ctx.subnormalize)


def test_context_2():
    set_context(context())

    with context() as ctx:
        assert ctx.precision == 53
        ctx.precision += 20
        assert ctx.precision == 73

    ctx = get_context()

    assert (ctx.precision == 53 and ctx.emax == 1073741823 and
            ctx.emin == -1073741823 and not ctx.subnormalize)

    with context(ieee(64)) as ctx:
        assert (ctx.precision == 53 and ctx.emax == 1024 and
                ctx.emin == -1073 and ctx.subnormalize)

    ctx = get_context()

    assert (ctx.precision == 53 and ctx.emax == 1073741823 and
            ctx.emin == -1073741823 and not ctx.subnormalize)

    with get_context() as ctx:
        assert ctx.precision == 53
        ctx.precision += 100
        assert ctx.precision == 153

    ctx = get_context()

    assert (ctx.precision == 53 and ctx.emax == 1073741823 and
            ctx.emin == -1073741823 and not ctx.subnormalize)

    with context(precision=200) as ctx:
        assert ctx.precision == 200
        ctx.precision += 100
        assert ctx.precision == 300

    ctx = get_context()

    assert (ctx.precision == 53 and ctx.emax == 1073741823 and
            ctx.emin == -1073741823 and not ctx.subnormalize)


def test_nested_context():
    set_context(context())

    r = [get_context().precision]

    with ieee(128):
        r.append(get_context().precision)
        with ieee(256):
            r.append(get_context().precision)
            with ieee(512):
                r.append(get_context().precision)
            r.append(get_context().precision)
        r.append(get_context().precision)
    r.append(get_context().precision)
    assert r == [53, 113, 237, 489, 237, 113, 53]


@pytest.mark.filterwarnings("ignore:local_context().*:DeprecationWarning")
def test_nested_local_context():
    set_context(context())

    r = [get_context().precision]

    with local_context(ieee(128)):
        r.append(get_context().precision)
        with local_context(ieee(256)):
            r.append(get_context().precision)
            with local_context(ieee(512)):
                r.append(get_context().precision)
            r.append(get_context().precision)
        r.append(get_context().precision)
    r.append(get_context().precision)
    assert r == [53, 113, 237, 489, 237, 113, 53]


def test_context_repr():
    ctx = get_context()
    assert repr(ctx) == \
"""context(precision=53, real_prec=Default, imag_prec=Default,\n\
        round=RoundToNearest, real_round=Default, imag_round=Default,\n\
        emax=1073741823, emin=-1073741823,\n        subnormalize=False,\n\
        trap_underflow=False, underflow=False,\n        trap_overflow=False,\
 overflow=False,\n        trap_inexact=False, inexact=False,\n\
        trap_invalid=False, invalid=False,\n        trap_erange=False,\
 erange=False,\n        trap_divzero=False, divzero=False,\n\
        allow_complex=False,\n        rational_division=False,\n\
        allow_release_gil=False)"""

    ctx.real_prec = 100
    ctx.imag_prec = 200
    assert repr(ctx) == \
"""context(precision=53, real_prec=100, imag_prec=200,\n\
        round=RoundToNearest, real_round=Default, imag_round=Default,\n\
        emax=1073741823, emin=-1073741823,\n        subnormalize=False,\n\
        trap_underflow=False, underflow=False,\n        trap_overflow=False,\
 overflow=False,\n        trap_inexact=False, inexact=False,\n\
        trap_invalid=False, invalid=False,\n        trap_erange=False,\
 erange=False,\n        trap_divzero=False, divzero=False,\n\
        allow_complex=False,\n        rational_division=False,\n\
        allow_release_gil=False)"""
    ctx.trap_invalid = True
    assert repr(ctx) == \
"""context(precision=53, real_prec=100, imag_prec=200,\n\
        round=RoundToNearest, real_round=Default, imag_round=Default,\n\
        emax=1073741823, emin=-1073741823,\n        subnormalize=False,\n\
        trap_underflow=False, underflow=False,\n        trap_overflow=False,\
 overflow=False,\n        trap_inexact=False, inexact=False,\n\
        trap_invalid=True, invalid=False,\n        trap_erange=False,\
 erange=False,\n        trap_divzero=False, divzero=False,\n\
        allow_complex=False,\n        rational_division=False,\n\
        allow_release_gil=False)"""
    pytest.raises(gmpy2.InvalidOperationError, lambda: mpfr('nan') % 123)
    assert repr(ctx) == \
"""context(precision=53, real_prec=100, imag_prec=200,\n\
        round=RoundToNearest, real_round=Default, imag_round=Default,\n\
        emax=1073741823, emin=-1073741823,\n        subnormalize=False,\n\
        trap_underflow=False, underflow=False,\n        trap_overflow=False,\
 overflow=False,\n        trap_inexact=False, inexact=False,\n\
        trap_invalid=True, invalid=True,\n        trap_erange=False,\
 erange=False,\n        trap_divzero=False, divzero=False,\n\
        allow_complex=False,\n        rational_division=False,\n\
        allow_release_gil=False)"""
    set_context(ieee(32))
    ctx = get_context()
    ctx.trap_underflow = True
    c = mpc(0.1 + 0.1j)
    pytest.raises(gmpy2.UnderflowResultError, lambda: c**201)
    assert repr(ctx) == \
"""context(precision=24, real_prec=Default, imag_prec=Default,\n\
        round=RoundToNearest, real_round=Default, imag_round=Default,\n\
        emax=128, emin=-148,\n        subnormalize=True,\n\
        trap_underflow=True, underflow=True,\n        trap_overflow=False,\
 overflow=False,\n        trap_inexact=False, inexact=True,\n\
        trap_invalid=False, invalid=False,\n        trap_erange=False,\
 erange=False,\n        trap_divzero=False, divzero=False,\n\
        allow_complex=False,\n        rational_division=False,\n\
        allow_release_gil=False)"""


def test_local_context_deprecated():
    with pytest.deprecated_call():
        local_context()

    with warnings.catch_warnings():
        warnings.simplefilter("error", DeprecationWarning)
        pytest.raises(DeprecationWarning, lambda: local_context())


@pytest.mark.filterwarnings("ignore:local_context().*:DeprecationWarning")
def test_local_context():
    ctx_orig = get_context()
    ctx_orig.precision = 123
    with context() as ctx:
        assert ctx.precision == 53
    with local_context() as ctx:
        assert ctx.precision == 123
        ctx.precision = 321
    with local_context(ctx_orig) as ctx:
        assert ctx.precision == 123

    pytest.raises(ValueError, lambda: local_context(1, 2))
    pytest.raises(ValueError, lambda: local_context(spam=123))