Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,44 @@

const splitInteger = require('./splitInteger');

function checkInvariants(result, value, numberOfParts) {
// length must equal numberOfParts
expect(result.length).toBe(numberOfParts);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an explicit assertion that every element in the returned array is an integer. The task checklist requires verifying the output is an array of integers. A simple assertion to include inside checkInvariants (near this spot) is: expect(result.every(Number.isInteger)).toBe(true);. This guarantees no non-integer values slip through the implementation.

// sum must equal value
expect(result.reduce((a, b) => a + b, 0)).toBe(value);

// must be sorted ascending
expect(result).toEqual([...result].sort((a, b) => a - b));

// max - min <= 1
const min = Math.min(...result);
const max = Math.max(...result);
expect(max - min).toBeLessThanOrEqual(1);
}

test(`should split a number into equal parts
if a value is divisible by a numberOfParts`, () => {

const result = splitInteger(20, 5);
expect(result).toEqual([4, 4, 4, 4, 4]);
checkInvariants(result, 20, 5);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test that covers the example splitInteger(6, 2) with expected result [3, 3]. The task description requires that example to be present in the test suite. Place it near other example tests and call checkInvariants(result, 6, 2) to reuse invariant checks.

});

test(`should return a part equals to a value
when splitting into 1 part`, () => {

const result = splitInteger(8, 1);
expect(result).toEqual([8]);
checkInvariants(result, 8, 1);
});

test('should sort parts ascending if they are not equal', () => {

const result = splitInteger(17, 4);
expect(result).toEqual([4, 4, 4, 5]);
checkInvariants(result, 17, 4);
});

test('should add zeros if value < numberOfParts', () => {

const result = splitInteger(3, 5);
expect(result).toEqual([0, 0, 1, 1, 1]); // fixed expectation
checkInvariants(result, 3, 5);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Critical: You call checkInvariants(result, 3, 5) here but checkInvariants is not defined in this test file. This will throw a ReferenceError when the tests run. Test helpers that call Jest expect must live in the test file (or be exported from a test utility), not inside the implementation module. Move the helper into this test file or replace this call with inline assertions (length, sum, sorted ascending, and max-min <= 1).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for the example splitInteger(32, 6) with expected result [5, 5, 5, 5, 6, 6]. Also consider adding a small table-driven or randomized test here that iterates several (value, numberOfParts) pairs and asserts all invariants via checkInvariants to increase coverage.

});