-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7837d86
commit c68f947
Showing
3 changed files
with
81 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,52 @@ | ||
local gcd = require "algo.Euclid".gcd | ||
local xgcd = require "algo.Euclid".xgcd | ||
|
||
local function negative_test(a, b) | ||
local DEBUG = require "algo.Debug":new(false) | ||
local debugf = DEBUG.debugf | ||
|
||
|
||
local function negative_test(f, a, b) | ||
assert(a < 0 or b < 0) | ||
local ok, err = pcall(gcd, a, b) | ||
local ok, err = pcall(f, a, b) | ||
assert(not ok) | ||
assert(string.find(err, "input numbers must not be negative")) | ||
end | ||
|
||
-- edge cases | ||
local function verify_xgcd(s, t, d, m, n) | ||
debugf("%d * %d + %d * %d = %d", m, s, n, t, d) | ||
assert(m * s + n * t == d) | ||
end | ||
|
||
-- gcd edge cases | ||
assert(gcd(0, 0) == 0) | ||
assert(gcd(1, 0) == 1) | ||
assert(gcd(0, 1) == 1) | ||
|
||
-- negative is not allowed | ||
negative_test(0, -1) | ||
negative_test(-1, 0) | ||
-- negative is not allowed for gcd | ||
negative_test(gcd, 0, -1) | ||
negative_test(gcd, -1, 0) | ||
|
||
-- positive tests | ||
-- gcd positive tests | ||
assert(gcd(123, 54) == 3) | ||
assert(gcd(54, 123) == 3) | ||
|
||
-- negative is not allowed for xgcd | ||
negative_test(xgcd, 0, -1) | ||
negative_test(xgcd, -1, 0) | ||
|
||
-- xgcd edge cases | ||
local d, m, n = xgcd(0, 0) | ||
verify_xgcd(0, 0, d, m, n) | ||
assert(d == 0 and m == 0 and n == 0) | ||
|
||
verify_xgcd(1, 0, xgcd(1, 0)) | ||
verify_xgcd(0, 1, xgcd(0, 1)) | ||
|
||
-- xgcd positive tests | ||
verify_xgcd(240, 46, xgcd(240, 46)) | ||
verify_xgcd(46, 240, xgcd(46, 240)) | ||
verify_xgcd(123, 54, xgcd(123, 54)) | ||
verify_xgcd(54, 123, xgcd(54, 123)) | ||
|
||
verify_xgcd(84, 33, xgcd(84, 33)) | ||
verify_xgcd(270, 192, xgcd(270, 192)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.