Skip to content

Commit

Permalink
test: add more tests, including lodash suite
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jun 30, 2024
1 parent 99729d7 commit b3539a9
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions src/number/tests/round.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe('round function', () => {
test('rounds to specified precision', () => {
expect(_.round(987.654, 3)).toBe(987.654)
expect(_.round(1.01, 1000)).toBe(1.01)
expect(_.round(1.005, 2)).toBe(1.01)

Check failure on line 13 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/number/tests/round.test.ts > round function > rounds to specified precision

AssertionError: expected 1 to be 1.01 // Object.is equality - Expected + Received - 1.01 + 1 ❯ src/number/tests/round.test.ts:13:31

Check failure on line 13 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.x)

src/number/tests/round.test.ts > round function > rounds to specified precision

AssertionError: expected 1 to be 1.01 // Object.is equality - Expected + Received - 1.01 + 1 ❯ src/number/tests/round.test.ts:13:31

Check failure on line 13 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.x)

src/number/tests/round.test.ts > round function > rounds to specified precision

AssertionError: expected 1 to be 1.01 // Object.is equality - Expected + Received - 1.01 + 1 ❯ src/number/tests/round.test.ts:13:31
expect(_.round(1.0049, 2)).toBe(1.0)
})

test('handles negative precisions', () => {
Expand Down Expand Up @@ -48,4 +50,134 @@ describe('round function', () => {
expect(_.round(1.23456789e50, 5)).toBe(1.23456789e50)
expect(_.round(1e50, -10)).toBe(1e50)
})

test('handles edge cases with large positive precision', () => {
let inputs = Array.from(_.range(1, 16, e => 10 ** e))
let actual = inputs.map(input => _.round(input, 309))
expect(actual).toEqual(inputs)

Check failure on line 57 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/number/tests/round.test.ts > round function > handles edge cases with large positive precision

AssertionError: expected [ 10, Infinity, Infinity, …(13) ] to deeply equal [ 10, 100, 1000, 10000, 100000, …(11) ] - Expected + Received Array [ 10, - 100, - 1000, - 10000, - 100000, - 1000000, - 10000000, - 100000000, - 1000000000, - 10000000000, - 100000000000, - 1000000000000, - 10000000000000, - 100000000000000, - 1000000000000000, - 10000000000000000, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, ] ❯ src/number/tests/round.test.ts:57:20

Check failure on line 57 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.x)

src/number/tests/round.test.ts > round function > handles edge cases with large positive precision

AssertionError: expected [ 10, Infinity, Infinity, …(13) ] to deeply equal [ 10, 100, 1000, 10000, 100000, …(11) ] - Expected + Received Array [ 10, - 100, - 1000, - 10000, - 100000, - 1000000, - 10000000, - 100000000, - 1000000000, - 10000000000, - 100000000000, - 1000000000000, - 10000000000000, - 100000000000000, - 1000000000000000, - 10000000000000000, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, ] ❯ src/number/tests/round.test.ts:57:20

Check failure on line 57 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.x)

src/number/tests/round.test.ts > round function > handles edge cases with large positive precision

AssertionError: expected [ 10, Infinity, Infinity, …(13) ] to deeply equal [ 10, 100, 1000, 10000, 100000, …(11) ] - Expected + Received Array [ 10, - 100, - 1000, - 10000, - 100000, - 1000000, - 10000000, - 100000000, - 1000000000, - 10000000000, - 100000000000, - 1000000000000, - 10000000000000, - 100000000000000, - 1000000000000000, - 10000000000000000, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, + Infinity, ] ❯ src/number/tests/round.test.ts:57:20

inputs = Array.from(_.range(-16, -1, e => 10 ** e))
actual = inputs.map(input => _.round(input, 309))
expect(actual).toEqual(inputs)
})

