Browse Source

Add some unit tests for RoundedLerp

Michael Ragazzon 4 years ago
parent
commit
85bbd93a53
2 changed files with 106 additions and 1 deletions
  1. 1 1
      Include/RmlUi/Core/Math.h
  2. 105 0
      Tests/Source/UnitTests/Math.cpp

+ 1 - 1
Include/RmlUi/Core/Math.h

@@ -82,7 +82,7 @@ Type Lerp(float t, Type v0, Type v1)
 	return v0 * (1.0f - t) + v1 * t;
 }
 
-Colourb RoundedLerp(float t, Colourb v0, Colourb v1);
+RMLUICORE_API Colourb RoundedLerp(float t, Colourb c0, Colourb c1);
 
 /// Evaluates if a number is, or close to, zero.
 /// @param[in] value The number to compare to zero.

+ 105 - 0
Tests/Source/UnitTests/Math.cpp

@@ -0,0 +1,105 @@
+/*
+ * This source file is part of RmlUi, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://github.com/mikke89/RmlUi
+ *
+ * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
+ * Copyright (c) 2019 The RmlUi Team, and contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include <RmlUi/Core/Math.h>
+#include <RmlUi/Core/Types.h>
+#include <doctest.h>
+
+using namespace Rml;
+
+TEST_CASE("Math")
+{
+	const Colourb c0(0, 0, 0, 255);
+	const Colourb c1(255, 0, 0, 255);
+	const Colourb c2(127, 0, 0, 255);
+
+	Colourb c;
+
+	c = Math::RoundedLerp(0.0f, c0, c1);
+	REQUIRE(c.red == c0.red);
+
+	c = Math::RoundedLerp(179.f / 255.f, c0, c1);
+	REQUIRE(c.red == 179);
+
+	c = Math::RoundedLerp(50.4f / 255.f, c0, c1);
+	REQUIRE(c.red == 50);
+
+	c = Math::RoundedLerp(50.6f / 255.f, c0, c1);
+	REQUIRE(c.red == 51);
+
+	c = Math::RoundedLerp(0.0f, c1, c0);
+	REQUIRE(c.red == c1.red);
+
+	c = Math::RoundedLerp(1.0f, c0, c1);
+	REQUIRE(c.red == c1.red);
+
+	c = Math::RoundedLerp(1.0f, c1, c0);
+	REQUIRE(c.red == c0.red);
+
+	c = Math::RoundedLerp(1.0f, c0, c2);
+	REQUIRE(c.red == c2.red);
+
+	c = Math::RoundedLerp(0.0f, c2, c1);
+	REQUIRE(c.red == c2.red);
+
+	// Full-range interpolation
+	for (int i = 0; i <= 255; i++)
+	{
+		const float t = float(i) / 255.f;
+		const auto r = Math::RoundedLerp(t, c0, c1).red;
+		REQUIRE(r == i);
+	}
+
+	// Reverse full-range interpolation
+	for (int i = 0; i <= 255; i++)
+	{
+		const float t = float(i) / 255.f;
+		const auto r = Math::RoundedLerp(t, c1, c0).red;
+		REQUIRE(r == 255 - i);
+	}
+
+	// Half-range interpolation
+	for (int i = 0; i <= 255; i++)
+	{
+		const float t = float(i) / 255.f;
+		const auto r = Math::RoundedLerp(t, c0, c2).red;
+		REQUIRE(r == i / 2);
+	}
+
+	// Same-color interpolation with fractional t
+	for (int i = 0; i <= 379; i++)
+	{
+		const float t = float(i) / 379.f;
+		const auto r0 = Math::RoundedLerp(t, c0, c0).red;
+		const auto r1 = Math::RoundedLerp(t, c1, c1).red;
+		const auto r2 = Math::RoundedLerp(t, c2, c2).red;
+		REQUIRE(r0 == c0.red);
+		REQUIRE(r1 == c1.red);
+		REQUIRE(r2 == c2.red);
+	}
+}