TestFLACSpec.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/LexicalPath.h>
  7. #include <LibAudio/FlacLoader.h>
  8. #include <LibCore/Directory.h>
  9. #include <LibTest/TestCase.h>
  10. struct FlacTest : Test::TestCase {
  11. FlacTest(LexicalPath path)
  12. : Test::TestCase(
  13. DeprecatedString::formatted("flac_spec_test_{}", path.basename()), [this]() { run(); }, false)
  14. , m_path(move(path))
  15. {
  16. }
  17. void run() const
  18. {
  19. auto result = Audio::FlacLoaderPlugin::create(m_path.string());
  20. if (result.is_error()) {
  21. FAIL(DeprecatedString::formatted("{} (at {})", result.error().description, result.error().index));
  22. return;
  23. }
  24. auto loader = result.release_value();
  25. while (true) {
  26. auto maybe_samples = loader->load_chunks(2 * MiB);
  27. if (maybe_samples.is_error()) {
  28. FAIL(DeprecatedString::formatted("{} (at {})", maybe_samples.error().description, maybe_samples.error().index));
  29. return;
  30. }
  31. maybe_samples.value().remove_all_matching([](auto& chunk) { return chunk.is_empty(); });
  32. if (maybe_samples.value().is_empty())
  33. return;
  34. }
  35. }
  36. LexicalPath m_path;
  37. };
  38. struct DiscoverFLACTestsHack {
  39. DiscoverFLACTestsHack()
  40. {
  41. // FIXME: Also run (our own) tests in this directory.
  42. (void)Core::Directory::for_each_entry("./SpecTests"sv, Core::DirIterator::Flags::SkipParentAndBaseDir, [](auto const& entry, auto const& directory) -> ErrorOr<IterationDecision> {
  43. auto path = LexicalPath::join(directory.path().string(), entry.name);
  44. if (path.extension() == "flac"sv)
  45. Test::add_test_case_to_suite(make_ref_counted<FlacTest>(path));
  46. return IterationDecision::Continue;
  47. });
  48. }
  49. };
  50. // Hack taken from TEST_CASE; the above constructor will run as part of global initialization before the tests are actually executed
  51. static struct DiscoverFLACTestsHack hack;