/**
* The following tests were copied from the Lodash repo.
*
* Some behavior is unsupported and therefore commented out:
* - Coercion of string arguments to numbers
* - Coercion of fractional precision to an integer
* - Coercion of NaN precision to an integer
* - Preservation of -0
*/
describe('lodash suite', () => {
for (const strategy of [Math.round, Math.ceil, Math.floor]) {
const round = (value: number, precision?: number) =>
_.round(value, precision, strategy)

const prefix = strategy.name + ': '
const isCeil = strategy === Math.ceil
const isFloor = strategy === Math.floor

test(prefix + 'return a rounded number without a precision', () => {
const actual = round(4.006)
expect(actual).toBe(isCeil ? 5 : 4)

Check failure on line 84 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/number/tests/round.test.ts > round function > lodash suite > ceil: return a rounded number without a precision

AssertionError: expected 4 to be 5 // Object.is equality - Expected + Received - 5 + 4 ❯ src/number/tests/round.test.ts:84:24

Check failure on line 84 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.x)

src/number/tests/round.test.ts > round function > lodash suite > ceil: return a rounded number without a precision

AssertionError: expected 4 to be 5 // Object.is equality - Expected + Received - 5 + 4 ❯ src/number/tests/round.test.ts:84:24

Check failure on line 84 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.x)

src/number/tests/round.test.ts > round function > lodash suite > ceil: return a rounded number without a precision

AssertionError: expected 4 to be 5 // Object.is equality - Expected + Received - 5 + 4 ❯ src/number/tests/round.test.ts:84:24
})

test(prefix + 'work with a precision of `0`', () => {
const actual = round(4.006, 0)
expect(actual).toBe(isCeil ? 5 : 4)

Check failure on line 89 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/number/tests/round.test.ts > round function > lodash suite > ceil: work with a precision of `0`

AssertionError: expected 4 to be 5 // Object.is equality - Expected + Received - 5 + 4 ❯ src/number/tests/round.test.ts:89:24

Check failure on line 89 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.x)

src/number/tests/round.test.ts > round function > lodash suite > ceil: work with a precision of `0`

AssertionError: expected 4 to be 5 // Object.is equality - Expected + Received - 5 + 4 ❯ src/number/tests/round.test.ts:89:24

Check failure on line 89 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.x)

src/number/tests/round.test.ts > round function > lodash suite > ceil: work with a precision of `0`

AssertionError: expected 4 to be 5 // Object.is equality - Expected + Received - 5 + 4 ❯ src/number/tests/round.test.ts:89:24
})

test(prefix + 'work with a positive precision', () => {
let actual = round(4.016, 2)
expect(actual).toBe(isFloor ? 4.01 : 4.02)

Check failure on line 94 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/number/tests/round.test.ts > round function > lodash suite > floor: work with a positive precision

AssertionError: expected 4.02 to be 4.01 // Object.is equality - Expected + Received - 4.01 + 4.02 ❯ src/number/tests/round.test.ts:94:24

Check failure on line 94 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.x)

src/number/tests/round.test.ts > round function > lodash suite > floor: work with a positive precision

AssertionError: expected 4.02 to be 4.01 // Object.is equality - Expected + Received - 4.01 + 4.02 ❯ src/number/tests/round.test.ts:94:24

Check failure on line 94 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.x)

src/number/tests/round.test.ts > round function > lodash suite > floor: work with a positive precision

AssertionError: expected 4.02 to be 4.01 // Object.is equality - Expected + Received - 4.01 + 4.02 ❯ src/number/tests/round.test.ts:94:24

actual = round(4.1, 2)
expect(actual).toBe(4.1)
})

test(prefix + 'work with a negative precision', () => {
const actual = round(4160, -2)
expect(actual).toBe(isFloor ? 4100 : 4200)

Check failure on line 102 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/number/tests/round.test.ts > round function > lodash suite > floor: work with a negative precision

AssertionError: expected 4200 to be 4100 // Object.is equality - Expected + Received - 4100 + 4200 ❯ src/number/tests/round.test.ts:102:24

Check failure on line 102 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.x)

src/number/tests/round.test.ts > round function > lodash suite > floor: work with a negative precision

AssertionError: expected 4200 to be 4100 // Object.is equality - Expected + Received - 4100 + 4200 ❯ src/number/tests/round.test.ts:102:24

Check failure on line 102 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.x)

src/number/tests/round.test.ts > round function > lodash suite > floor: work with a negative precision

AssertionError: expected 4200 to be 4100 // Object.is equality - Expected + Received - 4100 + 4200 ❯ src/number/tests/round.test.ts:102:24
})

// test(prefix + 'coerce `precision` to an integer', () => {
// let actual = round(4.006, Number.NaN)
// expect(actual).toBe(isCeil ? 5 : 4)
//
// const expected = isFloor ? 4.01 : 4.02
//
// actual = round(4.016, 2.6)
// expect(actual).toBe(expected)
//
// actual = round(4.016, '+2')
// expect(actual).toBe(expected)
// })

test(prefix + 'work with exponential notation and `precision`', () => {
const actual = round(5e1, 2)
expect(actual).toEqual(50)

// actual = func('5e', 1)
// expect(actual).toEqual(Number.NaN)
//
// actual = func('5e1e1', 1)
// expect(actual).toEqual(Number.NaN)
})

