Last active
March 6, 2019 14:58
-
-
Save gunar/78a53147eada85cd3b16d4dc374d0c38 to your computer and use it in GitHub Desktop.
Example of Property-based Testing using the fast-check library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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