Skip to content

Commit 4b63ac9

Browse files
authored
Merge pull request #2403 from umbraco/v15.1/chore/math-tests
Chore: Add unit tests for math functions
2 parents 54864b4 + bd9a092 commit 4b63ac9

8 files changed

+156
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { expect } from '@open-wc/testing';
2+
import { calculateExtrapolatedValue } from './math.js';
3+
4+
describe('calculateExtrapolatedValue', () => {
5+
it('should return NaN if the increase factor is less than 0', () => {
6+
expect(calculateExtrapolatedValue(1, -1)).to.be.NaN;
7+
});
8+
9+
it('should return NaN if the increase factor is equal to 1', () => {
10+
expect(calculateExtrapolatedValue(1, 1)).to.be.NaN;
11+
});
12+
13+
it('should return the extrapolated value', () => {
14+
expect(calculateExtrapolatedValue(1, 0)).to.equal(1);
15+
expect(calculateExtrapolatedValue(1, 0.3)).to.equal(1.4285714285714286);
16+
expect(calculateExtrapolatedValue(2, 0.5)).to.equal(4);
17+
expect(calculateExtrapolatedValue(3, 0.6)).to.equal(7.5);
18+
expect(calculateExtrapolatedValue(100, 0.2)).to.equal(125);
19+
expect(calculateExtrapolatedValue(500, 0.99)).to.equal(49999.999999999956);
20+
});
21+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { expect } from '@open-wc/testing';
2+
import { clamp } from './math.js';
3+
4+
describe('clamp', () => {
5+
it('should not allow the returned value to be lower than min', () => {
6+
expect(clamp(-1, 0, 1)).to.equal(0);
7+
expect(clamp(-100, 5, 100)).to.equal(5);
8+
expect(clamp(-50, -7, 20)).to.equal(-7);
9+
expect(clamp(100, 500, 502)).to.equal(500);
10+
});
11+
12+
it('should not allow the returned value to be higher than max', () => {
13+
expect(clamp(2, 0, 1)).to.equal(1);
14+
expect(clamp(100, 5, 100)).to.equal(100);
15+
expect(clamp(50, -7, 20)).to.equal(20);
16+
expect(clamp(1000, 500, 502)).to.equal(502);
17+
});
18+
19+
it('should return the value if it is within the min and max', () => {
20+
expect(clamp(0, 0, 1)).to.equal(0);
21+
expect(clamp(12, 10, 50)).to.equal(12);
22+
expect(clamp(-48, -50, 50)).to.equal(-48);
23+
expect(clamp(501, 500, 502)).to.equal(501);
24+
});
25+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { expect } from '@open-wc/testing';
2+
import { distance } from './math.js';
3+
4+
describe('distance', () => {
5+
it('should return the distance between two points', () => {
6+
expect(distance(5, 10)).to.equal(5);
7+
expect(distance(5.86732, 10.3989328)).to.equal(4.5316128);
8+
expect(distance(-25.8673, 12912.47831)).to.equal(12938.34561);
9+
});
10+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { expect } from '@open-wc/testing';
2+
import { getAccumulatedValueOfIndex } from './math.js';
3+
4+
describe('getAccumulatedValueOfIndex', () => {
5+
it('should return the accumulated value of an array up to a certain index', () => {
6+
expect(getAccumulatedValueOfIndex(0, [1, 2, 3, 4, 5])).to.equal(0);
7+
expect(getAccumulatedValueOfIndex(1, [1, 2, 3, 4, 5])).to.equal(1);
8+
expect(getAccumulatedValueOfIndex(2, [1, 2, 3, 4, 5])).to.equal(3);
9+
expect(getAccumulatedValueOfIndex(3, [1, 2, 3, 4, 5])).to.equal(6);
10+
expect(getAccumulatedValueOfIndex(4, [1, 2, 3, 4, 5])).to.equal(10);
11+
expect(getAccumulatedValueOfIndex(5, [1, 2, 3, 4, 5])).to.equal(15);
12+
});
13+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { expect } from '@open-wc/testing';
2+
import { getInterpolatedIndexOfPositionInWeightMap } from './math.js';
3+
4+
describe('getInterpolatedIndexOfPositionInWeightMap', () => {
5+
it('should return the interpolated index of a value in a weight map', () => {
6+
const weights = [10, 20, 30, 40, 50];
7+
expect(getInterpolatedIndexOfPositionInWeightMap(-10, weights)).to.equal(0);
8+
expect(getInterpolatedIndexOfPositionInWeightMap(0, weights)).to.equal(0);
9+
expect(getInterpolatedIndexOfPositionInWeightMap(5, weights)).to.equal(0.5);
10+
expect(getInterpolatedIndexOfPositionInWeightMap(15, weights)).to.equal(1.25);
11+
expect(getInterpolatedIndexOfPositionInWeightMap(35, weights)).to.equal(2.1666666666666665);
12+
expect(getInterpolatedIndexOfPositionInWeightMap(45, weights)).to.equal(2.5);
13+
expect(getInterpolatedIndexOfPositionInWeightMap(50, weights)).to.equal(2.6666666666666665);
14+
expect(getInterpolatedIndexOfPositionInWeightMap(60, weights)).to.equal(3);
15+
expect(getInterpolatedIndexOfPositionInWeightMap(100, weights)).to.equal(4);
16+
expect(getInterpolatedIndexOfPositionInWeightMap(5000, weights)).to.equal(5);
17+
});
18+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { expect } from '@open-wc/testing';
2+
import { inverseLerp, lerp } from './math.js';
3+
4+
describe('inverse lerp', () => {
5+
it('Calculate the inverse lerp factor for a value between two points.', () => {
6+
expect(inverseLerp(10, 20, 15)).to.equal(0.5);
7+
expect(inverseLerp(10, 20, 10)).to.equal(0);
8+
expect(inverseLerp(10, 20, 20)).to.equal(1);
9+
expect(inverseLerp(10, 20, 5)).to.equal(-0.5);
10+
expect(inverseLerp(10, 20, 25)).to.equal(1.5);
11+
});
12+
13+
it('Handle the case where start and end are equal.', () => {
14+
expect(inverseLerp(5, 5, 5)).to.equal(0);
15+
});
16+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { expect } from '@open-wc/testing';
2+
import { isWithinRect } from './math.js';
3+
4+
describe('isWithinRect', () => {
5+
const rect = new DOMRect(0, 0, 100, 100);
6+
7+
it('should return true if the point is within the rectangle', () => {
8+
expect(isWithinRect(50, 50, rect)).to.be.true;
9+
expect(isWithinRect(1, 1, rect)).to.be.true;
10+
expect(isWithinRect(99, 99, rect)).to.be.true;
11+
});
12+
13+
it('should return false if the point is outside the rectangle', () => {
14+
expect(isWithinRect(0, 0, rect)).to.be.false;
15+
expect(isWithinRect(100, 100, rect)).to.be.false;
16+
expect(isWithinRect(101, 50, rect)).to.be.false;
17+
expect(isWithinRect(50, 101, rect)).to.be.false;
18+
expect(isWithinRect(-1, 50, rect)).to.be.false;
19+
expect(isWithinRect(50, -1, rect)).to.be.false;
20+
});
21+
22+
it('should return false if the point is on the "border" of the rectangle', () => {
23+
expect(isWithinRect(0, 0, rect)).to.be.false;
24+
expect(isWithinRect(100, 100, rect)).to.be.false;
25+
expect(isWithinRect(100, 0, rect)).to.be.false;
26+
expect(isWithinRect(0, 100, rect)).to.be.false;
27+
});
28+
29+
it('should return true if the point is within the expanded rectangle', () => {
30+
expect(isWithinRect(110, 80, rect, 20)).to.be.true;
31+
expect(isWithinRect(80, 110, rect, 20)).to.be.true;
32+
});
33+
34+
it('should return false if the point is outside the expanded rectangle', () => {
35+
expect(isWithinRect(130, 80, rect, 20)).to.be.false;
36+
expect(isWithinRect(80, 130, rect, 20)).to.be.false;
37+
});
38+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect } from '@open-wc/testing';
2+
import { lerp } from './math.js';
3+
4+
describe('lerp', () => {
5+
it('Interpolate between two values.', () => {
6+
expect(lerp(1, 20, 0.5)).to.equal(10.5);
7+
expect(lerp(1, 100, 0.2)).to.equal(20.8);
8+
expect(lerp(2, 23, 0.4)).to.equal(10.4);
9+
expect(lerp(50, 250, 0.8)).to.equal(210);
10+
});
11+
12+
it('Ensure alpha is clamped to the range [0, 1].', () => {
13+
expect(lerp(10, 20, 1.5)).to.equal(20);
14+
});
15+
});

0 commit comments

Comments
 (0)