TestFLACSpec.cpp 2.7 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 <LibCore/Directory.h>
  8. #include <LibMedia/Audio/FlacLoader.h>
  9. #include <LibTest/TestCase.h>
  10. struct DiscoverFLACTestsHack {
  11. DiscoverFLACTestsHack()
  12. {
  13. // FIXME: Also run (our own) tests in this directory.
  14. (void)Core::Directory::for_each_entry("./FLAC/SpecTests"sv, Core::DirIterator::Flags::SkipParentAndBaseDir, [](auto const& entry, auto const& directory) -> ErrorOr<IterationDecision> {
  15. auto path = LexicalPath::join(directory.path().string(), entry.name);
  16. if (path.extension() == "flac"sv) {
  17. Test::add_test_case_to_suite(adopt_ref(*new ::Test::TestCase(
  18. ByteString::formatted("flac_spec_test_{}", path.basename()),
  19. [path = move(path)]() {
  20. auto file = Core::File::open(path.string(), Core::File::OpenMode::Read);
  21. if (file.is_error()) {
  22. FAIL(ByteString::formatted("{}", file.error()));
  23. return;
  24. }
  25. auto buffered_file = Core::InputBufferedFile::create(file.release_value());
  26. if (buffered_file.is_error()) {
  27. FAIL(ByteString::formatted("{}", buffered_file.error()));
  28. return;
  29. }
  30. auto result = Audio::FlacLoaderPlugin::create(buffered_file.release_value());
  31. if (result.is_error()) {
  32. FAIL(ByteString::formatted("{}", result.error()));
  33. return;
  34. }
  35. auto loader = result.release_value();
  36. while (true) {
  37. auto maybe_samples = loader->load_chunks(2 * MiB);
  38. if (maybe_samples.is_error()) {
  39. FAIL(ByteString::formatted("{}", maybe_samples.error()));
  40. return;
  41. }
  42. maybe_samples.value().remove_all_matching([](auto& chunk) { return chunk.is_empty(); });
  43. if (maybe_samples.value().is_empty())
  44. return;
  45. }
  46. },
  47. false)));
  48. }
  49. return IterationDecision::Continue;
  50. });
  51. }
  52. };
  53. // Hack taken from TEST_CASE; the above constructor will run as part of global initialization before the tests are actually executed
  54. static struct DiscoverFLACTestsHack hack;