|
| 1 | +describe("Modular inverse", function() |
| 2 | + local modular_inverse = require("math.modular_inverse") |
| 3 | + |
| 4 | + it("should handle general cases", function() |
| 5 | + assert.equal(5, modular_inverse(3, 7)) |
| 6 | + assert.equal(4, modular_inverse(-1, 5)) |
| 7 | + assert.equal(4, modular_inverse(4, 5)) |
| 8 | + assert.equal(4, modular_inverse(9, 5)) |
| 9 | + assert.equal(17, modular_inverse(5, 21)) |
| 10 | + assert.equal(1, modular_inverse(1, 100)) |
| 11 | + end) |
| 12 | + |
| 13 | + it("should handle cases when inputs are not co-prime", function() |
| 14 | + assert.equal(nil, modular_inverse(2, 2)) |
| 15 | + assert.equal(nil, modular_inverse(5, 15)) |
| 16 | + assert.equal(nil, modular_inverse(0, 1)) |
| 17 | + end) |
| 18 | + |
| 19 | + it("should throw error when modulus is zero", function() |
| 20 | + assert.has_error(function() |
| 21 | + modular_inverse(1, 0) |
| 22 | + end) |
| 23 | + end) |
| 24 | + |
| 25 | + it("should throw error when modulus is negative", function() |
| 26 | + assert.has_error(function() |
| 27 | + modular_inverse(1, -1) |
| 28 | + end) |
| 29 | + end) |
| 30 | +end) |
0 commit comments