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
50 changes: 41 additions & 9 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,52 @@

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

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

test('should split a number into equal parts if divisible', () => {
const parts = splitInteger(6, 2);
expect(parts).toEqual([3, 3]);
expect(parts).toHaveLength(2);
expect(parts.reduce((a, b) => a + b, 0)).toBe(6);
expect(parts.every(Number.isInteger)).toBe(true);
expect(Math.max(...parts) - Math.min(...parts)).toBeLessThanOrEqual(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 an explicit assertion that the returned array is sorted in ascending (non-decreasing) order for this test. The spec requires the array to be sorted even though other properties (sum, integer elements, max-min) are already checked. Example assertion to add after the existing expects:

expect(parts.every((v, i) => i === 0 || parts[i] >= parts[i - 1])).toBe(true);

This should be inserted after the last expect in this test so sorting is verified for splitInteger(6, 2).

expect(parts.every((v, i) => i === 0 || parts[i] >= parts[i - 1])).toBe(true);
});

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

test('should split a number into almost equal parts', () => {
const parts = splitInteger(32, 6);
expect(parts).toEqual([5, 5, 5, 5, 6, 6]);
expect(parts).toHaveLength(6);
expect(parts.reduce((a, b) => a + b, 0)).toBe(32);
expect(parts.every(Number.isInteger)).toBe(true);
expect(Math.max(...parts) - Math.min(...parts)).toBeLessThanOrEqual(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 the explicit sortedness assertion here as well. The test already checks toEqual([5, 5, 5, 5, 6, 6]), length, sum and max-min <= 1 — but the task requires a dedicated ascending-order check. Insert the same every((v,i) => ... ) check after the existing expects in this test for splitInteger(32, 6).

Comment on lines +16 to +21
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 two missing example tests required by the task description: 1) splitInteger(8, 1) must toEqual([8]) plus asserts for length, sum, integer elements, sortedness, and max - min <= 1; 2) splitInteger(17, 4) must toEqual([4, 4, 4, 5]) plus the same properties. Insert these new tests (for example) after the existing tests so all examples from the description are covered. See the task description for the exact expected outputs.

expect(parts.every((v, i) => i === 0 || parts[i] >= parts[i - 1])).toBe(true);
});

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

test('should handle value < numberOfParts', () => {
const parts = splitInteger(2, 5);
expect(parts).toEqual([0, 0, 0, 1, 1]);
expect(parts).toHaveLength(5);
expect(parts.reduce((a, b) => a + b, 0)).toBe(2);
expect(parts.every(Number.isInteger)).toBe(true);
expect(Math.max(...parts) - Math.min(...parts)).toBeLessThanOrEqual(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 the explicit sortedness assertion for this test too. Although you assert the exact array [0, 0, 0, 1, 1], include the dedicated ascending-order check so the test suite matches the spec. Place the check after the current expects in this value < numberOfParts test.

expect(parts.every((v, i) => i === 0 || parts[i] >= parts[i - 1])).toBe(true);
});

test('should add zeros if value < numberOfParts', () => {
test('should return [8] when splitting 8 into 1 part', () => {
const parts = splitInteger(8, 1);
expect(parts).toEqual([8]);
expect(parts).toHaveLength(1);
expect(parts.reduce((a, b) => a + b, 0)).toBe(8);
expect(parts.every(Number.isInteger)).toBe(true);
expect(Math.max(...parts) - Math.min(...parts)).toBeLessThanOrEqual(1);
expect(parts.every((v, i) => i === 0 || parts[i] >= parts[i - 1])).toBe(true);
});

test('should split 17 into 4 almost equal parts [4, 4, 4, 5]', () => {
const parts = splitInteger(17, 4);
expect(parts).toEqual([4, 4, 4, 5]);
expect(parts).toHaveLength(4);
expect(parts.reduce((a, b) => a + b, 0)).toBe(17);
expect(parts.every(Number.isInteger)).toBe(true);
expect(Math.max(...parts) - Math.min(...parts)).toBeLessThanOrEqual(1);
expect(parts.every((v, i) => i === 0 || parts[i] >= parts[i - 1])).toBe(true);
});