From 44a67d0dc61ce8a7182e65c9c22f538f1e546f90 Mon Sep 17 00:00:00 2001 From: Theodore Kruczek Date: Sun, 14 Jan 2024 21:03:02 -0500 Subject: [PATCH] test: :white_check_mark: add test for Vector and Vector3D --- src/operations/Vector.ts | 291 +++++++++++++----- test/operations/Random.test.ts | 24 ++ test/operations/Vector.test.ts | 269 ++++++++++++++++ test/operations/Vector3D.test.ts | 109 +++++++ .../__snapshots__/Random.test.ts.snap | 7 + .../__snapshots__/Vector.test.ts.snap | 218 +++++++++++++ .../__snapshots__/Vector3D.test.ts.snap | 138 +++++++++ 7 files changed, 980 insertions(+), 76 deletions(-) create mode 100644 test/operations/Random.test.ts create mode 100644 test/operations/Vector.test.ts create mode 100644 test/operations/Vector3D.test.ts create mode 100644 test/operations/__snapshots__/Random.test.ts.snap create mode 100644 test/operations/__snapshots__/Vector.test.ts.snap create mode 100644 test/operations/__snapshots__/Vector3D.test.ts.snap diff --git a/src/operations/Vector.ts b/src/operations/Vector.ts index f11e9dd..2e3a3d1 100644 --- a/src/operations/Vector.ts +++ b/src/operations/Vector.ts @@ -1,60 +1,100 @@ +import { Degrees, Radians } from 'src/main'; import { Matrix } from './Matrix'; import { Vector3D } from './Vector3D'; -// / Vector operations. +/** + * A Vector is a mathematical object that has both magnitude and direction. + */ export class Vector { - // / Create a new [Vector] object from an array of [_elements]. - constructor(public elements: T[] | Float64Array) { - this.length = elements.length; - } - - // / Create a zero-filled [Vector] of the provided [length]; - static zero(length: number): Vector { - return new Vector(new Array(length).fill(0)); - } - /** - * Create a [Vector] of the provided [length], filled with the - * provided [value]. + * The length of the vector. */ - static filled(length: number, value: number): Vector { - return new Vector(new Array(length).fill(value)); - } + readonly length: number; /** - * Create a [Vector] from the provided [elements] list. + * Represents a 3-dimensional vector. */ - static fromList(elements: number[]): Vector { - return new Vector(elements); - } - - // / Vector length. - readonly length: number; - - // / 3-dimensional origin. static readonly origin3 = new Vector([0, 0, 0]); - // / 6-dimensional origin. + /** + * Represents a vector with all elements set to zero. + */ static readonly origin6 = new Vector([0, 0, 0, 0, 0, 0]); - // / X-axis unit vector. + /** + * Represents the x-axis vector. + */ static readonly xAxis = new Vector([1, 0, 0]); - // / Y-axis unit vector. + /** + * Represents the y-axis vector. + */ static readonly yAxis = new Vector([0, 1, 0]); - // / Z-axis unit vector. + /** + * Represents the z-axis vector. + */ static readonly zAxis = new Vector([0, 0, 1]); - // / Negative x-axis unit vector. + /** + * Represents a vector pointing along the negative x-axis. + */ static readonly xAxisNeg = new Vector([-1, 0, 0]); - // / Negative y-axis unit vector. + /** + * Represents a vector pointing along the negative y-axis. + */ static readonly yAxisNeg = new Vector([0, -1, 0]); - // / Negative z-axis unit vector. + /** + * Represents a vector pointing along the negative z-axis. + */ static readonly zAxisNeg = new Vector([0, 0, -1]); + constructor(public elements: T[] | Float64Array) { + this.length = elements.length; + } + + /** + * Creates a zero vector of the specified length. + * @param length The length of the vector. + * + * @returns A new Vector object representing the zero vector. + */ + static zero(length: number): Vector { + return new Vector(new Array(length).fill(0)); + } + + /** + * Creates a new Vector with the specified length, filled with the specified + * value. + * @param length The length of the new Vector. + * + * @param value The value to fill the Vector with. + * + * @returns A new Vector filled with the specified value. + */ + static filled(length: number, value: number): Vector { + return new Vector(new Array(length).fill(value)); + } + + /** + * Creates a new Vector instance from an array of elements. + * + * @param elements - The array of elements to create the Vector from. + * @returns A new Vector instance. + */ + static fromList(elements: number[]): Vector { + return new Vector(elements); + } + + /** + * Returns a string representation of the vector. + * + * @param fixed - The number of digits to appear after the decimal point. + * Defaults to -1. + * @returns A string representation of the vector. + */ toString(fixed = -1): string { if (fixed < 0) { return `[${this.elements.join(', ')}]`; @@ -64,32 +104,50 @@ export class Vector { return `[${output.join(', ')}]`; } - // / X-axis component. + /** + * Returns a string representation of the x value of the vector. + */ get x(): number { return this.elements[0]; } - // / Y-axis component. + /** + * Returns a string representation of the y value of the vector. + */ get y(): number { return this.elements[1]; } - // / Z-axis component. + /** + * Returns a string representation of the z value of the vector. + */ get z(): number { return this.elements[2]; } - // / Convert the elements of this [Vector] to a list object. + /** + * Converts the vector elements to an array. + * + * @returns An array containing the vector elements. + */ toList(): number[] { return Array.from(this.elements); } - // / Copy the elements of this [Vector] to a new array. + /** + * Converts the vector to a Float64Array. + * + * @returns The vector as a Float64Array. + */ toArray(): Float64Array { return new Float64Array(this.elements); } - // / Return the magnitude of this vector. + /** + * Calculates the magnitude of the vector. + * + * @returns The magnitude of the vector. + */ magnitude(): number { let total = 0; @@ -100,7 +158,13 @@ export class Vector { return Math.sqrt(total); } - // / Return the result of adding this to another [Vector]. + /** + * Adds the elements of another vector to this vector and returns a new + * vector. + * @param v - The vector to add. + * + * @returns A new vector containing the sum of the elements. + */ add(v: Vector): Vector { const output = new Array(this.length); @@ -111,7 +175,12 @@ export class Vector { return new Vector(output); } - // / Return the result of subtracting this and another [Vector]. + /** + * Subtracts a vector from the current vector. + * @param v The vector to subtract. + * + * @returns A new vector representing the result of the subtraction. + */ subtract(v: Vector): Vector { const output = new Array(this.length); @@ -122,7 +191,12 @@ export class Vector { return new Vector(output); } - // / Return a copy of this [Vector] scaled by [n]; + /** + * Scales the vector by a given factor. + * @param n The scaling factor. + * + * @returns A new Vector object representing the scaled vector. + */ scale(n: number): Vector { const output = new Array(this.length); @@ -133,17 +207,30 @@ export class Vector { return new Vector(output); } - // / Return a copy of this [Vector] with the elements negated. + /** + * Negates the vector by scaling it by -1. + * @returns A new Vector object representing the negated vector. + */ negate(): Vector { return this.scale(-1); } - // / Return the Euclidean distance between this and another [Vector]. + /** + * Return the Euclidean distance between this and another Vector. + * @param v The vector to calculate the distance to. + * + * @returns The distance between the two vectors. + */ distance(v: Vector): number { return this.subtract(v).magnitude(); } - // / Convert this to a unit [Vector]. + /** + * Normalizes the vector, making it a unit vector with the same direction but + * a magnitude of 1. If the vector has a magnitude of 0, it returns a zero + * vector of the same length. + * @returns The normalized vector. + */ normalize(): Vector { const m = this.magnitude(); @@ -154,7 +241,12 @@ export class Vector { return this.scale(1.0 / m); } - // / Calculate the dot product of this and another [Vector]; + /** + * Calculates the dot product of this vector and another vector. + * + * @param v - The vector to calculate the dot product with. + * @returns The dot product of the two vectors. + */ dot(v: Vector): number { let total = 0; @@ -165,7 +257,12 @@ export class Vector { return total; } - // / Calculate the outer product between this and another [Vector]. + /** + * Calculates the outer product of this vector with another vector. + * @param v The vector to calculate the outer product with. + * + * @returns A matrix representing the outer product of the two vectors. + */ outer(v: Vector): Matrix { const result: number[][] = []; @@ -179,7 +276,12 @@ export class Vector { return new Matrix(result); } - // / Calculate the cross product of this and another [Vector]; + /** + * Calculates the cross product of this vector and the given vector. + * @param v - The vector to calculate the cross product with. + * + * @returns The resulting vector. + */ cross(v: Vector): Vector { const output = new Array(this.length); @@ -210,10 +312,12 @@ export class Vector { } /** - * Create a copy of this [Vector] rotated in the x-axis by angle - * [theta] _(rad)_. + * Rotates the vector around the X-axis by the specified angle. + * + * @param theta The angle in radians. + * @returns The rotated vector. */ - rotX(theta: number): Vector { + rotX(theta: Radians): Vector { const cosT = Math.cos(theta); const sinT = Math.sin(theta); const output = new Array(3); @@ -226,10 +330,12 @@ export class Vector { } /** - * Create a copy of this [Vector] rotated in the y-axis by angle - * [theta] _(rad)_. + * Rotates the vector around the Y-axis by the specified angle. + * + * @param theta The angle of rotation in radians. + * @returns A new Vector representing the rotated vector. */ - rotY(theta: number): Vector { + rotY(theta: Radians): Vector { const cosT = Math.cos(theta); const sinT = Math.sin(theta); const output = new Array(3); @@ -242,10 +348,12 @@ export class Vector { } /** - * Create a copy of this [Vector] rotated in the z-axis by angle - * [theta] _(rad)_. + * Rotates the vector around the Z-axis by the specified angle. + * + * @param theta The angle of rotation in radians. + * @returns A new Vector representing the rotated vector. */ - rotZ(theta: number): Vector { + rotZ(theta: Radians): Vector { const cosT = Math.cos(theta); const sinT = Math.sin(theta); const output = new Array(3); @@ -257,26 +365,41 @@ export class Vector { return new Vector(output); } - // / Calculate the angle _(rad)_ between this and another [Vector]. - angle(v: Vector): number { + /** + * Calculates the angle between this vector and another vector. + * @param v The other vector. + * + * @returns The angle between the two vectors in radians. + */ + angle(v: Vector): Radians { // better than acos for small angles const theta = Math.atan2(this.cross(v).magnitude(), this.dot(v)); if (isNaN(theta)) { - return 0.0; + return 0.0 as Radians; } - return theta; + return theta as Radians; } - // / Calculate the angle _(°)_ between this and another [Vector]. - angleDegrees(v: Vector): number { - return this.angle(v) * (180 / Math.PI); + /** + * Calculates the angle between this vector and another vector in degrees. + * @param v The other vector. + * + * @returns The angle between the two vectors in degrees. + */ + angleDegrees(v: Vector): Degrees { + return (this.angle(v) * (180 / Math.PI)) as Degrees; } /** - * Return `true` if line-of-sight exists between this and another [Vector] - * with a central body of the given [radius]. + * Determines if there is line of sight between this vector and another vector + * within a given radius. + * @param v - The vector to check line of sight with. + * + * @param radius - The radius within which line of sight is considered. + * + * @returns True if there is line of sight, false otherwise. */ sight(v: Vector, radius: number): boolean { const r1Mag2 = this.magnitude() ** 2; @@ -298,44 +421,60 @@ export class Vector { return los; } - // / Return the unit vector that bisects this and another [Vector]. + /** + * Returns the bisect vector between this vector and the given vector. The + * bisect vector is calculated by scaling this vector's magnitude by the + * magnitude of the given vector, adding the result to the product of scaling + * the given vector's magnitude by this vector's magnitude, and then + * normalizing the resulting vector. + * @param v - The vector to calculate the bisect with. + * + * @returns The bisect vector. + */ bisect(v: Vector): Vector { return this.scale(v.magnitude()).add(v.scale(this.magnitude())).normalize(); } /** * Joins the current vector with another vector. - * @param v The vector to join with. - * @returns A new vector that contains the elements of both vectors. + * @param v The vector to join with. @returns A new vector that contains the + * elements of both vectors. */ join(v: Vector): Vector { return new Vector(this.toList().concat(v.toList())); } /** - * Returns a new Vector containing a portion of the elements from the specified start index to the specified end index - * @param start The start index of the slice (inclusive). - * @param end The end index of the slice (exclusive). - * @returns A new Vector containing the sliced elements. + * Returns a new Vector containing a portion of the elements from the + * specified start index to the specified end index + * @param start The start index of the slice (inclusive). @param end The end + * index of the slice (exclusive). @returns A new Vector containing the sliced + * elements. */ slice(start: number, end: number): Vector { return new Vector(this.elements.slice(start, end)); } - // / Convert this [Vector] into a row [Matrix]. + /** + * Returns a new Matrix object representing the row vector. + * @returns {Matrix} The row vector as a Matrix object. + */ row(): Matrix { return new Matrix([this.toList()]); } - // / Convert this [Vector] into a column [Matrix]. + /** + * Returns a new Matrix object representing the column vector of this Vector. + * @returns {Matrix} The column vector as a Matrix object. + */ column(): Matrix { return new Matrix(this.toList().map((e) => [e])); } /** * Converts the elements at the specified index to a Vector3D object. - * @param index - The index of the elements to convert. - * @returns A new Vector3D object containing the converted elements. + * @param index - The index of the elements to convert. @returns A new + * Vector3D object containing the converted elements. */ toVector3D(index: number): Vector3D { return new Vector3D(this.elements[index], this.elements[index + 1], this.elements[index + 2]); diff --git a/test/operations/Random.test.ts b/test/operations/Random.test.ts new file mode 100644 index 0000000..2eacc07 --- /dev/null +++ b/test/operations/Random.test.ts @@ -0,0 +1,24 @@ +import { Random } from '../../src/main'; + +describe('Random', () => { + // nextFloat + it('should return a random float', () => { + const r = new Random(); + + expect(r.nextFloat()).toMatchSnapshot(); + }); + + // nextInt + it('should return a random int', () => { + const r = new Random(); + + expect(r.nextInt()).toMatchSnapshot(); + }); + + // nextBool + it('should return a random bool', () => { + const r = new Random(); + + expect(r.nextBool()).toMatchSnapshot(); + }); +}); diff --git a/test/operations/Vector.test.ts b/test/operations/Vector.test.ts new file mode 100644 index 0000000..3cfd1e4 --- /dev/null +++ b/test/operations/Vector.test.ts @@ -0,0 +1,269 @@ +import { DEG2RAD, Radians, Vector } from './../../src/main'; + +describe('Vector', () => { + // create a Vector with elements and get its length + it('should create a Vector with elements and get its length', () => { + const v = new Vector([1, 2, 3]); + + expect(v.length).toBe(3); + }); + + // create a Vector with all elements set to zero and get its length + it('should create a Vector with all elements set to zero and get its length', () => { + const v = Vector.zero(3); + + expect(v.length).toBe(3); + }); + + // create a Vector with all elements set to a value and get its length + it('should create a Vector with all elements set to a value and get its length', () => { + const v = Vector.filled(3, 5); + + expect(v.length).toBe(3); + }); + + // create a Vector from a list of elements and get its length + it('should create a Vector from a list of elements and get its length', () => { + const v = Vector.fromList([1, 2, 3]); + + expect(v.length).toBe(3); + }); + + // get the x, y, and z components of a Vector + it('should get the x, y, and z components of a Vector', () => { + const v = new Vector([1, 2, 3]); + + expect(v.x).toBe(1); + expect(v.y).toBe(2); + expect(v.z).toBe(3); + }); + + // get the magnitude of a Vector + it('should get the magnitude of a Vector', () => { + const v = new Vector([3, 4]); + + expect(v.magnitude()).toBe(5); + }); + + // create a Vector with an empty array of elements + it('should create a Vector with an empty array of elements', () => { + const v = new Vector([]); + + expect(v.length).toBe(0); + }); + + // create a Vector with a negative length + it('should create a Vector with a negative length', () => { + expect(() => new Vector([-1, -2, -3])).toMatchSnapshot(); + }); + + // add two Vectors and return a new Vector + it('should add two Vectors and return a new Vector', () => { + const v1 = new Vector([1, 2, 3]); + const v2 = new Vector([4, 5, 6]); + const result = v1.add(v2); + + expect(result).toMatchSnapshot(); + }); + + // subtract two Vectors and return a new Vector + it('should subtract two Vectors and return a new Vector when called', () => { + const v1 = new Vector([1, 2, 3]); + const v2 = new Vector([4, 5, 6]); + const result = v1.subtract(v2); + + expect(result).toMatchInlineSnapshot(` + Vector { + "elements": Array [ + -3, + -3, + -3, + ], + "length": 3, + } + `); + }); + + // scale a Vector by a scalar and return a new Vector + it('should scale a Vector by a scalar and return a new Vector when called with a scalar value', () => { + const v = new Vector([1, 2, 3]); + const scaledVector = v.scale(2); + + expect(scaledVector).toMatchInlineSnapshot(` + Vector { + "elements": Array [ + 2, + 4, + 6, + ], + "length": 3, + } + `); + }); + + // negate a Vector and return a new Vector + it('should negate a Vector and return a new Vector when calling the negate() method', () => { + const v = new Vector([1, 2, 3]); + const negated = v.negate(); + + expect(negated).toEqual(new Vector([-1, -2, -3])); + }); + + // normalize a Vector and return a new Vector + it('should normalize a Vector and return a new Vector', () => { + const v = new Vector([3, 4]); + const normalized = v.normalize(); + + expect(normalized).toMatchSnapshot(); + }); + + // calculate the dot product of two Vectors + it('should calculate the dot product of two Vectors', () => { + const v1 = new Vector([1, 2, 3]); + const v2 = new Vector([4, 5, 6]); + const dotProduct = v1.dot(v2); + + expect(dotProduct).toMatchSnapshot(); + }); + + // toString + it('should return a string representation of a Vector', () => { + const v = new Vector([1, 2, 3]); + + expect(v.toString()).toMatchSnapshot(); + }); + + // toList + it('should return a list representation of a Vector', () => { + const v = new Vector([1, 2, 3]); + + expect(v.toList()).toMatchSnapshot(); + }); + + // toArray + it('should return an array representation of a Vector', () => { + const v = new Vector([1, 2, 3]); + + expect(v.toArray()).toMatchSnapshot(); + }); + + // distance + it('should calculate the distance between two Vectors', () => { + const v1 = new Vector([1, 2, 3]); + const v2 = new Vector([4, 5, 6]); + + expect(v1.distance(v2)).toMatchSnapshot(); + }); + + // outer + it('should calculate the outer product of two Vectors', () => { + const v1 = new Vector([1, 2, 3]); + const v2 = new Vector([4, 5, 6]); + + expect(v1.outer(v2)).toMatchSnapshot(); + }); + + // cross + it('should calculate the cross product of two Vectors', () => { + const v1 = new Vector([1, 2, 3]); + const v2 = new Vector([4, 5, 6]); + + expect(v1.cross(v2)).toMatchSnapshot(); + }); + + // skewSymmetric + it('should calculate the skew symmetric matrix of a Vector', () => { + const v = new Vector([1, 2, 3]); + + expect(v.skewSymmetric()).toMatchSnapshot(); + }); + + // rotX + it('should rotate a Vector around the x-axis', () => { + const v = new Vector([1, 2, 3]); + + expect(v.rotX((90 * DEG2RAD) as Radians)).toMatchSnapshot(); + }); + + // rotY + it('should rotate a Vector around the y-axis', () => { + const v = new Vector([1, 2, 3]); + + expect(v.rotY((90 * DEG2RAD) as Radians)).toMatchSnapshot(); + }); + + // rotZ + it('should rotate a Vector around the z-axis', () => { + const v = new Vector([1, 2, 3]); + + expect(v.rotZ((90 * DEG2RAD) as Radians)).toMatchSnapshot(); + }); + + // angle + it('should calculate the angle between two Vectors', () => { + const v1 = new Vector([1, 2, 3]); + const v2 = new Vector([4, 5, 6]); + + expect(v1.angle(v2)).toMatchSnapshot(); + }); + + // angleDegrees + it('should calculate the angle between two Vectors in degrees', () => { + const v1 = new Vector([1, 2, 3]); + const v2 = new Vector([4, 5, 6]); + + expect(v1.angleDegrees(v2)).toMatchSnapshot(); + }); + + // sight + it('should calculate the sight of a Vector', () => { + const v = new Vector([1, 2, 3]); + const v2 = new Vector([4, 5, 6]); + + expect(v.sight(v2, (90 * DEG2RAD) as Radians)).toMatchSnapshot(); + }); + + // bisect + it('should calculate the bisect of two Vectors', () => { + const v1 = new Vector([1, 2, 3]); + const v2 = new Vector([4, 5, 6]); + + expect(v1.bisect(v2)).toMatchSnapshot(); + }); + + // slice + it('should slice a Vector', () => { + const v = new Vector([1, 2, 3]); + + expect(v.slice(1, 2)).toMatchSnapshot(); + }); + + // join + it('should join two Vectors', () => { + const v1 = new Vector([1, 2, 3]); + const v2 = new Vector([4, 5, 6]); + + expect(v1.join(v2)).toMatchSnapshot(); + }); + + // row + it('should get a row from a Vector', () => { + const v = new Vector([1, 2, 3]); + + expect(v.row()).toMatchSnapshot(); + }); + + // column + it('should get a column from a Vector', () => { + const v = new Vector([1, 2, 3]); + + expect(v.column()).toMatchSnapshot(); + }); + + // toVector3D + it('should convert a Vector to a Vector3D', () => { + const v = new Vector([1, 2, 3]); + + expect(v.toVector3D(1)).toMatchSnapshot(); + }); +}); diff --git a/test/operations/Vector3D.test.ts b/test/operations/Vector3D.test.ts new file mode 100644 index 0000000..758ce0e --- /dev/null +++ b/test/operations/Vector3D.test.ts @@ -0,0 +1,109 @@ +import { DEG2RAD, Radians, Vector, Vector3D } from '../../src/main'; + +describe('Vector3D', () => { + // fromVector + it('should create a Vector3D from a Vector3D', () => { + const v = Vector3D.fromVector(new Vector([1, 2, 3])); + + expect(v).toMatchSnapshot(); + }); + + // toArray + it('should return an array of elements', () => { + const v = new Vector3D(1, 2, 3); + + expect(v.toArray()).toMatchSnapshot(); + }); + + // getElement + it('should get an element from a Vector3D', () => { + const v = new Vector3D(1, 2, 3); + + expect(v.getElement(0)).toBe(1); + expect(v.getElement(1)).toBe(2); + expect(v.getElement(2)).toBe(3); + }); + + // toVector + it('should return a Vector3D as a Vector', () => { + const v = new Vector3D(1, 2, 3); + + expect(v.toVector()).toMatchSnapshot(); + }); + + // toString + it('should return a Vector3D as a string', () => { + const v = new Vector3D(1, 2, 3); + + expect(v.toString()).toMatchSnapshot(); + }); + + // distance + it('should return the distance between two Vector3Ds', () => { + const v1 = new Vector3D(1, 2, 3); + const v2 = new Vector3D(4, 5, 6); + + expect(v1.distance(v2)).toMatchSnapshot(); + }); + + // outer + it('should return the outer product of two Vector3Ds', () => { + const v1 = new Vector3D(1, 2, 3); + const v2 = new Vector3D(4, 5, 6); + + expect(v1.outer(v2)).toMatchSnapshot(); + }); + + // skewSymmetric + it('should return the skew symmetric of a Vector3D', () => { + const v = new Vector3D(1, 2, 3); + + expect(v.skewSymmetric()).toMatchSnapshot(); + }); + + // angleDegrees + it('should return the angle between two Vector3Ds in degrees', () => { + const v1 = new Vector3D(1, 2, 3); + const v2 = new Vector3D(4, 5, 6); + + expect(v1.angleDegrees(v2)).toMatchSnapshot(); + }); + + // sight + it('should return the sight of a Vector3D', () => { + const v = new Vector3D(1, 2, 3); + const v2 = new Vector3D(4, 5, 6); + + expect(v.sight(v2, (20 * DEG2RAD) as Radians)).toMatchSnapshot(); + }); + + // bisect + it('should return the bisect of two Vector3Ds', () => { + const v1 = new Vector3D(1, 2, 3); + const v2 = new Vector3D(4, 5, 6); + + expect(v1.bisect(v2)).toMatchSnapshot(); + }); + + // row + it('should return the row of a Vector3D', () => { + const v = new Vector3D(1, 2, 3); + + expect(v.row()).toMatchSnapshot(); + }); + + // column + it('should return the column of a Vector3D', () => { + const v = new Vector3D(1, 2, 3); + + expect(v.column()).toMatchSnapshot(); + }); + + // join + it('should return the join of two Vector3Ds', () => { + const v1 = new Vector3D(1, 2, 3); + const v2 = new Vector3D(4, 5, 6); + + expect(v1.join(v2)).toMatchSnapshot(); + }); +}); diff --git a/test/operations/__snapshots__/Random.test.ts.snap b/test/operations/__snapshots__/Random.test.ts.snap new file mode 100644 index 0000000..cd8909e --- /dev/null +++ b/test/operations/__snapshots__/Random.test.ts.snap @@ -0,0 +1,7 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Random should return a random bool 1`] = `false`; + +exports[`Random should return a random float 1`] = `0.21132115912208504`; + +exports[`Random should return a random int 1`] = `0`; diff --git a/test/operations/__snapshots__/Vector.test.ts.snap b/test/operations/__snapshots__/Vector.test.ts.snap new file mode 100644 index 0000000..c2c13a2 --- /dev/null +++ b/test/operations/__snapshots__/Vector.test.ts.snap @@ -0,0 +1,218 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Vector should add two Vectors and return a new Vector 1`] = ` +Vector { + "elements": Array [ + 5, + 7, + 9, + ], + "length": 3, +} +`; + +exports[`Vector should calculate the angle between two Vectors 1`] = `0.22572612855273394`; + +exports[`Vector should calculate the angle between two Vectors in degrees 1`] = `12.93315449189912`; + +exports[`Vector should calculate the bisect of two Vectors 1`] = ` +Vector { + "elements": Array [ + 0.3638667954886596, + 0.5556981892812022, + 0.7475295830737448, + ], + "length": 3, +} +`; + +exports[`Vector should calculate the cross product of two Vectors 1`] = ` +Vector { + "elements": Array [ + -3, + 6, + -3, + ], + "length": 3, +} +`; + +exports[`Vector should calculate the distance between two Vectors 1`] = `5.196152422706632`; + +exports[`Vector should calculate the dot product of two Vectors 1`] = `32`; + +exports[`Vector should calculate the outer product of two Vectors 1`] = ` +Matrix { + "columns": 3, + "elements": Array [ + Array [ + 4, + 5, + 6, + ], + Array [ + 8, + 10, + 12, + ], + Array [ + 12, + 15, + 18, + ], + ], + "rows": 3, +} +`; + +exports[`Vector should calculate the sight of a Vector 1`] = `true`; + +exports[`Vector should calculate the skew symmetric matrix of a Vector 1`] = ` +Matrix { + "columns": 3, + "elements": Array [ + Array [ + 0, + -3, + 2, + ], + Array [ + 3, + 0, + -1, + ], + Array [ + -2, + 1, + 0, + ], + ], + "rows": 3, +} +`; + +exports[`Vector should convert a Vector to a Vector3D 1`] = ` +Vector3D { + "x": 2, + "y": 3, + "z": undefined, +} +`; + +exports[`Vector should create a Vector with a negative length 1`] = `[Function]`; + +exports[`Vector should get a column from a Vector 1`] = ` +Matrix { + "columns": 1, + "elements": Array [ + Array [ + 1, + ], + Array [ + 2, + ], + Array [ + 3, + ], + ], + "rows": 3, +} +`; + +exports[`Vector should get a row from a Vector 1`] = ` +Matrix { + "columns": 3, + "elements": Array [ + Array [ + 1, + 2, + 3, + ], + ], + "rows": 1, +} +`; + +exports[`Vector should join two Vectors 1`] = ` +Vector { + "elements": Array [ + 1, + 2, + 3, + 4, + 5, + 6, + ], + "length": 6, +} +`; + +exports[`Vector should normalize a Vector and return a new Vector 1`] = ` +Vector { + "elements": Array [ + 0.6000000000000001, + 0.8, + ], + "length": 2, +} +`; + +exports[`Vector should return a list representation of a Vector 1`] = ` +Array [ + 1, + 2, + 3, +] +`; + +exports[`Vector should return a string representation of a Vector 1`] = `"[1, 2, 3]"`; + +exports[`Vector should return an array representation of a Vector 1`] = ` +Float64Array [ + 1, + 2, + 3, +] +`; + +exports[`Vector should rotate a Vector around the x-axis 1`] = ` +Vector { + "elements": Array [ + 1, + 3, + -1.9999999999999998, + ], + "length": 3, +} +`; + +exports[`Vector should rotate a Vector around the y-axis 1`] = ` +Vector { + "elements": Array [ + -3, + 2, + 1.0000000000000002, + ], + "length": 3, +} +`; + +exports[`Vector should rotate a Vector around the z-axis 1`] = ` +Vector { + "elements": Array [ + 2, + -0.9999999999999999, + 3, + ], + "length": 3, +} +`; + +exports[`Vector should slice a Vector 1`] = ` +Vector { + "elements": Array [ + 2, + ], + "length": 1, +} +`; diff --git a/test/operations/__snapshots__/Vector3D.test.ts.snap b/test/operations/__snapshots__/Vector3D.test.ts.snap new file mode 100644 index 0000000..2d32002 --- /dev/null +++ b/test/operations/__snapshots__/Vector3D.test.ts.snap @@ -0,0 +1,138 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Vector3D should create a Vector3D from a Vector3D 1`] = ` +Vector3D { + "x": 1, + "y": 2, + "z": 3, +} +`; + +exports[`Vector3D should return a Vector3D as a Vector 1`] = ` +Vector { + "elements": Array [ + 1, + 2, + 3, + ], + "length": 3, +} +`; + +exports[`Vector3D should return a Vector3D as a string 1`] = `"[1, 2, 3]"`; + +exports[`Vector3D should return an array of elements 1`] = ` +Float64Array [ + 1, + 2, + 3, +] +`; + +exports[`Vector3D should return the angle between two Vector3Ds in degrees 1`] = `12.93315449189912`; + +exports[`Vector3D should return the bisect of two Vector3Ds 1`] = ` +Vector3D { + "x": 0.36386679548865963, + "y": 0.5556981892812022, + "z": 0.7475295830737448, +} +`; + +exports[`Vector3D should return the column of a Vector3D 1`] = ` +Matrix { + "columns": 1, + "elements": Array [ + Array [ + 1, + ], + Array [ + 2, + ], + Array [ + 3, + ], + ], + "rows": 3, +} +`; + +exports[`Vector3D should return the distance between two Vector3Ds 1`] = `5.196152422706632`; + +exports[`Vector3D should return the join of two Vector3Ds 1`] = ` +Vector { + "elements": Float64Array [ + 1, + 2, + 3, + 4, + 5, + 6, + ], + "length": 6, +} +`; + +exports[`Vector3D should return the outer product of two Vector3Ds 1`] = ` +Matrix { + "columns": 3, + "elements": Array [ + Array [ + 4, + 5, + 6, + ], + Array [ + 8, + 10, + 12, + ], + Array [ + 12, + 15, + 18, + ], + ], + "rows": 3, +} +`; + +exports[`Vector3D should return the row of a Vector3D 1`] = ` +Matrix { + "columns": 3, + "elements": Array [ + Array [ + 1, + 2, + 3, + ], + ], + "rows": 1, +} +`; + +exports[`Vector3D should return the sight of a Vector3D 1`] = `true`; + +exports[`Vector3D should return the skew symmetric of a Vector3D 1`] = ` +Matrix { + "columns": 3, + "elements": Array [ + Array [ + 0, + -3, + 2, + ], + Array [ + 3, + 0, + -1, + ], + Array [ + -2, + 1, + 0, + ], + ], + "rows": 3, +} +`;