DEV Community

Nicolas DUBIEN
Nicolas DUBIEN

Posted on • Edited on

2 1

Advent of PBT 2021 - Day 17

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;
Enter fullscreen mode Exit fullscreen mode

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);
});
Enter fullscreen mode Exit fullscreen mode

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.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay