From 77daf85fc1855b32728b243b4d71d95a3a7b930e Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 26 Jul 2023 19:31:43 +0200 Subject: [PATCH] Add unit test for angles unwind math #1267 --- Source/Engine/Tests/TestMath.cs | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Source/Engine/Tests/TestMath.cs diff --git a/Source/Engine/Tests/TestMath.cs b/Source/Engine/Tests/TestMath.cs new file mode 100644 index 000000000..bc1bc1a8d --- /dev/null +++ b/Source/Engine/Tests/TestMath.cs @@ -0,0 +1,47 @@ +// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. + +#if FLAX_TESTS +using NUnit.Framework; + +namespace FlaxEngine.Tests +{ + /// + /// Tests for and . + /// + [TestFixture] + public class TestMath + { + /// + /// Test unwinding angles. + /// + [Test] + public void TestUnwind() + { + Assert.AreEqual(0.0f, Mathf.UnwindDegreesAccurate(0.0f)); + Assert.AreEqual(45.0f, Mathf.UnwindDegreesAccurate(45.0f)); + Assert.AreEqual(90.0f, Mathf.UnwindDegreesAccurate(90.0f)); + Assert.AreEqual(180.0f, Mathf.UnwindDegreesAccurate(180.0f)); + Assert.AreEqual(0.0f, Mathf.UnwindDegreesAccurate(360.0f)); + Assert.AreEqual(0.0f, Mathf.UnwindDegrees(0.0f)); + Assert.AreEqual(45.0f, Mathf.UnwindDegrees(45.0f)); + Assert.AreEqual(90.0f, Mathf.UnwindDegrees(90.0f)); + Assert.AreEqual(180.0f, Mathf.UnwindDegrees(180.0f)); + Assert.AreEqual(0.0f, Mathf.UnwindDegrees(360.0f)); + var fError = 0.001f; + var dError = 0.00001; + for (float f = -400.0f; f <= 400.0f; f += 0.1f) + { + var f1 = Mathf.UnwindDegreesAccurate(f); + var f2 = Mathf.UnwindDegrees(f); + if (Mathf.Abs(f1 - f2) >= fError) + throw new Exception($"Failed on angle={f}, {f1} != {f2}"); + var d = (double)f; + var d1 = Mathd.UnwindDegreesAccurate(d); + var d2 = Mathd.UnwindDegrees(d); + if (Mathd.Abs(d1 - d2) >= dError) + throw new Exception($"Failed on angle={d}, {d1} != {d2}"); + } + } + } +} +#endif