From ce4f810372d31ca03d0abb46199b86b7a896c414 Mon Sep 17 00:00:00 2001 From: Peter Fajdiga Date: Fri, 25 Oct 2024 12:32:27 +0200 Subject: [PATCH] fillSpace: return array of values --- src/lib/utils/fillSpace.ts | 10 +++++----- src/tests/units/utils/fillSpace.ts | 29 +++++++++++++++-------------- src/tests/utils/Assert.ts | 14 ++++++++++++++ 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/lib/utils/fillSpace.ts b/src/lib/utils/fillSpace.ts index 7c3fc2a..3ec4d87 100644 --- a/src/lib/utils/fillSpace.ts +++ b/src/lib/utils/fillSpace.ts @@ -6,16 +6,16 @@ function fillSpace(availableSpace: number, items: { min: number, max: number }[] let decreasable = 0; let low = -Infinity; let high = Infinity; - for (const constraint of items) { - const value = clamp(mean, constraint.min, constraint.max); + for (const item of items) { + const value = clamp(mean, item.min, item.max); requiredSpace += value; - if (mean > constraint.min) { + if (mean > item.min) { decreasable++; if (value > low) { low = value; } } - if (mean < constraint.max) { + if (mean < item.max) { increasable++; if (value < high) { high = value; @@ -38,7 +38,7 @@ function fillSpace(availableSpace: number, items: { min: number, max: number }[] } if (mean === oldMean) { - return mean; + return items.map(item => clamp(mean, item.min, item.max)); } } } diff --git a/src/tests/units/utils/fillSpace.ts b/src/tests/units/utils/fillSpace.ts index 71acaf8..50c9f91 100644 --- a/src/tests/units/utils/fillSpace.ts +++ b/src/tests/units/utils/fillSpace.ts @@ -2,7 +2,7 @@ tests.register("fillSpace", 1, () => { const testCases: { availableSpace: number, items: { min: number, max: number }[], - check: (result: number) => boolean, + expected: number[], }[] = [ { availableSpace: 600, @@ -10,7 +10,7 @@ tests.register("fillSpace", 1, () => { { min: 10, max: 600 }, { min: 10, max: 600 }, ], - check: r => r === 300, + expected: [300, 300], }, { availableSpace: 600, @@ -18,7 +18,7 @@ tests.register("fillSpace", 1, () => { { min: 10, max: 250 }, { min: 10, max: 500 }, ], - check: r => r === 350, + expected: [250, 350], }, { availableSpace: 600, @@ -26,7 +26,7 @@ tests.register("fillSpace", 1, () => { { min: 10, max: 250 }, { min: 400, max: 500 }, ], - check: r => r === 200, + expected: [200, 400], }, { availableSpace: 765, @@ -35,7 +35,7 @@ tests.register("fillSpace", 1, () => { { min: 10, max: 254 }, { min: 10, max: 500 }, ], - check: r => r === 261, + expected: [250, 254, 261], }, { availableSpace: 600, @@ -43,7 +43,7 @@ tests.register("fillSpace", 1, () => { { min: 10, max: 150 }, { min: 400, max: 500 }, ], - check: r => r === 450, + expected: [150, 450], }, { availableSpace: 750, @@ -53,7 +53,7 @@ tests.register("fillSpace", 1, () => { { min: 400, max: 500 }, { min: 10, max: 300 }, ], - check: r => r === 116, + expected: [116, 116, 400, 116], }, { availableSpace: 750, @@ -63,7 +63,7 @@ tests.register("fillSpace", 1, () => { { min: 400, max: 500 }, { min: 10, max: 300 }, ], - check: r => r === 115, + expected: [115, 120, 400, 115], }, { availableSpace: 1200, @@ -71,7 +71,7 @@ tests.register("fillSpace", 1, () => { { min: 10, max: 250 }, { min: 10, max: 500 }, ], - check: r => r >= 500, + expected: [250, 500], }, { availableSpace: 5, @@ -79,7 +79,7 @@ tests.register("fillSpace", 1, () => { { min: 10, max: 250 }, { min: 10, max: 500 }, ], - check: r => r <= 10, + expected: [10, 10], }, { availableSpace: 800, @@ -93,15 +93,16 @@ tests.register("fillSpace", 1, () => { { min: 109, max: 800 }, { min: 10, max: 800 }, ], - check: r => r === 110, + expected: [114, 93, 93, 93, 93, 93, 110, 110], }, ]; for (const testCase of testCases) { const result = fillSpace(testCase.availableSpace, testCase.items); - Assert.assert( - testCase.check(result), - { message: `got ${result} for test case ${JSON.stringify(testCase)}` }, + Assert.equalArrays( + result, + testCase.expected, + { message: JSON.stringify(testCase) }, ); } }); diff --git a/src/tests/utils/Assert.ts b/src/tests/utils/Assert.ts index 5832067..e43cce4 100644 --- a/src/tests/utils/Assert.ts +++ b/src/tests/utils/Assert.ts @@ -65,6 +65,20 @@ namespace Assert { ); } + export function equalArrays( + actual: any[], + expected: any[], + { message, skip=0 }: Options = {}, + ) { + assert( + actual.length === expected.length && actual.every((item, index) => item === expected[index]), + { + message: buildMessage(actual, expected, "Arrays not equal", message), + skip: skip + 1, + }, + ); + } + export function between( actual: any, min: any,