Skip to content

Commit

Permalink
test: add better test for luhn algo
Browse files Browse the repository at this point in the history
  • Loading branch information
Benricheson101 committed Nov 12, 2024
1 parent 11d6b8e commit fad5cd7
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/misc/luhnCheck.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {strictEqual} from 'node:assert';
import assert, {strictEqual} from 'node:assert';
import {describe, it} from 'node:test';
import {computeLuhnCheckDigit, isValidLuhn, toLuhn} from './luhnCheck';
import {
computeLuhnCheckDigit,
fromLuhn,
isValidLuhn,
toLuhn,
} from './luhnCheck';

describe('misc/luhnCheck', () => {
it('should correctly calculate the luhn check digit', () => {
Expand All @@ -15,4 +20,12 @@ describe('misc/luhnCheck', () => {
strictEqual(isValidLuhn(17893729974), true);
strictEqual(isValidLuhn(17893729972), false);
});

it('should correctly add and verify many check digits', () => {
for (let i = 0; i < 100_000; i++) {
const signed = toLuhn(i);
assert(isValidLuhn(signed));
strictEqual(fromLuhn(signed), i);
}
});
});

0 comments on commit fad5cd7

Please sign in to comment.