Skip to content

Commit

Permalink
add a test to write/read an EBML header
Browse files Browse the repository at this point in the history
  • Loading branch information
robUx4 committed Jan 1, 2024
1 parent 4eab5f1 commit eab0508
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ if (BUILD_TESTING)
target_link_libraries(test_id PUBLIC ebml)
add_test(test_id test_id)

add_executable(test_header test/test_header.cxx)
target_link_libraries(test_header PUBLIC ebml)
add_test(test_header test_header)

add_executable(test_infinite test/test_infinite.cxx)
target_link_libraries(test_infinite PUBLIC ebml)
add_test(test_infinite test_infinite)
Expand Down
59 changes: 59 additions & 0 deletions test/test_header.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright © 2024 Steve Lhomme.
// SPDX-License-Identifier: ISC

#include <ebml/EbmlHead.h>
#include <ebml/EbmlSubHead.h>
#include <ebml/EbmlMaster.h>
#include <ebml/EbmlStream.h>
#include <ebml/MemIOCallback.h>

int main(int /*argc*/, char** /*argv*/)
{
///// Writing test
libebml::MemIOCallback Ebml_file;
libebml::EbmlHead TestHead;

libebml::EDocType & MyDocType = libebml::GetChild<libebml::EDocType>(TestHead);
MyDocType.SetValue("matroska");

libebml::EDocTypeVersion & MyDocTypeVer = libebml::GetChild<libebml::EDocTypeVersion>(TestHead);
MyDocTypeVer.SetValue(1);

libebml::GetChild<libebml::EDocTypeReadVersion>(TestHead).SetValue(1);

libebml::GetChild<libebml::EMaxSizeLength>(TestHead).SetValue(8);

auto length = TestHead.Render(Ebml_file, libebml::EbmlElement::WriteAll);
if (length != 40)
return 1;

///// Reading test
Ebml_file.setFilePointer(0);
libebml::EbmlStream aStream(Ebml_file);
libebml::EbmlElement * ElementLevel0;

// find the EBML head in the file
ElementLevel0 = aStream.FindNextID(EBML_INFO(libebml::EbmlHead), 0xFFFFFFFFL);
if (ElementLevel0 == nullptr)
return 1;

if (ElementLevel0->GetClassId() != libebml::EbmlHead::ClassId())
return 1;

libebml::EbmlHead &ReadHead = static_cast<libebml::EbmlHead &>(*ElementLevel0);

auto read = ReadHead.ReadData(Ebml_file, libebml::SCOPE_ALL_DATA);
if (read != 35)
return 1;

libebml::EDocType & ReadDocType = libebml::GetChild<libebml::EDocType>(ReadHead);
const std::string & DocTypeStr = static_cast<const std::string &>(ReadDocType);
if (DocTypeStr != "matroska")
return 1;

auto & ReadMaxLength = libebml::GetChild<const libebml::EMaxSizeLength>(ReadHead);
if (static_cast<std::uint64_t>(ReadMaxLength) != 8)
return 1;

return 0;
}

0 comments on commit eab0508

Please sign in to comment.