works perfectly, fully tested

This commit is contained in:
Marc Wäckerlin
2016-08-23 13:09:14 +00:00
parent 2f35c1e3a0
commit 740b210135
3 changed files with 329 additions and 49 deletions

View File

@@ -19,6 +19,10 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
void initFromArray1() {
const Matrix<T,2,4> m {1, 2, 3, 4,
5, 6, 7, 8};
Matrix<T,2,4> m2;
m2 = {1, 2, 3, 4,
5, 6, 7, 8};
CPPUNIT_ASSERT_EQUAL(m, m2);
CPPUNIT_ASSERT_EQUAL((T)1, m[0][0]);
CPPUNIT_ASSERT_EQUAL((T)2, m[0][1]);
CPPUNIT_ASSERT_EQUAL((T)3, m[0][2]);
@@ -62,6 +66,9 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
Matrix<T,2,4> m2(m1);
m1[0][2] = 16;
m2[0][0] = 0;
Matrix<T,2,4> m3;
m3 = m2;
CPPUNIT_ASSERT_EQUAL(m2, m3);
CPPUNIT_ASSERT_EQUAL((T)1, m1[0][0]);
CPPUNIT_ASSERT_EQUAL((T)2, m1[0][1]);
CPPUNIT_ASSERT_EQUAL((T)16, m1[0][2]);
@@ -133,6 +140,7 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
const Matrix<T,2,4> res(-1, -2, -3, -4,
4, 3, 2, 1);
CPPUNIT_ASSERT_EQUAL(res, m);
CPPUNIT_ASSERT_EQUAL(res, -m2+m1);
}
template<typename T>
void scalar_mult() {
@@ -148,6 +156,15 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
CPPUNIT_ASSERT_EQUAL(two*m1*two, m1*four);
CPPUNIT_ASSERT_EQUAL(big*m1*four, m1*four*big);
}
template<typename T>
void scalar_div() {
const Matrix<T,2,4> m1(2, 4, 6, 8,
10, 12, 14, 16);
const Matrix<T,2,4> m2(1, 2, 3, 4,
5, 6, 7, 8);
CPPUNIT_ASSERT_EQUAL(m2, m1/(T)2);
CPPUNIT_ASSERT_EQUAL(m2, (T)3*m1/(T)6);
}
template<typename T>
void matrix_mult() {
const Matrix<T,2,3> m1(1, 2, 3,
@@ -169,6 +186,14 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
3, 6);
CPPUNIT_ASSERT_EQUAL(res, m.t());
}
template<typename T>
void apply() {
Matrix<T,3,3> m(2, -2, 4,
-2, 1, -6,
1, 0, -2);
Matrix<T,3,3> o(m);
CPPUNIT_ASSERT_EQUAL((T)3*o, m.apply([](T& t){t*=3;}));
}
template<typename T>
void gauss() {
Matrix<T,3,3> m(2, -2, 4,
@@ -211,8 +236,12 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
const Matrix<T,3,3> res(0, 1, 2,
-1, 2, 4,
-1, 2, 5);
Matrix<T,3,3> o1(m), o2(m);
m.inv();
CPPUNIT_ASSERT(m.similar(res, 0.0001));
CPPUNIT_ASSERT_EQUAL((T)2*res, (T)2/o1);
CPPUNIT_ASSERT_EQUAL(o1.i(), o1/o2);
CPPUNIT_ASSERT_EQUAL(res, m);
} {
Matrix<T,3,3> m(1, 2, 3,
0, 1, 4,
@@ -229,7 +258,34 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
const Matrix<T,3,3> res((T)12/11, (T)-6/11, (T)-1/11,
(T)5/22, (T)3/22, (T)-5/22,
(T)-2/11, (T)1/11, (T)2/11);
Matrix<T,3,3> o1(m), o2(m);
m.inv();
CPPUNIT_ASSERT_EQUAL((T)2*res, (T)2/o1);
CPPUNIT_ASSERT_EQUAL(o1.i(), o1/o2);
CPPUNIT_ASSERT_EQUAL(res, m);
} {
Matrix<T,3,3> m(-2, 0, 1,
9, 2, -3,
5, 1, -2);
const Matrix<T,3,3> res(-1, 1, -2,
3, -1, 3,
-1, 2, -4);
Matrix<T,3,3> o(m);
m.inv();
CPPUNIT_ASSERT_EQUAL(m.i(), m*o);
CPPUNIT_ASSERT_EQUAL(res, m);
} {
Matrix<T,4,4> m(2, 1, 4, 1,
-1, 1, 0, 2,
0, 0, 2, 4,
2, -2, 0, 1);
const Matrix<T,4,4> res((T)-1/3, (T)13/15, (T)-2/3, 0.6,
(T)1/3, (T)16/15, (T)-2/3, 0.2,
0, -0.8, 0.5, -0.4,
0, 0.4, 0, 0.2);
Matrix<T,4,4> o(m);
m.inv();
CPPUNIT_ASSERT_EQUAL(m.i(), m*o);
CPPUNIT_ASSERT_EQUAL(res, m);
} {
Matrix<T,2,2> m(4, 3,
@@ -240,6 +296,19 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
CPPUNIT_ASSERT_EQUAL(res, m);
}
}
template<typename T>
void stream() {
const Matrix<T,3,4> m1(1, 2, 3, 4,
5, 6, 7, 8,
1, 4, 2, 8);
Matrix<T,3,4> m2;
std::string res("[3x4]{1,2,3,4,5,6,7,8,1,4,2,8}");
std::stringstream ss;
ss<<m1;
CPPUNIT_ASSERT_EQUAL(res, ss.str());
ss>>m2;
CPPUNIT_ASSERT_EQUAL(m1, m2);
}
CPPUNIT_TEST_SUITE(TemplateMatrixTest);
CPPUNIT_TEST(initFromArray1<int>);
CPPUNIT_TEST(initFromArray1<long>);
@@ -295,6 +364,12 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
CPPUNIT_TEST(scalar_mult<unsigned long>);
CPPUNIT_TEST(scalar_mult<float>);
CPPUNIT_TEST(scalar_mult<double>);
CPPUNIT_TEST(scalar_div<int>);
CPPUNIT_TEST(scalar_div<long>);
CPPUNIT_TEST(scalar_div<unsigned>);
CPPUNIT_TEST(scalar_div<unsigned long>);
CPPUNIT_TEST(scalar_div<float>);
CPPUNIT_TEST(scalar_div<double>);
CPPUNIT_TEST(matrix_mult<int>);
CPPUNIT_TEST(matrix_mult<long>);
CPPUNIT_TEST(matrix_mult<unsigned>);
@@ -307,6 +382,12 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
CPPUNIT_TEST(transpose<unsigned long>);
CPPUNIT_TEST(transpose<float>);
CPPUNIT_TEST(transpose<double>);
CPPUNIT_TEST(apply<int>);
CPPUNIT_TEST(apply<long>);
CPPUNIT_TEST(apply<unsigned>);
CPPUNIT_TEST(apply<unsigned long>);
CPPUNIT_TEST(apply<float>);
CPPUNIT_TEST(apply<double>);
CPPUNIT_TEST(gauss<int>);
CPPUNIT_TEST(gauss<long>);
CPPUNIT_TEST(gauss<float>);
@@ -323,6 +404,13 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
CPPUNIT_TEST(i<double>);
CPPUNIT_TEST(inv<float>);
CPPUNIT_TEST(inv<double>);
CPPUNIT_TEST(inv<long double>);
CPPUNIT_TEST(stream<int>);
CPPUNIT_TEST(stream<long>);
CPPUNIT_TEST(stream<unsigned>);
CPPUNIT_TEST(stream<unsigned long>);
CPPUNIT_TEST(stream<float>);
CPPUNIT_TEST(stream<double>);
CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION(TemplateMatrixTest);
@@ -334,6 +422,11 @@ class VariableMatrixTest: public CppUnit::TestFixture {
Matrix<T> m(2,4,
1, 2, 3, 4,
5, 6, 7, 8);
Matrix<T> m2(2, 4);
m2 = {2, 4,
1, 2, 3, 4,
5, 6, 7, 8};
CPPUNIT_ASSERT_EQUAL(m, m2);
CPPUNIT_ASSERT_EQUAL((T)1, m[0][0]);
CPPUNIT_ASSERT_EQUAL((T)2, m[0][1]);
CPPUNIT_ASSERT_EQUAL((T)3, m[0][2]);
@@ -345,9 +438,9 @@ class VariableMatrixTest: public CppUnit::TestFixture {
}
template<typename T>
void initFromArray2() {
Matrix<T> m(2, 4,
1, 2, 3, 4,
5, 6, 7, 8);
Matrix<T> m(2, 4,
1, 2, 3, 4,
5, 6, 7, 8);
CPPUNIT_ASSERT_EQUAL((T)1, m[0][0]);
CPPUNIT_ASSERT_EQUAL((T)2, m[0][1]);
CPPUNIT_ASSERT_EQUAL((T)3, m[0][2]);
@@ -366,6 +459,9 @@ class VariableMatrixTest: public CppUnit::TestFixture {
Matrix<T> m2(m1);
m1[0][2] = 16;
m2[0][0] = 0;
Matrix<T> m3(2, 4);
m3 = m2;
CPPUNIT_ASSERT_EQUAL(m2, m3);
CPPUNIT_ASSERT_EQUAL((T)1, m1[0][0]);
CPPUNIT_ASSERT_EQUAL((T)2, m1[0][1]);
CPPUNIT_ASSERT_EQUAL((T)16, m1[0][2]);
@@ -445,6 +541,7 @@ class VariableMatrixTest: public CppUnit::TestFixture {
-1, -2, -3, -4,
4, 3, 2, 1);
CPPUNIT_ASSERT_EQUAL(res, m);
CPPUNIT_ASSERT_EQUAL(res, -m2+m1);
}
template<typename T>
void scalar_mult() {
@@ -461,6 +558,17 @@ class VariableMatrixTest: public CppUnit::TestFixture {
CPPUNIT_ASSERT_EQUAL(two*m1*two, m1*four);
CPPUNIT_ASSERT_EQUAL(big*m1*four, m1*four*big);
}
template<typename T>
void scalar_div() {
const Matrix<T> m1(2, 4,
2, 4, 6, 8,
10, 12, 14, 16);
const Matrix<T> m2(2, 4,
1, 2, 3, 4,
5, 6, 7, 8);
CPPUNIT_ASSERT_EQUAL(m2, m1/(T)2);
CPPUNIT_ASSERT_EQUAL(m2, (T)3*m1/(T)6);
}
template<typename T>
void matrix_mult() {
const Matrix<T> m1(2, 3,
@@ -487,6 +595,15 @@ class VariableMatrixTest: public CppUnit::TestFixture {
3, 6);
CPPUNIT_ASSERT_EQUAL(res, m.t());
}
template<typename T>
void apply() {
Matrix<T> m(3, 3,
2, -2, 4,
-2, 1, -6,
1, 0, -2);
Matrix<T> o(m);
CPPUNIT_ASSERT_EQUAL((T)3*o, m.apply([](T& t){t*=3;}));
}
template<typename T>
void gauss() {
Matrix<T> m(3, 3,
@@ -537,8 +654,11 @@ class VariableMatrixTest: public CppUnit::TestFixture {
0, 1, 2,
-1, 2, 4,
-1, 2, 5);
Matrix<T> o1(m), o2(m);
m.inv();
CPPUNIT_ASSERT(m.similar(res, 0.0001));
CPPUNIT_ASSERT_EQUAL((T)2*res, (T)2/o1);
CPPUNIT_ASSERT_EQUAL(o1.i(), o1/o2);
CPPUNIT_ASSERT_EQUAL(res, m);
} {
Matrix<T> m(3, 3,
1, 2, 3,
@@ -550,6 +670,54 @@ class VariableMatrixTest: public CppUnit::TestFixture {
-5, 4, 1);
m.inv();
CPPUNIT_ASSERT_EQUAL(res, m);
} {
Matrix<T> m(3, 3,
1, 2, 3,
0, 4, 5,
1, 0, 6);
const Matrix<T> res(3, 3,
(T)12/11, (T)-6/11, (T)-1/11,
(T)5/22, (T)3/22, (T)-5/22,
(T)-2/11, (T)1/11, (T)2/11);
m.inv();
CPPUNIT_ASSERT_EQUAL(res, m);
} {
Matrix<T> m(3, 3,
-2, 0, 1,
9, 2, -3,
5, 1, -2);
const Matrix<T> res(3, 3,
-1, 1, -2,
3, -1, 3,
-1, 2, -4);
Matrix<T> o(m);
m.inv();
CPPUNIT_ASSERT_EQUAL(m.i(), m*o);
CPPUNIT_ASSERT_EQUAL(res, m);
} {
Matrix<T> m(4, 4,
2, 1, 4, 1,
-1, 1, 0, 2,
0, 0, 2, 4,
2, -2, 0, 1);
const Matrix<T> res(4, 4,
(T)-1/3, (T)13/15, (T)-2/3, 0.6,
(T)1/3, (T)16/15, (T)-2/3, 0.2,
0, -0.8, 0.5, -0.4,
0, 0.4, 0, 0.2);
Matrix<T> o(m);
m.inv();
CPPUNIT_ASSERT_EQUAL(m.i(), m*o);
CPPUNIT_ASSERT_EQUAL(res, m);
} {
Matrix<T> m(2, 2,
4, 3,
3, 2);
const Matrix<T> res(2, 2,
-2, 3,
3, -4);
m.inv();
CPPUNIT_ASSERT_EQUAL(res, m);
}
}
CPPUNIT_TEST_SUITE(VariableMatrixTest);
@@ -602,11 +770,12 @@ class VariableMatrixTest: public CppUnit::TestFixture {
CPPUNIT_TEST(scalar_mult<float>);
CPPUNIT_TEST(scalar_mult<double>);
CPPUNIT_TEST(scalar_mult<int>);
CPPUNIT_TEST(scalar_mult<long>);
CPPUNIT_TEST(scalar_mult<unsigned>);
CPPUNIT_TEST(scalar_mult<unsigned long>);
CPPUNIT_TEST(scalar_mult<float>);
CPPUNIT_TEST(scalar_mult<double>);
CPPUNIT_TEST(scalar_div<int>);
CPPUNIT_TEST(scalar_div<long>);
CPPUNIT_TEST(scalar_div<unsigned>);
CPPUNIT_TEST(scalar_div<unsigned long>);
CPPUNIT_TEST(scalar_div<float>);
CPPUNIT_TEST(scalar_div<double>);
CPPUNIT_TEST(matrix_mult<int>);
CPPUNIT_TEST(matrix_mult<long>);
CPPUNIT_TEST(matrix_mult<unsigned>);
@@ -619,6 +788,12 @@ class VariableMatrixTest: public CppUnit::TestFixture {
CPPUNIT_TEST(transpose<unsigned long>);
CPPUNIT_TEST(transpose<float>);
CPPUNIT_TEST(transpose<double>);
CPPUNIT_TEST(apply<int>);
CPPUNIT_TEST(apply<long>);
CPPUNIT_TEST(apply<unsigned>);
CPPUNIT_TEST(apply<unsigned long>);
CPPUNIT_TEST(apply<float>);
CPPUNIT_TEST(apply<double>);
CPPUNIT_TEST(gauss<int>);
CPPUNIT_TEST(gauss<long>);
CPPUNIT_TEST(gauss<float>);
@@ -635,6 +810,7 @@ class VariableMatrixTest: public CppUnit::TestFixture {
CPPUNIT_TEST(i<double>);
CPPUNIT_TEST(inv<float>);
CPPUNIT_TEST(inv<double>);
CPPUNIT_TEST(inv<long double>);
CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION(VariableMatrixTest);