0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2025-03-14 00:38:41 -04:00

Vertices.area unit tests

This commit is contained in:
gsenden 2024-05-17 21:45:23 +02:00
parent 99f2295b86
commit 0c151ae7e7
2 changed files with 295 additions and 27 deletions

View file

@ -1,12 +1,200 @@
const Vector = require("../../src/geometry/Vector")
const TestSquare = [
const testSquare = [
{ x: 1., y: 1., },
{ x: 3., y: 1., },
{ x: 3., y: 3., },
{ x: 1., y: 3., },
];
const testBodyPartsWithoutParent = [
{
isSensor: true,
isSleeping: true,
isStatic: true,
angle: 201.,
anglePrev: 202.,
angularSpeed: 203.,
angularVelocity: 204.,
area: 205.,
axes: [{ x: 206., y: 207. }, { x: 208., y: 209. }],
bounds: {min: { x: 210., y: 211., },max: { x: 212., y: 213., },},
chamfer: [{x: 214., y: 215.},{x: 116., y: 217.}, ],
circleRadius: 218.,
collisionFilter: {category: 0x00DB, mask: 0x000000DC, group: 221},
constraintImpulse: { x: 222., y: 223., angle: 224. },
deltaTime: 225.,
density: 226.,
events: null,
force: {x: 227., y: 228.},
friction: 229.,
frictionAir: 230.,
frictionStatic: 231.,
id: 232,
inertia: 233.,
inverseInertia: 234.,
inverseMass: 235.,
label: 'Body',
mass: 236.,
motion: 237.,
parts: [],
plugin: {},
position: { x: 239., y: 240., },
positionImpulse: { x: 241., y: 242. },
positionPrev: { x: 243., y: 244., },
render: {visible: false, opacity: 245, strokeStyle: '#def', fillStyle: 'rgba(2,4,6,0.2)', lineWidth: 247, sprite: { xScale: 248, yScale: 249, xOffset: 250, yOffset: 251 }},
restitution: 252.,
sleepThreshold: 253,
slop: 254.,
speed: 255.,
timeScale: 256.,
torque: 257.,
totalContacts: 258.,
type: 'body',
velocity: { x: 259., y: 260., },
vertices: [{ x: 261., y: 262., },{ x: 263., y: 264., },],
},
{
isSensor: true,
isSleeping: true,
isStatic: true,
angle: 301.,
anglePrev: 302.,
angularSpeed: 303.,
angularVelocity: 304.,
area: 305.,
axes: [{ x: 306., y: 307. }, { x: 308., y: 309. }],
bounds: {min: { x: 310., y: 311., },max: { x: 312., y: 313., },},
chamfer: [{x: 314., y: 315.},{x: 316., y: 317.}, ],
circleRadius: 318.,
collisionFilter: {category: 0x013F, mask: 0x00000140, group: 321},
constraintImpulse: { x: 322., y: 323., angle: 324. },
deltaTime: 325.,
density: 326.,
events: null,
force: {x: 327., y: 328.},
friction: 329.,
frictionAir: 330.,
frictionStatic: 331.,
id: 332,
inertia: 333.,
inverseInertia: 334.,
inverseMass: 335.,
label: 'Body',
mass: 336.,
motion: 337.,
parts: [],
plugin: {},
position: { x: 339., y: 340., },
positionImpulse: { x: 341., y: 342. },
positionPrev: { x: 343., y: 344., },
render: {visible: false, opacity: 345, strokeStyle: '#def', fillStyle: 'rgba(3,4,6,0.2)', lineWidth: 347, sprite: { xScale: 348, yScale: 349, xOffset: 350, yOffset: 351 }},
restitution: 352.,
sleepThreshold: 353,
slop: 354.,
speed: 355.,
timeScale: 356.,
torque: 357.,
totalContacts: 358.,
type: 'body',
velocity: { x: 359., y: 360., },
vertices: [{ x: 361., y: 362., },{ x: 363., y: 364., },],
},
];
const testBodyWithoutParts = {
isSensor: false,
isSleeping: false,
isStatic: false,
angle: 101.,
anglePrev: 102.,
angularSpeed: 103.,
angularVelocity: 104.,
area: 105.,
axes: [{ x: 106., y: 107. }, { x: 108., y: 109. }],
bounds: { min: { x: 110., y: 111., }, max: { x: 112., y: 113, }, },
chamfer: [{ x: 114., y: 115. }, { x: 116., y: 117. },],
circleRadius: 118.,
collisionFilter: { category: 0x0077, mask: 0x00000078, group: 121 },
constraintImpulse: { x: 122., y: 123., angle: 124. },
deltaTime: 125.,
density: 126.,
events: null,
force: { x: 127., y: 128. },
friction: 129.,
frictionAir: 130.,
frictionStatic: 131.,
id: 132,
inertia: 133.,
inverseInertia: 134.,
inverseMass: 135.,
label: 'Body',
mass: 136.,
motion: 137.,
parts: [],
plugin: {},
position: { x: 139., y: 140., },
positionImpulse: { x: 141., y: 142. },
positionPrev: { x: 143., y: 144., },
render: { visible: false, opacity: 145, strokeStyle: '#abc', fillStyle: 'rgba(1,4,6,0.1)', lineWidth: 147, sprite: { xScale: 148, yScale: 149, xOffset: 150, yOffset: 151 } },
restitution: 152.,
sleepThreshold: 153,
slop: 154.,
speed: 155.,
timeScale: 156.,
torque: 157.,
totalContacts: 158.,
type: 'body',
velocity: { x: 159., y: 160., },
vertices: [{ x: 161., y: 162., }, { x: 163., y: 164., },],
parts: [],
};
const GetTestBodyPartsWithParent = () => {
let parts = [];
parts.push(testBodyWithoutParts);
parts.push(...testBodyPartsWithoutParent);
return parts;
};
const testBodyPartsWithParent = GetTestBodyPartsWithParent();
const GetTestBodyWithParts = () => {
const body = testBodyWithoutParts;
body.parts.push(...testBodyPartsWithParent);
return body;
};
const testBodyWithParts = GetTestBodyWithParts();
const testVerticesSqaureWithoutBody = [
{ x: 1., y: 1., index: 0, body: undefined, isInternal: false, },
{ x: 3., y: 1., index: 1, body: undefined, isInternal: false, },
{ x: 3., y: 3., index: 2, body: undefined, isInternal: false, },
{ x: 1., y: 3., index: 3, body: undefined, isInternal: false, },
];
const testVerticesNegAreaWithoutBody = [
{ x: 1., y: 1., index: 0, body: undefined, isInternal: false, },
{ x: 3., y: 1., index: 1, body: undefined, isInternal: false, },
{ x: -3., y: -3., index: 2, body: undefined, isInternal: false, },
];
const testVerticesAreaZeroWithoutBody = [
{ x: -1., y: -1., index: 0, body: undefined, isInternal: false, },
{ x: -3., y: -1., index: 1, body: undefined, isInternal: false, },
{ x: 3., y: 3., index: 2, body: undefined, isInternal: false, },
{ x: 1., y: 3., index: 3, body: undefined, isInternal: false, },
];
module.exports = {
TestSquare
testSquare,
testVerticesSqaureWithoutBody,
testVerticesAreaZeroWithoutBody,
testVerticesNegAreaWithoutBody,
testBodyPartsWithParent,
testBodyPartsWithoutParent,
testBodyWithParts,
testBodyWithoutParts,
};

View file

@ -1,14 +1,13 @@
const {assertFloat, assertXY} = require("../TestUtil");
const {TestSquare} = require("../TestData");
const {testSquare, testBodyWithParts, testVerticesSqaureWithoutBody, testVerticesAreaZeroWithoutBody, testVerticesNegAreaWithoutBody} = require("../TestData");
const Vector = require("../../../src/geometry/Vector");
const Vertices = require("../../../src/geometry/Vertices");
const Body = require("../../../src/body/Body");
describe('Vertices.create', () => {
it('should be able to create the vertices with valid points and body', () => {
// Arrange
const points = TestSquare;
const body = Body.create();
const points = testSquare;
const body = testBodyWithParts;
// Act
const result = Vertices.create(points, body);
@ -36,7 +35,7 @@ describe('Vertices.create', () => {
it('should be able to create the vertices with valid points and undefined body', () => {
// Arrange
const points = TestSquare;
const points = testSquare;
const body = undefined;
// Act
@ -66,7 +65,7 @@ describe('Vertices.create', () => {
it('should not be able to create the vertices with undefined points and valid body', () => {
// Arrange
const points = undefined;
const body = Body.create();
const body = testBodyWithParts;
// Act
// TODO: This causes a read from undefined. This should probably be fixed.
@ -81,7 +80,7 @@ describe('Vertices.fromPath', () => {
it('should be able to create the vertices with valid path', () => {
// Arrange
const path = "1 2 L 3, 4 L 5 6";
const body = Body.create();
const body = testBodyWithParts;
// Act
const result = Vertices.fromPath(path, body);
@ -131,7 +130,7 @@ describe('Vertices.fromPath', () => {
it('should (not?) be able to create the vertices with an invalid path', () => {
// Arrange
const path = "1 2 L123NND L 5 6";
const body = Body.create();
const body = testBodyWithParts;
// Act
// TODO: This causes the result to have NaN y on the second Vector. This probaby should be fixed.
@ -157,7 +156,7 @@ describe('Vertices.fromPath', () => {
it('should not be able to create the vertices with an undefined path', () => {
// Arrange
const path = undefined;
const body = Body.create();
const body = testBodyWithParts;
// Act
// TODO: This causes a read from undefined. This should probably be fixed.
@ -169,27 +168,108 @@ describe('Vertices.fromPath', () => {
});
});
describe('Vertices.area', () => {
it('should be able to calulate the area with valid vertices and signed true', () => {
// Arrange
const vertices = testVerticesNegAreaWithoutBody;
const signed = true;
// Act
const result = Vertices.area(vertices, signed);
// Assert
assertFloat(result, -4.);
});
it('should be able to calulate the area with valid vertices and signed false', () => {
// Arrange
const vertices = testVerticesNegAreaWithoutBody;
const signed = false;
// Act
const result = Vertices.area(vertices, signed);
// Assert
assertFloat(result, 4.);
});
it('should be able to calulate the area with valid vertices and signed undefined', () => {
// Arrange
const vertices = testVerticesNegAreaWithoutBody;
const signed = undefined;
// Act
const result = Vertices.area(vertices, signed);
// Assert
assertFloat(result, 4.);
});
it('should be able to calulate the area with valid vertices whose area add up to zero signed true', () => {
// Arrange
const vertices = testVerticesAreaZeroWithoutBody;
const signed = true;
// Act
const result = Vertices.area(vertices, signed);
// Assert
assertFloat(result, 0.);
});
it('should be able to calulate the area with valid vertices whose area add up to zero signed false', () => {
// Arrange
const vertices = testVerticesAreaZeroWithoutBody;
const signed = false;
// Act
const result = Vertices.area(vertices, signed);
// Assert
assertFloat(result, 0.);
});
it('should not be able to calulate the area with undefined vertices and signed true', () => {
// Arrange
const vertices = undefined;
const signed = true;
// Act
const result = () => Vertices.area(vertices, signed);
// Assert
// TODO: This causes a read from undefined. This should probably be fixed.
expect(result).toThrow("Cannot read properties of undefined (reading 'length')");
});
});
describe('Vertices.rotate', () => {
it('should be able to rotate the vertices in place', () => {
// Arrange
const vertices = TestSquare;
const angle = 37.;
const point = Vector.create(42., 42.);
// Act
const result = Vertices.rotate(vertices, angle, point);
// Assert
assertXY(result[0], -15.767039597396057, 37.0030873378779);
assertXY(result[1], -14.236211493505373, 35.716011071163905);
assertXY(result[2], -12.94913522679137, 37.24683917505459);
assertXY(result[3], -14.479963330682054, 38.533915441768585);
// Arrange
const points = testVerticesNegAreaWithoutBody;
const angle = 37.;
const point = Vector.create(42., 42.);
// Act
const result = Vertices.rotate(vertices, angle, point);
// Assert
assertXY(result[0], -15.767039597396057, 37.0030873378779);
assertXY(result[1], -14.236211493505373, 35.716011071163905);
assertXY(result[2], -12.94913522679137, 37.24683917505459);
assertXY(result[3], -14.479963330682054, 38.533915441768585);
});
it('should return undefined when rotating the vertices in place with angle set to zero', () => {
// Arrange
const vertices = TestSquare;
const vertices = testSquare;
const angle = 0.;
const point = Vector.create(42., 42.);
@ -203,7 +283,7 @@ describe('Vertices.rotate', () => {
it('should not be able to rotate the vertices in place with an undefined angle', () => {
// Arrange
const vertices = TestSquare;
const vertices = testSquare;
const angle = undefined;
const point = Vector.create(42., 42.);
@ -221,7 +301,7 @@ describe('Vertices.rotate', () => {
it('should not be able to rotate the vertices in place with undefined point', () => {
// Arrange
const vertices = TestSquare;
const vertices = testSquare;
const angle = 37.;
const point = undefined;