From 88c7607f511dd103e74a69632f14583e5f2974ae Mon Sep 17 00:00:00 2001 From: Oliwia Lesniak Date: Thu, 25 Sep 2025 10:32:45 +0200 Subject: [PATCH 1/3] Write tests --- src/splitInteger.test.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/splitInteger.test.js b/src/splitInteger.test.js index a610317d..a3166ad2 100644 --- a/src/splitInteger.test.js +++ b/src/splitInteger.test.js @@ -4,18 +4,23 @@ const splitInteger = require('./splitInteger'); test(`should split a number into equal parts if a value is divisible by a numberOfParts`, () => { - + expect(splitInteger(6, 2)).toEqual([3, 3]); + expect(splitInteger(20, 5)).toEqual([4, 4, 4, 4, 4]); }); test(`should return a part equals to a value when splitting into 1 part`, () => { - + expect(splitInteger(8, 1)).toEqual([8]); + expect(splitInteger(100, 1)).toEqual([100]); }); test('should sort parts ascending if they are not equal', () => { - + const result = splitInteger(17, 4); // → [4, 4, 4, 5] + expect(result).toEqual([4, 4, 4, 5]); + expect(result).toEqual(result.slice().sort((a, b) => a - b)); }); test('should add zeros if value < numberOfParts', () => { - + expect(splitInteger(3, 5)).toEqual([0, 0, 0, 1, 2]); + expect(splitInteger(1, 4)).toEqual([0, 0, 0, 1]); }); From 27e03373d8d3fa1599596182ad63bb8047a7871d Mon Sep 17 00:00:00 2001 From: Oliwia Lesniak Date: Thu, 25 Sep 2025 10:43:53 +0200 Subject: [PATCH 2/3] Write tests --- src/splitInteger.js | 15 +++++++++++++++ src/splitInteger.test.js | 7 ++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/splitInteger.js b/src/splitInteger.js index d3da7485..cb156ce2 100644 --- a/src/splitInteger.js +++ b/src/splitInteger.js @@ -19,5 +19,20 @@ function splitInteger(value, numberOfParts) { return parts; } +function checkInvariants(result, value, numberOfParts) { + // length must equal numberOfParts + expect(result.length).toBe(numberOfParts); + + // 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); +} module.exports = splitInteger; diff --git a/src/splitInteger.test.js b/src/splitInteger.test.js index a3166ad2..0474b03e 100644 --- a/src/splitInteger.test.js +++ b/src/splitInteger.test.js @@ -21,6 +21,11 @@ test('should sort parts ascending if they are not equal', () => { }); test('should add zeros if value < numberOfParts', () => { - expect(splitInteger(3, 5)).toEqual([0, 0, 0, 1, 2]); + expect(splitInteger(3, 5)).toEqual([0, 0, 1, 1, 1]); expect(splitInteger(1, 4)).toEqual([0, 0, 0, 1]); }); +test('should add zeros if value < numberOfParts', () => { + const result = splitInteger(3, 5); + expect(result).toEqual([0, 0, 1, 1, 1]); + checkInvariants(result, 3, 5); +}); From 472ccedcb33131b78b4b429b80e913ea7cbcde99 Mon Sep 17 00:00:00 2001 From: Oliwia Lesniak Date: Thu, 25 Sep 2025 10:51:38 +0200 Subject: [PATCH 3/3] Write tests --- src/splitInteger.js | 15 --------------- src/splitInteger.test.js | 36 +++++++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/splitInteger.js b/src/splitInteger.js index cb156ce2..d3da7485 100644 --- a/src/splitInteger.js +++ b/src/splitInteger.js @@ -19,20 +19,5 @@ function splitInteger(value, numberOfParts) { return parts; } -function checkInvariants(result, value, numberOfParts) { - // length must equal numberOfParts - expect(result.length).toBe(numberOfParts); - - // 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); -} module.exports = splitInteger; diff --git a/src/splitInteger.test.js b/src/splitInteger.test.js index 0474b03e..82117def 100644 --- a/src/splitInteger.test.js +++ b/src/splitInteger.test.js @@ -2,30 +2,44 @@ const splitInteger = require('./splitInteger'); +function checkInvariants(result, value, numberOfParts) { + // length must equal numberOfParts + expect(result.length).toBe(numberOfParts); + + // 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`, () => { - expect(splitInteger(6, 2)).toEqual([3, 3]); - expect(splitInteger(20, 5)).toEqual([4, 4, 4, 4, 4]); + const result = splitInteger(20, 5); + expect(result).toEqual([4, 4, 4, 4, 4]); + checkInvariants(result, 20, 5); }); test(`should return a part equals to a value when splitting into 1 part`, () => { - expect(splitInteger(8, 1)).toEqual([8]); - expect(splitInteger(100, 1)).toEqual([100]); + 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); // → [4, 4, 4, 5] + const result = splitInteger(17, 4); expect(result).toEqual([4, 4, 4, 5]); - expect(result).toEqual(result.slice().sort((a, b) => a - b)); + checkInvariants(result, 17, 4); }); -test('should add zeros if value < numberOfParts', () => { - expect(splitInteger(3, 5)).toEqual([0, 0, 1, 1, 1]); - expect(splitInteger(1, 4)).toEqual([0, 0, 0, 1]); -}); test('should add zeros if value < numberOfParts', () => { const result = splitInteger(3, 5); - expect(result).toEqual([0, 0, 1, 1, 1]); + expect(result).toEqual([0, 0, 1, 1, 1]); // fixed expectation checkInvariants(result, 3, 5); });