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.

CMatrixTest.hpp 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. ///-------------------------------------------------------------------------------------------------
  2. ///
  3. /// \file CMatrixTest.hpp
  4. /// \brief Test Definitions for OpenViBE Matrix Class.
  5. /// \author Thibaut Monseigne (Inria).
  6. /// \version 1.0.
  7. /// \date 11/05/2020.
  8. /// \copyright <a href="https://choosealicense.com/licenses/agpl-3.0/">GNU Affero General Public License v3.0</a>.
  9. ///
  10. ///-------------------------------------------------------------------------------------------------
  11. #pragma once
  12. #include <gtest/gtest.h>
  13. #include <openvibe/CMatrix.hpp>
  14. #include "utils.hpp"
  15. //---------------------------------------------------------------------------------------------------
  16. class CMatrix_Tests : public testing::Test
  17. {
  18. protected:
  19. OpenViBE::CMatrix m_mat;
  20. void SetUp() override
  21. {
  22. m_mat.resize(1, 2); // one row two column buffer not init
  23. m_mat.getBuffer()[0] = 10; // Buffer init with getBuffer and First element set
  24. m_mat.getBuffer()[1] = 20; // Second Element set (buffer already init so refresh function not run)
  25. m_mat.setDimensionLabel(0, 0, "dim0e0"); // Row label set
  26. m_mat.setDimensionLabel(1, 0, "dim1e0"); // Column 1 Label set
  27. m_mat.setDimensionLabel(1, 1, "dim1e1"); // Column 2 Label set
  28. }
  29. };
  30. //---------------------------------------------------------------------------------------------------
  31. //---------------------------------------------------------------------------------------------------
  32. TEST_F(CMatrix_Tests, Constructor)
  33. {
  34. OpenViBE::CMatrix res;
  35. ASSERT_EQ(0, res.getSize()) << "Default constructor doesn't have a size of 0.";
  36. EXPECT_STREQ("", res.getDimensionLabel(0, 0)) << "Default constructor has no dimension so no label.";
  37. ASSERT_EQ(2, m_mat.getSize()) << "Setup Matrix doesn't have 2 values.";
  38. ASSERT_EQ(2, m_mat.getDimensionCount()) << "Setup Matrix doesn't have 2 Dimensions.";
  39. ASSERT_EQ(1, m_mat.getDimensionSize(0)) << "Setup Matrix doesn't have 1 Row.";
  40. ASSERT_EQ(2, m_mat.getDimensionSize(1)) << "Setup Matrix doesn't have 2 Columns.";
  41. EXPECT_TRUE(AlmostEqual(10, m_mat.getBuffer()[0])) << "Setup Matrix 1st value isn't 10.";
  42. EXPECT_TRUE(AlmostEqual(20, m_mat.getBuffer()[1])) << "Setup Matrix 2nd value isn't 20.";
  43. EXPECT_STREQ("dim0e0", m_mat.getDimensionLabel(0, 0)) << "Setup Matrix Row Label isn't dim0e0.";
  44. EXPECT_STREQ("dim1e0", m_mat.getDimensionLabel(1, 0)) << "Setup Matrix 1st Column Label isn't dim1e0.";
  45. EXPECT_STREQ("dim1e1", m_mat.getDimensionLabel(1, 1)) << "Setup Matrix 2nd Column Label isn't dim1e1.";
  46. EXPECT_TRUE(m_mat.isBufferValid()) << "Setup Matrix isn't valid";
  47. m_mat.getBuffer()[0] = std::numeric_limits<double>::infinity();
  48. EXPECT_FALSE(m_mat.isBufferValid()) << "infinity isn't considered as invalid";
  49. m_mat.getBuffer()[0] = std::numeric_limits<double>::quiet_NaN();
  50. EXPECT_FALSE(m_mat.isBufferValid()) << "Setup Matrix isn't valid";
  51. }
  52. //---------------------------------------------------------------------------------------------------
  53. //---------------------------------------------------------------------------------------------------
  54. TEST_F(CMatrix_Tests, Constructor_Copy)
  55. {
  56. OpenViBE::CMatrix res(m_mat);
  57. EXPECT_TRUE(m_mat == res) << ErrorMsg("Copy Constructor", m_mat, res);
  58. res.getBuffer()[0] = 15;
  59. res.getBuffer()[1] = 25;
  60. res.setDimensionLabel(1, 0, "changed");
  61. EXPECT_TRUE(AlmostEqual(10, m_mat.getBuffer()[0])) << "Setup Matrix 1st value isn't 10.";
  62. EXPECT_TRUE(AlmostEqual(20, m_mat.getBuffer()[1])) << "Setup Matrix 2nd value isn't 20.";
  63. EXPECT_STREQ("dim1e0", m_mat.getDimensionLabel(1, 0)) << "Setup Matrix 1st Column Label isn't dim1e0.";
  64. EXPECT_TRUE(AlmostEqual(15, res.getBuffer()[0])) << "New Matrix 1st value isn't 15.";
  65. EXPECT_TRUE(AlmostEqual(25, res.getBuffer()[1])) << "New Matrix 2nd value isn't 25.";
  66. EXPECT_STREQ("changed", res.getDimensionLabel(1, 0)) << "New Matrix 1st Column Label isn't changed.";
  67. }
  68. //---------------------------------------------------------------------------------------------------
  69. //---------------------------------------------------------------------------------------------------
  70. TEST_F(CMatrix_Tests, Constructor_copy_in_array_push)
  71. {
  72. std::vector<OpenViBE::CMatrix> res;
  73. res.push_back(m_mat);
  74. res.push_back(m_mat);
  75. res[0].getBuffer()[0] = 15;
  76. res[0].getBuffer()[1] = 25;
  77. res[0].setDimensionLabel(1, 0, "changed");
  78. EXPECT_TRUE(AlmostEqual(15, res[0].getBuffer()[0])) << "1st Matrix 1st value isn't 15.";
  79. EXPECT_TRUE(AlmostEqual(25, res[0].getBuffer()[1])) << "1st Matrix 2nd value isn't 25.";
  80. EXPECT_STREQ("changed", res[0].getDimensionLabel(1, 0)) << "1st Matrix 1st Column Label isn't changed.";
  81. EXPECT_TRUE(AlmostEqual(10, res[1].getBuffer()[0])) << "2nd Matrix 1st value isn't 10.";
  82. EXPECT_TRUE(AlmostEqual(20, res[1].getBuffer()[1])) << "2nd Matrix 2nd value isn't 20.";
  83. EXPECT_STREQ("dim1e0", res[1].getDimensionLabel(1, 0)) << "2nd Matrix 1st Column Label isn't dim1e0.";
  84. }
  85. //---------------------------------------------------------------------------------------------------
  86. //---------------------------------------------------------------------------------------------------
  87. TEST_F(CMatrix_Tests, Operators)
  88. {
  89. OpenViBE::CMatrix res = m_mat;
  90. EXPECT_TRUE(m_mat.isDescriptionEqual(m_mat)) << ErrorMsg("equality description", m_mat, m_mat);
  91. EXPECT_TRUE(m_mat.isBufferEqual(m_mat)) << ErrorMsg("equality buffer", m_mat, m_mat);
  92. EXPECT_TRUE(m_mat.isBufferAlmostEqual(m_mat)) << ErrorMsg("Almost equality buffer", m_mat, m_mat);
  93. EXPECT_TRUE(m_mat == m_mat) << ErrorMsg("equality operator", m_mat, m_mat);
  94. EXPECT_TRUE(m_mat == res) << ErrorMsg("Copy assignement", m_mat, res);
  95. m_mat.getBuffer()[0] = 15;
  96. m_mat.getBuffer()[1] = 25;
  97. m_mat.setDimensionLabel(1, 0, "changed");
  98. EXPECT_TRUE(AlmostEqual(15, m_mat.getBuffer()[0])) << "Setup Matrix 1st value isn't 15.";
  99. EXPECT_TRUE(AlmostEqual(25, m_mat.getBuffer()[1])) << "Setup Matrix 2nd value isn't 25.";
  100. EXPECT_STREQ("changed", m_mat.getDimensionLabel(1, 0)) << "Setup Matrix 1st Column Label isn't changed.";
  101. EXPECT_TRUE(AlmostEqual(10, res.getBuffer()[0])) << "New Matrix 1st value isn't 10.";
  102. EXPECT_TRUE(AlmostEqual(20, res.getBuffer()[1])) << "New Matrix 2nd value isn't 20.";
  103. EXPECT_STREQ("dim1e0", res.getDimensionLabel(1, 0)) << "New Matrix 1st Column Label isn't dim1e0.";
  104. }
  105. //---------------------------------------------------------------------------------------------------
  106. //---------------------------------------------------------------------------------------------------
  107. TEST_F(CMatrix_Tests, Resize)
  108. {
  109. OpenViBE::CMatrix res(1, 2);
  110. res.resetBuffer();
  111. res.setDimensionLabel(0, 0, "label");
  112. ASSERT_EQ(2, res.getDimensionCount()) << "Matrix doesn't have 2 Dimensions.";
  113. ASSERT_EQ(1, res.getDimensionSize(0)) << "Matrix doesn't have 1 Row.";
  114. ASSERT_EQ(2, res.getDimensionSize(1)) << "Matrix doesn't have 2 Columns.";
  115. EXPECT_TRUE(AlmostEqual(0, res.getBuffer()[0])) << "Matrix 1st default value isn't 0 after resize.";
  116. EXPECT_TRUE(AlmostEqual(0, res.getBuffer()[1])) << "Matrix 2nd default value isn't 0 after resize.";
  117. EXPECT_STREQ("label", res.getDimensionLabel(0, 0)) << "Matrix Row Label isn't \"label\".";
  118. EXPECT_STREQ("", res.getDimensionLabel(1, 0)) << "Matrix 1st Col default Label isn't empty.";
  119. EXPECT_STREQ("", res.getDimensionLabel(1, 1)) << "Matrix 2nd Col default Label isn't empty.";
  120. res.resize(2, 2);
  121. res.setDimensionLabel(1, 1, "label");
  122. ASSERT_EQ(2, res.getDimensionCount()) << "Matrix doesn't have 2 Dimensions.";
  123. ASSERT_EQ(2, res.getDimensionSize(0)) << "Matrix doesn't have 2 Row.";
  124. ASSERT_EQ(2, res.getDimensionSize(1)) << "Matrix doesn't have 2 Columns.";
  125. EXPECT_STREQ("", res.getDimensionLabel(0, 0)) << "Matrix 1st Row default Label isn't empty."; // The resize remove all previous label and size
  126. EXPECT_STREQ("label", res.getDimensionLabel(1, 1)) << "Matrix 2nd Col Label isn't \"label\".";
  127. res.resetLabels();
  128. EXPECT_STREQ("", res.getDimensionLabel(1, 1)) << "Matrix 2nd Col reseted Label isn't empty.";
  129. res.resize(2);
  130. ASSERT_EQ(1, res.getDimensionCount()) << "Matrix doesn't have 1 Dimension.";
  131. ASSERT_EQ(2, res.getDimensionSize(0)) << "Matrix doesn't have 2 Row.";
  132. }
  133. //---------------------------------------------------------------------------------------------------
  134. //---------------------------------------------------------------------------------------------------
  135. TEST_F(CMatrix_Tests, Save_Load)
  136. {
  137. OpenViBE::CMatrix res;
  138. EXPECT_TRUE(m_mat.toTextFile("Save_2DMatrix-output.txt")) << "Error during Saving 2D Matrix : " << std::endl << m_mat << std::endl;
  139. EXPECT_TRUE(res.fromTextFile("Save_2DMatrix-output.txt")) << "Error during Loading 2D Matrix : " << std::endl << res << std::endl;
  140. EXPECT_TRUE(m_mat == res) << ErrorMsg("Save", m_mat, res);
  141. OpenViBE::CMatrix row(2);
  142. row.getBuffer()[0] = -1;
  143. row.getBuffer()[1] = -4.549746549678;
  144. EXPECT_TRUE(row.toTextFile("Save_2DMatrix-output.txt")) << "Error during Saving 1D Matrix : " << std::endl << row << std::endl;
  145. EXPECT_TRUE(res.fromTextFile("Save_2DMatrix-output.txt")) << "Error during Loading 1D Matrix : " << std::endl << res << std::endl;
  146. EXPECT_TRUE(row == res) << ErrorMsg("Save", row, res);
  147. }
  148. //---------------------------------------------------------------------------------------------------