Skip to content

Commit 2a7faeb

Browse files
committedSep 6, 2018
Add property based tests on levenshteinDistance
1 parent c73c333 commit 2a7faeb

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
 

‎src/algorithms/string/levenshtein-distance/__test__/levenshteinDistance.test.js

+31
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as fc from 'fast-check';
12
import levenshteinDistance from '../levenshteinDistance';
23

34
describe('levenshteinDistance', () => {
@@ -26,4 +27,34 @@ describe('levenshteinDistance', () => {
2627
// Needs to substitute the first 5 chars: INTEN by EXECU
2728
expect(levenshteinDistance('intention', 'execution')).toBe(5);
2829
});
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+
));
2960
});

0 commit comments

Comments
 (0)
Please sign in to comment.