File: test_float.py

package info (click to toggle)
python-sdjson 0.5.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 560 kB
  • sloc: python: 1,566; makefile: 6; sh: 6
file content (38 lines) | stat: -rw-r--r-- 973 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
# stdlib
import math

# 3rd party
import pytest

# this package
import sdjson


def test_floats() -> None:
	for num in [1617161771.7650001, math.pi, math.pi**100, math.pi**-100, 3.1]:
		assert float(sdjson.dumps(num)) == num
		assert sdjson.loads(sdjson.dumps(num)) == num


def test_ints() -> None:
	for num in [1, 1 << 32, 1 << 64]:
		assert sdjson.dumps(num) == str(num)
		assert int(sdjson.dumps(num)) == num


def test_out_of_range() -> None:
	assert sdjson.loads("[23456789012E666]") == [float("inf")]
	assert sdjson.loads("[-23456789012E666]") == [float("-inf")]


def test_allow_nan() -> None:
	for val in (float("inf"), float("-inf"), float("nan")):
		out = sdjson.dumps([val])
		if val == val:  # inf
			assert sdjson.loads(out) == [val]
		else:  # nan
			res = sdjson.loads(out)
			assert len(res) == 1
			assert res[0] != res[0]
		with pytest.raises(ValueError, match="Out of range float values are not JSON compliant"):
			sdjson.dumps([val], allow_nan=False)