Browse Source

Math unit tests

Panagiotis Christopoulos Charitos 15 years ago
parent
commit
bfa96f0d4b
4 changed files with 117 additions and 21 deletions
  1. 3 0
      src/Math/Vec4.h
  2. 34 10
      src/Math/Vec4.inl.h
  3. 1 1
      unit-tests/Main.cpp
  4. 79 10
      unit-tests/Math/Math.cpp

+ 3 - 0
src/Math/Vec4.h

@@ -36,6 +36,9 @@ class Vec4
 		explicit Vec4(float arr[]);
 		explicit Vec4(float arr[]);
 		explicit Vec4(float x, float y, float z, float w);
 		explicit Vec4(float x, float y, float z, float w);
 		explicit Vec4(const Vec2& v2, float z, float w);
 		explicit Vec4(const Vec2& v2, float z, float w);
+		#if defined(MATH_INTEL_SIMD)
+			explicit Vec4(const __m128& mm);
+		#endif
 		explicit Vec4(const Vec3& v3, float w);
 		explicit Vec4(const Vec3& v3, float w);
 		         Vec4(const Vec4& b);
 		         Vec4(const Vec4& b);
 		explicit Vec4(const Quat& q);
 		explicit Vec4(const Quat& q);

+ 34 - 10
src/Math/Vec4.inl.h

@@ -104,6 +104,14 @@ inline Vec4::Vec4(float x_, float y_, float z_, float w_)
 	#endif
 	#endif
 }
 }
 
 
