|
| 1 | +import * as fc from 'fast-check'; |
1 | 2 | import levenshteinDistance from '../levenshteinDistance';
|
2 | 3 |
|
3 | 4 | describe('levenshteinDistance', () => {
|
@@ -26,4 +27,34 @@ describe('levenshteinDistance', () => {
|
26 | 27 | // Needs to substitute the first 5 chars: INTEN by EXECU
|
27 | 28 | expect(levenshteinDistance('intention', 'execution')).toBe(5);
|
28 | 29 | });
|
| 30 | + |
| 31 | + // Property: |
| 32 | + // for any a, b strings |
| 33 | + // levenshteinDistance(a, b) should be equal to levenshteinDistance(b, a) |
| 34 | + it('should be symmetric [property]', () => fc.assert( |
| 35 | + fc.property( |
| 36 | + fc.fullUnicodeString(), fc.fullUnicodeString(), |
| 37 | + (a, b) => levenshteinDistance(a, b) === levenshteinDistance(b, a) |
| 38 | + ) |
| 39 | + )); |
| 40 | + |
| 41 | + // Property: |
| 42 | + // for any aBegin, aEnd, bBegin, bEnd, common strings |
| 43 | + // levenshteinDistance( |
| 44 | + // aBegin + common + aEnd, |
| 45 | + // bBegin + common + bEnd, |
| 46 | + // ) <= Math.max(aBegin.length, bBegin.length) + Math.max(aEnd.length, bEnd.length) |
| 47 | + it('should not consider common string in the difference [property]', () => fc.assert( |
| 48 | + fc.property( |
| 49 | + fc.fullUnicodeString(), fc.fullUnicodeString(), |
| 50 | + fc.fullUnicodeString(), fc.fullUnicodeString(), |
| 51 | + fc.fullUnicodeString(), |
| 52 | + (aBegin, aEnd, bBegin, bEnd, common) => |
| 53 | + levenshteinDistance(aBegin + common + aEnd, bBegin + common + bEnd) |
| 54 | + <= Math.max(aBegin.length, bBegin.length) + Math.max(aEnd.length, bEnd.length) |
| 55 | + // @TODO: Provide Unicode support for characters outside of the BMP plan. |
| 56 | + // @TODO: Replace previous line by the one below: |
| 57 | + // <= Math.max([...aBegin].length, [...bBegin].length) + Math.max([...aEnd].length, [...bEnd].length) |
| 58 | + ) |
| 59 | + )); |
29 | 60 | });
|
0 commit comments