]> git.tdb.fi Git - libs/math.git/commitdiff
Some basic test cases
authorMikko Rasa <tdb@tdb.fi>
Wed, 15 May 2013 07:28:00 +0000 (10:28 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 15 May 2013 07:28:00 +0000 (10:28 +0300)
.gitignore
tests/Build [new file with mode: 0644]
tests/box.cpp [new file with mode: 0644]
tests/matrix.cpp [new file with mode: 0644]
tests/sphere.cpp [new file with mode: 0644]
tests/transformedshape.cpp [new file with mode: 0644]

index d66094ef0633b898ea3b8e68632b2586757dd686..8354f320ca11844fbb4c4849a5d385fee750164d 100644 (file)
@@ -3,3 +3,4 @@ temp
 /libmspmath.a
 /libmspmath.so
 /mspmath.pc
+/tests/test
diff --git a/tests/Build b/tests/Build
new file mode 100644 (file)
index 0000000..e394192
--- /dev/null
@@ -0,0 +1,11 @@
+package "mspmath-tests"
+{
+       require "mspcore";
+       require "msptest";
+       require "mspmath";
+
+       program "test"
+       {
+               source ".";
+       };
+};
diff --git a/tests/box.cpp b/tests/box.cpp
new file mode 100644 (file)
index 0000000..860bb4e
--- /dev/null
@@ -0,0 +1,34 @@
+#include <msp/geometry/box.h>
+#include <msp/linal/vector3.h>
+#include <msp/test/test.h>
+
+using namespace Msp;
+
+class HyperBoxTests: public Test::RegisteredTest<HyperBoxTests>
+{
+public:
+       HyperBoxTests();
+
+       static const char *get_name() { return "HyperBox"; }
+
+private:
+       void ray_intersection();
+};
+
+HyperBoxTests::HyperBoxTests()
+{
+       add(&HyperBoxTests::ray_intersection, "Ray intersection");
+}
+
+void HyperBoxTests::ray_intersection()
+{
+       Geometry::Box<double> box(2, 4, 3);
+       Geometry::Ray<double, 3> ray(LinAl::Vector3<double>(10, 0, 0), LinAl::Vector3<double>(-1, 0, 0));
+       EXPECT(box.check_intersection(ray));
+       ray = Geometry::Ray<double, 3>(LinAl::Vector3<double>(10, 0, 0), LinAl::Vector3<double>(1, 0, 0));
+       EXPECT(!box.check_intersection(ray));
+       ray = Geometry::Ray<double, 3>(LinAl::Vector3<double>(9, 0, 11.45), LinAl::Vector3<double>(-1, 0, -1));
+       EXPECT(box.check_intersection(ray));
+       ray = Geometry::Ray<double, 3>(LinAl::Vector3<double>(9, 0, 11.55), LinAl::Vector3<double>(-1, 0, -1));
+       EXPECT(!box.check_intersection(ray));
+}
diff --git a/tests/matrix.cpp b/tests/matrix.cpp
new file mode 100644 (file)
index 0000000..10e3ffd
--- /dev/null
@@ -0,0 +1,62 @@
+#include <msp/linal/matrix.h>
+#include <msp/linal/squarematrix.h>
+#include <msp/test/test.h>
+
+using namespace Msp;
+
+class MatrixTests: public Test::RegisteredTest<MatrixTests>
+{
+public:
+       MatrixTests();
+
+       static const char *get_name() { return "Matrix"; }
+
+private:
+       void multiply();
+       void invert();
+};
+
+
+MatrixTests::MatrixTests()
+{
+       add(&MatrixTests::multiply, "Multiplication");
+       add(&MatrixTests::invert,   "Inversion");
+}
+
+void MatrixTests::multiply()
+{
+       static double data[] =
+       {
+               1, 2, 1, 2, 1, 2,
+               3, 0, 3, 0, 0, 3,
+               3, 6, 3, 3, 6, 3, 6, 3, 6,
+               9, 3, 9, 6
+       };
+
+       LinAl::Matrix<double, 3, 2> a(data);
+       LinAl::Matrix<double, 2, 3> b(data+6);
+       EXPECT(a*b == (LinAl::Matrix<double, 3, 3>(data+12)));
+       EXPECT(b*a == (LinAl::Matrix<double, 2, 2>(data+21)));
+}
+
+template<typename T, unsigned N>
+bool is_identity(const LinAl::Matrix<T, N, N> &m)
+{
+       for(unsigned i=0; i<N; ++i)
+               for(unsigned j=0; j<N; ++j)
+                       if(abs(T(i==j)-m(i, j))>1e-10)
+                               return false;
+       return true;
+}
+
+void MatrixTests::invert()
+{
+       static double data[] =
+       {
+               1, 4, 2, 4, 8, 6, 2, 2, 4
+       };
+
+       LinAl::SquareMatrix<double, 3> m(data);
+       LinAl::SquareMatrix<double, 3> i = LinAl::invert(m);
+       EXPECT(is_identity(i*m));
+}
diff --git a/tests/sphere.cpp b/tests/sphere.cpp
new file mode 100644 (file)
index 0000000..7d9a4bb
--- /dev/null
@@ -0,0 +1,34 @@
+#include <msp/geometry/circle.h>
+#include <msp/linal/vector2.h>
+#include <msp/test/test.h>
+
+using namespace Msp;
+
+class HyperSphereTests: public Test::RegisteredTest<HyperSphereTests>
+{
+public:
+       HyperSphereTests();
+
+       static const char *get_name() { return "HyperSphere"; }
+
+private:
+       void ray_intersection();
+};
+
+HyperSphereTests::HyperSphereTests()
+{
+       add(&HyperSphereTests::ray_intersection, "Ray intersection");
+}
+
+void HyperSphereTests::ray_intersection()
+{
+       Geometry::Circle<double> circle(1.5);
+       Geometry::Ray<double, 2> ray(LinAl::Vector2<double>(2.5, 0), LinAl::Vector2<double>(-1, 0));
+       EXPECT(circle.check_intersection(ray));
+       ray = Geometry::Ray<double, 2>(LinAl::Vector2<double>(2.5, 0), LinAl::Vector2<double>(1, 0));
+       EXPECT(!circle.check_intersection(ray));
+       ray = Geometry::Ray<double, 2>(LinAl::Vector2<double>(2.5, 0), LinAl::Vector2<double>(-4, 2.95));
+       EXPECT(circle.check_intersection(ray));
+       ray = Geometry::Ray<double, 2>(LinAl::Vector2<double>(2.5, 0), LinAl::Vector2<double>(-4, 3.05));
+       EXPECT(!circle.check_intersection(ray));
+}
diff --git a/tests/transformedshape.cpp b/tests/transformedshape.cpp
new file mode 100644 (file)
index 0000000..5468a73
--- /dev/null
@@ -0,0 +1,32 @@
+#include <msp/geometry/rectangle.h>
+#include <msp/geometry/transformedshape.h>
+#include <msp/test/test.h>
+
+using namespace Msp;
+
+class TransformedShapeTests: public Test::RegisteredTest<TransformedShapeTests>
+{
+public:
+       TransformedShapeTests();
+
+       static const char *get_name() { return "TransformedShape"; }
+
+private:
+       void ray_intersection();
+};
+
+TransformedShapeTests::TransformedShapeTests()
+{
+       add(&TransformedShapeTests::ray_intersection, "Ray intersection");
+}
+
+void TransformedShapeTests::ray_intersection()
+{
+       Geometry::TransformedShape<double, 2> shape(Geometry::Rectangle<double>(2, 1), Geometry::AffineTransformation<double, 2>::rotation(Geometry::Angle<double>::from_degrees(45)));
+       Geometry::Ray<double, 2> ray(LinAl::Vector2<double>(3, 1.05), LinAl::Vector2<double>(-1, 0));
+       EXPECT(shape.check_intersection(ray));
+       ray = Geometry::Ray<double, 2>(LinAl::Vector2<double>(2.65, 3.35), LinAl::Vector2<double>(-1, -1));
+       EXPECT(shape.check_intersection(ray));
+       ray = Geometry::Ray<double, 2>(LinAl::Vector2<double>(2.6, 3.4), LinAl::Vector2<double>(-1, -1));
+       EXPECT(!shape.check_intersection(ray));
+}