-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Write tests #1019
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?
Write tests #1019
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 |
|---|---|---|
|
|
@@ -2,20 +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`, () => { | ||
|
|
||
| const result = splitInteger(20, 5); | ||
| expect(result).toEqual([4, 4, 4, 4, 4]); | ||
| checkInvariants(result, 20, 5); | ||
|
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. Add a test that covers the example |
||
| }); | ||
|
|
||
| test(`should return a part equals to a value | ||
| when splitting into 1 part`, () => { | ||
|
|
||
| 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); | ||
| expect(result).toEqual([4, 4, 4, 5]); | ||
| checkInvariants(result, 17, 4); | ||
| }); | ||
|
|
||
| test('should add zeros if value < numberOfParts', () => { | ||
|
|
||
| const result = splitInteger(3, 5); | ||
| expect(result).toEqual([0, 0, 1, 1, 1]); // fixed expectation | ||
| checkInvariants(result, 3, 5); | ||
|
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. Critical: You call 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. Add a test for the example |
||
| }); | ||
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.
Add an explicit assertion that every element in the returned array is an integer. The task checklist requires verifying the output is an array of integers. A simple assertion to include inside
checkInvariants(near this spot) is:expect(result.every(Number.isInteger)).toBe(true);. This guarantees no non-integer values slip through the implementation.