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 d148118
Show file tree
Hide file tree
Showing 2 changed files with 72 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
68 changes: 68 additions & 0 deletions test/test_header.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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("webm");

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

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

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

auto length = TestHead.Render(Ebml_file, libebml::EbmlElement::WriteAll);
if (length != 36)
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);

int upper = 0;
ElementLevel0 = nullptr;
ReadHead.Read(aStream, EBML_CONTEXT(&ReadHead), upper, ElementLevel0, false);
if (ElementLevel0 != nullptr)
return 1;

if (upper != 0)
return 1;

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

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

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

return 0;
}

0 comments on commit d148118

Please sign in to comment.