template<class Vec> void testOperatorMultiply() { Vec vec; for (unsigned int i = 0; i < vec.getDimension(); i++) vec[i] = numbers[i]; Vec vec2; for (unsigned int i = vec.getDimension(); i < vec2.getDimension(); i++) vec2[i] = numbers[i]; // * auto vec3 = vec2 * 2; for (unsigned int i = 0; i < vec3.getDimension(); i++) ASSERT(vec3[i] == vec2[i] * 2); float dotProd = 0; for (unsigned int i = 0; i < vec3.getDimension(); i++) dotProd += vec2[i] * vec[i]; ASSERT(vec2 * vec == dotProd); // *= Vec copy { vec2 }; vec2 *= 0.5; for (unsigned int i = 0; i < vec2.getDimension(); i++) ASSERT(vec2[i] == copy[i] * 0.5); copy = vec; vec *= vec2; for (unsigned int i = 0; i < vec.getDimension(); i++) ASSERT(vec[i] == copy[i] * vec2[i]); }
template<class Vec> void testCoordinates() { Vec vec; for (unsigned int i = 0; i < vec.getDimension(); i++) vec[i] = numbers[i]; for (unsigned int i = 0; i < vec.getDimension(); i++) ASSERT_EQ(vec[i], numbers[i]); }
template<class Vec> void testGetDataPointer() { Vec vec; for (unsigned int i = 0; i < vec.getDimension(); i++) vec[i] = numbers[i]; for (unsigned int i = 0; i < vec.getDimension(); i++) ASSERT(vec.getDataPointer()[i] == vec[i]); }
template<class Vec> void testLength() { Vec vec; for (unsigned int i = 0; i < vec.getDimension(); i++) vec[i] = numbers[i]; // getSquaredLength() auto sqLen = 0.0; for (unsigned int i = 0; i < vec.getDimension(); i++) sqLen += vec[i] * vec[i]; ASSERT(dbgl::isSimilar(vec.getSquaredLength(), (float )sqLen)); // getLength() ASSERT(dbgl::isSimilar(vec.getLength(), (float )std::sqrt(sqLen))); }
template<class Vec> void testOperatorComparison() { Vec vec; for (unsigned int i = 0; i < vec.getDimension(); i++) vec[i] = numbers[i]; Vec vec2; for (unsigned int i = vec.getDimension(); i < vec2.getDimension(); i++) vec2[i] = numbers[i]; // == auto vec3 = vec2; ASSERT(vec2 == vec3); // != ASSERT(vec != vec2); }
template<class Vec> void testConstructor() { Vec vec; for (unsigned int i = 0; i < vec.getDimension(); i++) ASSERT_EQ(vec[i], 0); Vec2f vec2 { 42, 23 }; Vec vecFromSmaller { vec2 }; ASSERT_EQ(vecFromSmaller[0], 42); ASSERT_EQ(vecFromSmaller[1], 23); Vec copy { vec }; ASSERT_EQ(copy, vec); Vec init { 1, -3.7f }; ASSERT_EQ(init[0], 1); ASSERT_EQ(init[1], -3.7f); for (unsigned int i = 2; i < Vec::getDimension(); i++) ASSERT_EQ(init[i], 0); bool tooManyArguments = true; try { Vec error { -1.5f, 1.0f, 0.256f, 25.0f, -100.38585f, 0.00001f, 42.1337f, 571, 43.495f }; } catch (...) { tooManyArguments = false; } ASSERT_EQ(tooManyArguments, false); }
template<class Vec> void testOperatorAdd() { Vec vec; for (unsigned int i = 0; i < vec.getDimension(); i++) vec[i] = numbers[i]; Vec vec2; for (unsigned int i = vec.getDimension(); i < vec2.getDimension(); i++) vec2[i] = numbers[i]; // + auto vec3 = vec + vec2; for (unsigned int i = 0; i < vec3.getDimension(); i++) ASSERT(vec3[i] == vec[i] + vec2[i]); // += Vec copy { vec }; vec += vec3; for (unsigned int i = 0; i < vec.getDimension(); i++) ASSERT(vec[i] == copy[i] + vec3[i]); }
template<class Vec> void testOperatorDivide() { Vec vec; for (unsigned int i = 0; i < vec.getDimension(); i++) vec[i] = numbers[i]; Vec vec2; for (unsigned int i = vec.getDimension(); i < vec2.getDimension(); i++) vec2[i] = numbers[i]; // / auto vec3 = vec2 / 2; for (unsigned int i = 0; i < vec3.getDimension(); i++) ASSERT(vec3[i] == vec2[i] / 2); // /= Vec copy { vec2 }; vec2 /= 0.5; for (unsigned int i = 0; i < vec2.getDimension(); i++) ASSERT(vec2[i] == copy[i] / 0.5); }
template<class Vec> void testOperatorAssign() { Vec vec; Vec vec2; for (unsigned int i = 0; i < vec2.getDimension(); i++) vec2[i] = numbers[i]; vec = vec2; for (unsigned int i = 0; i < vec.getDimension(); i++) ASSERT_EQ(vec[i], vec2[i]); }
template<class Vec> void testNormalize() { Vec vec; for (unsigned int i = 0; i < vec.getDimension(); i++) vec[i] = numbers[i]; // getNormalized auto temp = vec.getNormalized(); ASSERT(dbgl::isSimilar(temp.getLength(), 1.0f)); // normalize vec.normalize(); ASSERT(dbgl::isSimilar(vec.getLength(), 1.0f)); }
template<class Vec> void testCross() { Vec vec; for (unsigned int i = 0; i < vec.getDimension(); i++) vec[i] = numbers[i]; Vec vec2; for (unsigned int i = vec.getDimension(); i < vec2.getDimension(); i++) vec2[i] = numbers[i]; auto temp = vec.cross(vec2); ASSERT_EQ(temp.getDimension(), vec.getDimension()); for (unsigned int i = 0; i < temp.getDimension(); i++) { unsigned int i1 = i + 1; unsigned int i2 = i + 2; if (i2 >= temp.getDimension()) i2 -= temp.getDimension(); if (i1 >= temp.getDimension()) i1 -= temp.getDimension(); ASSERT(dbgl::isSimilar(temp[i], vec[i1] * vec2[i2] - vec[i2] * vec2[i1])); } }
template<class Vec> void testOperatorSubtract() { Vec vec; for (unsigned int i = 0; i < vec.getDimension(); i++) vec[i] = numbers[i]; Vec vec2; for (unsigned int i = vec.getDimension(); i < vec2.getDimension(); i++) vec2[i] = numbers[i]; // - auto vec3 = vec - vec2; for (unsigned int i = 0; i < vec3.getDimension(); i++) ASSERT(vec3[i] == vec[i] - vec2[i]); // -= Vec copy { vec }; vec -= vec3; for (unsigned int i = 0; i < vec.getDimension(); i++) ASSERT(vec[i] == copy[i] - vec3[i]); // -(unary) copy = vec; vec = -vec; for (unsigned int i = 0; i < vec.getDimension(); i++) ASSERT(vec[i] == -copy[i]); }