Skip to content

Commit

Permalink
fix: Add unit tests for math utility functions and fix median calcula…
Browse files Browse the repository at this point in the history
…tion (#4753)

### Description
This pull request introduces the following changes to the math utility
functions:
#### Fix Median Calculation:
Corrected the median function to handle even-length arrays properly by
averaging the two middle numbers.

#### Add Unit Tests:
Implemented unit tests for the math utility functions using Chai to
ensure their correctness and reliability.
The functions tested include:
- `median`: Tests for both odd and even-length arrays.
- `sum`: Verifies the sum of an array of numbers.
- `mean`: Checks the calculation of the mean.
- `stdDev`: Validates the standard deviation calculation, including
cases with negative numbers.
- `randomInt`: Ensures the generated random integer falls within the
specified range.
<!--
What's included in this PR?
-->

### Drive-by changes
- Code: Updated the median function in math.ts to correctly calculate
the median for even-length arrays.
- Tests: Added a new test file math.test.ts with comprehensive test
cases for each function.
<!--
Are there any minor or drive-by changes also included?
-->

### Related issues

<!--
- Fixes [#[issue number
here]](#4754)
-->
Fixes
[#[4754]](#4754)

### Backward compatibility
Yes
<!--
Are these changes backward compatible? Are there any infrastructure
implications, e.g. changes that would prohibit deploying older commits
using this infra tooling?

Yes/No
-->

### Testing

- All tests have been executed and passed successfully, confirming the
correctness of the functions.
- Special attention was given to edge cases, such as arrays with
negative numbers and random integer generation.
<!--
What kind of testing have these changes undergone?

None/Manual/Unit Tests
-->
  • Loading branch information
tiendn authored Oct 27, 2024
1 parent 892a1d8 commit 0410815
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/healthy-boats-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperlane-xyz/utils': patch
---

fix median utils func + add test
48 changes: 48 additions & 0 deletions typescript/utils/src/math.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { expect } from 'chai';

import { mean, median, randomInt, stdDev, sum } from './math.js';

describe('Math Utility Functions', () => {
describe('median', () => {
it('should return the median of an odd-length array', () => {
expect(median([1, 3, 2])).to.equal(2);
});

it('should return the median of an even-length array', () => {
expect(median([1, 2, 3, 4])).to.equal(2.5);
});

it('should return the median of an even-length array with non sorted numbers', () => {
expect(median([1, 2, 0, 4, 5, 6])).to.equal(3);
});
});

describe('sum', () => {
it('should return the sum of an array', () => {
expect(sum([1, 2, 3, 4])).to.equal(10);
});
});

describe('mean', () => {
it('should return the mean of an array', () => {
expect(mean([1, 2, 3, 4])).to.equal(2.5);
});
});

describe('stdDev', () => {
it('should return the standard deviation of an array', () => {
expect(stdDev([1, 2, 3, 4])).to.be.closeTo(1.118, 0.001);
});
});

describe('randomInt', () => {
it('should return a random integer within the specified range', () => {
const min = 1;
const max = 10;
const result = randomInt(max, min);
expect(result).to.be.at.least(min);
expect(result).to.be.below(max);
expect(result % 1).to.equal(0); // its an integer
});
});
});
2 changes: 1 addition & 1 deletion typescript/utils/src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export function median(a: number[]): number {
const sorted = a.slice().sort();
const mid = Math.floor(sorted.length / 2);
const median =
sorted.length % 2 == 0 ? (sorted[mid] + sorted[mid + 1]) / 2 : sorted[mid];
sorted.length % 2 == 0 ? (sorted[mid - 1] + sorted[mid]) / 2 : sorted[mid];
return median;
}

Expand Down

0 comments on commit 0410815

Please sign in to comment.