// test(prefix + 'preserve the sign of `0`', () => {
// const values: [number, number?][] = [
// [0],
// [-0],
// // ['0'],
// // ['-0'],
// [0, 1],
// [-0, 1],
// // ['0', 1],
// // ['-0', 1],
// ]
// const expected = [
// Number.POSITIVE_INFINITY,
// Number.NEGATIVE_INFINITY,
// // Number.POSITIVE_INFINITY,
// // Number.NEGATIVE_INFINITY,
// Number.POSITIVE_INFINITY,
// Number.NEGATIVE_INFINITY,
// // Number.POSITIVE_INFINITY,
// // Number.NEGATIVE_INFINITY,
// ]
//
// const actual = values.map(args => 1 / round.apply(undefined, args))
//
// expect(actual).toEqual(expected)
// })

test(prefix + "don't return `NaN` for large `precision` values", () => {
// Sanity checks
expect(1e-323).not.toBe(0)
expect(1e308).not.toBe(Number.POSITIVE_INFINITY)
// biome-ignore lint/correctness/noPrecisionLoss:
expect([1e-324, 1e309]).toEqual([0, Number.POSITIVE_INFINITY])

// Any negative precision below -323 would result in NaN, but we
// limit the precision to avoid that.
expect(round(Number.MIN_SAFE_INTEGER, -324)).toBe(

Check failure on line 165 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/number/tests/round.test.ts > round function > lodash suite > round: don't return `NaN` for large `precision` values

AssertionError: expected -0 to be +0 // Object.is equality - Expected + Received - 0 + -0 ❯ src/number/tests/round.test.ts:165:54

Check failure on line 165 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/number/tests/round.test.ts > round function > lodash suite > ceil: don't return `NaN` for large `precision` values

AssertionError: expected -0 to be +0 // Object.is equality - Expected + Received - 0 + -0 ❯ src/number/tests/round.test.ts:165:54

Check failure on line 165 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/number/tests/round.test.ts > round function > lodash suite > floor: don't return `NaN` for large `precision` values

AssertionError: expected -0 to be -Infinity // Object.is equality - Expected + Received - -Infinity + -0 ❯ src/number/tests/round.test.ts:165:54

Check failure on line 165 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.x)

src/number/tests/round.test.ts > round function > lodash suite > round: don't return `NaN` for large `precision` values

AssertionError: expected -0 to be +0 // Object.is equality - Expected + Received - 0 + -0 ❯ src/number/tests/round.test.ts:165:54

Check failure on line 165 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.x)

src/number/tests/round.test.ts > round function > lodash suite > ceil: don't return `NaN` for large `precision` values

AssertionError: expected -0 to be +0 // Object.is equality - Expected + Received - 0 + -0 ❯ src/number/tests/round.test.ts:165:54

Check failure on line 165 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (20.x)

src/number/tests/round.test.ts > round function > lodash suite > floor: don't return `NaN` for large `precision` values

AssertionError: expected -0 to be -Infinity // Object.is equality - Expected + Received - -Infinity + -0 ❯ src/number/tests/round.test.ts:165:54

Check failure on line 165 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.x)

src/number/tests/round.test.ts > round function > lodash suite > round: don't return `NaN` for large `precision` values

AssertionError: expected -0 to be +0 // Object.is equality - Expected + Received - 0 + -0 ❯ src/number/tests/round.test.ts:165:54

Check failure on line 165 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.x)

src/number/tests/round.test.ts > round function > lodash suite > ceil: don't return `NaN` for large `precision` values

AssertionError: expected -0 to be +0 // Object.is equality - Expected + Received - 0 + -0 ❯ src/number/tests/round.test.ts:165:54

Check failure on line 165 in src/number/tests/round.test.ts

View workflow job for this annotation

GitHub Actions / Test (22.x)

src/number/tests/round.test.ts > round function > lodash suite > floor: don't return `NaN` for large `precision` values

AssertionError: expected -0 to be -Infinity // Object.is equality - Expected + Received - -Infinity + -0 ❯ src/number/tests/round.test.ts:165:54
isFloor ? Number.NEGATIVE_INFINITY : 0,
)
expect(round(Number.MAX_SAFE_INTEGER, -324)).toBe(
isCeil ? Number.POSITIVE_INFINITY : 0,
)

// Any positive precision above 308 would result in NaN, but we
// limit the precision to avoid that.
expect(round(Number.MIN_SAFE_INTEGER, 309)).toBe(
Number.MIN_SAFE_INTEGER + 1,
)
expect(round(Number.MAX_SAFE_INTEGER, 309)).toBe(
Number.MAX_SAFE_INTEGER - 1,
)
})
}
})
})

0 comments on commit b3539a9

Please sign in to comment.