Skip to content

Commit

Permalink
Merge pull request #2403 from umbraco/v15.1/chore/math-tests
Browse files Browse the repository at this point in the history
Chore: Add unit tests for math functions
  • Loading branch information
nielslyngsoe authored Oct 4, 2024
2 parents 54864b4 + bd9a092 commit 4b63ac9
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/packages/core/utils/math/calculate-extrapolated-value.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect } from '@open-wc/testing';
import { calculateExtrapolatedValue } from './math.js';

describe('calculateExtrapolatedValue', () => {
it('should return NaN if the increase factor is less than 0', () => {
expect(calculateExtrapolatedValue(1, -1)).to.be.NaN;
});

it('should return NaN if the increase factor is equal to 1', () => {
expect(calculateExtrapolatedValue(1, 1)).to.be.NaN;
});

it('should return the extrapolated value', () => {
expect(calculateExtrapolatedValue(1, 0)).to.equal(1);
expect(calculateExtrapolatedValue(1, 0.3)).to.equal(1.4285714285714286);
expect(calculateExtrapolatedValue(2, 0.5)).to.equal(4);
expect(calculateExtrapolatedValue(3, 0.6)).to.equal(7.5);
expect(calculateExtrapolatedValue(100, 0.2)).to.equal(125);
expect(calculateExtrapolatedValue(500, 0.99)).to.equal(49999.999999999956);
});
});
25 changes: 25 additions & 0 deletions src/packages/core/utils/math/clamp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect } from '@open-wc/testing';
import { clamp } from './math.js';

describe('clamp', () => {
it('should not allow the returned value to be lower than min', () => {
expect(clamp(-1, 0, 1)).to.equal(0);
expect(clamp(-100, 5, 100)).to.equal(5);
expect(clamp(-50, -7, 20)).to.equal(-7);
expect(clamp(100, 500, 502)).to.equal(500);
});

it('should not allow the returned value to be higher than max', () => {
expect(clamp(2, 0, 1)).to.equal(1);
expect(clamp(100, 5, 100)).to.equal(100);
expect(clamp(50, -7, 20)).to.equal(20);
expect(clamp(1000, 500, 502)).to.equal(502);
});

it('should return the value if it is within the min and max', () => {
expect(clamp(0, 0, 1)).to.equal(0);
expect(clamp(12, 10, 50)).to.equal(12);
expect(clamp(-48, -50, 50)).to.equal(-48);
expect(clamp(501, 500, 502)).to.equal(501);
});
});
10 changes: 10 additions & 0 deletions src/packages/core/utils/math/distance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect } from '@open-wc/testing';
import { distance } from './math.js';

