aboutsummaryrefslogtreecommitdiff
path: root/tests/utils.cc
diff options
context:
space:
mode:
Diffstat (limited to 'tests/utils.cc')
-rw-r--r--tests/utils.cc77
1 files changed, 77 insertions, 0 deletions
diff --git a/tests/utils.cc b/tests/utils.cc
index b73cf01..e91ea87 100644
--- a/tests/utils.cc
+++ b/tests/utils.cc
@@ -21,6 +21,7 @@
#include <memory>
#include <string>
+#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "gtest/gtest.h"
#include "src/dsp/dsp.h"
@@ -30,6 +31,30 @@
namespace libgav1 {
namespace test_utils {
+namespace {
+
+int CloseFile(FILE* stream) { return fclose(stream); }
+
+bool ReadFileToString(absl::string_view file_name, std::string* const string) {
+ using FilePtr = std::unique_ptr<FILE, decltype(&CloseFile)>;
+ FilePtr file(fopen(std::string(file_name).c_str(), "rb"), &CloseFile);
+ if (file == nullptr) return false;
+
+ do {
+ int c = fgetc(file.get());
+ if (ferror(file.get()) != 0) return false;
+
+ if (c != EOF) {
+ string->append(1, static_cast<char>(c));
+ } else {
+ break;
+ }
+ } while (true);
+
+ return true;
+}
+
+} // namespace
void ResetDspTable(const int bitdepth) {
dsp::Dsp* const dsp = dsp_internal::GetWritableDspTable(bitdepth);
@@ -116,5 +141,57 @@ void CheckMd5Digest(const char name[], const char function_name[],
EXPECT_STREQ(expected_digest, actual_digest);
}
+namespace {
+
+std::string GetSourceDir() {
+#if defined(__ANDROID__)
+ // Test files must be manually supplied. This path is frequently
+ // available on development devices.
+ return std::string("/data/local/tmp/tests/data");
+#elif defined(LIBGAV1_FLAGS_SRCDIR)
+ return std::string(LIBGAV1_FLAGS_SRCDIR) + "/tests/data";
+#else
+ return std::string(".");
+#endif // defined(__ANDROID__)
+}
+
+std::string GetTempDir() {
+ const char* path = getenv("TMPDIR");
+ if (path == nullptr || path[0] == '\0') path = getenv("TEMP");
+ if (path != nullptr && path[0] != '\0') return std::string(path);
+
+#if defined(__ANDROID__)
+ return std::string("/data/local/tmp");
+#elif defined(LIBGAV1_FLAGS_TMPDIR)
+ return std::string(LIBGAV1_FLAGS_TMPDIR);
+#else
+ return std::string(".");
+#endif // defined(__ANDROID__)
+}
+
+} // namespace
+
+std::string GetTestInputFilePath(absl::string_view file_name) {
+ const char* const path = getenv("LIBGAV1_TEST_DATA_PATH");
+ if (path != nullptr && path[0] != '\0') {
+ return std::string(path) + "/" + std::string(file_name);
+ }
+ return GetSourceDir() + "/" + std::string(file_name);
+}
+
+std::string GetTestOutputFilePath(absl::string_view file_name) {
+ return GetTempDir() + "/" + std::string(file_name);
+}
+
+void GetTestData(absl::string_view file_name, const bool is_output_file,
+ std::string* const output) {
+ ASSERT_NE(output, nullptr);
+ const std::string absolute_file_path = is_output_file
+ ? GetTestOutputFilePath(file_name)
+ : GetTestInputFilePath(file_name);
+
+ ASSERT_TRUE(ReadFileToString(absolute_file_path, output));
+}
+
} // namespace test_utils
} // namespace libgav1