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.

uoEBMLReaderTest.cpp 5.7KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <iomanip>
  23. #include <fstream>
  24. #include "ebml/IReader.h"
  25. #include "ebml/CReader.h"
  26. #include "ebml/CReaderHelper.h"
  27. #include "ovtAssert.h"
  28. std::ofstream g_OutputStream;
  29. class CReaderCallBack : public EBML::IReaderCallBack
  30. {
  31. public:
  32. CReaderCallBack() { }
  33. ~CReaderCallBack() override { }
  34. bool isMasterChild(const EBML::CIdentifier& identifier) override
  35. {
  36. if (identifier == EBML_Identifier_Header) { return true; }
  37. if (identifier == EBML::CIdentifier(0xffff)) { return true; }
  38. return false;
  39. }
  40. void openChild(const EBML::CIdentifier& identifier) override
  41. {
  42. m_CurrentID = identifier;
  43. for (int i = 0; i < m_Depth; ++i) { g_OutputStream << " "; }
  44. g_OutputStream << "Opening child node [0x" << std::setw(16) << std::setfill('0') << std::hex << m_CurrentID << std::dec << "]\n";
  45. m_Depth++;
  46. }
  47. void processChildData(const void* buffer, const size_t size) override
  48. {
  49. for (int i = 0; i < m_Depth; ++i) { g_OutputStream << " "; }
  50. if (m_CurrentID == EBML_Identifier_DocType) { g_OutputStream << "Got doc type : [" << m_helper.getStr(buffer, size) << "]\n"; }
  51. else if (m_CurrentID == EBML_Identifier_EBMLVersion)
  52. {
  53. g_OutputStream << "Got EBML version : [0x" << std::setw(16) << std::setfill('0') << std::hex << m_helper.getUInt(buffer, size) << std::dec << "]\n";
  54. }
  55. else if (m_CurrentID == EBML_Identifier_EBMLIdLength)
  56. {
  57. g_OutputStream << "Got EBML ID length : [0x" << std::setw(16) << std::setfill('0') << std::hex << m_helper.getUInt(buffer, size) << std::dec <<
  58. "]\n";
  59. }
  60. else if (m_CurrentID == EBML_Identifier_DocTypeVersion)
  61. {
  62. g_OutputStream << "Got doc type version : [0x" << std::setw(16) << std::setfill('0') << std::hex << m_helper.getUInt(buffer, size) << std::dec <<
  63. "]\n";
  64. }
  65. else if (m_CurrentID == EBML_Identifier_DocTypeReadVersion)
  66. {
  67. g_OutputStream << "Got doc type read version : [0x" << std::setw(16) << std::setfill('0') << std::hex << m_helper.getUInt(buffer, size) << std::dec
  68. << "]\n";
  69. }
  70. else if (m_CurrentID == EBML::CIdentifier(0x1234))
  71. {
  72. g_OutputStream << "Got uinteger : [0x" << std::setw(16) << std::setfill('0') << std::hex << m_helper.getUInt(buffer, size) << std::dec << "]\n";
  73. }
  74. else if (m_CurrentID == EBML::CIdentifier(0xffffffffffffffffLL))
  75. {
  76. g_OutputStream << "Got uinteger : [0x" << std::setw(16) << std::setfill('0') << std::hex << m_helper.getUInt(buffer, size) << std::dec << "]\n";
  77. }
  78. else if (m_CurrentID == EBML::CIdentifier(0x4321)) { g_OutputStream << "Got double : [" << m_helper.getDouble(buffer, size) << "]\n"; }
  79. else if (m_CurrentID == EBML::CIdentifier(0x8765)) { g_OutputStream << "Got float : [" << m_helper.getDouble(buffer, size) << "]\n"; }
  80. else { g_OutputStream << "Got " << size << " data bytes, node id not known\n"; }
  81. }
  82. void closeChild() override
  83. {
  84. m_Depth--;
  85. for (int i = 0; i < m_Depth; ++i) { g_OutputStream << " "; }
  86. g_OutputStream << "Node closed\n";
  87. }
  88. private:
  89. int m_Depth = 0;
  90. EBML::CReaderHelper m_helper;
  91. EBML::CIdentifier m_CurrentID;
  92. };
  93. int uoEBMLReaderTest(int argc, char* argv[])
  94. {
  95. OVT_ASSERT(argc == 3, "Failure to retrieve tests arguments. Expecting: data_dir output_dir");
  96. std::string dataFile = std::string(argv[1]) + "ref_data.ebml";
  97. std::string expectedFile = std::string(argv[1]) + "ref_result.txt";
  98. std::string outputFile = std::string(argv[2]) + "uoEBMLReaderTest.txt";
  99. // The test parses a known ebml file,
  100. // writes the results into a text file and compares the output
  101. // text file to a reference text file.
  102. g_OutputStream.open(outputFile);
  103. OVT_ASSERT(g_OutputStream.is_open(), "Failure to open output file for writing");
  104. // parsing
  105. for (size_t n = 17; n >= 1; n--)
  106. {
  107. CReaderCallBack callback;
  108. EBML::CReader reader(callback);
  109. g_OutputStream << "testing with n=" << n << std::endl;
  110. FILE* file = fopen(dataFile.c_str(), "rb");
  111. OVT_ASSERT(file != nullptr, "Failure to open data file for reading");
  112. unsigned char* c = new unsigned char[n];
  113. size_t i = 0;
  114. while (!feof(file))
  115. {
  116. i = fread(c, 1, n * sizeof(unsigned char), file);
  117. reader.processData(c, i);
  118. }
  119. delete[] c;
  120. fclose(file);
  121. }
  122. g_OutputStream.close();
  123. // comparison part
  124. std::ifstream generatedStream(outputFile);
  125. std::ifstream expectedStream(expectedFile);
  126. OVT_ASSERT(generatedStream.is_open(), "Failure to open generated results for reading");
  127. OVT_ASSERT(expectedStream.is_open(), "Failure to open expected results for reading");
  128. std::string generatedString;
  129. std::string expectedString;
  130. while (std::getline(expectedStream, expectedString))
  131. {
  132. OVT_ASSERT(std::getline(generatedStream, generatedString), "Failure to retrieve a line to match");
  133. OVT_ASSERT_STREQ(expectedString, generatedString, "Failure to match expected line to generated line");
  134. }
  135. // last check to verify the expected file has no additional line
  136. OVT_ASSERT(!std::getline(generatedStream, generatedString), "Failure to match expected file size and generated file size");
  137. return EXIT_SUCCESS;
  138. }