Debian Codegraph @

rpython/rtyper/test/test_rfloat.py

raw symbols SCIP
1 import math
2 import sys
3 from rpython.translator.translator import TranslationContext
4 from rpython.rtyper.test import snippet
5 from rpython.rtyper.test.tool import BaseRtypingTest
6 from rpython.rlib.rarithmetic import (
7 r_uint, r_longlong, r_ulonglong, r_singlefloat)
8 from rpython.rlib.objectmodel import compute_hash
9
10 class TestSnippet(object):
11
12 def _test(self, func, types):
13 t = TranslationContext()
14 t.buildannotator().build_types(func, types)
15 t.buildrtyper().specialize()
16 t.checkgraphs()
17
18 def test_not1(self):
19 self._test(snippet.not1, [float])
20
21 def test_not2(self):
22 self._test(snippet.not2, [float])
23
24 def test_float1(self):
25 self._test(snippet.float1, [float])
26
27 def test_float_cast1(self):
28 self._test(snippet.float_cast1, [float])
29
30
31 class TestRfloat(BaseRtypingTest):
32
33 inf = 'inf'
34 minus_inf = '-inf'
35 nan = 'nan'
36
37 def test_float2str(self):
38 def fn(f):
39 return str(f)
40
41 res = self.interpret(fn, [1.5])
42 assert float(self.ll_to_string(res)) == 1.5
43 res = self.interpret(fn, [-1.5])
44 assert float(self.ll_to_string(res)) == -1.5
45 inf = 1e200 * 1e200
46 nan = inf/inf
47 res = self.interpret(fn, [inf])
48 assert self.ll_to_string(res) == self.inf
49 res = self.interpret(fn, [-inf])
50 assert self.ll_to_string(res) == self.minus_inf
51 res = self.interpret(fn, [nan])
52 assert self.ll_to_string(res) == self.nan
53
54 def test_string_mod_float(self):
55 def fn(f):
56 return '%f' % f
57
58 res = self.interpret(fn, [1.5])
59 assert float(self.ll_to_string(res)) == 1.5
60
61 def test_int_conversion(self):
62 def fn(f):
63 return int(f)
64
65 res = self.interpret(fn, [1.0])
66 assert res == 1
67 assert type(res) is int
68 res = self.interpret(fn, [2.34])
69 assert res == fn(2.34)
70
71 def test_longlong_conversion(self):
72 def fn(f):
73 return r_longlong(f)
74
75 res = self.interpret(fn, [1.0])
76 assert res == 1
77 # r_longlong is int on a 64 bit system
78 if sys.maxint == 2**63 - 1:
79 assert self.is_of_type(res, int)
80 else:
81 assert self.is_of_type(res, r_longlong)
82 res = self.interpret(fn, [2.34])
83 assert res == fn(2.34)
84 big = float(0x7fffffffffffffff)
85 x = big - 1.e10
86 assert x != big
87 assert fn(x) == 9223372026854775808
88
89 def test_to_r_uint(self):
90 def fn(x):
91 return r_uint(x)
92
93 res = self.interpret(fn, [12.34])
94 assert res == 12
95 bigval = sys.maxint * 1.234
96 res = self.interpret(fn, [bigval])
97 assert long(res) == long(bigval)
98
99 def test_from_r_uint(self):
100 def fn(n):
101 return float(r_uint(n)) / 2
102
103 res = self.interpret(fn, [41])
104 assert self.float_eq(res, 20.5)
105 res = self.interpret(fn, [-9])
106 assert self.float_eq(res, 0.5 * ((sys.maxint+1)*2 - 9))
107
108 def test_to_r_ulonglong(self):
109 def fn(x):
110 return r_ulonglong(x)
111 res = self.interpret(fn, [12.34])
112 assert res == 12
113 bigval = sys.maxint * 1.234
114 res = self.interpret(fn, [bigval])
115 assert long(res) == long(bigval)
116
117 def test_from_r_ulonglong(self):
118 def fn(n):
119 return float(r_ulonglong(n)) / 2
120 res = self.interpret(fn, [41])
121 assert self.float_eq(res, 20.5)
122
123 def test_r_singlefloat(self):
124 def fn(x):
125 y = r_singlefloat(x)
126 return float(y)
127
128 res = self.interpret(fn, [2.1])
129 assert res != 2.1 # precision lost
130 assert abs(res - 2.1) < 1E-6
131
132 def test_float_constant_conversions(self):
133 DIV = r_longlong(10 ** 10)
134 def fn():
135 return 420000000000.0 / DIV
136
137 res = self.interpret(fn, [])
138 assert self.float_eq(res, 42.0)
139
140 def test_exceptions(self):
141 def fn(x, y, z):
142 try:
143 # '/', when float-based, cannot raise in RPython!
144 # the try:finally: only tests an annotation bug.
145 x /= (y / z)
146 finally:
147 return x
148 self.interpret(fn, [1.0, 2.0, 3.0])
149
150 def test_copysign(self):
151 def fn(x, y):
152 return math.copysign(x, y)
153 assert self.interpret(fn, [42, -1]) == -42
154 assert self.interpret(fn, [42, -0.0]) == -42
155 assert self.interpret(fn, [42, 0.0]) == 42
156
157 def test_rstring_to_float(self):
158 from rpython.rlib.rfloat import rstring_to_float
159 def fn(i):
160 s = ['42.3', '123.4'][i]
161 return rstring_to_float(s)
162 assert self.interpret(fn, [0]) == 42.3
163
164 def test_isnan(self):
165 def fn(x, y):
166 n1 = x * x
167 n2 = y * y * y
168 return math.isnan(n1 / n2)
169 assert self.interpret(fn, [1e200, 1e200]) # nan
170 assert not self.interpret(fn, [1e200, 1.0]) # +inf
171 assert not self.interpret(fn, [1e200, -1.0]) # -inf
172 assert not self.interpret(fn, [42.5, 2.3]) # +finite
173 assert not self.interpret(fn, [42.5, -2.3]) # -finite
174
175 def test_isinf(self):
176 def fn(x, y):
177 n1 = x * x
178 n2 = y * y * y
179 return math.isinf(n1 / n2)
180 assert self.interpret(fn, [1e200, 1.0]) # +inf
181 assert self.interpret(fn, [1e200, -1.0]) # -inf
182 assert not self.interpret(fn, [1e200, 1e200]) # nan
183 assert not self.interpret(fn, [42.5, 2.3]) # +finite
184 assert not self.interpret(fn, [42.5, -2.3]) # -finite
185
186 def test_isfinite(self):
187 from rpython.rlib import rfloat
188 def fn(x, y):
189 n1 = x * x
190 n2 = y * y * y
191 return rfloat.isfinite(n1 / n2)
192 assert self.interpret(fn, [42.5, 2.3]) # +finite
193 assert self.interpret(fn, [42.5, -2.3]) # -finite
194 assert not self.interpret(fn, [1e200, 1.0]) # +inf
195 assert not self.interpret(fn, [1e200, -1.0]) # -inf
196 assert not self.interpret(fn, [1e200, 1e200]) # nan
197
198 def test_formatd(self):
199 from rpython.rlib.rfloat import formatd
200 def f(x):
201 return formatd(x, 'f', 2, 0)
202 res = self.ll_to_string(self.interpret(f, [10/3.0]))
203 assert res == '3.33'
204
205 def test_formatd_g(self):
206 from rpython.rlib import rfloat
207 for flags in [0, rfloat.DTSF_ADD_DOT_0]:
208 def f(y):
209 return rfloat.formatd(y, 'g', 2, flags)
210
211 assert self.ll_to_string(self.interpret(f, [3.0])) == f(3.0)
212
213 def test_formatd_repr(self):
214 from rpython.rlib.rfloat import formatd
215 def f(x):
216 return formatd(x, 'r', 0, 0)
217 res = self.ll_to_string(self.interpret(f, [1.1]))
218 assert res == '1.1'
219
220 def test_formatd_huge(self):
221 from rpython.rlib.rfloat import formatd
222 def f(x):
223 return formatd(x, 'f', 1234, 0)
224 res = self.ll_to_string(self.interpret(f, [1.0]))
225 assert res == '1.' + 1234 * '0'
226
227 def test_formatd_F(self):
228 from rpython.translator.c.test.test_genc import compile
229 from rpython.rlib.rfloat import formatd
230
231 def func(x):
232 # Test the %F format, which is not supported by
233 # the Microsoft's msvcrt library.
234 return formatd(x, 'F', 4)
235
236 f = compile(func, [float])
237 assert f(10/3.0) == '3.3333'
238
239 def test_string_to_float(self):
240 from rpython.rlib.rfloat import rstring_to_float
241 def func(x):
242 if x == 0:
243 s = '1e23'
244 else:
245 s = '-1e23'
246 return rstring_to_float(s)
247
248 assert self.interpret(func, [0]) == 1e23
249 assert self.interpret(func, [1]) == -1e23
250
251 def test_hash(self):
252 def fn(f):
253 return compute_hash(f)
254 res = self.interpret(fn, [1.5])
255 assert res == compute_hash(1.5)
256
257 def test_float_constant_inf(self):
258 from rpython.rlib.rfloat import INFINITY
259 def fn():
260 return INFINITY # float('inf') is not supported by RPython so far
261 res = self.interpret(fn, [])
262 assert res == float('inf')