From adf69811cdcf07e30a0f5b74b50072a6539489e3 Mon Sep 17 00:00:00 2001 From: Fabian Date: Wed, 6 Oct 2021 20:16:03 +0200 Subject: [PATCH 1/4] Migrate doctest for Vector2.js --- Data-Structures/Vectors/Vector2.js | 71 +------------------------ Data-Structures/Vectors/Vector2.test.js | 58 ++++++++++++++++++++ 2 files changed, 60 insertions(+), 69 deletions(-) create mode 100644 Data-Structures/Vectors/Vector2.test.js diff --git a/Data-Structures/Vectors/Vector2.js b/Data-Structures/Vectors/Vector2.js index 8b70736a7e..393ebb875c 100644 --- a/Data-Structures/Vectors/Vector2.js +++ b/Data-Structures/Vectors/Vector2.js @@ -4,75 +4,6 @@ * https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics)). */ -/* -Doctests - -// Test equalsExactly-method -> new Vector2(1, 0).equalsExactly(new Vector2(1, 0)) -true -> new Vector2(1.23, 4.56).equalsExactly(new Vector2(0, 0)) -false - -// Test equalsApproximately-method -> new Vector2(1, 0).equalsApproximately(new Vector2(1, 0.0000001), 0.000001) -true -> new Vector2(1.23, 4.56).equalsApproximately(new Vector2(1.24, 4.56), 0.000001) -false - -// Test add-method -> new Vector2(1, 0).add(new Vector2(0, 1)).equalsApproximately(new Vector2(1, 1), 0.000001) -true -> new Vector2(-3.3, -9).add(new Vector2(-2.2, 3)).equalsApproximately(new Vector2(-5.5, -6), 0.000001) -true - -// Test subtract-method -> new Vector2(1, 0).subtract(new Vector2(0, 1)).equalsApproximately(new Vector2(1, -1), 0.000001) -true -> new Vector2(234.5, 1.7).subtract(new Vector2(3.3, 2.7)).equalsApproximately(new Vector2(231.2, -1), 0.000001) -true - -// Test multiply-method -> new Vector2(1, 0).multiply(5).equalsApproximately(new Vector2(5, 0), 0.000001) -true -> new Vector2(3.41, -7.12).multiply(-3.1).equalsApproximately(new Vector2(-10.571, 22.072), 0.000001) -true - -// Test length-method -> new Vector2(1, 0).length() -1 -> new Vector2(-1, 1).length() -Math.sqrt(2) - -// Test normalize-method -> new Vector2(1, 0).normalize().equalsApproximately(new Vector2(1, 0), 0.000001) -true -> new Vector2(1, -1).normalize().equalsApproximately(new Vector2(Math.sqrt(2) / 2, -Math.sqrt(2) / 2), 0.000001) -true - -// Test distance-method -> new Vector2(0, 0).distance(new Vector2(0, -1)) -1 -> new Vector2(1, 0).distance(new Vector2(0, 1)) -Math.sqrt(2) - -// Test dotProduct-method -> new Vector2(1, 0).dotProduct(new Vector2(0, 1)) -0 -> new Vector2(1, 2).dotProduct(new Vector2(3, 4)) -1 * 3 + 2 * 4 - -// Test rotate-method -> new Vector2(0, -1).rotate(Math.PI / 2).equalsApproximately(new Vector2(1, 0), 0.000001) -true -> new Vector2(1.23, -4.56).rotate(Math.PI).equalsApproximately(new Vector2(-1.23, 4.56), 0.000001) -true - -// Test angleBetween-method -> new Vector2(1, 0).angleBetween(new Vector2(0, 1)) -Math.PI / 2 -> new Vector2(1, 0).angleBetween(new Vector2(1, -1)) --Math.PI / 4 -*/ class Vector2 { // eslint-disable-line no-unused-vars constructor (x, y) { this.x = x @@ -203,3 +134,5 @@ class Vector2 { // eslint-disable-line no-unused-vars return Math.atan2(vector.y, vector.x) - Math.atan2(this.y, this.x) } } + +export { Vector2 } diff --git a/Data-Structures/Vectors/Vector2.test.js b/Data-Structures/Vectors/Vector2.test.js new file mode 100644 index 0000000000..989cc0dc11 --- /dev/null +++ b/Data-Structures/Vectors/Vector2.test.js @@ -0,0 +1,58 @@ +import { Vector2 } from './Vector2.js' + +describe('Tests for Vector2 data scructure implementation', () => { + test('tests equalsExactly-method', () => { + expect(new Vector2(1, 0).equalsExactly(new Vector2(1, 0))).toBe(true) + expect(new Vector2(1.23, 4.56).equalsExactly(new Vector2(0, 0))).toBe(false) + }) + + test('tests equalsApproximately-method', () => { + expect(new Vector2(1, 0).equalsApproximately(new Vector2(1, 0.0000001), 0.000001)).toBe(true) + expect(new Vector2(1.23, 4.56).equalsApproximately(new Vector2(1.24, 4.56), 0.000001)).toBe(false) + }) + + test('tests add-method', () => { + expect(new Vector2(1, 0).add(new Vector2(0, 1)).equalsApproximately(new Vector2(1, 1), 0.000001)).toBe(true) + expect(new Vector2(-3.3, -9).add(new Vector2(-2.2, 3)).equalsApproximately(new Vector2(-5.5, -6), 0.000001)).toBe(true) + }) + + test('tests subtract-method', () => { + expect(new Vector2(1, 0).subtract(new Vector2(0, 1)).equalsApproximately(new Vector2(1, -1), 0.000001)).toBe(true) + expect(new Vector2(234.5, 1.7).subtract(new Vector2(3.3, 2.7)).equalsApproximately(new Vector2(231.2, -1), 0.000001)).toBe(true) + }) + + test('tests multiply-method', () => { + expect(new Vector2(1, 0).multiply(5).equalsApproximately(new Vector2(5, 0), 0.000001)).toBe(true) + expect(new Vector2(3.41, -7.12).multiply(-3.1).equalsApproximately(new Vector2(-10.571, 22.072), 0.000001)).toBe(true) + }) + + test('tests length-method', () => { + expect(new Vector2(1, 0).length()).toBe(1) + expect(new Vector2(-1, 1).length()).toBe(Math.sqrt(2)) + }) + + test('tests normalize-method', () => { + expect(new Vector2(1, 0).normalize().equalsApproximately(new Vector2(1, 0), 0.000001)).toBe(true) + expect(new Vector2(1, -1).normalize().equalsApproximately(new Vector2(Math.sqrt(2) / 2, -Math.sqrt(2) / 2), 0.000001)).toBe(true) + }) + + test('tests distance-method', () => { + expect(new Vector2(0, 0).distance(new Vector2(0, -1))).toBe(1) + expect(new Vector2(1, 0).distance(new Vector2(0, 1))).toBe(Math.sqrt(2)) + }) + + test('tests dotProduct-method', () => { + expect(new Vector2(1, 0).dotProduct(new Vector2(0, 1))).toBe(0) + expect(new Vector2(1, 2).dotProduct(new Vector2(3, 4))).toBe(1 * 3 + 2 * 4) + }) + + test('tests rotate-method', () => { + expect(new Vector2(0, -1).rotate(Math.PI / 2).equalsApproximately(new Vector2(1, 0), 0.000001)).toBe(true) + expect(new Vector2(1.23, -4.56).rotate(Math.PI).equalsApproximately(new Vector2(-1.23, 4.56), 0.000001)).toBe(true) + }) + + test('tests angleBetween-method', () => { + expect(new Vector2(1, 0).angleBetween(new Vector2(0, 1))).toBe(Math.PI / 2) + expect(new Vector2(1, 0).angleBetween(new Vector2(1, -1))).toBe(-Math.PI / 4) + }) +}) From 6aa9997f3afe1412c18fd2ff756d95f9daa82995 Mon Sep 17 00:00:00 2001 From: Fabian Date: Wed, 6 Oct 2021 21:51:47 +0200 Subject: [PATCH 2/4] Remove eslint disabling for no-unused-vars since now the class is exported at the end of the file --- Data-Structures/Vectors/Vector2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data-Structures/Vectors/Vector2.js b/Data-Structures/Vectors/Vector2.js index 393ebb875c..0c152c04e3 100644 --- a/Data-Structures/Vectors/Vector2.js +++ b/Data-Structures/Vectors/Vector2.js @@ -4,7 +4,7 @@ * https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics)). */ -class Vector2 { // eslint-disable-line no-unused-vars +class Vector2 { constructor (x, y) { this.x = x this.y = y From 6f7f48ee0d358e2ccd685fd17f380bed9d8a5e12 Mon Sep 17 00:00:00 2001 From: Fabian Date: Wed, 6 Oct 2021 22:03:26 +0200 Subject: [PATCH 3/4] Group all related tests to own jest describe blocks --- Data-Structures/Vectors/Vector2.test.js | 113 +++++++++++++++++------- 1 file changed, 79 insertions(+), 34 deletions(-) diff --git a/Data-Structures/Vectors/Vector2.test.js b/Data-Structures/Vectors/Vector2.test.js index 989cc0dc11..c3f1857f96 100644 --- a/Data-Structures/Vectors/Vector2.test.js +++ b/Data-Structures/Vectors/Vector2.test.js @@ -1,58 +1,103 @@ import { Vector2 } from './Vector2.js' -describe('Tests for Vector2 data scructure implementation', () => { - test('tests equalsExactly-method', () => { - expect(new Vector2(1, 0).equalsExactly(new Vector2(1, 0))).toBe(true) - expect(new Vector2(1.23, 4.56).equalsExactly(new Vector2(0, 0))).toBe(false) +describe('Vector2', () => { + describe('#equalsExactly', () => { + it('should compare equality correctly', () => { + expect(new Vector2(1, 0).equalsExactly(new Vector2(1, 0))).toBe(true) + + expect(new Vector2(1.23, 4.56).equalsExactly(new Vector2(0, 0))).toBe(false) + }) }) - test('tests equalsApproximately-method', () => { - expect(new Vector2(1, 0).equalsApproximately(new Vector2(1, 0.0000001), 0.000001)).toBe(true) - expect(new Vector2(1.23, 4.56).equalsApproximately(new Vector2(1.24, 4.56), 0.000001)).toBe(false) + describe('#equalsApproximately', () => { + it('should compare equality (approximately) correctly', () => { + expect(new Vector2(1, 0).equalsApproximately(new Vector2(1, 0.0000001), 0.000001)) + .toBe(true) + + expect(new Vector2(1.23, 4.56).equalsApproximately(new Vector2(1.24, 4.56), 0.000001)) + .toBe(false) + }) }) - test('tests add-method', () => { - expect(new Vector2(1, 0).add(new Vector2(0, 1)).equalsApproximately(new Vector2(1, 1), 0.000001)).toBe(true) - expect(new Vector2(-3.3, -9).add(new Vector2(-2.2, 3)).equalsApproximately(new Vector2(-5.5, -6), 0.000001)).toBe(true) + describe('#add', () => { + it('should add two vectors correctly', () => { + expect(new Vector2(1, 0).add(new Vector2(0, 1)).equalsApproximately(new Vector2(1, 1), 0.000001)) + .toBe(true) + + expect(new Vector2(-3.3, -9).add(new Vector2(-2.2, 3)).equalsApproximately(new Vector2(-5.5, -6), 0.000001)) + .toBe(true) + }) }) - test('tests subtract-method', () => { - expect(new Vector2(1, 0).subtract(new Vector2(0, 1)).equalsApproximately(new Vector2(1, -1), 0.000001)).toBe(true) - expect(new Vector2(234.5, 1.7).subtract(new Vector2(3.3, 2.7)).equalsApproximately(new Vector2(231.2, -1), 0.000001)).toBe(true) + describe('#subtract', () => { + it('should subtract two vectors correctly', () => { + expect(new Vector2(1, 0).subtract(new Vector2(0, 1)).equalsApproximately(new Vector2(1, -1), 0.000001)) + .toBe(true) + + expect(new Vector2(234.5, 1.7).subtract(new Vector2(3.3, 2.7)).equalsApproximately(new Vector2(231.2, -1), 0.000001)) + .toBe(true) + }) }) - test('tests multiply-method', () => { - expect(new Vector2(1, 0).multiply(5).equalsApproximately(new Vector2(5, 0), 0.000001)).toBe(true) - expect(new Vector2(3.41, -7.12).multiply(-3.1).equalsApproximately(new Vector2(-10.571, 22.072), 0.000001)).toBe(true) + describe('#multiply', () => { + it('should multiply two vectors correctly', () => { + expect(new Vector2(1, 0).multiply(5).equalsApproximately(new Vector2(5, 0), 0.000001)) + .toBe(true) + + expect(new Vector2(3.41, -7.12).multiply(-3.1).equalsApproximately(new Vector2(-10.571, 22.072), 0.000001)) + .toBe(true) + }) }) - test('tests length-method', () => { - expect(new Vector2(1, 0).length()).toBe(1) - expect(new Vector2(-1, 1).length()).toBe(Math.sqrt(2)) + describe('#length', () => { + it('should calculate it\'s length correctly', () => { + expect(new Vector2(1, 0).length()).toBe(1) + + expect(new Vector2(-1, 1).length()).toBe(Math.sqrt(2)) + }) }) - test('tests normalize-method', () => { - expect(new Vector2(1, 0).normalize().equalsApproximately(new Vector2(1, 0), 0.000001)).toBe(true) - expect(new Vector2(1, -1).normalize().equalsApproximately(new Vector2(Math.sqrt(2) / 2, -Math.sqrt(2) / 2), 0.000001)).toBe(true) + describe('#normalize', () => { + it('should normalize vectors correctly', () => { + expect(new Vector2(1, 0).normalize().equalsApproximately(new Vector2(1, 0), 0.000001)) + .toBe(true) + + expect(new Vector2(1, -1).normalize().equalsApproximately(new Vector2(Math.sqrt(2) / 2, -Math.sqrt(2) / 2), 0.000001)) + .toBe(true) + }) }) - test('tests distance-method', () => { - expect(new Vector2(0, 0).distance(new Vector2(0, -1))).toBe(1) - expect(new Vector2(1, 0).distance(new Vector2(0, 1))).toBe(Math.sqrt(2)) + describe('#distance', () => { + it('should calculate the distance between two vectors correctly', () => { + expect(new Vector2(0, 0).distance(new Vector2(0, -1))).toBe(1) + + expect(new Vector2(1, 0).distance(new Vector2(0, 1))).toBe(Math.sqrt(2)) + }) }) - test('tests dotProduct-method', () => { - expect(new Vector2(1, 0).dotProduct(new Vector2(0, 1))).toBe(0) - expect(new Vector2(1, 2).dotProduct(new Vector2(3, 4))).toBe(1 * 3 + 2 * 4) + describe('#dotProduct', () => { + it('should calculate the dot product correctly', () => { + expect(new Vector2(1, 0).dotProduct(new Vector2(0, 1))).toBe(0) + + expect(new Vector2(1, 2).dotProduct(new Vector2(3, 4))).toBe(11) // 1 * 3 + 2 * 4 + }) }) - test('tests rotate-method', () => { - expect(new Vector2(0, -1).rotate(Math.PI / 2).equalsApproximately(new Vector2(1, 0), 0.000001)).toBe(true) - expect(new Vector2(1.23, -4.56).rotate(Math.PI).equalsApproximately(new Vector2(-1.23, 4.56), 0.000001)).toBe(true) + describe('#rotate', () => { + it('should rotate a vector correctly', () => { + expect(new Vector2(0, -1).rotate(Math.PI / 2).equalsApproximately(new Vector2(1, 0), 0.000001)) + .toBe(true) + + expect(new Vector2(1.23, -4.56).rotate(Math.PI).equalsApproximately(new Vector2(-1.23, 4.56), 0.000001)) + .toBe(true) + }) }) - test('tests angleBetween-method', () => { - expect(new Vector2(1, 0).angleBetween(new Vector2(0, 1))).toBe(Math.PI / 2) - expect(new Vector2(1, 0).angleBetween(new Vector2(1, -1))).toBe(-Math.PI / 4) + describe('#angleBetween', () => { + it('should calculate the angle between two vectors correctly', () => { + expect(new Vector2(1, 0).angleBetween(new Vector2(0, 1))).toBe(Math.PI / 2) + + expect(new Vector2(1, 0).angleBetween(new Vector2(1, -1))).toBe(-Math.PI / 4) + }) }) }) From 5df4edd3186da0d6d7217d402506f4cf19897677 Mon Sep 17 00:00:00 2001 From: Fabian Date: Wed, 6 Oct 2021 22:10:48 +0200 Subject: [PATCH 4/4] Improve the doc-comment by fixing it's formatting --- Data-Structures/Vectors/Vector2.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Data-Structures/Vectors/Vector2.js b/Data-Structures/Vectors/Vector2.js index 0c152c04e3..780abbfcb9 100644 --- a/Data-Structures/Vectors/Vector2.js +++ b/Data-Structures/Vectors/Vector2.js @@ -1,7 +1,8 @@ /** - * In mathematics and physics, a vector is an element of a vector space. The Vector2-class - * implements 2-dimensional vectors together with various vector-operations (description adapted from - * https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics)). + * In mathematics and physics, a vector is an element of a vector space. + * + * The Vector2-class implements 2-dimensional vectors together with various vector-operations. + * @see https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics). */ class Vector2 {