Skip to content

Commit

Permalink
Add `should have at most the same number of each character as its inp…
Browse files Browse the repository at this point in the history
…uts`
  • Loading branch information
dubzzz committed Oct 22, 2019
1 parent f281006 commit 5c2e725
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/diff-sequences/src/__tests__/index.property.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ const findCommonItems = (a: Array<string>, b: Array<string>): Array<string> => {
return array;
};

const extractCount = (data: Array<string>): Map<string, number> => {
const countPerChar = new Map<string, number>();
for (const item of data) {
const currentCount = countPerChar.get(item) || 0;
countPerChar.set(item, currentCount + 1);
}
return countPerChar;
};

const flatten = (data: Array<Array<string>>) => {
const array: Array<string> = [];
for (const items of data) {
Expand Down Expand Up @@ -63,6 +72,20 @@ it('should have at most the length of its inputs', () => {
);
});

it('should have at most the same number of each character as its inputs', () => {
fc.assert(
fc.property(fc.array(fc.char()), fc.array(fc.char()), (a, b) => {
const commonItems = findCommonItems(a, b);
const commonCount = extractCount(commonItems);
const aCount = extractCount(a);
for (const [item, count] of commonCount) {
const countOfItemInA = aCount.get(item) || 0;
expect(countOfItemInA).toBeGreaterThanOrEqual(count);
}
}),
);
});

it('should be no-op when passing common items', () => {
fc.assert(
fc.property(fc.array(fc.char()), fc.array(fc.char()), (a, b) => {
Expand Down

0 comments on commit 5c2e725

Please sign in to comment.