-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(up, down, halfUp): fix handling of numbers close to 0 and roundin…
- Loading branch information
1 parent
274714b
commit 6b30aa0
Showing
20 changed files
with
675 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,69 @@ | ||
import { calculator } from '@dinero.js/calculator-number'; | ||
import * as fc from 'fast-check'; | ||
|
||
import { down } from '../down'; | ||
|
||
describe('down', () => { | ||
describe('decimal factors', () => { | ||
it('rounds down with a positive quotient below half', () => { | ||
expect(down(14, 10, calculator)).toBe(1); | ||
it('does not round with a positive integer quotient', () => { | ||
expect(down(20, 10, calculator)).toBe(2); | ||
}); | ||
it('rounds down with a negative quotient below half', () => { | ||
expect(down(-14, 10, calculator)).toBe(-2); | ||
it('does not round with a negative integer quotient', () => { | ||
expect(down(-20, 10, calculator)).toBe(-2); | ||
}); | ||
it('does not round with a zero quotient', () => { | ||
expect(down(0, 10, calculator)).toBe(0); | ||
}); | ||
it('rounds down with a positive half quotient', () => { | ||
expect(down(15, 10, calculator)).toBe(1); | ||
}); | ||
it('rounds down with a negative half quotient', () => { | ||
expect(down(-15, 10, calculator)).toBe(-2); | ||
}); | ||
it('rounds down with a positive quotient above half', () => { | ||
expect(down(16, 10, calculator)).toBe(1); | ||
it('rounds down with any positive float quotient', () => { | ||
fc.assert( | ||
fc.property(fc.integer({ min: 1, max: 9 }), (a) => { | ||
expect(down(a, 10, calculator)).toBe(0); | ||
}) | ||
); | ||
}); | ||
it('rounds down with a negative quotient above half', () => { | ||
expect(down(-16, 10, calculator)).toBe(-2); | ||
it('rounds down with any negative float quotient', () => { | ||
fc.assert( | ||
fc.property(fc.integer({ min: -9, max: -1 }), (a) => { | ||
expect(down(a, 10, calculator)).toBe(-1); | ||
}) | ||
); | ||
}); | ||
}); | ||
describe('non-decimal factors', () => { | ||
it('rounds down with a positive quotient below half', () => { | ||
expect(down(22, 5, calculator)).toBe(4); | ||
it('does not round with a positive integer quotient', () => { | ||
expect(down(20, 5, calculator)).toBe(4); | ||
}); | ||
it('does not round with a negative integer quotient', () => { | ||
expect(down(-20, 5, calculator)).toBe(-4); | ||
}); | ||
it('rounds down with a negative quotient below half', () => { | ||
expect(down(-22, 5, calculator)).toBe(-5); | ||
it('does not round with a zero quotient', () => { | ||
expect(down(0, 5, calculator)).toBe(0); | ||
}); | ||
it('rounds down with a positive half quotient', () => { | ||
expect(down(3, 2, calculator)).toBe(1); | ||
}); | ||
it('rounds down with a negative half quotient', () => { | ||
expect(down(-3, 2, calculator)).toBe(-2); | ||
}); | ||
it('rounds down with a positive quotient above half', () => { | ||
expect(down(24, 5, calculator)).toBe(4); | ||
it('rounds down with any positive float', () => { | ||
fc.assert( | ||
fc.property(fc.integer({ min: 1, max: 4 }), (a) => { | ||
expect(down(a, 5, calculator)).toBe(0); | ||
}) | ||
); | ||
}); | ||
it('rounds down with a negative quotient above half', () => { | ||
expect(down(-24, 5, calculator)).toBe(-5); | ||
it('rounds down with any negative float', () => { | ||
fc.assert( | ||
fc.property(fc.integer({ min: -4, max: -1 }), (a) => { | ||
expect(down(a, 5, calculator)).toBe(-1); | ||
}) | ||
); | ||
}); | ||
}); | ||
}); |
83 changes: 67 additions & 16 deletions
83
packages/core/src/divide/__tests__/halfAwayFromZero.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,97 @@ | ||
import { calculator } from '@dinero.js/calculator-number'; | ||
import * as fc from 'fast-check'; | ||
|
||
import { halfAwayFromZero } from '../halfAwayFromZero'; | ||
|
||
describe('halfAwayFromZero', () => { | ||
describe('decimal factors', () => { | ||
it('rounds down with a positive quotient below half', () => { | ||
expect(halfAwayFromZero(14, 10, calculator)).toBe(1); | ||
it('does not round with a positive integer quotient', () => { | ||
expect(halfAwayFromZero(20, 10, calculator)).toBe(2); | ||
}); | ||
it('rounds up with a negative quotient below half', () => { | ||
expect(halfAwayFromZero(-14, 10, calculator)).toBe(-1); | ||
it('does not round with a negative integer quotient', () => { | ||
expect(halfAwayFromZero(-20, 10, calculator)).toBe(-2); | ||
}); | ||
it('does not round with a zero quotient', () => { | ||
expect(halfAwayFromZero(0, 10, calculator)).toBe(0); | ||
}); | ||
it('rounds to the nearest integer away from zero with a positive half quotient', () => { | ||
expect(halfAwayFromZero(15, 10, calculator)).toBe(2); | ||
}); | ||
it('rounds to the nearest integer away from zero with a negative half quotient', () => { | ||
expect(halfAwayFromZero(-25, 10, calculator)).toBe(-3); | ||
}); | ||
it('rounds up with a positive quotient above half', () => { | ||
expect(halfAwayFromZero(16, 10, calculator)).toBe(2); | ||
it('rounds up with any positive float quotient above half', () => { | ||
fc.assert( | ||
fc.property(fc.integer({ min: 6, max: 9 }), (a) => { | ||
expect(halfAwayFromZero(a, 10, calculator)).toBe(1); | ||
}) | ||
); | ||
}); | ||
it('rounds down with any negative quotient above half', () => { | ||
fc.assert( | ||
fc.property(fc.integer({ min: -9, max: -6 }), (a) => { | ||
expect(halfAwayFromZero(a, 10, calculator)).toBe(-1); | ||
}) | ||
); | ||
}); | ||
it('rounds down with any positive float quotient below half', () => { | ||
fc.assert( | ||
fc.property(fc.integer({ min: 1, max: 4 }), (a) => { | ||
expect(halfAwayFromZero(a, 10, calculator)).toBe(0); | ||
}) | ||
); | ||
}); | ||
it('rounds down with a negative quotient above half', () => { | ||
expect(halfAwayFromZero(-16, 10, calculator)).toBe(-2); | ||
it('rounds up with any negative quotient below half', () => { | ||
fc.assert( | ||
fc.property(fc.integer({ min: -4, max: -1 }), (a) => { | ||
expect(halfAwayFromZero(a, 10, calculator)).toBe(-0); | ||
}) | ||
); | ||
}); | ||
}); | ||
describe('non-decimal factors', () => { | ||
it('rounds down with a positive quotient below half', () => { | ||
expect(halfAwayFromZero(22, 5, calculator)).toBe(4); | ||
it('does not round with a positive integer quotient', () => { | ||
expect(halfAwayFromZero(20, 5, calculator)).toBe(4); | ||
}); | ||
it('rounds up with a negative quotient below half', () => { | ||
expect(halfAwayFromZero(-22, 5, calculator)).toBe(-4); | ||
it('does not round with a negative integer quotient', () => { | ||
expect(halfAwayFromZero(-20, 5, calculator)).toBe(-4); | ||
}); | ||
it('does not round with a zero quotient', () => { | ||
expect(halfAwayFromZero(0, 5, calculator)).toBe(0); | ||
}); | ||
it('rounds to the nearest integer away from zero with a positive half quotient', () => { | ||
expect(halfAwayFromZero(3, 2, calculator)).toBe(2); | ||
}); | ||
it('rounds to the nearest integer away from zero with a negative half quotient', () => { | ||
expect(halfAwayFromZero(-5, 2, calculator)).toBe(-3); | ||
}); | ||
it('rounds up with a positive quotient above half', () => { | ||
expect(halfAwayFromZero(24, 5, calculator)).toBe(5); | ||
it('rounds up with any positive float quotient above half', () => { | ||
fc.assert( | ||
fc.property(fc.integer({ min: 3, max: 4 }), (a) => { | ||
expect(halfAwayFromZero(a, 5, calculator)).toBe(1); | ||
}) | ||
); | ||
}); | ||
it('rounds down with any negative quotient above half', () => { | ||
fc.assert( | ||
fc.property(fc.integer({ min: -4, max: -3 }), (a) => { | ||
expect(halfAwayFromZero(a, 5, calculator)).toBe(-1); | ||
}) | ||
); | ||
}); | ||
it('rounds down with any positive float quotient below half', () => { | ||
fc.assert( | ||
fc.property(fc.integer({ min: 1, max: 2 }), (a) => { | ||
expect(halfAwayFromZero(a, 5, calculator)).toBe(0); | ||
}) | ||
); | ||
}); | ||
it('rounds down with a negative quotient above half', () => { | ||
expect(halfAwayFromZero(-24, 5, calculator)).toBe(-5); | ||
it('rounds up with any negative quotient below half', () => { | ||
fc.assert( | ||
fc.property(fc.integer({ min: -2, max: -1 }), (a) => { | ||
expect(halfAwayFromZero(a, 5, calculator)).toBe(-0); | ||
}) | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
6b30aa0
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.
Successfully deployed to the following URLs:
dinerojs – ./
dinerojs-git-main-dinerojs.vercel.app
v2.dinerojs.com
dinerojs-dinerojs.vercel.app