avocado/packages/math/test/vector.js
2022-03-14 15:07:47 -05:00

100 lines
3.5 KiB
JavaScript

import {expect} from 'chai';
import {QUARTER_PI, TAU} from '../src/math';
import * as Vector from '../src/vector';
describe('Vector', () => {
it('can scale', () => {
expect(Vector.scale([0.5, 1.5], 2)).to.deep.equal([1, 3]);
});
it('can add', () => {
expect(Vector.add([1, 2], [1, 1])).to.deep.equal([2, 3]);
});
it('can sub', () => {
expect(Vector.sub([9, 5], [5, 2])).to.deep.equal([4, 3]);
});
it('can mul', () => {
expect(Vector.mul([3, 5], [5, 5])).to.deep.equal([15, 25]);
});
it('can div', () => {
expect(Vector.div([15, 5], [5, 5])).to.deep.equal([3, 1]);
});
it('can mod', () => {
expect(Vector.mod([13, 6], [5, 5])).to.deep.equal([3, 1]);
});
it('can distance', () => {
expect(Vector.distance([0, 0], [1, 1])).to.equal(Math.sqrt(2));
});
it('can min', () => {
expect(Vector.min([-10, 10], [0, 0])).to.deep.equal([-10, 0]);
});
it('can max', () => {
expect(Vector.max([-10, 10], [0, 0])).to.deep.equal([0, 10]);
});
it('can clamp', () => {
expect(Vector.clamp([-10, 10], [0, 0], [5, 5])).to.deep.equal([0, 5]);
});
it('can round', () => {
expect(Vector.round([3.14, 4.70])).to.deep.equal([3, 5]);
});
it('can dot', () => {
expect(Vector.dot([2, 3], [4, 5])).to.equal(23);
});
it('can normalize', () => {
expect(Vector.normalize([0.5, 0.7])).to.deep.equal([0.5812381937190965, 0.813733471206735]);
});
it('can abs', () => {
expect(Vector.abs([23, -5.20])).to.deep.equal([23, 5.20]);
});
it('can floor', () => {
expect(Vector.floor([3.14, 4.70])).to.deep.equal([3, 4]);
});
it('can area', () => {
expect(Vector.area([3, 6])).to.equal(18);
});
it('can deep copy', () => {
const vector = [0, 0];
const vector2 = Vector.copy(vector);
expect(Vector.equals(vector, vector2)).to.be.true;
vector[0] = 1;
expect(Vector.equals(vector, vector2)).to.be.false;
});
it('can test for 0', () => {
expect(Vector.isZero([0, 0])).to.be.true;
expect(Vector.isZero([1, 0])).to.be.false;
});
it('can test for NULL', () => {
expect(Vector.isNull([0, 1])).to.be.true;
expect(Vector.isNull([1, 1])).to.be.false;
expect(Vector.isNull(null)).to.be.true;
expect(Vector.isNull([1])).to.be.true;
expect(Vector.isNull([1, 1, 1])).to.be.true;
});
it('can calculate angle', () => {
expect(Vector.toRadians([1, 0])).to.be.closeTo(0, 0.0001);
expect(Vector.toRadians([1, 1])).to.be.closeTo(TAU - 1 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([0, 1])).to.be.closeTo(TAU - 2 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([-1, 1])).to.be.closeTo(TAU - 3 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([-1, 0])).to.be.closeTo(TAU - 4 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([-1, -1])).to.be.closeTo(TAU - 5 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([0, -1])).to.be.closeTo(TAU - 6 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([1, -1])).to.be.closeTo(TAU - 7 * QUARTER_PI, 0.0001);
});
it('can convert to/from directions', () => {
expect(Vector.toDirection4([0, 1])).to.equal(2);
expect(Vector.toDirection4([1, 0])).to.equal(1);
expect(Vector.toDirection8([1, 1])).to.equal(5);
expect(Vector.toDirection8([1, 0])).to.equal(1);
expect(Vector.toDirection([0, 1], 4)).to.equal(2);
for (let i = 0; i < 8; ++i) {
expect(i).to.equal(Vector.toDirection(Vector.fromDirection(i), 8));
}
});
it('can convert to object', () => {
expect(Vector.toObject([0, 16])).to.deep.equal({
x: 0,
y: 16,
});
});
});