/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */ /* Author(s): Anton Deguet Created on: 2003-12-16 (C) Copyright 2003-2013 Johns Hopkins University (JHU), All Rights Reserved. --- begin cisst license - do not edit --- This software is provided "as is" under an open source license, with no warranty. The complete license can be found in license.txt and http://www.cisst.org/cisst/license.txt. --- end cisst license --- */ #include "vctFixedSizeMatrixTest.h" #include "vctGenericContainerTest.h" #include "vctGenericMatrixTest.h" #include #include #include #include #include #include template void vctFixedSizeMatrixTest::TestAssignment(void) { enum {ROWS = 7, COLS = 2}; typedef _elementType value_type; vctFixedSizeMatrix matrix1; vctRandom(matrix1, value_type(-10), value_type(10)); vctFixedSizeMatrix matrix2; vctGenericMatrixTest::TestAssignment(matrix1, matrix2); } void vctFixedSizeMatrixTest::TestAssignmentDouble(void) { TestAssignment(); } void vctFixedSizeMatrixTest::TestAssignmentFloat(void) { TestAssignment(); } void vctFixedSizeMatrixTest::TestAssignmentInt(void) { TestAssignment(); } template void vctFixedSizeMatrixTest::TestZeros(void) { enum {ROWS = 10, COLS = 6}; // even numbers typedef _elementType value_type; /* test on compact matrices for both storage orders */ typedef vctFixedSizeMatrix MatrixType1; typedef vctFixedSizeMatrix MatrixType2; MatrixType1 matrix1; vctGenericMatrixTest::TestZeros(matrix1); MatrixType2 matrix2; vctGenericMatrixTest::TestZeros(matrix2); /* test on matrices compact by rows or columns. */ typedef vctFixedSizeMatrixRef SubMatrixType1; typedef vctFixedSizeMatrixRef SubMatrixType2; SubMatrixType1 subMatrix1(matrix1); vctGenericMatrixTest::TestZeros(subMatrix1); SubMatrixType2 subMatrix2(matrix2); vctGenericMatrixTest::TestZeros(subMatrix2); /* test on last case, i.e. neither row nor column compact */ typedef vctFixedSizeMatrixRef SubsampledMatrixType1; typedef vctFixedSizeMatrixRef SubsampledMatrixType2; SubsampledMatrixType1 subsampledMatrix1(matrix1.Pointer()); vctGenericMatrixTest::TestZeros(subsampledMatrix1); SubsampledMatrixType2 subsampledMatrix2(matrix2.Pointer()); vctGenericMatrixTest::TestZeros(subsampledMatrix2); } void vctFixedSizeMatrixTest::TestZerosDouble(void) { TestZeros(); } void vctFixedSizeMatrixTest::TestZerosFloat(void) { TestZeros(); } void vctFixedSizeMatrixTest::TestZerosInt(void) { TestZeros(); } template void vctFixedSizeMatrixTest::TestAccessMethods(void) { enum {ROWS = 3, COLS = 5}; typedef _elementType value_type; vctFixedSizeMatrix inputMatrix; vctRandom(inputMatrix, value_type(-10), value_type(10)); typename vctFixedSizeMatrix::ConstRefType constInputMatrix(inputMatrix.Pointer()); vctGenericMatrixTest::TestAccessMethods(inputMatrix, constInputMatrix); } void vctFixedSizeMatrixTest::TestAccessMethodsDouble(void) { TestAccessMethods(); } void vctFixedSizeMatrixTest::TestAccessMethodsFloat(void) { TestAccessMethods(); } void vctFixedSizeMatrixTest::TestAccessMethodsInt(void) { TestAccessMethods(); } template void vctFixedSizeMatrixTest::TestExchangeAndPermutation(void) { enum {ROWS = 4, COLS = 5}; typedef _elementType value_type; typedef vctFixedSizeMatrix<_elementType, ROWS, COLS> MatrixType; MatrixType matrix; vctRandom(matrix, value_type(-10), value_type(10)); cmnRandomSequence & randomSequence = cmnRandomSequence::GetInstance(); const unsigned int row1 = randomSequence.ExtractRandomInt(0, ROWS); const unsigned int row2 = randomSequence.ExtractRandomInt(0, ROWS); const unsigned int col1 = randomSequence.ExtractRandomInt(0, COLS); const unsigned int col2 = randomSequence.ExtractRandomInt(0, COLS); vct::index_type rowPermutation[ROWS]; randomSequence.ExtractRandomPermutation(ROWS, rowPermutation); vct::index_type colPermutation[COLS]; randomSequence.ExtractRandomPermutation(COLS, colPermutation); vctGenericMatrixTest::TestExchangeAndPermutationOperations(matrix, row1, row2, col1, col2, rowPermutation, colPermutation); } void vctFixedSizeMatrixTest::TestExchangeAndPermutationDouble(void) { TestExchangeAndPermutation(); } void vctFixedSizeMatrixTest::TestExchangeAndPermutationFloat(void) { TestExchangeAndPermutation(); } void vctFixedSizeMatrixTest::TestExchangeAndPermutationInt(void) { TestExchangeAndPermutation(); } template void vctFixedSizeMatrixTest::TestSelect(void) { enum {INPUT_ROWS = 7, INPUT_COLS = 8}; enum {OUTPUT_ROWS = 4, OUTPUT_COLS = 4}; typedef _elementType value_type; vctFixedSizeMatrix<_elementType, INPUT_ROWS, INPUT_COLS> inputMatrix; vctFixedSizeVector rowIndexes; vctFixedSizeVector colIndexes; vctFixedSizeMatrix<_elementType, OUTPUT_ROWS, INPUT_COLS> selectedRows; vctFixedSizeMatrix<_elementType, INPUT_ROWS, OUTPUT_COLS> selectedCols; vctRandom(inputMatrix, value_type(-10), value_type(10)); vctRandom(rowIndexes, static_cast(0), static_cast(INPUT_ROWS)); vctRandom(colIndexes, static_cast(0), static_cast(INPUT_COLS)); vctGenericMatrixTest::TestSelect(inputMatrix, rowIndexes, colIndexes, selectedRows, selectedCols); } void vctFixedSizeMatrixTest::TestSelectDouble(void) { vctFixedSizeMatrixTest::TestSelect(); } void vctFixedSizeMatrixTest::TestSelectFloat(void) { vctFixedSizeMatrixTest::TestSelect(); } void vctFixedSizeMatrixTest::TestSelectInt(void) { vctFixedSizeMatrixTest::TestSelect(); } template void vctFixedSizeMatrixTest::TestSoMiOperations(void) { enum {ROWS = 2, COLS = 4}; typedef _elementType value_type; vctFixedSizeMatrix matrix; vctFixedSizeMatrix result; vctRandom(matrix, value_type(-10), value_type(10)); vctGenericContainerTest::TestSoCiOperations(matrix, result); } void vctFixedSizeMatrixTest::TestSoMiOperationsDouble(void) { TestSoMiOperations(); } void vctFixedSizeMatrixTest::TestSoMiOperationsFloat(void) { TestSoMiOperations(); } void vctFixedSizeMatrixTest::TestSoMiOperationsInt(void) { TestSoMiOperations(); } template void vctFixedSizeMatrixTest::TestSoMiMiOperations(void) { enum {ROWS = 5, COLS = 9}; typedef _elementType value_type; vctFixedSizeMatrix matrix1; vctFixedSizeMatrix matrix2; vctFixedSizeMatrix matrix3; vctRandom(matrix1, value_type(-10), value_type(10)); vctRandom(matrix2, value_type(-10), value_type(10)); vctGenericContainerTest::TestSoCiCiOperations(matrix1, matrix2, matrix3); } void vctFixedSizeMatrixTest::TestSoMiMiOperationsDouble(void) { TestSoMiMiOperations(); } void vctFixedSizeMatrixTest::TestSoMiMiOperationsFloat(void) { TestSoMiMiOperations(); } void vctFixedSizeMatrixTest::TestSoMiMiOperationsInt(void) { TestSoMiMiOperations(); } template void vctFixedSizeMatrixTest::TestMioMiOperations(void) { enum {ROWS = 8, COLS = 4}; typedef _elementType value_type; vctFixedSizeMatrix matrix1; vctFixedSizeMatrix matrix2; vctFixedSizeMatrix result; vctRandom(matrix1, value_type(-10), value_type(10)); vctRandom(matrix2, value_type(-10), value_type(10)); RemoveQuasiZero(matrix2); vctGenericContainerTest::TestCioCiOperations(matrix1, matrix2, result); } void vctFixedSizeMatrixTest::TestMioMiOperationsDouble(void) { TestMioMiOperations(); } void vctFixedSizeMatrixTest::TestMioMiOperationsFloat(void) { TestMioMiOperations(); } void vctFixedSizeMatrixTest::TestMioMiOperationsInt(void) { TestMioMiOperations(); } template void vctFixedSizeMatrixTest::TestMioSiMiOperations(void) { enum {ROWS = 5, COLS = 6}; typedef _elementType value_type; vctFixedSizeMatrix matrix1; value_type scalar; vctFixedSizeMatrix matrix2; vctFixedSizeMatrix result; vctRandom(matrix1, value_type(-10), value_type(10)); cmnRandomSequence & randomSequence = cmnRandomSequence::GetInstance(); randomSequence.ExtractRandomValue(value_type(-10), value_type(10), scalar); vctRandom(matrix2, value_type(-10), value_type(10)); RemoveQuasiZero(matrix2); vctGenericContainerTest::TestCioSiCiOperations(matrix1, scalar, matrix2, result); } void vctFixedSizeMatrixTest::TestMioSiMiOperationsDouble(void) { TestMioSiMiOperations(); } void vctFixedSizeMatrixTest::TestMioSiMiOperationsFloat(void) { TestMioSiMiOperations(); } void vctFixedSizeMatrixTest::TestMioSiMiOperationsInt(void) { TestMioSiMiOperations(); } template void vctFixedSizeMatrixTest::TestMioMiMiOperations(void) { enum {ROWS = 5, COLS = 6}; typedef _elementType value_type; vctFixedSizeMatrix matrix1; vctFixedSizeMatrix matrix2; vctFixedSizeMatrix matrix3; vctFixedSizeMatrix result; vctRandom(matrix1, value_type(-10), value_type(10)); vctRandom(matrix2, value_type(-10), value_type(10)); vctRandom(matrix3, value_type(-10), value_type(10)); RemoveQuasiZero(matrix3); vctGenericContainerTest::TestCioCiCiOperations(matrix1, matrix2, matrix3, result); } void vctFixedSizeMatrixTest::TestMioMiMiOperationsDouble(void) { TestMioMiMiOperations(); } void vctFixedSizeMatrixTest::TestMioMiMiOperationsFloat(void) { TestMioMiMiOperations(); } void vctFixedSizeMatrixTest::TestMioMiMiOperationsInt(void) { TestMioMiMiOperations(); } template void vctFixedSizeMatrixTest::TestMoMiMiOperations(void) { enum {ROWS = 4, COLS = 5}; typedef _elementType value_type; vctFixedSizeMatrix matrix1; vctFixedSizeMatrix matrix2; vctFixedSizeMatrix result; vctRandom(matrix1, value_type(-10), value_type(10)); vctRandom(matrix2, value_type(-10), value_type(10)); RemoveQuasiZero(matrix2); vctGenericContainerTest::TestCoCiCiOperations(matrix1, matrix2, result); } void vctFixedSizeMatrixTest::TestMoMiMiOperationsDouble(void) { TestMoMiMiOperations(); } void vctFixedSizeMatrixTest::TestMoMiMiOperationsFloat(void) { TestMoMiMiOperations(); } void vctFixedSizeMatrixTest::TestMoMiMiOperationsInt(void) { TestMoMiMiOperations(); } template void vctFixedSizeMatrixTest::TestProductOperations(void) { // result size enum {ROWS = 4, COLS = 5, COMSIZE = 7}; typedef _elementType value_type; vctFixedSizeMatrix matrix1; vctFixedSizeMatrix matrix2; vctFixedSizeMatrix matrix3; vctFixedSizeVector vector1; vctFixedSizeVector vector2; vctRandom(matrix1, value_type(-10), value_type(10)); vctRandom(matrix2, value_type(-10), value_type(10)); vctRandom(vector1, value_type(-10), value_type(10)); vctGenericMatrixTest::TestMatrixMatrixProductOperations(matrix1, matrix2, matrix3); vctGenericMatrixTest::TestMatrixVectorProductOperations(matrix1, vector1, vector2); vctRandom(vector2, value_type(-10), value_type(10)); vctGenericMatrixTest::TestVectorMatrixProductOperations(matrix1, vector2, vector1); vctFixedSizeMatrix matrix4; vctFixedSizeMatrix matrix5; vctGenericMatrixTest::TestMatrixMatrixProductExceptions(matrix4, matrix5); } void vctFixedSizeMatrixTest::TestProductOperationsDouble(void) { TestProductOperations(); } void vctFixedSizeMatrixTest::TestProductOperationsFloat(void) { TestProductOperations(); } void vctFixedSizeMatrixTest::TestProductOperationsInt(void) { TestProductOperations(); } template void vctFixedSizeMatrixTest::TestMoMiOperations(void) { enum {ROWS = 2, COLS = 10}; typedef _elementType value_type; vctFixedSizeMatrix matrix; vctFixedSizeMatrix result; vctRandom(matrix, value_type(-10), value_type(10)); vctGenericContainerTest::TestCoCiOperations(matrix, result); } void vctFixedSizeMatrixTest::TestMoMiOperationsDouble(void) { TestMoMiOperations(); } void vctFixedSizeMatrixTest::TestMoMiOperationsFloat(void) { TestMoMiOperations(); } void vctFixedSizeMatrixTest::TestMoMiOperationsInt(void) { TestMoMiOperations(); } template void vctFixedSizeMatrixTest::TestMoMiSiOperations(void) { enum {ROWS = 8, COLS = 8}; typedef _elementType value_type; value_type scalar; cmnRandomSequence & randomSequence = cmnRandomSequence::GetInstance(); randomSequence.ExtractRandomValue(value_type(-5), value_type(5), scalar); vctFixedSizeMatrix matrix; vctFixedSizeMatrix result; if ((scalar < value_type(1)) && (scalar > -value_type(1))) { scalar = value_type(3); } vctRandom(matrix, value_type(-10), value_type(10)); vctGenericContainerTest::TestCoCiSiOperations(matrix, scalar, result); } void vctFixedSizeMatrixTest::TestMoMiSiOperationsDouble(void) { TestMoMiSiOperations(); } void vctFixedSizeMatrixTest::TestMoMiSiOperationsFloat(void) { TestMoMiSiOperations(); } void vctFixedSizeMatrixTest::TestMoMiSiOperationsInt(void) { TestMoMiSiOperations(); } template void vctFixedSizeMatrixTest::TestMoSiMiOperations(void) { enum {ROWS = 3, COLS = 5}; typedef _elementType value_type; value_type scalar; vctFixedSizeMatrix matrix; vctFixedSizeMatrix result; cmnRandomSequence & randomSequence = cmnRandomSequence::GetInstance(); randomSequence.ExtractRandomValue(value_type(-5), value_type(5), scalar); vctRandom(matrix, value_type(-10), value_type(10)); RemoveQuasiZero(matrix); vctGenericContainerTest::TestCoSiCiOperations(scalar, matrix, result); } void vctFixedSizeMatrixTest::TestMoSiMiOperationsDouble(void) { TestMoSiMiOperations(); } void vctFixedSizeMatrixTest::TestMoSiMiOperationsFloat(void) { TestMoSiMiOperations(); } void vctFixedSizeMatrixTest::TestMoSiMiOperationsInt(void) { TestMoSiMiOperations(); } template void vctFixedSizeMatrixTest::TestMioSiOperations(void) { enum {ROWS = 6, COLS = 3}; typedef _elementType value_type; vctFixedSizeMatrix matrix; vctFixedSizeMatrix result; value_type scalar; cmnRandomSequence & randomSequence = cmnRandomSequence::GetInstance(); randomSequence.ExtractRandomValue(value_type(-5), value_type(5), scalar); if ((scalar < value_type(1)) && (scalar > -value_type(1))) { scalar = value_type(3); } vctRandom(matrix, value_type(-10), value_type(10)); vctGenericContainerTest::TestCioSiOperations(matrix, scalar, result); } void vctFixedSizeMatrixTest::TestMioSiOperationsDouble(void) { TestMioSiOperations(); } void vctFixedSizeMatrixTest::TestMioSiOperationsFloat(void) { TestMioSiOperations(); } void vctFixedSizeMatrixTest::TestMioSiOperationsInt(void) { TestMioSiOperations(); } template void vctFixedSizeMatrixTest::TestMioOperations(void) { enum {ROWS = 2, COLS = 3}; typedef _elementType value_type; vctFixedSizeMatrix matrix; vctFixedSizeMatrix result; vctRandom(matrix, value_type(-10), value_type(10)); vctGenericContainerTest::TestCioOperations(matrix, result); } void vctFixedSizeMatrixTest::TestMioOperationsDouble(void) { TestMioOperations(); } void vctFixedSizeMatrixTest::TestMioOperationsFloat(void) { TestMioOperations(); } void vctFixedSizeMatrixTest::TestMioOperationsInt(void) { TestMioOperations(); } template void vctFixedSizeMatrixTest::TestSoMiSiOperations(void) { enum {ROWS = 4, COLS = 4}; typedef _elementType value_type; vctFixedSizeMatrix matrix; vctFixedSizeMatrix result; value_type scalar; cmnRandomSequence & randomSequence = cmnRandomSequence::GetInstance(); randomSequence.ExtractRandomValue(value_type(-5), value_type(5), scalar); vctRandom(matrix, value_type(-10), value_type(10)); vctGenericContainerTest::TestSoCiSiOperations(matrix, scalar, result); } void vctFixedSizeMatrixTest::TestSoMiSiOperationsDouble(void) { TestSoMiSiOperations(); } void vctFixedSizeMatrixTest::TestSoMiSiOperationsFloat(void) { TestSoMiSiOperations(); } void vctFixedSizeMatrixTest::TestSoMiSiOperationsInt(void) { TestSoMiSiOperations(); } template void vctFixedSizeMatrixTest::TestMinAndMax(void) { enum {ROWS = 5, COLS = 4}; typedef _elementType value_type; vctFixedSizeMatrix matrix; vctRandom(matrix, value_type(-50), value_type(50)); vctGenericContainerTest::TestMinAndMax(matrix); } void vctFixedSizeMatrixTest::TestMinAndMaxDouble(void) { TestMinAndMax(); } void vctFixedSizeMatrixTest::TestMinAndMaxFloat(void) { TestMinAndMax(); } void vctFixedSizeMatrixTest::TestMinAndMaxInt(void) { TestMinAndMax(); } template void vctFixedSizeMatrixTest::TestIterators(void) { enum {ROWS = 3, COLS = 5}; typedef _elementType value_type; vctFixedSizeMatrix matrix; vctRandom(matrix, value_type(-10), value_type(10)); vctGenericContainerTest::TestIterators(matrix); } void vctFixedSizeMatrixTest::TestIteratorsDouble(void) { TestIterators(); } void vctFixedSizeMatrixTest::TestIteratorsFloat(void) { TestIterators(); } void vctFixedSizeMatrixTest::TestIteratorsInt(void) { TestIterators(); } template void vctFixedSizeMatrixTest::TestSTLFunctions(void) { enum {ROWS = 3, COLS = 5}; typedef _elementType value_type; vctFixedSizeMatrix matrix; vctFixedSizeMatrix result; vctRandom(matrix, value_type(-10), value_type(10)); vctGenericContainerTest::TestSTLFunctions(matrix, result); } void vctFixedSizeMatrixTest::TestSTLFunctionsDouble(void) { TestSTLFunctions(); } void vctFixedSizeMatrixTest::TestSTLFunctionsFloat(void) { TestSTLFunctions(); } void vctFixedSizeMatrixTest::TestSTLFunctionsInt(void) { TestSTLFunctions(); } template void vctFixedSizeMatrixTest::TestFastCopyOf(void) { enum {ROWS = 3, COLS = 5}; typedef _elementType value_type; // dynamic matrix vctFixedSizeMatrix destination; vctDynamicMatrix validSource(ROWS, COLS, _storageOrder); vctRandom(validSource, value_type(-10), value_type(10)); vctGenericContainerTest::TestFastCopyOf(validSource, destination); // test for different storage order vctDynamicMatrix invalidSource(ROWS, COLS, !_storageOrder); CPPUNIT_ASSERT(!destination.FastCopyCompatible(invalidSource)); CPPUNIT_ASSERT(!destination.FastCopyOf(invalidSource)); // test for exception if different size vctDynamicMatrix largerMatrix(ROWS + 1, COLS + 1, _storageOrder); vctRandom(largerMatrix, value_type(-10), value_type(10)); vctGenericContainerTest::TestFastCopyOfException(largerMatrix, destination); // test for compact by row or col vctDynamicMatrixRef compactSub(largerMatrix, 0, 0, ROWS, COLS); vctGenericContainerTest::TestFastCopyOf(compactSub, destination); // test for incompatible strides, since we will not make any memory access these are totally wrong vctDynamicMatrixRef invalidRef(ROWS, COLS, 100, 100, largerMatrix.Pointer()); CPPUNIT_ASSERT(!destination.FastCopyCompatible(invalidRef)); CPPUNIT_ASSERT(!destination.FastCopyOf(invalidRef)); // safe tests using a larger source and not performing checks CPPUNIT_ASSERT(largerMatrix.FastCopyOf(destination, vctFastCopy::SkipChecks)); CPPUNIT_ASSERT(destination.FastCopyOf(invalidSource, vctFastCopy::SkipChecks)); // fixed size matrix vctFixedSizeMatrix validSourceFixed; vctRandom(validSourceFixed, value_type(-10), value_type(10)); vctGenericContainerTest::TestFastCopyOf(validSourceFixed, destination); } void vctFixedSizeMatrixTest::TestFastCopyOfDouble(void) { TestFastCopyOf(); TestFastCopyOf(); } void vctFixedSizeMatrixTest::TestFastCopyOfFloat(void) { TestFastCopyOf(); TestFastCopyOf(); } void vctFixedSizeMatrixTest::TestFastCopyOfInt(void) { TestFastCopyOf(); TestFastCopyOf(); } CPPUNIT_TEST_SUITE_REGISTRATION(vctFixedSizeMatrixTest);