Skip to content

Commit

Permalink
test: append for tcomplex; refine tfloat
Browse files Browse the repository at this point in the history
  • Loading branch information
litlighilit committed Jul 31, 2024
1 parent 077b00c commit 633641c
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 19 deletions.
94 changes: 88 additions & 6 deletions tests/tcomplex.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,89 @@

test "complex":
let z = complex(3.0, -4.0)
check z.imag == -4.0
check $z == "(3-4j)"
check z.conjugate() == complex(3.0, 4.0)
check abs(z) == 5.0

proc assertFloatsAreIdentical(x, y: float) =
template `<->`(a, b: bool): bool = not (a xor b)
check (x.isnan <-> y.isnan) or
x == y and (x != 0.0 or copySign(1.0, x) == copySign(1.0, y))

template check(c: PyComplex; r, i) =
let nc = c
assertFloatsAreIdentical nc.real, r
assertFloatsAreIdentical nc.imag, i

suite "complex.__init__(str)":
test "from str":
check(complex("1"), 1.0, 0.0)
check(complex("1j"), 0.0, 1.0)
check(complex("-1"), -1.0, 0.0)
check(complex("+1"), 1.0, 0.0)
check(complex("1+2j"), 1.0, 2.0)
check(complex("(1+2j)"), 1.0, 2.0)
check(complex("(1.5+4.25j)"), 1.5, 4.25)
check(complex("4.25+1J"), 4.25, 1.0)
check(complex(" ( +4.25-6J )"), 4.25, -6.0)
check(complex(" ( +4.25-J )"), 4.25, -1.0)
check(complex(" ( +4.25+j )"), 4.25, 1.0)
check(complex("J"), 0.0, 1.0)
check(complex("( j )"), 0.0, 1.0)
check(complex("+J"), 0.0, 1.0)
check(complex("( -j)"), 0.0, -1.0)
check(complex("1-1j"), 1.0, -1.0)
check(complex("1J"), 0.0, 1.0)

check(complex("1e-500"), 0.0, 0.0)
check(complex("-1e-500j"), 0.0, -0.0)
check(complex("1e-500+1e-500j"), 0.0, 0.0)
check(complex("-1e-500+1e-500j"), -0.0, 0.0)
check(complex("1e-500-1e-500j"), 0.0, -0.0)
check(complex("-1e-500-1e-500j"), -0.0, -0.0)

# SF bug 543840: complex(string) accepts strings with \0
# Fixed in 2.3.
assertRaises(ValueError, complex, "1+1j\0j")
assertRaises(ValueError, complex, "")
assertRaises(ValueError, complex, "\0")
assertRaises(ValueError, complex, "3\x009")
assertRaises(ValueError, complex, "1+")
assertRaises(ValueError, complex, "1+1j+1j")
assertRaises(ValueError, complex, "--")
assertRaises(ValueError, complex, "(1+2j")
assertRaises(ValueError, complex, "1+2j)")
assertRaises(ValueError, complex, "1+(2j)")
assertRaises(ValueError, complex, "(1+2j)123")
assertRaises(ValueError, complex, "x")
assertRaises(ValueError, complex, "1j+2")
assertRaises(ValueError, complex, "1e1ej")
assertRaises(ValueError, complex, "1e++1ej")
assertRaises(ValueError, complex, ")1+2j(")
# the following three are accepted by Python 2.6
assertRaises(ValueError, complex, "1..1j")
assertRaises(ValueError, complex, "1.11.1j")
assertRaises(ValueError, complex, "1e1.1j")

# check whitespace processing
assertEqual(complex("\u2003(\u20021+1j ) "), complex(1, 1))
# Invalid unicode string
# See bpo-34087
assertRaises(ValueError, complex, "\u3053\u3093\u306b\u3061\u306f")

test "negative_nans_from_string":
assertEqual(copysign(1.0, complex("nan").real), 1.0)
assertEqual(copysign(1.0, complex("-nan").real), -1.0)
assertEqual(copysign(1.0, complex("-nanj").imag), -1.0)
assertEqual(copysign(1.0, complex("-nan-nanj").real), -1.0)
assertEqual(copysign(1.0, complex("-nan-nanj").imag), -1.0)

suite "complex":
test "init":
let z = complex(3.0, -4.0)
check z.imag == -4.0
check(complex(imag=1.5), 0.0, 1.5)
check(complex(real=4.25, imag=1.5), 4.25, 1.5)
check(complex(4.25, imag=1.5), 4.25, 1.5)
test "str":
let z = complex(3.0, -4.0)
check $z == "(3-4j)"
test "op":
let z = complex(3.0, -4.0)
check z.conjugate() == complex(3.0, 4.0)
check abs(z) == 5.0
19 changes: 6 additions & 13 deletions tests/tfloat.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,9 @@ const
EPS = fromHex("0x0.0000000000001p0") # diff between 1.0 and next float up

proc identical(x, y: float) =
if x.isnan or y.isnan:
check x.isnan == y.isnan
return
if x == y and (x != 0.0 or copySign(1.0, x)):
return
debugEcho repr(x) & " not identical to " & repr(y)
debugEcho x.hex() & " not identical to " & y.hex()
#let pos = instantiationInfo(index = -1) # no use considering this file will be included
#debugEcho f"at {pos.filename}({pos.line},{pos.column})"
fail()
template `<->`(a, b: bool): bool = not (a xor b)
check (x.isnan <-> y.isnan) or
x == y and (x != 0.0 or copySign(1.0, x) == copySign(1.0, y))

suite "float.fromhex":
test "literals":
Expand Down Expand Up @@ -356,7 +349,7 @@ suite "float":

for _ in range(10000):
var f = rand 1.0
f *= pow(10.0, float rand(2 .. 15))
f *= pow(10.0, float rand(2 .. 10))
let (n, d) = f.as_integer_ratio()
check almostEqual(n/d, f)

Expand All @@ -370,6 +363,6 @@ suite "float":
(float(-2.1).as_integer_ratio()))
check (-2100, 1) == float(-2100.0).as_integer_ratio()

expect(OverflowError): discard Inf.as_integer_ratio()
expect(OverflowError): discard (-Inf).as_integer_ratio()
expect(OverflowDefect): discard Inf.as_integer_ratio()
expect(OverflowDefect): discard (-Inf).as_integer_ratio()
expect(ValueError): discard (NaN).as_integer_ratio()

0 comments on commit 633641c

Please sign in to comment.