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.

uoMatrixToolkitTest.cpp 5.0KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*********************************************************************
  2. * Software License Agreement (AGPL-3 License)
  3. *
  4. * OpenViBE SDK Test Software
  5. * Based on OpenViBE V1.1.0, Copyright (C) Inria, 2006-2015
  6. * Copyright (C) Inria, 2015-2017,V1.0
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program.
  19. * If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <iostream>
  22. #include <random>
  23. #include <toolkit/ovtk_all.h>
  24. #include "ovtAssert.h"
  25. static std::default_random_engine gen(777);
  26. static std::uniform_real_distribution<double> dist(0.0, 100.0);
  27. void fillMatrix(OpenViBE::CMatrix& matrix)
  28. {
  29. for (size_t i = 0; i < matrix.getDimensionCount(); ++i)
  30. {
  31. for (size_t j = 0; j < matrix.getDimensionSize(i); ++j)
  32. {
  33. std::stringstream label;
  34. label << "Label " << j + 1 << " of Dimension " << i + 1;
  35. matrix.setDimensionLabel(i, j, label.str());
  36. }
  37. }
  38. for (size_t i = 0; i < matrix.getBufferElementCount(); ++i) { matrix.getBuffer()[i] = dist(gen); }
  39. }
  40. bool testMatrix(OpenViBE::CMatrix& expectedMatrix, const std::string& textFile, const size_t precision = 6)
  41. {
  42. const double threshold = 1.0 / std::pow(10.0, double(precision - 2));
  43. fillMatrix(expectedMatrix);
  44. if (!OpenViBE::Toolkit::Matrix::saveToTextFile(expectedMatrix, textFile.c_str(), precision))
  45. {
  46. std::cerr << "Error: saving matrix to file " << textFile << "\n";
  47. return false;
  48. }
  49. OpenViBE::CMatrix resultMatrix;
  50. if (!OpenViBE::Toolkit::Matrix::loadFromTextFile(resultMatrix, textFile.c_str()))
  51. {
  52. std::cerr << "Error: loading matrix from file " << textFile << "\n";
  53. return false;
  54. }
  55. if (!OpenViBE::Toolkit::Matrix::isDescriptionSimilar(expectedMatrix, resultMatrix))
  56. {
  57. std::cerr << "Error: Descriptions differ between expected matrix and result matrix after save/load\n";
  58. return false;
  59. }
  60. for (size_t i = 0; i < expectedMatrix.getBufferElementCount(); ++i)
  61. {
  62. const double error = std::fabs(expectedMatrix.getBuffer()[i] - resultMatrix.getBuffer()[i]);
  63. if (error > threshold)
  64. {
  65. std::cerr << "Error: Data differs at index " << i << ", error " << error << " (thresold = " << threshold << ")\n";
  66. return false;
  67. }
  68. }
  69. return true;
  70. }
  71. int uoMatrixToolkitTest(int argc, char* argv[])
  72. {
  73. OVT_ASSERT(argc == 2, "Failure to retrieve tests arguments. Expecting: output_dir");
  74. const std::string oMatrixFile = std::string(argv[1]) + "uoMatrixToolkitTest.txt";
  75. OpenViBE::CMatrix source;
  76. source.resize(1);
  77. OVT_ASSERT(testMatrix(source, oMatrixFile), "Failed to test matrix with parameters [dimension_count; dimension_size] = [1; {0,1}]");
  78. source.resize(5);
  79. OVT_ASSERT(testMatrix(source, oMatrixFile), "Failed to test matrix with parameters [dimension_count; dimension_size] = [1; {0,5}]");
  80. source.resize(1, 1);
  81. OVT_ASSERT(testMatrix(source, oMatrixFile), "Failed to test matrix with parameters [dimension_count; dimension_size] = [2; {0,1},{1,1}]");
  82. source.resize(1, 7);
  83. OVT_ASSERT(testMatrix(source, oMatrixFile), "Failed to test matrix with parameters [dimension_count; dimension_size] = [2; {0,1},{1,7}]");
  84. source.resize(9, 1);
  85. OVT_ASSERT(testMatrix(source, oMatrixFile), "Failed to test matrix with parameters [dimension_count; dimension_size] = [2; {0,9},{1,1}]");
  86. source.resize(2, 4);
  87. OVT_ASSERT(testMatrix(source, oMatrixFile), "Failed to test matrix with parameters [dimension_count; dimension_size] = [2; {0,2},{1,4}]");
  88. source.resize(3, 15);
  89. OVT_ASSERT(testMatrix(source, oMatrixFile), "Failed to test matrix with parameters [dimension_count; dimension_size] = [2; {0,3},{1,15}]");
  90. source.resize({ 1, 1, 1 });
  91. OVT_ASSERT(testMatrix(source, oMatrixFile), "Failed to test matrix with parameters [dimension_count; dimension_size] = [3; {0,1},{1,1},{2,1}]");
  92. source.resize({ 1, 1, 5 });
  93. OVT_ASSERT(testMatrix(source, oMatrixFile), "Failed to test matrix with parameters [dimension_count; dimension_size] = [3; {0,1},{1,1},{2,5}]");
  94. source.resize({ 2, 3, 6 });
  95. OVT_ASSERT(testMatrix(source, oMatrixFile), "Failed to test matrix with parameters [dimension_count; dimension_size] = [3; {0,2},{1,3},{2,6}]");
  96. source.resize({ 9, 5, 2, 3 });
  97. OVT_ASSERT(testMatrix(source, oMatrixFile), "Failed to test matrix with parameters [dimension_count; dimension_size] = [4; {0,9},{1,5},{2,2},{3,3}]");
  98. // special cases at boundaries
  99. source.resize(0, 0);
  100. OVT_ASSERT(testMatrix(source, oMatrixFile), "Failed to test matrix with parameters [dimension_count; dimension_size] = [2; {0,0},{1,0}]");
  101. OpenViBE::CMatrix emptySource;
  102. OVT_ASSERT(!testMatrix(emptySource, oMatrixFile), "Failed to test matrix with no parameter");
  103. return EXIT_SUCCESS;
  104. }
  105. //==========================End OF File==============================