#include #include "../Tests.h" #include "core/Vector.h" const float eps = 0.0001f; #define V3 CoreVector3 #define CV3(a, b, c) \ &(V3) { \ a, b, c \ } #define CV30 CV3(0, 0, 0) #define IV3 CoreIntVector3 #define CIV3(a, b, c) \ &(IV3) { \ a, b, c \ } #define CIV30 CIV3(0, 0, 0) static void testVector3B(const char* file, int line, const V3* wanted, const V3* actual) { for(int i = 0; i < 3; i++) { coreTestFloat(file, line, wanted->data[i], actual->data[i], eps); } } #define testVector3(wanted, actual) \ testVector3B(__FILE__, __LINE__, wanted, actual) static void testIntVector3(const IV3* wanted, const IV3* actual) { for(int i = 0; i < 3; i++) { CORE_TEST_INT(wanted->data[i], actual->data[i]); } } static void testSetAngles() { float root = sqrtf(2) * 0.5f; testVector3(CV3(1, 0, 0), coreAngles(CV30, 0, 0)); testVector3(CV3(root, 0, -root), coreAngles(CV30, 45, 0)); testVector3(CV3(0, 0, -1), coreAngles(CV30, 90, 0)); testVector3(CV3(-root, 0, -root), coreAngles(CV30, 135, 0)); testVector3(CV3(-1, 0, 0), coreAngles(CV30, 180, 0)); testVector3(CV3(-root, 0, root), coreAngles(CV30, 225, 0)); testVector3(CV3(0, 0, 1), coreAngles(CV30, 270, 0)); testVector3(CV3(root, 0, root), coreAngles(CV30, 315, 0)); testVector3(CV3(0, 1, 0), coreAngles(CV30, 0, 90)); testVector3(CV3(0, 1, 0), coreAngles(CV30, 90, 90)); testVector3(CV3(0, 1, 0), coreAngles(CV30, 180, 90)); testVector3(CV3(0, 1, 0), coreAngles(CV30, 270, 90)); testVector3(CV3(0, -1, 0), coreAngles(CV30, 0, -90)); testVector3(CV3(0, -1, 0), coreAngles(CV30, 90, -90)); testVector3(CV3(0, -1, 0), coreAngles(CV30, 180, -90)); testVector3(CV3(0, -1, 0), coreAngles(CV30, 270, -90)); testVector3(CV3(root, root, 0), coreAngles(CV30, 0, 45)); testVector3(CV3(0, root, -root), coreAngles(CV30, 90, 45)); testVector3(CV3(-root, root, 0), coreAngles(CV30, 180, 45)); testVector3(CV3(0, root, root), coreAngles(CV30, 270, 45)); testVector3(CV3(root, -root, 0), coreAngles(CV30, 0, -45)); testVector3(CV3(0, -root, -root), coreAngles(CV30, 90, -45)); testVector3(CV3(-root, -root, 0), coreAngles(CV30, 180, -45)); testVector3(CV3(0, -root, root), coreAngles(CV30, 270, -45)); testVector3(CV3(0.5f, root, -0.5f), coreAngles(CV30, 45, 45)); } static void testCross() { testVector3(CV3(0, 0, 1), coreCross(CV30, CV3(1, 0, 0), CV3(0, 1, 0))); testVector3(CV3(0, -1, 0), coreCross(CV30, CV3(1, 0, 0), CV3(0, 0, 1))); testVector3(CV3(0, 0, -1), coreCross(CV30, CV3(0, 1, 0), CV3(1, 0, 0))); testVector3(CV3(1, 0, 0), coreCross(CV30, CV3(0, 1, 0), CV3(0, 0, 1))); testVector3(CV3(0, 1, 0), coreCross(CV30, CV3(0, 0, 1), CV3(1, 0, 0))); testVector3(CV3(-1, 0, 0), coreCross(CV30, CV3(0, 0, 1), CV3(0, 1, 0))); } static void testSetAdd() { V3 v = {0}; coreAddSetV3(&v, CV3(1, 2, 3)); testVector3(CV3(1, 2, 3), &v); coreAddSetV3(&v, CV3(2, 3, 4)); testVector3(CV3(3, 5, 7), &v); } static void testAdd() { testVector3(CV3(1, 2, 3), coreAddV3(CV30, CV30, CV3(1, 2, 3))); testVector3(CV3(3, 5, 7), coreAddV3(CV30, CV3(1, 2, 3), CV3(2, 3, 4))); } static void testSetSub() { V3 v = {0}; coreSubSetV3(&v, CV3(1, 2, 3)); testVector3(CV3(-1, -2, -3), &v); coreSubSetV3(&v, CV3(2, 3, 4)); testVector3(CV3(-3, -5, -7), &v); } static void testSub() { testVector3(CV3(1, 2, 3), coreSubV3(CV30, CV30, CV3(-1, -2, -3))); testVector3(CV3(-1, -1, -1), coreSubV3(CV30, CV3(1, 2, 3), CV3(2, 3, 4))); } static void testSetMul() { V3 v = {1, 2, 3}; coreMulSetV3F(&v, 3); testVector3(CV3(3, 6, 9), &v); coreMulSetV3F(&v, -2); testVector3(CV3(-6, -12, -18), &v); } static void testMul() { testVector3(CV3(3, 6, 9), coreMulV3F(CV30, CV3(1, 2, 3), 3)); } static void testSetMulVector() { V3 v = {1, 2, 3}; coreMulSetV3(&v, CV3(2, 1, 3)); testVector3(CV3(2, 2, 9), &v); coreMulSetV3(&v, CV3(-3, 4, -2)); testVector3(CV3(-6, 8, -18), &v); } static void testMulVector() { testVector3(CV3(-2, -2, -9), coreMulV3(CV30, CV3(2, 1, 3), CV3(-1, -2, -3))); testVector3(CV3(2, 2, 9), coreMulV3(CV30, CV3(1, 2, 3), CV3(2, 1, 3))); } static void testSetDiv() { V3 v = {12, 24, 9}; coreDivSetV3F(&v, 3); testVector3(CV3(4, 8, 3), &v); coreDivSetV3F(&v, -2); testVector3(CV3(-2, -4, -1.5f), &v); } static void testDiv() { testVector3(CV3(-1, -2, -3), coreDivV3F(CV30, CV3(-3, -6, -9), 3)); } static void testSetDivVector() { V3 v = {3, 4, 6}; coreDivSetV3(&v, CV3(2, 1, 3)); testVector3(CV3(1.5f, 4, 2), &v); coreDivSetV3(&v, CV3(-3, 4, -2)); testVector3(CV3(-0.5f, 1, -1), &v); } static void testDivVector() { testVector3(CV3(-2, -0.5f, -1), coreDivV3(CV30, CV3(2, 1, 3), CV3(-1, -2, -3))); testVector3(CV3(0.5f, 2, 1), coreDivV3(CV30, CV3(1, 2, 3), CV3(2, 1, 3))); } static void testSetInvert() { testVector3(CV3(-1, 2, 3), coreInvertSetV3(CV3(1, -2, -3))); } static void testInvert() { testVector3(CV3(-1, 2, 3), coreInvertV3(CV30, CV3(1, -2, -3))); } static void testDot() { CORE_TEST_FLOAT(9, coreDotV3(CV3(-4, 2, -3), CV3(-1, -2, -3)), eps); CORE_TEST_FLOAT(-22, coreDotV3(CV3(2, 2, -4), CV3(1, -2, 5)), eps); } static void testSquareLength() { CORE_TEST_FLOAT(29, coreSquareLengthV3(CV3(-4, 2, -3)), eps); CORE_TEST_FLOAT(24, coreSquareLengthV3(CV3(2, 2, -4)), eps); } static void testLength() { CORE_TEST_FLOAT(3, coreLengthV3(CV3(-2, 2, -1)), eps); CORE_TEST_FLOAT(7, coreLengthV3(CV3(6, 2, -3)), eps); } static void testNormalize() { V3 v1 = {-2, 2, -1}; V3 v2; coreMulV3F(&v2, &v1, 1.0f / 3.0f); coreNormalizeV3(&v1); testVector3(&v2, &v1); V3 v3 = {6, 2, -3}; V3 v4; coreMulV3F(&v4, &v3, 1.0f / 7.0f); coreNormalizeV3(&v3); testVector3(&v4, &v3); } static void testCast() { testVector3(CV3(-2.0f, 2.0f, 9.0f), coreConvertIV3(CV30, CIV3(-2, 2, 9))); testIntVector3(CIV3(-2, 2, 9), coreConvertV3(CIV30, CV3(-2.5f, 2.6f, 9.0f))); } static void testToString() { V3 v = {4, 5, 6}; char buffer[64]; coreToStringV3(&v, buffer, sizeof(buffer)); CORE_TEST_STRING("[4.000, 5.000, 6.000]", buffer); } void coreTestVector() { testSetAngles(); testCross(); testSetAdd(); testAdd(); testSetSub(); testSub(); testSetMul(); testMul(); testSetMulVector(); testMulVector(); testSetDiv(); testDiv(); testSetDivVector(); testDivVector(); testSetInvert(); testInvert(); testDot(); testSquareLength(); testLength(); testNormalize(); testCast(); testToString(); }