Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Sep 27, 2024
1 parent e7f5640 commit f0c6673
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
17 changes: 16 additions & 1 deletion vlib/math/big/big_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ const add_test_data = [
AddTest{ 100, -2, 98},
AddTest{ -100, -2, -102},
AddTest{ -2, -100, -102},
//
AddTest{ 1, 200, 201},
AddTest{ -200, -1, -201},
AddTest{ 2, 100, 102},
AddTest{ -100, -2, -102},
AddTest{ 100, -2, 98},
AddTest{ 2, -100, -98},
//
AddTest{ 2, 3, 5 },
AddTest{ 1024, 1024, 2048 },
AddTest{ 1024, 1024, 2048 },
Expand Down Expand Up @@ -181,6 +189,13 @@ const sub_test_data = [
SubTest{ 100, -100, 200},
SubTest{ -100, -100, 0},
//
SubTest{ 1, 200, -199},
SubTest{ -200, -1, -199},
SubTest{ 2, 100, -98},
SubTest{ -100, -2, -98},
SubTest{ 100, -2, 102},
SubTest{ 2, -100, 102},
//
SubTest{ 2, 3, -1 },
SubTest{ 3, 2, 1 },
SubTest{ 1024, 1024, 0 },
Expand Down Expand Up @@ -578,7 +593,7 @@ fn test_is_odd() {

fn test_addition() {
for t in add_test_data {
assert t.augend.parse() + t.addend.parse() == t.sum.parse()
assert t.augend.parse() + t.addend.parse() == t.sum.parse(), 't.augend: ${t.augend} + t.addend: ${t.addend}'
}
}

Expand Down
13 changes: 7 additions & 6 deletions vlib/math/big/integer.v
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,12 @@ pub fn (augend Integer) + (addend Integer) Integer {
return addend.subtract(augend)
}
// Unequal signs, left is positive:
return augend.subtract(addend)
res := augend.subtract(addend)
cmp := augend.abs_cmp(addend)
if cmp < 0 {
return res.neg()
}
return res
}

// - returns the difference of the integers `minuend` and `subtrahend`
Expand All @@ -325,11 +330,7 @@ pub fn (minuend Integer) - (subtrahend Integer) Integer {
if minuend.signum == subtrahend.signum {
return minuend.subtract(subtrahend)
}
// Unequal signs, left is negative:
if minuend.signum == -1 {
return minuend.add(subtrahend)
}
// Unequal signs, left is positive:
// Unequal signs:
return minuend.add(subtrahend)
}

Expand Down

0 comments on commit f0c6673

Please sign in to comment.