Skip to content

Commit 08b3c9c

Browse files
committed
fixed tests for addition/subtraction
1 parent 06a55c2 commit 08b3c9c

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/bigints.nim

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,9 @@ proc unsignedSubtraction(a: var BigInt, b, c: BigInt) =
310310
negate(a)
311311

312312
proc additionInt(a: var BigInt, b: BigInt, c: int32) =
313-
if Negative in b.flags:
313+
if b.isZero:
314+
a = c.initBigInt
315+
elif Negative in b.flags:
314316
if c < 0:
315317
unsignedAdditionInt(a, b, c)
316318
a.flags.incl(Negative)
@@ -358,7 +360,9 @@ template optAddInt*{x = y + z}(x,y: BigInt, z: int32) = additionInt(x, y, z)
358360
template optAdd*{x = y + z}(x,y,z: BigInt) = addition(x, y, z)
359361

360362
proc subtractionInt(a: var BigInt, b: BigInt, c: int32) =
361-
if Negative in b.flags:
363+
if b.isZero:
364+
a = (-c).initBigInt
365+
elif Negative in b.flags:
362366
if c < 0:
363367
# TODO: is this right?
364368
unsignedSubtractionInt(a, b, c)

tests/tester.nim

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ test "empty limbs when uninitialized (https://github.com/def-/nim-bigints/issues
118118
# logic around sign might also play a role
119119
var
120120
zeroEmpty: BigInt # should be treated as zero, same with -zeroEmpty
121-
result: BigInt
122121
let
123122
zeroInt32: int32 = 0
124123
oneInt32: int32 = 1
@@ -156,10 +155,10 @@ test "empty limbs when uninitialized (https://github.com/def-/nim-bigints/issues
156155
check -zeroEmpty == zero # error: fixed
157156

158157
# proc unsignedAdditionInt(a: var BigInt, b: BigInt, c: int32)
159-
check zeroEmpty + 1.int32 == one # fatal[IndexError] in bigints.nim(181) unsignedAdditionInt
160-
check -zeroEmpty + 1.int32 == one # fatal[IndexError] in bigints.nim(245) unsignedSubtractionInt
161-
check zeroEmpty + (-1).int32 == -one # fatal[IndexError] in bigints.nim(245) unsignedSubtractionInt
162-
check -zeroEmpty + (-1).int32 == -one # fatal[IndexError] in bigints.nim(181) unsignedAdditionInt
158+
check zeroEmpty + 1.int32 == one # fixed: fatal[IndexError] in bigints.nim(181) unsignedAdditionInt
159+
check -zeroEmpty + 1.int32 == one # fixed: fatal[IndexError] in bigints.nim(245) unsignedSubtractionInt
160+
check zeroEmpty + (-1).int32 == -one # fixed: fatal[IndexError] in bigints.nim(245) unsignedSubtractionInt
161+
check -zeroEmpty + (-1).int32 == -one # fixed: fatal[IndexError] in bigints.nim(181) unsignedAdditionInt
163162

164163
# proc unsignedAddition(a: var BigInt, b, c: BigInt)
165164
check zeroEmpty + one == one # ok
@@ -180,10 +179,10 @@ test "empty limbs when uninitialized (https://github.com/def-/nim-bigints/issues
180179
check -zeroEmpty + -bigOne == -bigOne # ok
181180

182181
# proc unsignedSubtractionInt(a: var BigInt, b: BigInt, c: int32)
183-
check zeroEmpty - 1.int32 == -one # fatal[IndexError] in bigints.nim(245) unsignedSubtractionInt
184-
check -zeroEmpty - 1.int32 == -one # fatal[IndexError] in bigints.nim(181) unsignedAdditionInt
185-
check zeroEmpty - (-1).int32 == one # fatal[IndexError] in bigints.nim(181) unsignedAdditionInt
186-
check -zeroEmpty - (-1).int32 == one # fatal[IndexError] in bigints.nim(245) unsignedSubtractionInt
182+
check zeroEmpty - 1.int32 == -one # fixed: fatal[IndexError] in bigints.nim(245) unsignedSubtractionInt
183+
check -zeroEmpty - 1.int32 == -one # fixed: fatal[IndexError] in bigints.nim(181) unsignedAdditionInt
184+
check zeroEmpty - (-1).int32 == one # fixed: fatal[IndexError] in bigints.nim(181) unsignedAdditionInt
185+
check -zeroEmpty - (-1).int32 == one # fixed: fatal[IndexError] in bigints.nim(245) unsignedSubtractionInt
187186

188187
# proc unsignedSubtraction(a: var BigInt, b, c: BigInt)
189188
check zeroEmpty - one == -one # ok

0 commit comments

Comments
 (0)