Skip to content
Open
Show file tree
Hide file tree
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: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"init": "mate-scripts init",
"start": "mate-scripts start",
"format": "prettier --ignore-path .prettierignore --write './src/**/*.js'",
"format": "prettier --ignore-path .prettierignore --write src/**/*.js",
"lint": "npm run format && mate-scripts lint",
"test:only": "mate-scripts test",
"update": "mate-scripts update",
Expand Down
86 changes: 80 additions & 6 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,86 @@

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

test(`should split a number into equal parts
if a value is divisible by a numberOfParts`, () => {});
describe('splitInteger', () => {
// Provided examples
test('should return [8] for splitInteger(8, 1)', () => {
expect(splitInteger(8, 1)).toEqual([8]);
});

test(`should return a part equals to a value
when splitting into 1 part`, () => {});
test('should return [3, 3] for splitInteger(6, 2)', () => {
expect(splitInteger(6, 2)).toEqual([3, 3]);
});

test('should sort parts ascending if they are not equal', () => {});
test('should return [4, 4, 4, 5] for splitInteger(17, 4)', () => {
expect(splitInteger(17, 4)).toEqual([4, 4, 4, 5]);
});

test('should add zeros if value < numberOfParts', () => {});
test('should return [5, 5, 5, 5, 6, 6] for splitInteger(32, 6)', () => {
expect(splitInteger(32, 6)).toEqual([5, 5, 5, 5, 6, 6]);
});

// General property checks
describe('general properties', () => {
const testCases = [
{ value: 10, parts: 3, expected: [3, 3, 4] },
{ value: 7, parts: 3, expected: [2, 2, 3] },
{
value: 100,
parts: 10,
expected: [10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
},
{ value: 5, parts: 2, expected: [2, 3] },
{ value: 1, parts: 3, expected: [0, 0, 1] },
];

test.each(testCases)(
'should split $value into $parts parts',
({ value, parts, expected }) => {
const result = splitInteger(value, parts);

// 1. Correct length
expect(result).toHaveLength(parts);

// 2. Sum equals original value
const sum = result.reduce((acc, num) => acc + num, 0);

expect(sum).toBe(value);

// 3. Difference between max and min is <= 1
const max = Math.max(...result);
const min = Math.min(...result);

expect(max - min).toBeLessThanOrEqual(1);

// 4. Sorted ascending
const sortedResult = [...result].sort((a, b) => a - b);

expect(result).toEqual(sortedResult);

// 5. Matches expected (for sanity)
expect(result).toEqual(expected);
},
);
});

// Edge cases
describe('edge cases', () => {
test('should handle a single part equal to the value', () => {
expect(splitInteger(42, 1)).toEqual([42]);
});

test('should handle value smaller than the number of parts', () => {
expect(splitInteger(3, 5)).toEqual([0, 0, 1, 1, 1]);
});

test('should handle large numbers', () => {
const value = 1000000;
const parts = 7;
const result = splitInteger(value, parts);

expect(result).toHaveLength(parts);
expect(result.reduce((acc, num) => acc + num, 0)).toBe(value);
expect(Math.max(...result) - Math.min(...result)).toBeLessThanOrEqual(1);
});
});
});
Loading