Amend divmod (#22131)

* Add Overflow checks & test adjust

* Avoiding nimvm differences in tests

* distinguish DivByZeroDefect
This commit is contained in:
Ryan McConnell
2023-06-20 07:04:34 -04:00
committed by GitHub
parent f524d60fa1
commit db41f04ab0
2 changed files with 17 additions and 5 deletions

View File

@@ -101,6 +101,11 @@ when defined(c) or defined(cpp):
doAssert divmod(5, 2) == (2, 1)
doAssert divmod(5, -3) == (-1, 2)
when T is cint | clong | clonglong:
when compileOption("overflowChecks"):
if y == 0:
raise new(DivByZeroDefect)
elif (x == T.low and y == -1.T):
raise new(OverflowDefect)
let res = divmod_c(x, y)
result[0] = res.quot
result[1] = res.rem
@@ -824,7 +829,7 @@ else: # JS
doAssert 6.5 mod -2.5 == 1.5
doAssert -6.5 mod -2.5 == -1.5
func divmod*(num, denom: int): (int, int) =
func divmod*[T:SomeInteger](num, denom: T): (T, T) =
runnableExamples:
doAssert divmod(5, 2) == (2, 1)
doAssert divmod(5, -3) == (-1, 2)

View File

@@ -190,10 +190,17 @@ template main() =
block: # divmod
doAssert divmod(int.high, 1) == (int.high, 0)
doAssert divmod(-1073741823, 17) == (-63161283, -12)
when not defined(js):
doAssert divmod(int32.high, 1.int32) == (int32.high, 0.int32)
doAssert divmod(1073741823.int32, 5.int32) == (214748364.int32, 3.int32)
doAssert divmod(4611686018427387903.int64, 5.int64) == (922337203685477580.int64, 3.int64)
doAssert divmod(int32.high, 1.int32) == (int32.high, 0.int32)
doAssert divmod(1073741823.int32, 5.int32) == (214748364.int32, 3.int32)
doAssert divmod(4611686018427387903.int64, 5.int64) == (922337203685477580.int64, 3.int64)
when not defined(js) and (not compileOption("panics")) and compileOption("overflowChecks"):
when nimvm:
discard # cannot catch OverflowDefect here
else:
doAssertRaises(OverflowDefect, (discard divmod(cint.low, -1.cint)))
doAssertRaises(OverflowDefect, (discard divmod(clong.low, -1.clong)))
doAssertRaises(OverflowDefect, (discard divmod(clonglong.low, -1.clonglong)))
doAssertRaises(DivByZeroDefect, (discard divmod(1, 0)))
block: # log
doAssert log(4.0, 3.0) ==~ ln(4.0) / ln(3.0)