+// constructor [__m128]
+#if defined(MATH_INTEL_SIMD)
+	inline Vec4::Vec4(const __m128& mm_)
+	{
+		mm = mm_;
+	}
+#endif
+
 // constructor [vec2, float, float]
 // constructor [vec2, float, float]
 inline Vec4::Vec4(const Vec2& v2, float z_, float w_)
 inline Vec4::Vec4(const Vec2& v2, float z_, float w_)
 {
 {
@@ -147,32 +155,48 @@ inline Vec4::Vec4(const Quat& q)
 // +
 // +
 inline Vec4 Vec4::operator+(const Vec4& b) const
 inline Vec4 Vec4::operator+(const Vec4& b) const
 {
 {
-	return Vec4(x() + b.x(), y() + b.y(), z() + b.z(), w() + b.w());
+	#if defined(MATH_INTEL_SIMD)
+		return Vec4(_mm_add_ps(mm, b.mm));
+	#else
+		return Vec4(x() + b.x(), y() + b.y(), z() + b.z(), w() + b.w());
+	#endif
 }
 }
 
 
 // +=
 // +=
 inline Vec4& Vec4::operator+=(const Vec4& b)
 inline Vec4& Vec4::operator+=(const Vec4& b)
 {
 {
-	x() += b.x();
-	y() += b.y();
-	z() += b.z();
-	w() += b.w();
+	#if defined(MATH_INTEL_SIMD)
+		mm = _mm_add_ps(mm, b.mm);
+	#else
+		x() += b.x();
+		y() += b.y();
+		z() += b.z();
+		w() += b.w();
+	#endif
 	return SELF;
 	return SELF;
 }
 }
 
 
 // -
 // -
 inline Vec4 Vec4::operator-(const Vec4& b) const
 inline Vec4 Vec4::operator-(const Vec4& b) const
 {
 {
-	return Vec4(x() - b.x(), y() - b.y(), z() - b.z(), w() - b.w());
+	#if defined(MATH_INTEL_SIMD)
+		return Vec4(_mm_sub_ps(mm, b.mm));
+	#else
+		return Vec4(x() - b.x(), y() - b.y(), z() - b.z(), w() - b.w());
+	#endif
 }
 }
 
 
 // -=
 // -=
 inline Vec4& Vec4::operator-=(const Vec4& b)
 inline Vec4& Vec4::operator-=(const Vec4& b)
 {
 {
-	x() -= b.x();
-	y() -= b.y();
-	z() -= b.z();
-	w() -= b.w();
+	#if defined(MATH_INTEL_SIMD)
+		mm = _mm_sub_ps(mm, b.mm);
+	#else
+		x() -= b.x();
+		y() -= b.y();
+		z() -= b.z();
+		w() -= b.w();
+	#endif
 	return SELF;
 	return SELF;
 }
 }
 
 

+ 1 - 1
unit-tests/Main.cpp

@@ -7,7 +7,7 @@
 int main(int argc, char** argv)
 int main(int argc, char** argv)
 {
 {
 	// Init app
 	// Init app
-	AppSingleton::getInstance().init(0, NULL);
+	//AppSingleton::getInstance().init(0, NULL);
 
 
 	// Tests
 	// Tests
 	::testing::InitGoogleTest(&argc, argv);
 	::testing::InitGoogleTest(&argc, argv);

+ 79 - 10
unit-tests/Math/Math.cpp

@@ -1,24 +1,93 @@
 #include <gtest/gtest.h>
 #include <gtest/gtest.h>
 #include <iostream>
 #include <iostream>
 #include "Math.h"
 #include "Math.h"
+#include "Util.h"
 
 
 
 
-TEST(MathTests, Init)
+const float RANGE_MIN = -1.00123;
+const float RANGE_MAX = 9.9990001;
+
+
+static float randFloat()
+{
+	return Util::randRange(RANGE_MIN, RANGE_MAX);
+}
+
+
+TEST(MathTests, Alignment)
+{
+	const int FS = sizeof(float); // float size
+
+	EXPECT_EQ(sizeof(Vec2), FS * 2);
+	EXPECT_EQ(sizeof(Vec3), FS * 3);
+	EXPECT_EQ(sizeof(Vec4), FS * 4);
+	EXPECT_EQ(sizeof(Quat), FS * 4);
+	EXPECT_EQ(sizeof(Euler), FS * 3);
+	EXPECT_EQ(sizeof(Mat3), FS * 9);
+	EXPECT_EQ(sizeof(Mat4), FS * 16);
+}
+
+
+TEST(MathTests, Constructors)
+{
+	{
+		EXPECT_EQ(Vec4().x(), 0.0);
+		EXPECT_EQ(Vec4().y(), 0.0);
+		EXPECT_EQ(Vec4().z(), 0.0);
+		EXPECT_EQ(Vec4().w(), 0.0);
+
+		float f = randFloat();
+		Vec4 a(f);
+		EXPECT_EQ(a.x(), f);
+		EXPECT_EQ(a.y(), f);
+		EXPECT_EQ(a.z(), f);
+		EXPECT_EQ(a.w(), f);
+
+		float arr[4] = {randFloat(), randFloat(), randFloat(), randFloat()};
+		a = Vec4(arr);
+		EXPECT_EQ(a[0], arr[0]);
+		EXPECT_EQ(a.y(), arr[1]);
+		EXPECT_EQ(a.z(), arr[2]);
+		EXPECT_EQ(a[3], arr[3]);
+	}
+
+
+}
+
+
+template<typename Type, int size, Type (Type::* op)(const Type&) const>
+void testOperator()
 {
 {
-	Vec4 a, b;
-	EXPECT_EQ(a, Vec4(0.0));
-	EXPECT_EQ(b, Vec4(0.0));
+	Type a, b, c;
+
+	for(int i = 0; i < size; i++)
+	{
+		a[i] = randFloat();
+		b[i] = randFloat();
+		//c[i] = a[i] + b[i];
+	}
+
+	EXPECT_EQ((a.*op)(b), c);
+
+	/*a += b;
+
+	EXPECT_EQ(a, c);*/
 }
 }
 
 
 
 
 TEST(MathTests, Addition)
 TEST(MathTests, Addition)
 {
 {
-	float arr0[] = {1.0001, -2.0, 0.00000012300123, 400.0};
-	float arr1[] = {1.0, 2.0, -3.0, 4.0};
+	testOperator<Vec4, 4, &Vec4::operator+ >();
+}
+
 
 
-	Vec4 a(arr0), b(arr1);
-	EXPECT_EQ(a + b, Vec4(arr0[0] + arr1[0], arr0[1] + arr1[1], arr0[2] + arr1[2], arr0[3] + arr1[3]));
+TEST(MathTests, Substraction)
+{
+	Vec4 a(randFloat(), randFloat(), randFloat(), randFloat());
+	Vec4 b(randFloat(), randFloat(), randFloat(), randFloat());
+	EXPECT_EQ(a - b, Vec4(a.x() - b.x(), a.y() - b.y(), a.z() - b.z(), a.w() - b.w()));
 
 
-	//a += b;
-	//EXPECT_EQ(a, Vec4(arr0[0] + arr0[0], arr0[1] + arr0[1], arr0[2] + arr0[2], arr0[3] + arr0[3]));
+	Vec4 c = a;
+	a -= b;
+	EXPECT_EQ(a, Vec4(c.x() - b.x(), c.y() - b.y(), c.z() - b.z(), c.w() - b.w()));
 }
 }