Debian Codegraph @

mypyc/test-data/run-floats.test

raw
1 # Test cases for floats (compile and run)
2
3 [case testFloatOps]
4 from __future__ import annotations
5 from typing import Final, Any, cast
6 from testutil import assertRaises, float_vals, FLOAT_MAGIC
7 import math
8
9 def test_arithmetic() -> None:
10 zero = float(0.0)
11 one = zero + 1.0
12 x = one + one / 2.0
13 assert x == 1.5
14 assert x - one == 0.5
15 assert x * x == 2.25
16 assert x / 2.0 == 0.75
17 assert x * (-0.5) == -0.75
18 assert -x == -1.5
19 for x in float_vals:
20 assert repr(-x) == repr(getattr(x, "__neg__")())
21
22 for y in float_vals:
23 assert repr(x + y) == repr(getattr(x, "__add__")(y))
24 assert repr(x - y) == repr(getattr(x, "__sub__")(y))
25 assert repr(x * y) == repr(getattr(x, "__mul__")(y))
26 if y != 0:
27 assert repr(x / y) == repr(getattr(x, "__truediv__")(y))
28
29 def test_mod() -> None:
30 zero = float(0.0)
31 one = zero + 1.0
32 x = one + one / 2.0
33 assert x % 0.4 == 0.29999999999999993
34 assert (-x) % 0.4 == 0.10000000000000009
35 assert x % -0.4 == -0.10000000000000009
36 assert (-x) % -0.4 == -0.29999999999999993
37 for x in float_vals:
38 for y in float_vals:
39 if y != 0:
40 assert repr(x % y) == repr(getattr(x, "__mod__")(y))
41
42 def test_floor_div() -> None:
43 for x in float_vals:
44 for y in float_vals:
45 if y != 0:
46 assert repr(x // y) == repr(getattr(x, "__floordiv__")(y))
47 else:
48 with assertRaises(ZeroDivisionError, "float floor division by zero"):
49 x // y
50
51 def test_mixed_arithmetic() -> None:
52 zf = float(0.0)
53 zn = int()
54 assert (zf + 5.5) + (zn + 1) == 6.5
55 assert (zn - 2) - (zf - 5.5) == 3.5
56 x = zf + 3.4
57 x += zn + 2
58 assert x == 5.4
59
60 def test_arithmetic_errors() -> None:
61 zero = float(0.0)
62 one = zero + 1.0
63 with assertRaises(ZeroDivisionError, "float division by zero"):
64 print(one / zero)
65 with assertRaises(ZeroDivisionError, "float modulo"):
66 print(one % zero)
67
68 def test_comparisons() -> None:
69 zero = float(0.0)
70 one = zero + 1.0
71 x = one + one / 2.0
72 assert x < (1.51 + zero)
73 assert not (x < (1.49 + zero))
74 assert x > (1.49 + zero)
75 assert not (x > (1.51 + zero))
76 assert x <= (1.5 + zero)
77 assert not (x <= (1.49 + zero))
78 assert x >= (1.5 + zero)
79 assert not (x >= (1.51 + zero))
80 for x in float_vals:
81 for y in float_vals:
82 assert (x <= y) == getattr(x, "__le__")(y)
83 assert (x < y) == getattr(x, "__lt__")(y)
84 assert (x >= y) == getattr(x, "__ge__")(y)
85 assert (x > y) == getattr(x, "__gt__")(y)
86 assert (x == y) == getattr(x, "__eq__")(y)
87 assert (x != y) == getattr(x, "__ne__")(y)
88
89 def test_mixed_comparisons() -> None:
90 zf = float(0.0)
91 zn = int()
92 if (zf + 1.0) == (zn + 1):
93 assert True
94 else:
95 assert False
96 if (zf + 1.1) == (zn + 1):
97 assert False
98 else:
99 assert True
100 assert (zf + 1.1) != (zn + 1)
101 assert (zf + 1.1) > (zn + 1)
102 assert not (zf + 0.9) > (zn + 1)
103 assert (zn + 1) < (zf + 1.1)
104
105 def test_boxing_and_unboxing() -> None:
106 x = 1.5
107 boxed: Any = x
108 assert repr(boxed) == "1.5"
109 assert type(boxed) is float
110 y: float = boxed
111 assert y == x
112 boxed_int: Any = 5
113 assert [type(boxed_int)] == [int] # Avoid mypy type narrowing
114 z: float = boxed_int
115 assert z == 5.0
116 for xx in float_vals:
117 bb: Any = xx
118 yy: float = bb
119 assert repr(xx) == repr(bb)
120 assert repr(xx) == repr(yy)
121 for b in True, False:
122 boxed_bool: Any = b
123 assert type(boxed_bool) is bool
124 zz: float = boxed_bool
125 assert zz == int(b)
126
127 def test_unboxing_failure() -> None:
128 boxed: Any = '1.5'
129 with assertRaises(TypeError):
130 x: float = boxed
131
132 def identity(x: float) -> float:
133 return x
134
135 def test_coerce_from_int_literal() -> None:
136 assert identity(34) == 34.0
137 assert identity(-1) == -1.0
138
139 def test_coerce_from_short_tagged_int() -> None:
140 n = int() - 17
141 assert identity(n) == -17.0
142 for i in range(-300, 300):
143 assert identity(i) == float(i)
144
145 def test_coerce_from_long_tagged_int() -> None:
146 n = int() + 2**100
147 x = identity(n)
148 assert repr(x) == '1.2676506002282294e+30'
149 n = int() - 2**100
150 y = identity(n)
151 assert repr(y) == '-1.2676506002282294e+30'
152
153 def test_coerce_from_very_long_tagged_int() -> None:
154 n = int() + 10**1000
155 with assertRaises(OverflowError, "int too large to convert to float"):
156 identity(n)
157 with assertRaises(OverflowError, "int too large to convert to float"):
158 identity(int(n))
159 n = int() - 10**1000
160 with assertRaises(OverflowError, "int too large to convert to float"):
161 identity(n)
162 with assertRaises(OverflowError, "int too large to convert to float"):
163 identity(int(n))
164
165 def test_explicit_conversion_from_int() -> None:
166 float_any: Any = float
167 a = [0, 1, 2, 3, -1, -2, 13257, -928745]
168 for n in range(1, 100):
169 for delta in -1, 0, 1, 2342345:
170 a.append(2**n + delta)
171 a.append(-2**n + delta)
172 for x in a:
173 assert repr(float(x)) == repr(float_any(x))
174
175 def test_explicit_conversion_to_int() -> None:
176 int_any: Any = int
177 for x in float_vals:
178 if math.isinf(x):
179 with assertRaises(OverflowError, "cannot convert float infinity to integer"):
180 int(x)
181 elif math.isnan(x):
182 with assertRaises(ValueError, "cannot convert float NaN to integer"):
183 int(x)
184 else:
185 assert repr(int(x)) == repr(int_any(x))
186
187 # Test some edge cases
188 assert 2**30 == int(2.0**30 + int())
189 assert 2**30 - 1 == int(1073741823.9999999 + int()) # math.nextafter(2.0**30, 0))
190 assert -2**30 - 1 == int(-2.0**30 - 1 + int())
191 assert -2**30 == int(-1073741824.9999998 + int()) # math.nextafter(-2.0**30 - 1, 0)
192 assert 2**62 == int(2.0**62 + int())
193 assert 2**62 == int(2.0**62 - 1 + int())
194 assert -2**62 == int(-2.0**62 + int())
195 assert -2**62 == int(-2.0**62 - 1 + int())
196
197 def str_to_float(x: str) -> float:
198 return float(x)
199
200 def test_str_to_float() -> None:
201 assert str_to_float("1") == 1.0
202 assert str_to_float("1.234567") == 1.234567
203 assert str_to_float("44324") == 44324.0
204 assert str_to_float("23.4") == 23.4
205 assert str_to_float("-43.44e-4") == -43.44e-4
206 assert str_to_float("-43.44e-4") == -43.44e-4
207 assert math.isinf(str_to_float("inf"))
208 assert math.isinf(str_to_float("-inf"))
209 assert str_to_float("inf") > 0.0
210 assert str_to_float("-inf") < 0.0
211 assert math.isnan(str_to_float("nan"))
212 assert math.isnan(str_to_float("NaN"))
213 assert repr(str_to_float("-0.0")) == "-0.0"
214
215 def test_abs() -> None:
216 assert abs(0.0) == 0.0
217 assert abs(-1.234567) == 1.234567
218 assert abs(44324.732) == 44324.732
219 assert abs(-23.4) == 23.4
220 assert abs(-43.44e-4) == 43.44e-4
221 abs_any: Any = abs
222 for x in float_vals:
223 assert repr(abs(x)) == repr(abs_any(x))
224
225 def test_float_min_max() -> None:
226 for x in float_vals:
227 for y in float_vals:
228 min_any: Any = min
229 assert repr(min(x, y)) == repr(min_any(x, y))
230 max_any: Any = max
231 assert repr(max(x, y)) == repr(max_any(x, y))
232
233 def default(x: float = 2) -> float:
234 return x + 1
235
236 def test_float_default_value() -> None:
237 assert default(1.2) == 2.2
238 for i in range(-200, 200):
239 assert default(float(i)) == i + 1
240 assert default() == 3.0
241
242 def test_float_default_value_wrapper() -> None:
243 f: Any = default
244 assert f(1.2) == 2.2
245 for i in range(-200, 200):
246 assert f(float(i)) == i + 1
247 assert f() == 3.0
248
249 class C:
250 def __init__(self, x: float) -> None:
251 self.x = x
252
253 def test_float_attr() -> None:
254 for i in range(-200, 200):
255 f = float(i)
256 c = C(f)
257 assert c.x == f
258 a: Any = c
259 assert a.x == f
260 c.x = FLOAT_MAGIC
261 assert c.x == FLOAT_MAGIC
262 assert a.x == FLOAT_MAGIC
263 a.x = 1.0
264 assert a.x == 1.0
265 a.x = FLOAT_MAGIC
266 assert a.x == FLOAT_MAGIC
267
268 class D:
269 def __init__(self, x: float) -> None:
270 if x:
271 self.x = x
272
273 def test_float_attr_maybe_undefned() -> None:
274 for i in range(-200, 200):
275 if i == 0:
276 d = D(0.0)
277 with assertRaises(AttributeError):
278 d.x
279 a: Any = d
280 with assertRaises(AttributeError):
281 a.x
282 d.x = FLOAT_MAGIC
283 assert d.x == FLOAT_MAGIC
284 assert a.x == FLOAT_MAGIC
285 d.x = 0.0
286 assert d.x == 0.0
287 assert a.x == 0.0
288 a.x = FLOAT_MAGIC
289 assert a.x == FLOAT_MAGIC
290 d = D(0.0)
291 a = cast(Any, d)
292 a.x = FLOAT_MAGIC
293 assert d.x == FLOAT_MAGIC
294 else:
295 f = float(i)
296 d = D(f)
297 assert d.x == f
298 a2: Any = d
299 assert a2.x == f
300
301 def f(x: float) -> float:
302 return x + 1
303
304 def test_return_values() -> None:
305 a: Any = f
306 for i in range(-200, 200):
307 x = float(i)
308 assert f(x) == x + 1
309 assert a(x) == x + 1
310 for x in float_vals:
311 if not math.isnan(x):
312 assert f(x) == x + 1
313 else:
314 assert math.isnan(f(x))
315
316 def exc() -> float:
317 raise IndexError('x')
318
319 def test_exception() -> None:
320 with assertRaises(IndexError):
321 exc()
322 a: Any = exc
323 with assertRaises(IndexError):
324 a()
325
326 def test_undefined_local_var() -> None:
327 if not int():
328 x = -113.0
329 assert x == -113.0
330 if int():
331 y = -113.0
332 with assertRaises(UnboundLocalError, 'local variable "y" referenced before assignment'):
333 print(y)
334 if not int():
335 x2 = -1.0
336 assert x2 == -1.0
337 if int():
338 y2 = -1.0
339 with assertRaises(UnboundLocalError, 'local variable "y2" referenced before assignment'):
340 print(y2)
341
342 def test_tuples() -> None:
343 t1: tuple[float, float] = (1.5, 2.5)
344 assert t1 == tuple([1.5, 2.5])
345 n = int() + 5
346 t2: tuple[float, float, float, float] = (n, 1.5, -7, -113)
347 assert t2 == tuple([5.0, 1.5, -7.0, -113.0])
348
349 [case testFloatGlueMethodsAndInheritance]
350 from typing import Final, Any
351
352 from mypy_extensions import trait
353
354 from testutil import assertRaises
355
356 MAGIC: Final = -113.0
357
358 class Base:
359 def foo(self) -> float:
360 return 5.0
361
362 def bar(self, x: float = 2.0) -> float:
363 return x + 1
364
365 def hoho(self, x: float) -> float:
366 return x - 1
367
368 class Derived(Base):
369 def foo(self, x: float = 5.0) -> float:
370 return x + 10
371
372 def bar(self, x: float = 3, y: float = 20) -> float:
373 return x + y + 2
374
375 def hoho(self, x: float = 7) -> float:
376 return x - 2
377
378 def test_derived_adds_bitmap() -> None:
379 b: Base = Derived()
380 assert b.foo() == 15
381
382 def test_derived_adds_another_default_arg() -> None:
383 b: Base = Derived()
384 assert b.bar() == 25
385 assert b.bar(1) == 23
386 assert b.bar(MAGIC) == MAGIC + 22
387
388 def test_derived_switches_arg_to_have_default() -> None:
389 b: Base = Derived()
390 assert b.hoho(5) == 3
391 assert b.hoho(MAGIC) == MAGIC - 2
392
393 @trait
394 class T:
395 @property
396 def x(self) -> float: ...
397 @property
398 def y(self) -> float: ...
399
400 class C(T):
401 x: float = 1.0
402 y: float = 4
403
404 def test_read_only_property_in_trait_implemented_as_attribute() -> None:
405 c = C()
406 c.x = 5.5
407 assert c.x == 5.5
408 c.x = MAGIC
409 assert c.x == MAGIC
410 assert c.y == 4
411 c.y = 6.5
412 assert c.y == 6.5
413 t: T = C()
414 assert t.y == 4
415 t = c
416 assert t.x == MAGIC
417 c.x = 55.5
418 assert t.x == 55.5
419 assert t.y == 6.5
420 a: Any = c
421 assert a.x == 55.5
422 assert a.y == 6.5
423 a.x = 7.0
424 a.y = 8.0
425 assert a.x == 7
426 assert a.y == 8
427
428 class D(T):
429 xx: float
430
431 @property
432 def x(self) -> float:
433 return self.xx
434
435 @property
436 def y(self) -> float:
437 raise TypeError
438
439 def test_read_only_property_in_trait_implemented_as_property() -> None:
440 d = D()
441 d.xx = 5.0
442 assert d.x == 5
443 d.xx = MAGIC
444 assert d.x == MAGIC
445 with assertRaises(TypeError):
446 d.y
447 t: T = d
448 assert t.x == MAGIC
449 d.xx = 6.0
450 assert t.x == 6
451 with assertRaises(TypeError):
452 t.y
453
454 @trait
455 class T2:
456 x: float
457 y: float
458
459 class C2(T2):
460 pass
461
462 def test_inherit_trait_attribute() -> None:
463 c = C2()
464 c.x = 5.0
465 assert c.x == 5
466 c.x = MAGIC
467 assert c.x == MAGIC
468 with assertRaises(AttributeError):
469 c.y
470 c.y = 6.0
471 assert c.y == 6.0
472 t: T2 = C2()
473 with assertRaises(AttributeError):
474 t.y
475 t = c
476 assert t.x == MAGIC
477 c.x = 55.0
478 assert t.x == 55
479 assert t.y == 6
480 a: Any = c
481 assert a.x == 55
482 assert a.y == 6
483 a.x = 7.0
484 a.y = 8.0
485 assert a.x == 7
486 assert a.y == 8
487
488 class D2(T2):
489 x: float
490 y: float = 4
491
492 def test_implement_trait_attribute() -> None:
493 d = D2()
494 d.x = 5.0
495 assert d.x == 5
496 d.x = MAGIC
497 assert d.x == MAGIC
498 assert d.y == 4
499 d.y = 6.0
500 assert d.y == 6
501 t: T2 = D2()
502 assert t.y == 4
503 t = d
504 assert t.x == MAGIC
505 d.x = 55.0
506 assert t.x == 55
507 assert t.y == 6
508 a: Any = d
509 assert a.x == 55
510 assert a.y == 6
511 a.x = 7.0
512 a.y = 8.0
513 assert a.x == 7
514 assert a.y == 8
515
516 [case testIsInstance]
517 from copysubclass import subc
518 from testutil import float_vals
519 from typing import Any
520 def test_built_in() -> None:
521 for f in float_vals:
522 assert isinstance(float(0) + f, float)
523 assert isinstance(subc(f), float)
524
525 assert not isinstance(set(), float)
526 assert not isinstance((), float)
527 assert not isinstance((1.0, 2.0), float)
528 assert not isinstance({3.14}, float)
529 assert not isinstance(int() + 1, float)
530 assert not isinstance(str() + '4.2', float)
531
532 def test_user_defined() -> None:
533 from userdefinedfloat import float
534
535 f: Any = 3.14
536 assert isinstance(float(), float)
537 assert not isinstance(f, float)
538
539 [file copysubclass.py]
540 class subc(float):
541 pass
542
543 [file userdefinedfloat.py]
544 class float:
545 pass