describe('distance', () => {
it('should return the distance between two points', () => {
expect(distance(5, 10)).to.equal(5);
expect(distance(5.86732, 10.3989328)).to.equal(4.5316128);
expect(distance(-25.8673, 12912.47831)).to.equal(12938.34561);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect } from '@open-wc/testing';
import { getAccumulatedValueOfIndex } from './math.js';

describe('getAccumulatedValueOfIndex', () => {
it('should return the accumulated value of an array up to a certain index', () => {
expect(getAccumulatedValueOfIndex(0, [1, 2, 3, 4, 5])).to.equal(0);
expect(getAccumulatedValueOfIndex(1, [1, 2, 3, 4, 5])).to.equal(1);
expect(getAccumulatedValueOfIndex(2, [1, 2, 3, 4, 5])).to.equal(3);
expect(getAccumulatedValueOfIndex(3, [1, 2, 3, 4, 5])).to.equal(6);
expect(getAccumulatedValueOfIndex(4, [1, 2, 3, 4, 5])).to.equal(10);
expect(getAccumulatedValueOfIndex(5, [1, 2, 3, 4, 5])).to.equal(15);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect } from '@open-wc/testing';
import { getInterpolatedIndexOfPositionInWeightMap } from './math.js';

describe('getInterpolatedIndexOfPositionInWeightMap', () => {
it('should return the interpolated index of a value in a weight map', () => {
const weights = [10, 20, 30, 40, 50];
expect(getInterpolatedIndexOfPositionInWeightMap(-10, weights)).to.equal(0);
expect(getInterpolatedIndexOfPositionInWeightMap(0, weights)).to.equal(0);
expect(getInterpolatedIndexOfPositionInWeightMap(5, weights)).to.equal(0.5);
expect(getInterpolatedIndexOfPositionInWeightMap(15, weights)).to.equal(1.25);
expect(getInterpolatedIndexOfPositionInWeightMap(35, weights)).to.equal(2.1666666666666665);
expect(getInterpolatedIndexOfPositionInWeightMap(45, weights)).to.equal(2.5);
expect(getInterpolatedIndexOfPositionInWeightMap(50, weights)).to.equal(2.6666666666666665);
expect(getInterpolatedIndexOfPositionInWeightMap(60, weights)).to.equal(3);
expect(getInterpolatedIndexOfPositionInWeightMap(100, weights)).to.equal(4);
expect(getInterpolatedIndexOfPositionInWeightMap(5000, weights)).to.equal(5);
});
});
16 changes: 16 additions & 0 deletions src/packages/core/utils/math/inverse-lerp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect } from '@open-wc/testing';
import { inverseLerp, lerp } from './math.js';

describe('inverse lerp', () => {
it('Calculate the inverse lerp factor for a value between two points.', () => {
expect(inverseLerp(10, 20, 15)).to.equal(0.5);
expect(inverseLerp(10, 20, 10)).to.equal(0);
expect(inverseLerp(10, 20, 20)).to.equal(1);
expect(inverseLerp(10, 20, 5)).to.equal(-0.5);
expect(inverseLerp(10, 20, 25)).to.equal(1.5);
});

it('Handle the case where start and end are equal.', () => {
expect(inverseLerp(5, 5, 5)).to.equal(0);
});
});
38 changes: 38 additions & 0 deletions src/packages/core/utils/math/is-within-rect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { expect } from '@open-wc/testing';
import { isWithinRect } from './math.js';

describe('isWithinRect', () => {
const rect = new DOMRect(0, 0, 100, 100);

it('should return true if the point is within the rectangle', () => {
expect(isWithinRect(50, 50, rect)).to.be.true;
expect(isWithinRect(1, 1, rect)).to.be.true;
expect(isWithinRect(99, 99, rect)).to.be.true;
});

it('should return false if the point is outside the rectangle', () => {
expect(isWithinRect(0, 0, rect)).to.be.false;
expect(isWithinRect(100, 100, rect)).to.be.false;
expect(isWithinRect(101, 50, rect)).to.be.false;
expect(isWithinRect(50, 101, rect)).to.be.false;
expect(isWithinRect(-1, 50, rect)).to.be.false;
expect(isWithinRect(50, -1, rect)).to.be.false;
});

it('should return false if the point is on the "border" of the rectangle', () => {
expect(isWithinRect(0, 0, rect)).to.be.false;
expect(isWithinRect(100, 100, rect)).to.be.false;
expect(isWithinRect(100, 0, rect)).to.be.false;
expect(isWithinRect(0, 100, rect)).to.be.false;
});

it('should return true if the point is within the expanded rectangle', () => {
expect(isWithinRect(110, 80, rect, 20)).to.be.true;
expect(isWithinRect(80, 110, rect, 20)).to.be.true;
});

it('should return false if the point is outside the expanded rectangle', () => {
expect(isWithinRect(130, 80, rect, 20)).to.be.false;
expect(isWithinRect(80, 130, rect, 20)).to.be.false;
});
});
15 changes: 15 additions & 0 deletions src/packages/core/utils/math/lerp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from '@open-wc/testing';
import { lerp } from './math.js';

describe('lerp', () => {
it('Interpolate between two values.', () => {
expect(lerp(1, 20, 0.5)).to.equal(10.5);
expect(lerp(1, 100, 0.2)).to.equal(20.8);
expect(lerp(2, 23, 0.4)).to.equal(10.4);
expect(lerp(50, 250, 0.8)).to.equal(210);
});

it('Ensure alpha is clamped to the range [0, 1].', () => {
expect(lerp(10, 20, 1.5)).to.equal(20);
});
});

0 comments on commit 4b63ac9

Please sign in to comment.