webm2pes: Begin addition of tests.

Just the basics:
- Some new test utilities.
- Basic setup for webm2pes testing.

Change-Id: I16bf0f5ef36e7c01f2788b2c92600d6a936bbd40
This commit is contained in:
Tom Finegan
2016-01-21 11:50:37 -08:00
parent 9299bbb6ed
commit 453bf44d32
6 changed files with 139 additions and 1 deletions

View File

@@ -90,4 +90,14 @@ if (ENABLE_TESTS)
"${LIBWEBM_SRC_DIR}/testing/test_util.cc"
"${LIBWEBM_SRC_DIR}/testing/test_util.h")
target_link_libraries(parser_tests LINK_PUBLIC webm gtest)
add_executable(webm2pes_tests
"${LIBWEBM_SRC_DIR}/common/libwebm_utils.cc"
"${LIBWEBM_SRC_DIR}/common/libwebm_utils.h"
"${LIBWEBM_SRC_DIR}/testing/test_util.cc"
"${LIBWEBM_SRC_DIR}/testing/test_util.h"
"${LIBWEBM_SRC_DIR}/webm2pes.cc"
"${LIBWEBM_SRC_DIR}/webm2pes.h"
"${LIBWEBM_SRC_DIR}/testing/webm2pes_tests.cc")
target_link_libraries(webm2pes_tests LINK_PUBLIC webm gtest)
endif (ENABLE_TESTS)

View File

@@ -7,9 +7,13 @@
// be found in the AUTHORS file in the root of the source tree.
#include "testing/test_util.h"
#include <sys/stat.h>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <ios>
#include <memory>
#include <string>
@@ -18,7 +22,9 @@
namespace libwebm {
namespace test {
std::string GetTestDataDir() { return std::getenv("LIBWEBM_TEST_DATA_PATH"); }
std::string GetTestDataDir() {
return std::getenv("LIBWEBM_TEST_DATA_PATH");
}
std::string GetTestFilePath(const std::string& name) {
const std::string libwebm_testdata_dir = GetTestDataDir();
@@ -50,5 +56,37 @@ bool CompareFiles(const std::string& file1, const std::string& file2) {
return std::feof(f1.get()) && std::feof(f2.get());
}
std::string GetTempFileName() {
// TODO(tomfinegan): This is only test code, but it would be nice to avoid
// using std::tmpnam().
return std::tmpnam(nullptr);
}
std::uint64_t GetFileSize(const std::string& file_name) {
std::uint64_t file_size = 0;
#ifndef _MSC_VER
struct stat st = {0};
if (stat(file_name.c_str(), &st) == 0) {
#else
struct _stat st = {0};
if (_stat(file_name.c_str(), &st) == 0) {
#endif
file_size = st.st_size;
}
return file_size;
}
TempFileDeleter::TempFileDeleter() {
file_name_ = GetTempFileName();
}
TempFileDeleter::~TempFileDeleter() {
std::ifstream file(file_name_);
if (file.good()) {
file.close();
std::remove(file_name_.c_str());
}
}
} // namespace test
} // namespace libwebm

View File

@@ -51,6 +51,27 @@ std::string GetTestFilePath(const std::string& name);
// files match exactly, false otherwise.
bool CompareFiles(const std::string& file1, const std::string& file2);
// Returns a temporary file name.
std::string GetTempFileName();
// Returns size of file specified by |file_name|, or 0 upon failure.
std::uint64_t GetFileSize(const std::string& file_name);
// Manages life of temporary file specified at time of construction. Deletes
// file upon destruction.
class TempFileDeleter {
public:
TempFileDeleter();
explicit TempFileDeleter(std::string file_name) : file_name_(file_name) {}
~TempFileDeleter();
TempFileDeleter(const TempFileDeleter&) = delete;
TempFileDeleter(TempFileDeleter&&) = delete;
const std::string& name() const { return file_name_; }
private:
std::string file_name_;
};
} // namespace test
} // namespace libwebm

65
testing/webm2pes_tests.cc Normal file
View File

@@ -0,0 +1,65 @@
// Copyright (c) 2016 The WebM project authors. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
#include "webm2pes.h"
#include <cstdint>
#include <cstdio>
#include <limits>
#include <vector>
#include "gtest/gtest.h"
#include "common/libwebm_utils.h"
#include "testing/test_util.h"
namespace {
class Webm2PesTests : public ::testing::Test {
public:
typedef std::vector<std::uint8_t> PesFileData;
Webm2PesTests() {}
bool CreateAndLoadTestInput() {
libwebm::Webm2Pes converter(input_file_name_, temp_file_name_.name());
EXPECT_TRUE(converter.ConvertToFile());
pes_file_size_ = libwebm::test::GetFileSize(pes_file_name());
EXPECT_GT(pes_file_size_, 0);
pes_file_data_.reserve(pes_file_size_);
EXPECT_EQ(pes_file_size_, pes_file_data_.capacity());
libwebm::FilePtr file = libwebm::FilePtr(
std::fopen(pes_file_name().c_str(), "rb"), libwebm::FILEDeleter());
EXPECT_EQ(std::fread(&pes_file_data_[0], 1, pes_file_size_, file.get()),
pes_file_size_);
return true;
}
~Webm2PesTests() = default;
const std::string& pes_file_name() const { return temp_file_name_.name(); }
std::uint64_t pes_file_size() const { return pes_file_size_; }
const PesFileData& pes_file_data() const { return pes_file_data_; }
private:
const libwebm::test::TempFileDeleter temp_file_name_;
const std::string input_file_name_ =
libwebm::test::GetTestFilePath("bbb_480p_vp9_opus_1second.webm");
std::uint64_t pes_file_size_ = 0;
PesFileData pes_file_data_;
};
TEST_F(Webm2PesTests, CreatePesFile) {
EXPECT_TRUE(CreateAndLoadTestInput());
}
TEST_F(Webm2PesTests, CanParseFirstPacket) {
EXPECT_TRUE(CreateAndLoadTestInput());
}
} // namespace
int main(int argc, char* argv[]) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -302,6 +302,7 @@ bool Webm2Pes::ConvertToFile() {
cluster = webm_parser_->GetNext(cluster);
}
std::fflush(output_file_.get());
return true;
}

View File

@@ -195,6 +195,9 @@ class Webm2Pes {
// Input timecode scale.
std::int64_t timecode_scale_ = 1000000;
};
} // namespace libwebm
#endif // LIBWEBM_WEBM2PES_H_