|
@@ -0,0 +1,271 @@
|
|
|
+#include "tests/VectorTests.h"
|
|
|
+
|
|
|
+#include "math/Vector.h"
|
|
|
+#include "test/Test.h"
|
|
|
+
|
|
|
+const float eps = 0.0001f;
|
|
|
+
|
|
|
+template<int N, typename T>
|
|
|
+static void compareVectors(const Core::Vector<N, T>& wanted,
|
|
|
+ const Core::Vector<N, T>& actual) {
|
|
|
+ for(int i = 0; i < N; i++) {
|
|
|
+ CORE_TEST_FLOAT(static_cast<float>(wanted[i]),
|
|
|
+ static_cast<float>(actual[i]), eps);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static void testInitAndRead() {
|
|
|
+ Core::Vector3 v1;
|
|
|
+ Core::Vector2 v2(1.0f, 2.0f);
|
|
|
+ Core::Vector3 v3(3.0f, 4.0f, 5.0f);
|
|
|
+ Core::Vector4 v4(6.0f, 7.0f, 8.0f, 9.0f);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+static void testSetAngles() {
|
|
|
+ float root = sqrtf(2.0f) * 0.5f;
|
|
|
+ compareVectors(Core::Vector3(1.0f, 0.0f, 0.0f),
|
|
|
+ Core::Vector3().setAngles(0.0f, 0.0f));
|
|
|
+ compareVectors(Core::Vector3(root, 0.0f, -root),
|
|
|
+ Core::Vector3().setAngles(45.0f, 0.0f));
|
|
|
+ compareVectors(Core::Vector3(0.0f, 0.0f, -1.0f),
|
|
|
+ Core::Vector3().setAngles(90.0f, 0.0f));
|
|
|
+ compareVectors(Core::Vector3(-root, 0.0f, -root),
|
|
|
+ Core::Vector3().setAngles(135.0f, 0.0f));
|
|
|
+ compareVectors(Core::Vector3(-1.0f, 0.0f, 0.0f),
|
|
|
+ Core::Vector3().setAngles(180.0f, 0.0f));
|
|
|
+ compareVectors(Core::Vector3(-root, 0.0f, root),
|
|
|
+ Core::Vector3().setAngles(225.0f, 0.0f));
|
|
|
+ compareVectors(Core::Vector3(0.0f, 0.0f, 1.0f),
|
|
|
+ Core::Vector3().setAngles(270.0f, 0.0f));
|
|
|
+ compareVectors(Core::Vector3(root, 0.0f, root),
|
|
|
+ Core::Vector3().setAngles(315.0f, 0.0f));
|
|
|
+
|
|
|
+ compareVectors(Core::Vector3(0.0f, 1.0f, 0.0f),
|
|
|
+ Core::Vector3().setAngles(0.0f, 90.0f));
|
|
|
+ compareVectors(Core::Vector3(0.0f, 1.0f, 0.0f),
|
|
|
+ Core::Vector3().setAngles(90.0f, 90.0f));
|
|
|
+ compareVectors(Core::Vector3(0.0f, 1.0f, 0.0f),
|
|
|
+ Core::Vector3().setAngles(180.0f, 90.0f));
|
|
|
+ compareVectors(Core::Vector3(0.0f, 1.0f, 0.0f),
|
|
|
+ Core::Vector3().setAngles(270.0f, 90.0f));
|
|
|
+
|
|
|
+ compareVectors(Core::Vector3(0.0f, -1.0f, 0.0f),
|
|
|
+ Core::Vector3().setAngles(0.0f, -90.0f));
|
|
|
+ compareVectors(Core::Vector3(0.0f, -1.0f, 0.0f),
|
|
|
+ Core::Vector3().setAngles(90.0f, -90.0f));
|
|
|
+ compareVectors(Core::Vector3(0.0f, -1.0f, 0.0f),
|
|
|
+ Core::Vector3().setAngles(180.0f, -90.0f));
|
|
|
+ compareVectors(Core::Vector3(0.0f, -1.0f, 0.0f),
|
|
|
+ Core::Vector3().setAngles(270.0f, -90.0f));
|
|
|
+
|
|
|
+ compareVectors(Core::Vector3(root, root, 0.0f),
|
|
|
+ Core::Vector3().setAngles(0.0f, 45.0f));
|
|
|
+ compareVectors(Core::Vector3(0.0f, root, -root),
|
|
|
+ Core::Vector3().setAngles(90.0f, 45.0f));
|
|
|
+ compareVectors(Core::Vector3(-root, root, 0.0f),
|
|
|
+ Core::Vector3().setAngles(180.0f, 45.0f));
|
|
|
+ compareVectors(Core::Vector3(0.0f, root, root),
|
|
|
+ Core::Vector3().setAngles(270.0f, 45.0f));
|
|
|
+
|
|
|
+ compareVectors(Core::Vector3(root, -root, 0.0f),
|
|
|
+ Core::Vector3().setAngles(0.0f, -45.0f));
|
|
|
+ compareVectors(Core::Vector3(0.0f, -root, -root),
|
|
|
+ Core::Vector3().setAngles(90.0f, -45.0f));
|
|
|
+ compareVectors(Core::Vector3(-root, -root, 0.0f),
|
|
|
+ Core::Vector3().setAngles(180.0f, -45.0f));
|
|
|
+ compareVectors(Core::Vector3(0.0f, -root, root),
|
|
|
+ Core::Vector3().setAngles(270.0f, -45.0f));
|
|
|
+
|
|
|
+ compareVectors(Core::Vector3(0.5f, root, -0.5f),
|
|
|
+ Core::Vector3().setAngles(45.0f, 45.0f));
|
|
|
+}
|
|
|
+
|
|
|
+static void testCross() {
|
|
|
+ compareVectors(
|
|
|
+ Core::Vector3(0.0f, 0.0f, 1.0f),
|
|
|
+ Core::Vector3(1.0f, 0.0f, 0.0f).cross(Core::Vector3(0.0f, 1.0f, 0.0f)));
|
|
|
+ compareVectors(
|
|
|
+ Core::Vector3(0.0f, -1.0f, 0.0f),
|
|
|
+ Core::Vector3(1.0f, 0.0f, 0.0f).cross(Core::Vector3(0.0f, 0.0f, 1.0f)));
|
|
|
+ compareVectors(
|
|
|
+ Core::Vector3(0.0f, 0.0f, -1.0f),
|
|
|
+ Core::Vector3(0.0f, 1.0f, 0.0f).cross(Core::Vector3(1.0f, 0.0f, 0.0f)));
|
|
|
+ compareVectors(
|
|
|
+ Core::Vector3(1.0f, 0.0f, 0.0f),
|
|
|
+ Core::Vector3(0.0f, 1.0f, 0.0f).cross(Core::Vector3(0.0f, 0.0f, 1.0f)));
|
|
|
+ compareVectors(
|
|
|
+ Core::Vector3(0.0f, 1.0f, 0.0f),
|
|
|
+ Core::Vector3(0.0f, 0.0f, 1.0f).cross(Core::Vector3(1.0f, 0.0f, 0.0f)));
|
|
|
+ compareVectors(
|
|
|
+ Core::Vector3(-1.0f, 0.0f, 0.0f),
|
|
|
+ Core::Vector3(0.0f, 0.0f, 1.0f).cross(Core::Vector3(0.0f, 1.0f, 0.0f)));
|
|
|
+}
|
|
|
+
|
|
|
+static void testSetAdd() {
|
|
|
+ Core::Vector3 v;
|
|
|
+ v += Core::Vector3(1.0f, 2.0f, 3.0f);
|
|
|
+ compareVectors(Core::Vector3(1.0f, 2.0f, 3.0f), v);
|
|
|
+ v += Core::Vector3(2.0f, 3.0f, 4.0f);
|
|
|
+ compareVectors(Core::Vector3(3.0f, 5.0f, 7.0f), v);
|
|
|
+}
|
|
|
+
|
|
|
+static void testAdd() {
|
|
|
+ compareVectors(Core::Vector3(1.0f, 2.0f, 3.0f),
|
|
|
+ Core::Vector3() + Core::Vector3(1.0f, 2.0f, 3.0f));
|
|
|
+ compareVectors(Core::Vector3(3.0f, 5.0f, 7.0f),
|
|
|
+ Core::Vector3(1.0f, 2.0f, 3.0f) +
|
|
|
+ Core::Vector3(2.0f, 3.0f, 4.0f));
|
|
|
+}
|
|
|
+
|
|
|
+static void testSetSub() {
|
|
|
+ Core::Vector3 v;
|
|
|
+ v -= Core::Vector3(1.0f, 2.0f, 3.0f);
|
|
|
+ compareVectors(Core::Vector3(-1.0f, -2.0f, -3.0f), v);
|
|
|
+ v -= Core::Vector3(2.0f, 3.0f, 4.0f);
|
|
|
+ compareVectors(Core::Vector3(-3.0f, -5.0f, -7.0f), v);
|
|
|
+}
|
|
|
+
|
|
|
+static void testSub() {
|
|
|
+ compareVectors(Core::Vector3(1.0f, 2.0f, 3.0f),
|
|
|
+ Core::Vector3() - Core::Vector3(-1.0f, -2.0f, -3.0f));
|
|
|
+ compareVectors(Core::Vector3(-1.0f, -1.0f, -1.0f),
|
|
|
+ Core::Vector3(1.0f, 2.0f, 3.0f) -
|
|
|
+ Core::Vector3(2.0f, 3.0f, 4.0f));
|
|
|
+}
|
|
|
+
|
|
|
+static void testInvert() {
|
|
|
+ compareVectors(Core::Vector3(-1.0f, 2.0f, 3.0f),
|
|
|
+ -Core::Vector3(1.0f, -2.0f, -3.0f));
|
|
|
+}
|
|
|
+
|
|
|
+static void testSetMul() {
|
|
|
+ Core::Vector3 v(1.0f, 2.0f, 3.0f);
|
|
|
+ v *= 3.0f;
|
|
|
+ compareVectors(Core::Vector3(3.0f, 6.0f, 9.0f), v);
|
|
|
+ v *= -2.0f;
|
|
|
+ compareVectors(Core::Vector3(-6.0f, -12.0f, -18.0f), v);
|
|
|
+}
|
|
|
+
|
|
|
+static void testMul() {
|
|
|
+ compareVectors(Core::Vector3(-3.0f, -6.0f, -9.0f),
|
|
|
+ 3.0f * Core::Vector3(-1.0f, -2.0f, -3.0f));
|
|
|
+ compareVectors(Core::Vector3(3.0f, 6.0f, 9.0f),
|
|
|
+ Core::Vector3(1.0f, 2.0f, 3.0f) * 3.0);
|
|
|
+}
|
|
|
+
|
|
|
+static void testSetMulVector() {
|
|
|
+ Core::Vector3 v(1.0f, 2.0f, 3.0f);
|
|
|
+ v *= Core::Vector3(2.0f, 1.0f, 3.0f);
|
|
|
+ compareVectors(Core::Vector3(2.0f, 2.0f, 9.0f), v);
|
|
|
+ v *= Core::Vector3(-3.0f, 4.0f, -2.0f);
|
|
|
+ compareVectors(Core::Vector3(-6.0f, 8.0f, -18.0f), v);
|
|
|
+}
|
|
|
+
|
|
|
+static void testMulVector() {
|
|
|
+ compareVectors(Core::Vector3(-2.0f, -2.0f, -9.0f),
|
|
|
+ Core::Vector3(2.0f, 1.0f, 3.0f) *
|
|
|
+ Core::Vector3(-1.0f, -2.0f, -3.0f));
|
|
|
+ compareVectors(Core::Vector3(2.0f, 2.0f, 9.0f),
|
|
|
+ Core::Vector3(1.0f, 2.0f, 3.0f) *
|
|
|
+ Core::Vector3(2.0f, 1.0f, 3.0f));
|
|
|
+}
|
|
|
+
|
|
|
+static void testSetDiv() {
|
|
|
+ Core::Vector3 v(12.0f, 24.0f, 9.0f);
|
|
|
+ v /= 3.0f;
|
|
|
+ compareVectors(Core::Vector3(4.0f, 8.0f, 3.0f), v);
|
|
|
+ v /= -2.0f;
|
|
|
+ compareVectors(Core::Vector3(-2.0f, -4.0f, -1.5f), v);
|
|
|
+}
|
|
|
+
|
|
|
+static void testDiv() {
|
|
|
+ compareVectors(Core::Vector3(-1.0f, -2.0f, -3.0f),
|
|
|
+ Core::Vector3(-3.0f, -6.0f, -9.0f) / 3.0f);
|
|
|
+}
|
|
|
+
|
|
|
+static void testDot() {
|
|
|
+ CORE_TEST_FLOAT(9.0f,
|
|
|
+ Core::Vector3(-4.0f, 2.0f, -3.0f)
|
|
|
+ .dot(Core::Vector3(-1.0f, -2.0f, -3.0f)),
|
|
|
+ eps);
|
|
|
+ CORE_TEST_FLOAT(
|
|
|
+ -22.0f,
|
|
|
+ Core::Vector3(2.0f, 2.0f, -4.0f).dot(Core::Vector3(1.0f, -2.0f, 5.0f)),
|
|
|
+ eps);
|
|
|
+}
|
|
|
+
|
|
|
+static void testSquareLength() {
|
|
|
+ CORE_TEST_FLOAT(29.0f, Core::Vector3(-4.0f, 2.0f, -3.0f).squareLength(),
|
|
|
+ eps);
|
|
|
+ CORE_TEST_FLOAT(24.0f, Core::Vector3(2.0f, 2.0f, -4.0f).squareLength(),
|
|
|
+ eps);
|
|
|
+}
|
|
|
+
|
|
|
+static void testLength() {
|
|
|
+ CORE_TEST_FLOAT(3.0f, Core::Vector3(-2.0f, 2.0f, -1.0f).length(), eps);
|
|
|
+ CORE_TEST_FLOAT(7.0f, Core::Vector3(6.0f, 2.0f, -3.0f).length(), eps);
|
|
|
+}
|
|
|
+
|
|
|
+static void testNormalize() {
|
|
|
+ Core::Vector3 v1(-2.0f, 2.0f, -1.0f);
|
|
|
+ Core::Vector3 v2 = v1 * (1.0f / 3.0f);
|
|
|
+ v1.normalize();
|
|
|
+ compareVectors(v2, v1);
|
|
|
+
|
|
|
+ Core::Vector3 v3(6.0f, 2.0f, -3.0f);
|
|
|
+ Core::Vector3 v4 = v3 * (1.0f / 7.0f);
|
|
|
+ v3.normalize();
|
|
|
+ compareVectors(v4, v3);
|
|
|
+}
|
|
|
+
|
|
|
+static void testCast() {
|
|
|
+ compareVectors(Core::Vector3(-2.5f, 2.6f, 9.0f).toInt(),
|
|
|
+ Core::IntVector3(-2, 2, 9));
|
|
|
+ compareVectors(Core::IntVector3(-2.5f, 2.6f, 9.0f).toFloat(),
|
|
|
+ Core::Vector3(-2.0f, 2.0f, 9.0f));
|
|
|
+}
|
|
|
+
|
|
|
+static void testToString() {
|
|
|
+ Core::ArrayString<200> s;
|
|
|
+ CORE_TEST_FALSE(s.append(Core::Vector<1, float>()));
|
|
|
+ CORE_TEST_FALSE(s.append(Core::Vector2(2.0f, 3.0f)));
|
|
|
+ CORE_TEST_FALSE(s.append(Core::Vector3(4.0f, 5.0f, 6.0f)));
|
|
|
+ Core::ArrayString<200> s2;
|
|
|
+ CORE_TEST_FALSE(s2.append("[0.00][2.00, 3.00][4.00, 5.00, 6.00]"));
|
|
|
+ CORE_TEST_EQUAL(s2, s);
|
|
|
+}
|
|
|
+
|
|
|
+void Core::VectorTests::test() {
|
|
|
+ testInitAndRead();
|
|
|
+ testSetAngles();
|
|
|
+ testCross();
|
|
|
+ testSetAdd();
|
|
|
+ testAdd();
|
|
|
+ testSetSub();
|
|
|
+ testSub();
|
|
|
+ testInvert();
|
|
|
+ testSetMul();
|
|
|
+ testMul();
|
|
|
+ testSetMulVector();
|
|
|
+ testMulVector();
|
|
|
+ testSetDiv();
|
|
|
+ testDiv();
|
|
|
+ testDot();
|
|
|
+ testSquareLength();
|
|
|
+ testLength();
|
|
|
+ testNormalize();
|
|
|
+ testCast();
|
|
|
+ testToString();
|
|
|
+}
|