TestFLACSpec.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 <AK/Types.h>
  8. #include <LibAudio/FlacLoader.h>
  9. #include <LibCore/DirIterator.h>
  10. #include <LibCore/Stream.h>
  11. #include <LibTest/TestCase.h>
  12. struct FlacTest : Test::TestCase {
  13. FlacTest(LexicalPath path)
  14. : Test::TestCase(
  15. String::formatted("flac_spec_test_{}", path.basename()), [this]() { run(); }, false)
  16. , m_path(std::move(path))
  17. {
  18. }
  19. void run() const
  20. {
  21. auto result = Audio::FlacLoaderPlugin::try_create(m_path.string());
  22. if (result.is_error()) {
  23. FAIL(String::formatted("{} (at {})", result.error().description, result.error().index));
  24. return;
  25. }
  26. auto loader = result.release_value();
  27. while (true) {
  28. auto maybe_samples = loader->get_more_samples(2 * MiB);
  29. if (maybe_samples.is_error()) {
  30. FAIL(String::formatted("{} (at {})", maybe_samples.error().description, maybe_samples.error().index));
  31. return;
  32. }
  33. if (maybe_samples.value().is_empty())
  34. return;
  35. }
  36. }
  37. LexicalPath m_path;
  38. };
  39. struct DiscoverFLACTestsHack {
  40. DiscoverFLACTestsHack()
  41. {
  42. // FIXME: Also run (our own) tests in this directory.
  43. auto test_iterator = Core::DirIterator { "./SpecTests", Core::DirIterator::Flags::SkipParentAndBaseDir };
  44. while (test_iterator.has_next()) {
  45. auto file = LexicalPath { test_iterator.next_full_path() };
  46. if (file.extension() == "flac"sv) {
  47. Test::add_test_case_to_suite(make_ref_counted<FlacTest>(move(file)));
  48. }
  49. }
  50. }
  51. };
  52. // Hack taken from TEST_CASE; the above constructor will run as part of global initialization before the tests are actually executed
  53. static struct DiscoverFLACTestsHack hack;