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.

uoEBMLWriterTest.cpp 4.4KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <iomanip>
  22. #include <fstream>
  23. #include <cstdio>
  24. #include "ebml/defines.h"
  25. #include "ebml/IWriter.h"
  26. #include "ebml/CWriterHelper.h"
  27. #include "ovtAssert.h"
  28. #include <iostream>
  29. #define OVP_NodeId_OpenViBEStream_Header EBML::CIdentifier(0xF59505AB, 0x3684C8D8)
  30. #define OVP_NodeId_OpenViBEStream_Header_Compression EBML::CIdentifier(0x40358769, 0x166380D1)
  31. #define OVP_NodeId_OpenViBEStream_Header_StreamType EBML::CIdentifier(0x732EC1D1, 0xFE904087)
  32. #define OVP_NodeId_OpenViBEStream_Buffer EBML::CIdentifier(0x2E60AD18, 0x87A29BDF)
  33. #define OVP_NodeId_OpenViBEStream_Buffer_StreamIndex EBML::CIdentifier(0x30A56D8A, 0xB9C12238)
  34. #define OVP_NodeId_OpenViBEStream_Buffer_StartTime EBML::CIdentifier(0x093E6A0A, 0xC5A9467B)
  35. #define OVP_NodeId_OpenViBEStream_Buffer_EndTime EBML::CIdentifier(0x8B5CCCD9, 0xC5024F29)
  36. #define OVP_NodeId_OpenViBEStream_Buffer_Content EBML::CIdentifier(0x8D4B0BE8, 0x7051265C)
  37. #ifndef M_PI
  38. #define M_PI 3.1415926535897932384626433832795
  39. #endif
  40. class CWriterCallBack : public EBML::IWriterCallBack
  41. {
  42. public:
  43. CWriterCallBack(const char* filename) { m_file = std::fopen(filename, "wb"); }
  44. ~CWriterCallBack() override { if (m_file) { std::fclose(m_file); } } // in case release is not called
  45. void write(const void* buffer, const size_t size) override { if (m_file) { std::fwrite(buffer, size_t(size), 1, m_file); } }
  46. // necessary thjor the test to close the stream and re-open it for inspection
  47. void release()
  48. {
  49. std::fclose(m_file);
  50. m_file = nullptr;
  51. }
  52. private:
  53. std::FILE* m_file{ nullptr };
  54. };
  55. int uoEBMLWriterTest(int argc, char* argv[])
  56. {
  57. OVT_ASSERT(argc == 3, "Failure to retrieve tests arguments. Expecting: data_dir output_dir");
  58. const std::string expectedFile = std::string(argv[1]) + "ref_data.ebml";
  59. const std::string outputFile = std::string(argv[2]) + "uoEBMLWriterTest.ebml";
  60. // The test serializes a known ebml sequence and compares the output
  61. // to a reference.
  62. // serializing
  63. CWriterCallBack callback(outputFile.c_str());
  64. EBML::IWriter* writer = createWriter(callback);
  65. EBML::CWriterHelper helper;
  66. helper.connect(writer);
  67. helper.openChild(EBML_Identifier_Header);
  68. helper.openChild(EBML_Identifier_DocType);
  69. helper.setStr("matroska");
  70. helper.closeChild();
  71. helper.openChild(EBML_Identifier_DocTypeVersion);
  72. helper.setUInt(1);
  73. helper.closeChild();
  74. helper.openChild(EBML_Identifier_DocTypeReadVersion);
  75. helper.setInt(655356);
  76. helper.closeChild();
  77. helper.closeChild();
  78. helper.openChild(0x1234);
  79. helper.setUInt(0);
  80. helper.closeChild();
  81. helper.openChild(0xffffffffffffffffLL);
  82. helper.setUInt(0xff000000ff000000LL);
  83. helper.closeChild();
  84. helper.openChild(0x4321);
  85. helper.setDouble(M_PI);
  86. helper.closeChild();
  87. helper.openChild(0x8765);
  88. helper.setFloat(float(M_PI));
  89. helper.closeChild();
  90. writer->release();
  91. callback.release();
  92. // comparison part
  93. std::ifstream generatedStream(outputFile, std::ios::binary);
  94. std::ifstream expectedStream(expectedFile, std::ios::binary);
  95. OVT_ASSERT(generatedStream.is_open(), "Failure to open generated ebml stream for reading");
  96. OVT_ASSERT(expectedStream.is_open(), "Failure to open reference stream for reading");
  97. char generatedChar;
  98. char expectedChar;
  99. while (expectedStream.get(expectedChar))
  100. {
  101. OVT_ASSERT(generatedStream.get(generatedChar), "Failure to retrieve a char to match");
  102. OVT_ASSERT(generatedChar == expectedChar, "Failure to match expected char to generated char");
  103. }
  104. // last check to verify the expected file has no additional line
  105. OVT_ASSERT(!generatedStream.get(generatedChar), "Failure to match expected file size and generated file size");
  106. return EXIT_SUCCESS;
  107. }