/* -*- 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: 2012-07-09 (C) Copyright 2012-2020 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 "vctGenericContainerTest.h" #include "vctDataFunctionsDynamicMatrixTest.h" #include #include #include #include void vctDataFunctionsDynamicMatrixTest::TestDataCopy(void) { typedef vctDynamicMatrix DataType; DataType source, data; source.SetSize(7, 3); vctRandom(source, -1.0, 1.0); cmnData::Copy(data, source); CPPUNIT_ASSERT(source.Equal(data)); } void vctDataFunctionsDynamicMatrixTest::TestBinarySerializationStream(void) { cmnDataFormat local, remote; std::stringstream stream; typedef vctDynamicMatrix DataType; DataType m1, m2, mReference; m1.SetSize(12, 23); m2.SetSize(12, 23); mReference.SetSize(12, 23); vctRandom(mReference, -10.0, 10.0); m1 = mReference; cmnData::SerializeBinary(m1, stream); m1.SetAll(0); cmnData::DeSerializeBinary(m2, stream, local, remote); CPPUNIT_ASSERT_EQUAL(mReference, m2); } void vctDataFunctionsDynamicMatrixTest::TestTextSerializationStream(void) { std::stringstream stream; typedef vctDynamicMatrix DataType; DataType m1, m2, mReference; m1.SetSize(4, 5); mReference.SetSize(4,5); m2.SetSize(2, 3); // intentionally different vctRandom(mReference, -10.0, 10.0); m1 = mReference; cmnData::SerializeText(m1, stream, ','); m1.SetAll(0); cmnData::DeSerializeText(m2, stream, ','); CPPUNIT_ASSERT(mReference.AlmostEqual(m2, 0.01)); // low precision due to stream out loss CPPUNIT_ASSERT(!stream.fail()); // try without delimiter, using space stream.clear(); vctRandom(mReference, -20.0, 20.0); m1 = mReference; cmnData::SerializeText(m1, stream, ' '); m2.SetSize(2, 3); // intentionally different m2.SetAll(0.0); cmnData::DeSerializeText(m2, stream, ' '); CPPUNIT_ASSERT(mReference.AlmostEqual(m2, 0.01)); // low precision due to stream out loss CPPUNIT_ASSERT(!stream.fail()); // try with the wrong delimiter bool exceptionReceived = false; stream.clear(); vctRandom(mReference, -20.0, 20.0); m1 = mReference; cmnData::SerializeText(m1, stream, '|'); m2.SetSize(2, 3); // intentionally different m2.SetAll(0.0); try { cmnData::DeSerializeText(m2, stream, ','); } catch (std::runtime_error &) { exceptionReceived = true; } CPPUNIT_ASSERT(exceptionReceived); } void vctDataFunctionsDynamicMatrixTest::TestScalar(void) { typedef vctDynamicMatrix DataType; DataType mInt; mInt.SetSize(6, 3); CPPUNIT_ASSERT_EQUAL(static_cast(mInt.size() + 2), cmnData::ScalarNumber(mInt)); CPPUNIT_ASSERT_EQUAL(false, cmnData::ScalarNumberIsFixed(mInt)); typedef vctDynamicMatrix > DataType2; DataType2 mmDouble; mmDouble.SetSize(4, 5); size_t numberOfScalarsExpected = 2; // size of the containing matrix for (size_t i = 0; i < mmDouble.rows(); ++i) { for (size_t j = 0; j < mmDouble.cols(); ++j) { mmDouble.Element(i, j).SetSize(i + 1, j + 1); // + 1 to make sure we don't have a 0 size matrix numberOfScalarsExpected += ((i + 1) * (j + 1)); numberOfScalarsExpected += 2; // size of each contained matrix } } CPPUNIT_ASSERT_EQUAL(numberOfScalarsExpected, cmnData::ScalarNumber(mmDouble)); CPPUNIT_ASSERT_EQUAL(false, cmnData::ScalarNumberIsFixed(mmDouble)); typedef vctDynamicMatrix DataType3; DataType3 mString; mString.SetSize(3, 2); CPPUNIT_ASSERT_EQUAL(static_cast(2), cmnData::ScalarNumber(mString)); CPPUNIT_ASSERT_EQUAL(false, cmnData::ScalarNumberIsFixed(mString)); size_t row, col, subRow, subCol, position; bool exceptionReceived = false; std::string description; // exception expected if index too high try { description = cmnData::ScalarDescription(mInt, cmnData::ScalarNumber(mInt) + 1); } catch (std::out_of_range &) { exceptionReceived = true; } CPPUNIT_ASSERT(exceptionReceived); exceptionReceived = false; try { description = cmnData::ScalarDescription(mmDouble, cmnData::ScalarNumber(mmDouble) + 1); } catch (std::out_of_range &) { exceptionReceived = true; } CPPUNIT_ASSERT(exceptionReceived); // exception expected for any index above 2 exceptionReceived = false; try { description = cmnData::ScalarDescription(mString, cmnData::ScalarNumber(mString) + 1); } catch (std::out_of_range &) { exceptionReceived = true; } CPPUNIT_ASSERT(exceptionReceived); // get scalar position = 0; CPPUNIT_ASSERT_EQUAL(static_cast(mInt.rows()), cmnData::Scalar(mInt, position)); position++; CPPUNIT_ASSERT_EQUAL(static_cast(mInt.cols()), cmnData::Scalar(mInt, position)); position++; for (row = 0; row < mInt.rows(); ++row) { for (col = 0; col < mInt.cols(); ++col) { mInt.Element(row, col) = static_cast(row * 10 + col); CPPUNIT_ASSERT_EQUAL(static_cast(row * 10 + col), cmnData::Scalar(mInt, position)); position++; } } // check size position = 0; CPPUNIT_ASSERT_EQUAL(static_cast(mmDouble.rows()), cmnData::Scalar(mmDouble, position)); position++; CPPUNIT_ASSERT_EQUAL(static_cast(mmDouble.cols()), cmnData::Scalar(mmDouble, position)); position++; for (row = 0; row < mmDouble.rows(); ++row) { for (col = 0; col < mmDouble.cols(); ++col) { CPPUNIT_ASSERT_EQUAL(static_cast(mmDouble.Element(row, col).rows()), cmnData::Scalar(mmDouble, position)); position++; CPPUNIT_ASSERT_EQUAL(static_cast(mmDouble.Element(row, col).cols()), cmnData::Scalar(mmDouble, position)); position++; for (subRow = 0; subRow < mmDouble.Element(row, col).rows(); ++subRow) { for (subCol = 0; subCol < mmDouble.Element(row, col).cols(); ++subCol) { mmDouble.Element(row, col).Element(subRow, subCol) = static_cast(row * col * 100 + subRow * subCol); CPPUNIT_ASSERT_EQUAL(static_cast(row * col * 100 + subRow * subCol), cmnData::Scalar(mmDouble, position)); position++; } } } } } CPPUNIT_TEST_SUITE_REGISTRATION(vctDataFunctionsDynamicMatrixTest);