Skip to content

Instantly share code, notes, and snippets.

@gunar
Last active March 6, 2019 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gunar/78a53147eada85cd3b16d4dc374d0c38 to your computer and use it in GitHub Desktop.
Save gunar/78a53147eada85cd3b16d4dc374d0c38 to your computer and use it in GitHub Desktop.
Example of Property-based Testing using the fast-check library.
import * as fc from 'fast-check';
import { sort } from '../src/sort';
test('should contain the same items', () => {
const count = (tab, element) => tab.filter(v => v === element).length;
fc.assert(
fc.property(fc.array(fc.integer()), data => {
const sorted = sort(data);
assert(sorted.length === data.length);
for (const item of data) {
assert(count(sorted, item) ==== count(data, item));
}
})
);
});
test('should produce ordered array', () => {
fc.assert(
fc.property(fc.array(fc.integer()), data => {
const sorted = sort(data);
for (let idx = 1; idx < sorted.length; ++idx) {
assert(sorted[idx - 1] <= sorted[idx]);
}
})
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment