-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_context.py
More file actions
230 lines (206 loc) · 8.42 KB
/
test_context.py
File metadata and controls
230 lines (206 loc) · 8.42 KB
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
# Copyright 2009--2015 Mark Dickinson.
#
# This file is part of the bigfloat package.
#
# The bigfloat package is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# The bigfloat package is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with the bigfloat package. If not, see <http://www.gnu.org/licenses/>.
import unittest
import mpfr
from bigfloat.context import (
getcontext,
Context,
DefaultContext,
RoundAwayFromZero,
RoundTiesToEven,
RoundTowardNegative,
RoundTowardPositive,
RoundTowardZero,
_temporary_exponent_bounds,
)
from bigfloat.ieee import quadruple_precision
from bigfloat.rounding_mode import (
ROUND_TIES_TO_EVEN,
ROUND_TOWARD_ZERO,
ROUND_TOWARD_POSITIVE,
ROUND_TOWARD_NEGATIVE,
ROUND_AWAY_FROM_ZERO,
)
all_rounding_modes = [
ROUND_TIES_TO_EVEN,
ROUND_TOWARD_ZERO,
ROUND_TOWARD_POSITIVE,
ROUND_TOWARD_NEGATIVE,
ROUND_AWAY_FROM_ZERO,
]
class ContextTests(unittest.TestCase):
def test__temporary_exponent_bounds(self):
# Failed calls to _temporary_exponent_bounds shouldn't affect emin or
# emax.
original_emin = mpfr.mpfr_get_emin()
original_emax = mpfr.mpfr_get_emax()
# Call to _temporary_exponent_bounds should restore original values.
with _temporary_exponent_bounds(-10, 10):
self.assertEqual(mpfr.mpfr_get_emin(), -10)
self.assertEqual(mpfr.mpfr_get_emax(), 10)
self.assertEqual(mpfr.mpfr_get_emin(), original_emin)
self.assertEqual(mpfr.mpfr_get_emax(), original_emax)
# Even erroneous calls should restore originals.
with self.assertRaises(OverflowError):
with _temporary_exponent_bounds(-10, 10 ** 100):
pass
self.assertEqual(mpfr.mpfr_get_emin(), original_emin)
self.assertEqual(mpfr.mpfr_get_emax(), original_emax)
with self.assertRaises(OverflowError):
with _temporary_exponent_bounds(-10 ** 100, 10):
pass
self.assertEqual(mpfr.mpfr_get_emin(), original_emin)
self.assertEqual(mpfr.mpfr_get_emax(), original_emax)
with self.assertRaises(OverflowError):
with _temporary_exponent_bounds(-10 ** 100, 10 ** 100):
pass
self.assertEqual(mpfr.mpfr_get_emin(), original_emin)
self.assertEqual(mpfr.mpfr_get_emax(), original_emax)
def test_attributes(self):
c = Context(
emin=-999,
emax=999,
precision=100,
subnormalize=True,
rounding=ROUND_TIES_TO_EVEN,
)
self.assertIsInstance(c.precision, int)
self.assertIsInstance(c.emax, int)
self.assertIsInstance(c.emin, int)
self.assertIsInstance(c.subnormalize, bool)
self.assertIn(c.rounding, all_rounding_modes)
def test_bad_rounding_mode(self):
with self.assertRaises(ValueError):
Context(rounding=-1)
def test_default_context(self):
self.assertEqual(
DefaultContext,
quadruple_precision + RoundTiesToEven,
)
def test_rounding_contexts(self):
with RoundTiesToEven:
self.assertEqual(getcontext().rounding, ROUND_TIES_TO_EVEN)
with RoundTowardPositive:
self.assertEqual(getcontext().rounding, ROUND_TOWARD_POSITIVE)
with RoundTowardNegative:
self.assertEqual(getcontext().rounding, ROUND_TOWARD_NEGATIVE)
with RoundTiesToEven:
self.assertEqual(getcontext().rounding, ROUND_TIES_TO_EVEN)
with RoundAwayFromZero:
self.assertEqual(getcontext().rounding, ROUND_AWAY_FROM_ZERO)
# Rounding contexts should not affect existing settings for
# precision, exponents, etc.
original_contexts = [
Context(
precision=234,
emin=-9999,
emax=9999,
subnormalize=True,
rounding=ROUND_TOWARD_NEGATIVE,
),
Context(
precision=5,
emin=-10,
emax=10,
subnormalize=False,
rounding=ROUND_AWAY_FROM_ZERO,
),
]
rounding_contexts = [
RoundTiesToEven,
RoundTowardPositive,
RoundTowardNegative,
RoundTowardZero,
RoundAwayFromZero,
]
for original_context in original_contexts:
for rounding_context in rounding_contexts:
with original_context:
with rounding_context:
self.assertEqual(
getcontext().precision,
original_context.precision,
)
self.assertEqual(
getcontext().emin,
original_context.emin,
)
self.assertEqual(
getcontext().emax,
original_context.emax,
)
self.assertEqual(
getcontext().subnormalize,
original_context.subnormalize,
)
self.assertEqual(
getcontext().rounding,
rounding_context.rounding,
)
def test_hashable(self):
# create equal but non-identical contexts
c1 = Context(emin=-999, emax=999, precision=100,
subnormalize=True, rounding=ROUND_TOWARD_POSITIVE)
c2 = (Context(emax=999, emin=-999, rounding=ROUND_TOWARD_POSITIVE) +
Context(precision=100, subnormalize=True))
self.assertEqual(hash(c1), hash(c2))
self.assertEqual(c1, c2)
self.assertIs(c1 == c2, True)
self.assertIs(c1 != c2, False)
# distinct contexts
d1 = Context(emin=-999, emax=999, precision=100,
subnormalize=True, rounding=ROUND_TOWARD_POSITIVE)
d2 = Context(emin=-999, emax=999, precision=101,
subnormalize=True, rounding=ROUND_TOWARD_POSITIVE)
self.assertIs(d1 != d2, True)
self.assertIs(d1 == d2, False)
def test_with(self):
# check use of contexts in with statements
c = Context(emin=-123, emax=456, precision=1729,
subnormalize=True, rounding=ROUND_TOWARD_POSITIVE)
d = Context(emin=0, emax=10585, precision=20,
subnormalize=False, rounding=ROUND_TOWARD_NEGATIVE)
with c:
# check nested with
with d:
self.assertEqual(getcontext().precision, d.precision)
self.assertEqual(getcontext().emin, d.emin)
self.assertEqual(getcontext().emax, d.emax)
self.assertEqual(getcontext().subnormalize, d.subnormalize)
self.assertEqual(getcontext().rounding, d.rounding)
# check context is restored on normal exit
self.assertEqual(getcontext().precision, c.precision)
self.assertEqual(getcontext().emin, c.emin)
self.assertEqual(getcontext().emax, c.emax)
self.assertEqual(getcontext().subnormalize, c.subnormalize)
self.assertEqual(getcontext().rounding, c.rounding)
# check context is restored on abnormal exit, and that exceptions
# raised within the with block are propagated
try:
with d:
raise ValueError
except ValueError:
pass
else:
self.fail('ValueError not propagated from with block')
self.assertEqual(getcontext().precision, c.precision)
self.assertEqual(getcontext().emin, c.emin)
self.assertEqual(getcontext().emax, c.emax)
self.assertEqual(getcontext().subnormalize, c.subnormalize)
self.assertEqual(getcontext().rounding, c.rounding)
if __name__ == '__main__':
unittest.main()