Skip to content

Commit b283885

Browse files
committed
test_udecimal: Improve tests based on reported test cases
also, for the common part, ensure that desktop Python's own decimal implementation is used. "print(+c.flags[Foo])" is because udecimal returns 0/1 for these, while Python's decimal returns False/True.
1 parent ec6f61c commit b283885

File tree

1 file changed

+90
-51
lines changed

1 file changed

+90
-51
lines changed

examples/test_udecimal.py

Lines changed: 90 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,83 @@
22
#
33
# SPDX-License-Identifier: Unlicense
44
# pylint: disable=redefined-builtin,wildcard-import,wrong-import-position,unused-wildcard-import,unused-import,broad-except,undefined-variable,used-before-assignment
5-
from jepler_udecimal import *
65

7-
setcontext(ExtendedContext)
8-
print(Decimal(0))
9-
print(Decimal("1"))
10-
print(Decimal("-.0123"))
11-
print(Decimal(123456))
12-
print(Decimal("123.45e12345678"))
13-
print(Decimal("1.33") + Decimal("1.27"))
14-
print(Decimal("12.34") + Decimal("3.87") - Decimal("18.41"))
15-
dig = Decimal(1)
16-
print(dig / Decimal(3))
17-
getcontext().prec = 18
18-
print(dig / Decimal(3))
19-
print(dig.sqrt())
20-
print(Decimal(3).sqrt())
21-
print(Decimal(3) ** 123)
22-
inf = Decimal(1) / Decimal(0)
23-
print(inf)
24-
neginf = Decimal(-1) / Decimal(0)
25-
print(neginf)
26-
print(neginf + inf)
27-
print(neginf * inf)
286
try:
29-
print(dig / 0)
30-
except Exception as e:
31-
print("Division by zero")
32-
getcontext().traps[DivisionByZero] = 1
33-
try:
34-
print(dig / 0)
35-
except Exception as e:
36-
print("Division by zero")
37-
c = Context()
38-
c.traps[InvalidOperation] = 0
39-
print(c.flags[InvalidOperation])
40-
try:
41-
c.divide(Decimal(0), Decimal(0))
42-
except Exception as e:
43-
print("Division by zero")
44-
c.traps[InvalidOperation] = 1
45-
print(c.flags[InvalidOperation])
46-
c.flags[InvalidOperation] = 0
47-
print(c.flags[InvalidOperation])
48-
try:
49-
print(c.divide(Decimal(0), Decimal(0)))
50-
except Exception as e:
51-
print("Division by zero")
52-
print(c.flags[InvalidOperation])
53-
try:
54-
print(c.divide(Decimal(0), Decimal(0)))
55-
except Exception as e:
56-
print("Division by zero")
57-
print(c.flags[InvalidOperation])
7+
from decimal import (
8+
Decimal,
9+
getcontext,
10+
setcontext,
11+
ExtendedContext,
12+
DivisionByZero,
13+
InvalidOperation,
14+
Context,
15+
localcontext,
16+
)
17+
except ImportError:
18+
from jepler_udecimal import (
19+
Decimal,
20+
getcontext,
21+
setcontext,
22+
ExtendedContext,
23+
DivisionByZero,
24+
InvalidOperation,
25+
Context,
26+
localcontext,
27+
)
28+
29+
with localcontext():
30+
setcontext(ExtendedContext)
31+
print(Decimal(0))
32+
print(Decimal("1"))
33+
print(Decimal("-.0123"))
34+
print(Decimal(123456))
35+
print(Decimal("123.45e12345678"))
36+
print(Decimal("1.33") + Decimal("1.27"))
37+
print(Decimal("12.34") + Decimal("3.87") - Decimal("18.41"))
38+
dig = Decimal(1)
39+
print(dig / Decimal(3))
40+
getcontext().prec = 18
41+
print(dig / Decimal(3))
42+
print(dig.sqrt())
43+
print(Decimal(3).sqrt())
44+
print(Decimal(3) ** 123)
45+
inf = Decimal(1) / Decimal(0)
46+
print(inf)
47+
neginf = Decimal(-1) / Decimal(0)
48+
print(neginf)
49+
print(neginf + inf)
50+
print(neginf * inf)
51+
try:
52+
print(dig / 0)
53+
except Exception as e:
54+
print("Division by zero")
55+
getcontext().traps[DivisionByZero] = 1
56+
try:
57+
print(dig / 0)
58+
except Exception as e:
59+
print("Division by zero")
60+
c = Context()
61+
c.traps[InvalidOperation] = 0
62+
print(+c.flags[InvalidOperation])
63+
try:
64+
c.divide(Decimal(0), Decimal(0))
65+
except Exception as e:
66+
print("Division by zero")
67+
c.traps[InvalidOperation] = 1
68+
print(+c.flags[InvalidOperation])
69+
c.flags[InvalidOperation] = 0
70+
print(+c.flags[InvalidOperation])
71+
try:
72+
print(c.divide(Decimal(0), Decimal(0)))
73+
except Exception as e:
74+
print("Division by zero")
75+
print(+c.flags[InvalidOperation])
76+
try:
77+
print(c.divide(Decimal(0), Decimal(0)))
78+
except Exception as e:
79+
print("Division by zero")
80+
print(+c.flags[InvalidOperation])
81+
5882
import jepler_udecimal.utrig
5983
from jepler_udecimal import Decimal
6084

@@ -64,3 +88,18 @@
6488
print(Decimal(".4").tan())
6589
print(Decimal(".5").cos())
6690
print(Decimal(".6").sin())
91+
print(Decimal("0").sin())
92+
print(Decimal("360").sin())
93+
print(Decimal("360e19").sin())
94+
print(Decimal("NaN").cos())
95+
print(Decimal("NaN").tan())
96+
print(Decimal("NaN").sin())
97+
try:
98+
print(Decimal("2").acos())
99+
except Exception as e:
100+
print("exception")
101+
try:
102+
print(Decimal("2").asin())
103+
except Exception as e:
104+
print("exception")
105+
print(Decimal("2").atan())

0 commit comments

Comments
 (0)