From: Mikko Rasa Date: Wed, 15 May 2013 07:28:00 +0000 (+0300) Subject: Some basic test cases X-Git-Url: http://git.tdb.fi/?p=libs%2Fmath.git;a=commitdiff_plain;h=eb02e224876676a2d78f7305456a7d6f67d3dee2 Some basic test cases --- diff --git a/.gitignore b/.gitignore index d66094e..8354f32 100644 --- a/.gitignore +++ b/.gitignore @@ -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 index 0000000..e394192 --- /dev/null +++ b/tests/Build @@ -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 index 0000000..860bb4e --- /dev/null +++ b/tests/box.cpp @@ -0,0 +1,34 @@ +#include +#include +#include + +using namespace Msp; + +class HyperBoxTests: public Test::RegisteredTest +{ +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 box(2, 4, 3); + Geometry::Ray ray(LinAl::Vector3(10, 0, 0), LinAl::Vector3(-1, 0, 0)); + EXPECT(box.check_intersection(ray)); + ray = Geometry::Ray(LinAl::Vector3(10, 0, 0), LinAl::Vector3(1, 0, 0)); + EXPECT(!box.check_intersection(ray)); + ray = Geometry::Ray(LinAl::Vector3(9, 0, 11.45), LinAl::Vector3(-1, 0, -1)); + EXPECT(box.check_intersection(ray)); + ray = Geometry::Ray(LinAl::Vector3(9, 0, 11.55), LinAl::Vector3(-1, 0, -1)); + EXPECT(!box.check_intersection(ray)); +} diff --git a/tests/matrix.cpp b/tests/matrix.cpp new file mode 100644 index 0000000..10e3ffd --- /dev/null +++ b/tests/matrix.cpp @@ -0,0 +1,62 @@ +#include +#include +#include + +using namespace Msp; + +class MatrixTests: public Test::RegisteredTest +{ +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 a(data); + LinAl::Matrix b(data+6); + EXPECT(a*b == (LinAl::Matrix(data+12))); + EXPECT(b*a == (LinAl::Matrix(data+21))); +} + +template +bool is_identity(const LinAl::Matrix &m) +{ + for(unsigned i=0; i1e-10) + return false; + return true; +} + +void MatrixTests::invert() +{ + static double data[] = + { + 1, 4, 2, 4, 8, 6, 2, 2, 4 + }; + + LinAl::SquareMatrix m(data); + LinAl::SquareMatrix i = LinAl::invert(m); + EXPECT(is_identity(i*m)); +} diff --git a/tests/sphere.cpp b/tests/sphere.cpp new file mode 100644 index 0000000..7d9a4bb --- /dev/null +++ b/tests/sphere.cpp @@ -0,0 +1,34 @@ +#include +#include +#include + +using namespace Msp; + +class HyperSphereTests: public Test::RegisteredTest +{ +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 circle(1.5); + Geometry::Ray ray(LinAl::Vector2(2.5, 0), LinAl::Vector2(-1, 0)); + EXPECT(circle.check_intersection(ray)); + ray = Geometry::Ray(LinAl::Vector2(2.5, 0), LinAl::Vector2(1, 0)); + EXPECT(!circle.check_intersection(ray)); + ray = Geometry::Ray(LinAl::Vector2(2.5, 0), LinAl::Vector2(-4, 2.95)); + EXPECT(circle.check_intersection(ray)); + ray = Geometry::Ray(LinAl::Vector2(2.5, 0), LinAl::Vector2(-4, 3.05)); + EXPECT(!circle.check_intersection(ray)); +} diff --git a/tests/transformedshape.cpp b/tests/transformedshape.cpp new file mode 100644 index 0000000..5468a73 --- /dev/null +++ b/tests/transformedshape.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +using namespace Msp; + +class TransformedShapeTests: public Test::RegisteredTest +{ +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 shape(Geometry::Rectangle(2, 1), Geometry::AffineTransformation::rotation(Geometry::Angle::from_degrees(45))); + Geometry::Ray ray(LinAl::Vector2(3, 1.05), LinAl::Vector2(-1, 0)); + EXPECT(shape.check_intersection(ray)); + ray = Geometry::Ray(LinAl::Vector2(2.65, 3.35), LinAl::Vector2(-1, -1)); + EXPECT(shape.check_intersection(ray)); + ray = Geometry::Ray(LinAl::Vector2(2.6, 3.4), LinAl::Vector2(-1, -1)); + EXPECT(!shape.check_intersection(ray)); +}