fix: GetEuclidGCD(0, 0) is 0 (#1621)

This commit is contained in:
Piotr Idzik
2024-02-28 07:17:22 +01:00
committed by GitHub
parent 0e0cf98ce7
commit c067a34fae
2 changed files with 18 additions and 9 deletions

View File

@ -8,7 +8,6 @@ export function GetEuclidGCD(a, b) {
if (typeof a !== 'number' || typeof b !== 'number') { if (typeof a !== 'number' || typeof b !== 'number') {
throw new TypeError('Arguments must be numbers') throw new TypeError('Arguments must be numbers')
} }
if (a === 0 && b === 0) return undefined // infinitely many numbers divide 0
a = Math.abs(a) a = Math.abs(a)
b = Math.abs(b) b = Math.abs(b)
while (b !== 0) { while (b !== 0) {

View File

@ -1,12 +1,22 @@
import { GetEuclidGCD } from '../GetEuclidGCD' import { GetEuclidGCD } from '../GetEuclidGCD'
function testEuclidGCD(n, m, expected) { describe('GetEuclidGCD', () => {
test('Testing on ' + n + ' and ' + m + '!', () => { it.each([
expect(GetEuclidGCD(n, m)).toBe(expected) [5, 20, 5],
[109, 902, 1],
[290, 780, 10],
[104, 156, 52],
[0, 100, 100],
[-5, 50, 5],
[0, 0, 0],
[1, 1234567, 1]
])('returns correct result for %i and %j', (inputA, inputB, expected) => {
expect(GetEuclidGCD(inputA, inputB)).toBe(expected)
expect(GetEuclidGCD(inputB, inputA)).toBe(expected)
}) })
}
testEuclidGCD(5, 20, 5) it('should throw when any of the inputs is not a number', () => {
testEuclidGCD(109, 902, 1) expect(() => GetEuclidGCD('1', 2)).toThrowError()
testEuclidGCD(290, 780, 10) expect(() => GetEuclidGCD(1, '2')).toThrowError()
testEuclidGCD(104, 156, 52) })
})