Advent of PBT 2021 — Learn how to use property based testing and fast-check through examples
Our algorithm today is: isHumbleNumber.
It comes with the following documentation and prototype:
/** * Check if a number is an Humber number. * * Humble numbers are positive integers which have * no prime factors > 7. * * Source: https://rosettacode.org/wiki/Humble_numbers * * @param n - The number to be checked, * superior or equal to zero and up to 2**31 -1 */ declare function isHumbleNumber(n: number): boolean;
We already wrote some examples based tests for it:
it("should consider any prime number <=7 as humble", () => { expect(isHumbleNumber(2)).toBe(true); expect(isHumbleNumber(3)).toBe(true); expect(isHumbleNumber(5)).toBe(true); expect(isHumbleNumber(7)).toBe(true); }); it("should consider any number <=7 as humble", () => { expect(isHumbleNumber(0)).toBe(true); expect(isHumbleNumber(1)).toBe(true); expect(isHumbleNumber(4)).toBe(true); expect(isHumbleNumber(6)).toBe(true); }); it("should consider any compositite of 2, 3, 5 or 7 as humble", () => { expect(isHumbleNumber(2 * 2 * 2 * 2 * 2)).toBe(true); expect(isHumbleNumber(2 * 2 * 3 * 3 * 5 * 5 * 7 * 7)).toBe(true); }); it("should consider number with prime factor >7 as non-humble", () => { expect(isHumbleNumber(11)).toBe(false); expect(isHumbleNumber(2 * 11)).toBe(false); });
How would you cover it with Property Based Tests?
In order to ease your task we provide you with an already setup CodeSandbox, with examples based tests already written and a possible implementation of the algorithm: https://codesandbox.io/s/advent-of-pbt-day-17-qvdwh?file=/src/index.spec.ts&previewwindow=tests
You wanna see the solution? Here is the set of properties I came with to cover today's algorithm: https://dev.to/dubzzz/advent-of-pbt-2021-day-17-solution-3gfe
Back to "Advent of PBT 2021" to see topics covered during the other days and their solutions.
More about this serie on @ndubien or with the hashtag #AdventOfPBT.
Top comments (0)