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.

uoStreamedMatrixTest.cpp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <iostream>
  2. #include "gtest/gtest.h"
  3. #include "ovtAssert.h"
  4. #include "../common/ovtKernelContext.h"
  5. #include <openvibe/ov_all.h>
  6. #include <ovp_global_defines.h>
  7. namespace {
  8. const char* kernelConfig = nullptr;
  9. class StreamedMatrixTest : public testing::Test
  10. {
  11. protected:
  12. StreamedMatrixTest() { }
  13. ~StreamedMatrixTest() override { }
  14. void SetUp() override
  15. {
  16. m_kernelCtx.initialize();
  17. m_kernelCtx->getPluginManager().addPluginsFromFiles(m_kernelCtx->getConfigurationManager().expand("${Path_Lib}/*openvibe-plugins-sdk-stream-codecs*"));
  18. m_decoderId = OpenViBE::CIdentifier::undefined();
  19. m_decoderId = m_kernelCtx->getAlgorithmManager().createAlgorithm(OVP_GD_ClassId_Algorithm_StreamedMatrixDecoder);
  20. ASSERT_NE(OpenViBE::CIdentifier::undefined(), m_decoderId);
  21. m_encoderId = OpenViBE::CIdentifier::undefined();
  22. m_encoderId = m_kernelCtx->getAlgorithmManager().createAlgorithm(OVP_GD_ClassId_Algorithm_StreamedMatrixEncoder);
  23. ASSERT_NE(OpenViBE::CIdentifier::undefined(), m_encoderId);
  24. }
  25. void TearDown() override
  26. {
  27. ASSERT_TRUE(m_kernelCtx->getAlgorithmManager().releaseAlgorithm(m_decoderId));
  28. m_decoderId = OpenViBE::CIdentifier::undefined();
  29. ASSERT_TRUE(m_kernelCtx->getAlgorithmManager().releaseAlgorithm(m_encoderId));
  30. m_encoderId = OpenViBE::CIdentifier::undefined();
  31. m_kernelCtx.uninitialize();
  32. }
  33. OpenViBE::CIdentifier m_decoderId;
  34. OpenViBE::CIdentifier m_encoderId;
  35. OpenViBE::Test::ctx m_kernelCtx;
  36. };
  37. TEST_F(StreamedMatrixTest, test_init)
  38. {
  39. auto& decoder = m_kernelCtx->getAlgorithmManager().getAlgorithm(m_decoderId);
  40. auto& encoder = m_kernelCtx->getAlgorithmManager().getAlgorithm(m_encoderId);
  41. EXPECT_TRUE(decoder.initialize());
  42. EXPECT_TRUE(encoder.initialize());
  43. EXPECT_TRUE(decoder.uninitialize());
  44. EXPECT_TRUE(encoder.uninitialize());
  45. }
  46. TEST_F(StreamedMatrixTest, matrix_decoder_does_not_crash_when_given_an_empty_matrix)
  47. {
  48. auto& encoder = m_kernelCtx->getAlgorithmManager().getAlgorithm(m_encoderId);
  49. auto& decoder = m_kernelCtx->getAlgorithmManager().getAlgorithm(m_decoderId);
  50. EXPECT_TRUE(encoder.initialize());
  51. EXPECT_TRUE(decoder.initialize());
  52. OpenViBE::CMatrix mat(0);
  53. OpenViBE::Kernel::TParameterHandler<const OpenViBE::CMatrix*> iMatrix(
  54. encoder.getInputParameter(OVP_GD_Algorithm_StreamedMatrixEncoder_InputParameterId_Matrix));
  55. OpenViBE::Kernel::TParameterHandler<const OpenViBE::IMemoryBuffer*> oBuffer(
  56. encoder.getOutputParameter(OVP_GD_Algorithm_StreamedMatrixEncoder_OutputParameterId_EncodedMemoryBuffer));
  57. OpenViBE::Kernel::TParameterHandler<const OpenViBE::IMemoryBuffer*> iBuffer(
  58. decoder.getInputParameter(OVP_GD_Algorithm_StreamedMatrixDecoder_InputParameterId_MemoryBufferToDecode));
  59. OpenViBE::Kernel::TParameterHandler<const OpenViBE::CMatrix*> oMatrix(
  60. decoder.getOutputParameter(OVP_GD_Algorithm_StreamedMatrixDecoder_OutputParameterId_Matrix));
  61. iMatrix = &mat;
  62. iBuffer.setReferenceTarget(oBuffer);
  63. encoder.process(OVP_GD_Algorithm_StreamedMatrixEncoder_InputTriggerId_EncodeHeader);
  64. decoder.process();
  65. EXPECT_TRUE(decoder.isOutputTriggerActive(OVP_GD_Algorithm_StreamedMatrixDecoder_OutputTriggerId_ReceivedHeader));
  66. EXPECT_EQ(mat.getDimensionCount(), oMatrix->getDimensionCount());
  67. EXPECT_TRUE(encoder.uninitialize());
  68. EXPECT_TRUE(decoder.uninitialize());
  69. }
  70. TEST_F(StreamedMatrixTest, matrix_encoded_and_then_decoded_is_identical_to_original)
  71. {
  72. auto& encoder = m_kernelCtx->getAlgorithmManager().getAlgorithm(m_encoderId);
  73. auto& decoder = m_kernelCtx->getAlgorithmManager().getAlgorithm(m_decoderId);
  74. EXPECT_TRUE(encoder.initialize());
  75. EXPECT_TRUE(decoder.initialize());
  76. OpenViBE::CMatrix mat(2, 3);
  77. size_t elemCount = 1;
  78. for (size_t dim = 0; dim < mat.getDimensionCount(); ++dim)
  79. {
  80. const auto size = mat.getDimensionSize(dim);
  81. for (size_t entry = 0; entry < size; ++entry)
  82. {
  83. std::string label = std::to_string(dim) + ":" + std::to_string(entry);
  84. mat.setDimensionLabel(0, 0, label.c_str());
  85. }
  86. elemCount *= size;
  87. }
  88. for (size_t i = 0; i < elemCount; ++i) { mat.getBuffer()[i] = double(i); }
  89. OpenViBE::Kernel::TParameterHandler<const OpenViBE::CMatrix*> iMatrix(
  90. encoder.getInputParameter(OVP_GD_Algorithm_StreamedMatrixEncoder_InputParameterId_Matrix));
  91. OpenViBE::Kernel::TParameterHandler<const OpenViBE::IMemoryBuffer*> oBuffer(
  92. encoder.getOutputParameter(OVP_GD_Algorithm_StreamedMatrixEncoder_OutputParameterId_EncodedMemoryBuffer));
  93. OpenViBE::Kernel::TParameterHandler<const OpenViBE::IMemoryBuffer*> iBuffer(
  94. decoder.getInputParameter(OVP_GD_Algorithm_StreamedMatrixDecoder_InputParameterId_MemoryBufferToDecode));
  95. OpenViBE::Kernel::TParameterHandler<const OpenViBE::CMatrix*> oMatrix(
  96. decoder.getOutputParameter(OVP_GD_Algorithm_StreamedMatrixDecoder_OutputParameterId_Matrix));
  97. iMatrix = &mat;
  98. iBuffer.setReferenceTarget(oBuffer);
  99. encoder.process(OVP_GD_Algorithm_StreamedMatrixEncoder_InputTriggerId_EncodeHeader);
  100. decoder.process();
  101. EXPECT_TRUE(decoder.isOutputTriggerActive(OVP_GD_Algorithm_StreamedMatrixDecoder_OutputTriggerId_ReceivedHeader));
  102. EXPECT_EQ(mat.getDimensionCount(), oMatrix->getDimensionCount());
  103. EXPECT_TRUE(encoder.uninitialize());
  104. EXPECT_TRUE(decoder.uninitialize());
  105. }
  106. } // namespace
  107. int uoStreamedMatrixTest(int argc, char* argv[])
  108. {
  109. if (argc > 1) { kernelConfig = argv[1]; }
  110. testing::InitGoogleTest(&argc, argv);
  111. ::testing::GTEST_FLAG(filter) = "StreamedMatrixTest.*";
  112. return RUN_ALL_TESTS();
  113. }