C++ matrix template classe for mathematics.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
7.7 KiB

/*! @file
@id $Id$
*/
// 1 2 3 4 5 6 7 8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
#include <matrix.hxx>
#include <cppunit/TestFixture.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/XmlOutputter.h>
#include <fstream>
class TemplateMatrixTest: public CppUnit::TestFixture {
public:
template<typename T>
void initFromArray1() {
Matrix<T,2,4> m {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]);
CPPUNIT_ASSERT_EQUAL((T)4, m[0][3]);
CPPUNIT_ASSERT_EQUAL((T)5, m[1][0]);
CPPUNIT_ASSERT_EQUAL((T)6, m[1][1]);
CPPUNIT_ASSERT_EQUAL((T)7, m[1][2]);
CPPUNIT_ASSERT_EQUAL((T)8, m[1][3]);
}
template<typename T>
void initFromArray2() {
Matrix<T,2,4> m(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]);
CPPUNIT_ASSERT_EQUAL((T)4, m[0][3]);
CPPUNIT_ASSERT_EQUAL((T)5, m[1][0]);
CPPUNIT_ASSERT_EQUAL((T)6, m[1][1]);
CPPUNIT_ASSERT_EQUAL((T)7, m[1][2]);
CPPUNIT_ASSERT_EQUAL((T)8, m[1][3]);
}
template<typename T>
void initFromArray3() {
Matrix<T,2,4> m({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]);
CPPUNIT_ASSERT_EQUAL((T)4, m[0][3]);
CPPUNIT_ASSERT_EQUAL((T)5, m[1][0]);
CPPUNIT_ASSERT_EQUAL((T)6, m[1][1]);
CPPUNIT_ASSERT_EQUAL((T)7, m[1][2]);
CPPUNIT_ASSERT_EQUAL((T)8, m[1][3]);
}
template<typename T>
void initFromOtherMatrix() {
Matrix<T,2,4> m1(1, 2, 3, 4,
5, 6, 7, 8);
m1[1][2] = 13;
Matrix<T,2,4> m2(m1);
m1[0][2] = 16;
m2[0][0] = 0;
CPPUNIT_ASSERT_EQUAL((T)1, m1[0][0]);
CPPUNIT_ASSERT_EQUAL((T)2, m1[0][1]);
CPPUNIT_ASSERT_EQUAL((T)16, m1[0][2]);
CPPUNIT_ASSERT_EQUAL((T)4, m1[0][3]);
CPPUNIT_ASSERT_EQUAL((T)5, m1[1][0]);
CPPUNIT_ASSERT_EQUAL((T)6, m1[1][1]);
CPPUNIT_ASSERT_EQUAL((T)13, m1[1][2]);
CPPUNIT_ASSERT_EQUAL((T)8, m1[1][3]);
CPPUNIT_ASSERT_EQUAL((T)0, m2[0][0]);
CPPUNIT_ASSERT_EQUAL((T)2, m2[0][1]);
CPPUNIT_ASSERT_EQUAL((T)3, m2[0][2]);
CPPUNIT_ASSERT_EQUAL((T)4, m2[0][3]);
CPPUNIT_ASSERT_EQUAL((T)5, m2[1][0]);
CPPUNIT_ASSERT_EQUAL((T)6, m2[1][1]);
CPPUNIT_ASSERT_EQUAL((T)13, m2[1][2]);
CPPUNIT_ASSERT_EQUAL((T)8, m2[1][3]);
}
template<typename T>
void initFromDefault() {
Matrix<T,2,2> m;
CPPUNIT_ASSERT_EQUAL((T)0, m[0][0]);
CPPUNIT_ASSERT_EQUAL((T)0, m[0][1]);
CPPUNIT_ASSERT_EQUAL((T)0, m[1][0]);
CPPUNIT_ASSERT_EQUAL((T)0, m[1][1]);
}
CPPUNIT_TEST_SUITE(TemplateMatrixTest);
CPPUNIT_TEST(initFromArray1<int>);
CPPUNIT_TEST(initFromArray1<long>);
CPPUNIT_TEST(initFromArray1<unsigned>);
CPPUNIT_TEST(initFromArray1<unsigned long>);
CPPUNIT_TEST(initFromArray1<float>);
CPPUNIT_TEST(initFromArray1<double>);
CPPUNIT_TEST(initFromArray2<int>);
CPPUNIT_TEST(initFromArray2<long>);
CPPUNIT_TEST(initFromArray2<unsigned>);
CPPUNIT_TEST(initFromArray2<unsigned long>);
CPPUNIT_TEST(initFromArray2<float>);
CPPUNIT_TEST(initFromArray2<double>);
CPPUNIT_TEST(initFromArray3<int>);
CPPUNIT_TEST(initFromArray3<long>);
CPPUNIT_TEST(initFromArray3<unsigned>);
CPPUNIT_TEST(initFromArray3<unsigned long>);
CPPUNIT_TEST(initFromArray3<float>);
CPPUNIT_TEST(initFromArray3<double>);
CPPUNIT_TEST(initFromOtherMatrix<int>);
CPPUNIT_TEST(initFromOtherMatrix<long>);
CPPUNIT_TEST(initFromOtherMatrix<unsigned>);
CPPUNIT_TEST(initFromOtherMatrix<unsigned long>);
CPPUNIT_TEST(initFromOtherMatrix<float>);
CPPUNIT_TEST(initFromOtherMatrix<double>);
CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION(TemplateMatrixTest);
class VariableMatrixTest: public CppUnit::TestFixture {
public:
template<typename T>
void initFromArray1() {
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]);
CPPUNIT_ASSERT_EQUAL((T)4, m[0][3]);
CPPUNIT_ASSERT_EQUAL((T)5, m[1][0]);
CPPUNIT_ASSERT_EQUAL((T)6, m[1][1]);
CPPUNIT_ASSERT_EQUAL((T)7, m[1][2]);
CPPUNIT_ASSERT_EQUAL((T)8, m[1][3]);
}
template<typename T>
void initFromArray2() {
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]);
CPPUNIT_ASSERT_EQUAL((T)4, m[0][3]);
CPPUNIT_ASSERT_EQUAL((T)5, m[1][0]);
CPPUNIT_ASSERT_EQUAL((T)6, m[1][1]);
CPPUNIT_ASSERT_EQUAL((T)7, m[1][2]);
CPPUNIT_ASSERT_EQUAL((T)8, m[1][3]);
}
template<typename T>
void initFromOtherMatrix() {
Matrix<T> m1(2, 4,
1, 2, 3, 4,
5, 6, 7, 8);
m1[1][2] = 13;
Matrix<T> m2(m1);
m1[0][2] = 16;
m2[0][0] = 0;
CPPUNIT_ASSERT_EQUAL((T)1, m1[0][0]);
CPPUNIT_ASSERT_EQUAL((T)2, m1[0][1]);
CPPUNIT_ASSERT_EQUAL((T)16, m1[0][2]);
CPPUNIT_ASSERT_EQUAL((T)4, m1[0][3]);
CPPUNIT_ASSERT_EQUAL((T)5, m1[1][0]);
CPPUNIT_ASSERT_EQUAL((T)6, m1[1][1]);
CPPUNIT_ASSERT_EQUAL((T)13, m1[1][2]);
CPPUNIT_ASSERT_EQUAL((T)8, m1[1][3]);
CPPUNIT_ASSERT_EQUAL((T)0, m2[0][0]);
CPPUNIT_ASSERT_EQUAL((T)2, m2[0][1]);
CPPUNIT_ASSERT_EQUAL((T)3, m2[0][2]);
CPPUNIT_ASSERT_EQUAL((T)4, m2[0][3]);
CPPUNIT_ASSERT_EQUAL((T)5, m2[1][0]);
CPPUNIT_ASSERT_EQUAL((T)6, m2[1][1]);
CPPUNIT_ASSERT_EQUAL((T)13, m2[1][2]);
CPPUNIT_ASSERT_EQUAL((T)8, m2[1][3]);
}
template<typename T>
void initFromDefault() {
Matrix<T> m(2, 4);
CPPUNIT_ASSERT_EQUAL((T)0, m[0][0]);
CPPUNIT_ASSERT_EQUAL((T)0, m[0][1]);
CPPUNIT_ASSERT_EQUAL((T)0, m[1][0]);
CPPUNIT_ASSERT_EQUAL((T)0, m[1][1]);
}
CPPUNIT_TEST_SUITE(VariableMatrixTest);
CPPUNIT_TEST(initFromArray1<int>);
CPPUNIT_TEST(initFromArray1<long>);
CPPUNIT_TEST(initFromArray1<unsigned>);
CPPUNIT_TEST(initFromArray1<unsigned long>);
CPPUNIT_TEST(initFromArray1<float>);
CPPUNIT_TEST(initFromArray1<double>);
CPPUNIT_TEST(initFromArray2<int>);
CPPUNIT_TEST(initFromArray2<long>);
CPPUNIT_TEST(initFromArray2<unsigned>);
CPPUNIT_TEST(initFromArray2<unsigned long>);
CPPUNIT_TEST(initFromArray2<float>);
CPPUNIT_TEST(initFromArray2<double>);
CPPUNIT_TEST(initFromOtherMatrix<int>);
CPPUNIT_TEST(initFromOtherMatrix<long>);
CPPUNIT_TEST(initFromOtherMatrix<unsigned>);
CPPUNIT_TEST(initFromOtherMatrix<unsigned long>);
CPPUNIT_TEST(initFromOtherMatrix<float>);
CPPUNIT_TEST(initFromOtherMatrix<double>);
CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION(VariableMatrixTest);
int main(int argc, char** argv) try {
std::ofstream ofs((*argv+std::string(".xml")).c_str());
CppUnit::TextUi::TestRunner runner;
runner.setOutputter(new CppUnit::XmlOutputter(&runner.result(), ofs));
runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
return runner.run() ? 0 : 1;
} catch (std::exception& e) {
std::cerr<<"***Exception: "<<e.what()<<std::endl;
return 1;
}