From 30fe33bb0bcffe8a06b1b93b34b7e5340a63f6a4 Mon Sep 17 00:00:00 2001 From: elexis Date: Mon, 29 Jan 2018 09:51:20 +0000 Subject: [PATCH] Add another Vector2D rotation test (illustrating the most common rmgen rotation transformation). TS_ASSERT_EQUALS_APPROX must not silently pass if epsilon wasn't given. Sort Vector2D.rotate summands by component rather than saving one character. This was SVN commit r21062. --- binaries/data/mods/public/globalscripts/vector.js | 2 +- .../simulation/components/tests/test_Vector.js | 15 +++++++++++++++ binaries/data/tests/test_setup.js | 3 +++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/binaries/data/mods/public/globalscripts/vector.js b/binaries/data/mods/public/globalscripts/vector.js index 3a404135d4..5d10e4c5b5 100644 --- a/binaries/data/mods/public/globalscripts/vector.js +++ b/binaries/data/mods/public/globalscripts/vector.js @@ -77,7 +77,7 @@ Vector2D.prototype.rotate = function(angle) return this.set( this.x * cos + this.y * sin, - this.y * cos - this.x * sin); + -this.x * sin + this.y * cos); }; /** diff --git a/binaries/data/mods/public/simulation/components/tests/test_Vector.js b/binaries/data/mods/public/simulation/components/tests/test_Vector.js index 92f1f7876b..8ae3e6b715 100644 --- a/binaries/data/mods/public/simulation/components/tests/test_Vector.js +++ b/binaries/data/mods/public/simulation/components/tests/test_Vector.js @@ -74,6 +74,21 @@ var brokenVector = { } } +// Test Vector2D rotation further +{ + let epsilon = 0.00000001; + + for (let i = 0; i <= 128; ++i) + { + let angle = i / 128 * Math.PI; + let vec1 = new Vector2D(Math.cos(angle), Math.sin(angle)); + let vec2 = new Vector2D(1, 0).rotate(-angle); + + TS_ASSERT_EQUALS_APPROX(vec1.x, vec2.x, epsilon); + TS_ASSERT_EQUALS_APPROX(vec1.y, vec2.y, epsilon); + } +} + // Test Vector2D rotation around a center { let epsilon = 0.00000001; diff --git a/binaries/data/tests/test_setup.js b/binaries/data/tests/test_setup.js index 94d1980b14..f4c0f78cfd 100644 --- a/binaries/data/tests/test_setup.js +++ b/binaries/data/tests/test_setup.js @@ -34,6 +34,9 @@ global.TS_ASSERT_EQUALS = function TS_ASSERT_EQUALS(x, y) global.TS_ASSERT_EQUALS_APPROX = function TS_ASSERT_EQUALS_APPROX(x, y, maxDifference) { + if (!Number.isInteger(maxDifference)) + fail("Test must pass a maximum difference!"); + if (Math.abs(x - y) > maxDifference) fail("Expected almost equal, got " + uneval(x) + " !== " + uneval(y)); }