-
Notifications
You must be signed in to change notification settings - Fork 1.1k
splitInteger tests #1040
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
splitInteger tests #1040
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,18 +4,51 @@ const splitInteger = require('./splitInteger'); | |
|
|
||
| test(`should split a number into equal parts | ||
| if a value is divisible by a numberOfParts`, () => { | ||
| const result = splitInteger(8, 2); | ||
|
|
||
| const min = Math.min(...result); | ||
| const max = Math.max(...result); | ||
|
|
||
| expect(max - min).toBeLessThanOrEqual(1); | ||
| expect(result).toStrictEqual([4, 4]); | ||
| }); | ||
|
|
||
| test(`should return a part equals to a value | ||
| when splitting into 1 part`, () => { | ||
| const result = splitInteger(6, 1); | ||
|
|
||
| expect(result[0]).toBe(6); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assertion is redundant because |
||
| expect(result).toStrictEqual([6]); | ||
| }); | ||
|
|
||
| test('should sort parts ascending if they are not equal', () => { | ||
| const result = splitInteger(5, 3); | ||
| const sorted = [...result].sort((a, b) => a - b); | ||
|
|
||
| const min = Math.min(...result); | ||
| const max = Math.max(...result); | ||
|
|
||
| expect(max - min).toBeLessThanOrEqual(1); | ||
| expect(result).toStrictEqual(sorted); | ||
|
Comment on lines
+26
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These checks are helpful for understanding the requirements, but they are redundant. The |
||
| expect(result).toStrictEqual([1, 2, 2]); | ||
| }); | ||
|
|
||
| test('should add zeros if value < numberOfParts', () => { | ||
| const result = splitInteger(6, 8); | ||
|
|
||
| function countZeros(arr) { | ||
| let count = 0; | ||
|
|
||
| for (const a of arr) { | ||
| if (a === 0) { | ||
| count++; | ||
| } | ||
| } | ||
|
|
||
| return count; | ||
| } | ||
|
|
||
| expect(result).toStrictEqual([0, 0, 1, 1, 1, 1, 1, 1]); | ||
| expect(countZeros(result)).toBe(2); | ||
| expect(result.reduce((a, b) => a + b, 0)).toBe(6); | ||
|
Comment on lines
+39
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These calculations and the assertion on line 12 are redundant. The
toStrictEqual([4, 4])on line 13 is a comprehensive check that already guarantees the difference between the max and min values is0, which satisfies the requirement.