1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:07:35 +00:00

Tests: Refactor FLAC spec test to not use a TestCase subclass

The deallocation of the test cases at the very end happens through a
NonnullRefPtr<TestCase>, meaning the deallocation will assume the wrong
object size and trip up ASAN. Therefore, we cannot use a TestCase
subclass.

I also took this opportunity and made use of the new LoaderError
formatter.
This commit is contained in:
kleines Filmröllchen 2023-03-14 21:50:20 +01:00 committed by Linus Groh
parent 904a4dd314
commit 73a367a00a

View file

@ -9,19 +9,19 @@
#include <LibCore/Directory.h> #include <LibCore/Directory.h>
#include <LibTest/TestCase.h> #include <LibTest/TestCase.h>
struct FlacTest : Test::TestCase { struct DiscoverFLACTestsHack {
FlacTest(LexicalPath path) DiscoverFLACTestsHack()
: Test::TestCase(
DeprecatedString::formatted("flac_spec_test_{}", path.basename()), [this]() { run(); }, false)
, m_path(move(path))
{ {
} // FIXME: Also run (our own) tests in this directory.
(void)Core::Directory::for_each_entry("./SpecTests"sv, Core::DirIterator::Flags::SkipParentAndBaseDir, [](auto const& entry, auto const& directory) -> ErrorOr<IterationDecision> {
void run() const auto path = LexicalPath::join(directory.path().string(), entry.name);
{ if (path.extension() == "flac"sv) {
auto result = Audio::FlacLoaderPlugin::create(m_path.string()); Test::add_test_case_to_suite(adopt_ref(*new ::Test::TestCase(
DeprecatedString::formatted("flac_spec_test_{}", path.basename()),
[path = move(path)]() {
auto result = Audio::FlacLoaderPlugin::create(path.string());
if (result.is_error()) { if (result.is_error()) {
FAIL(DeprecatedString::formatted("{} (at {})", result.error().description, result.error().index)); FAIL(DeprecatedString::formatted("{}", result.error()));
return; return;
} }
@ -30,26 +30,16 @@ struct FlacTest : Test::TestCase {
while (true) { while (true) {
auto maybe_samples = loader->load_chunks(2 * MiB); auto maybe_samples = loader->load_chunks(2 * MiB);
if (maybe_samples.is_error()) { if (maybe_samples.is_error()) {
FAIL(DeprecatedString::formatted("{} (at {})", maybe_samples.error().description, maybe_samples.error().index)); FAIL(DeprecatedString::formatted("{}", maybe_samples.error()));
return; return;
} }
maybe_samples.value().remove_all_matching([](auto& chunk) { return chunk.is_empty(); }); maybe_samples.value().remove_all_matching([](auto& chunk) { return chunk.is_empty(); });
if (maybe_samples.value().is_empty()) if (maybe_samples.value().is_empty())
return; return;
} }
},
false)));
} }
LexicalPath m_path;
};
struct DiscoverFLACTestsHack {
DiscoverFLACTestsHack()
{
// FIXME: Also run (our own) tests in this directory.
(void)Core::Directory::for_each_entry("./SpecTests"sv, Core::DirIterator::Flags::SkipParentAndBaseDir, [](auto const& entry, auto const& directory) -> ErrorOr<IterationDecision> {
auto path = LexicalPath::join(directory.path().string(), entry.name);
if (path.extension() == "flac"sv)
Test::add_test_case_to_suite(make_ref_counted<FlacTest>(path));
return IterationDecision::Continue; return IterationDecision::Continue;
}); });
} }