2021-04-25 05:53:23 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2021-04-25 05:53:23 +00:00
|
|
|
* Copyright (c) 2021, Andrew Kaster <akaster@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2021-04-25 05:53:23 +00:00
|
|
|
#include <AK/Function.h>
|
2023-03-06 13:47:32 +00:00
|
|
|
#include <AK/Vector.h>
|
2023-10-27 09:47:43 +00:00
|
|
|
#include <LibTest/Macros.h>
|
2023-10-23 22:35:15 +00:00
|
|
|
#include <LibTest/Randomized/RandomnessSource.h>
|
2021-04-25 05:53:23 +00:00
|
|
|
#include <LibTest/TestCase.h>
|
2023-10-23 22:19:04 +00:00
|
|
|
#include <LibTest/TestResult.h>
|
2021-04-25 05:53:23 +00:00
|
|
|
|
|
|
|
namespace Test {
|
|
|
|
|
|
|
|
class TestSuite {
|
|
|
|
public:
|
|
|
|
static TestSuite& the()
|
|
|
|
{
|
|
|
|
if (s_global == nullptr)
|
|
|
|
s_global = new TestSuite();
|
|
|
|
return *s_global;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void release()
|
|
|
|
{
|
|
|
|
if (s_global)
|
|
|
|
delete s_global;
|
|
|
|
s_global = nullptr;
|
|
|
|
}
|
|
|
|
|
2023-03-06 13:17:01 +00:00
|
|
|
int run(Vector<NonnullRefPtr<TestCase>> const&);
|
2023-12-16 14:19:34 +00:00
|
|
|
int main(ByteString const& suite_name, Span<StringView> arguments);
|
|
|
|
Vector<NonnullRefPtr<TestCase>> find_cases(ByteString const& search, bool find_tests, bool find_benchmarks);
|
2022-04-01 17:58:27 +00:00
|
|
|
void add_case(NonnullRefPtr<TestCase> const& test_case)
|
2021-04-25 05:53:23 +00:00
|
|
|
{
|
|
|
|
m_cases.append(test_case);
|
|
|
|
}
|
|
|
|
|
2023-10-23 22:19:04 +00:00
|
|
|
TestResult current_test_result() const { return m_current_test_result; }
|
|
|
|
void set_current_test_result(TestResult result) { m_current_test_result = result; }
|
2021-04-25 05:53:23 +00:00
|
|
|
|
2021-08-29 18:55:05 +00:00
|
|
|
void set_suite_setup(Function<void()> setup) { m_setup = move(setup); }
|
2023-10-23 22:35:15 +00:00
|
|
|
// The RandomnessSource is where generators record / replay random data
|
|
|
|
// from. Initially a live "truly random" RandomnessSource is used, and when
|
|
|
|
// a failure is found, a set of hardcoded RandomnessSources is used during
|
|
|
|
// shrinking.
|
|
|
|
void set_randomness_source(Randomized::RandomnessSource source) { m_randomness_source = move(source); }
|
|
|
|
Randomized::RandomnessSource& randomness_source() { return m_randomness_source; }
|
2021-08-29 18:55:05 +00:00
|
|
|
|
2023-10-23 23:26:28 +00:00
|
|
|
// Dictates whether FAIL(), EXPECT() and similar macros in LibTest/Macros.h
|
|
|
|
// print messages or not. This is important for randomized tests because
|
|
|
|
// they run the test function many times in a row, and we only want to
|
|
|
|
// report the _minimal_ (shrunk) failure to the user, not all of them.
|
|
|
|
bool is_reporting_enabled() { return m_reporting_enabled; }
|
|
|
|
void enable_reporting() { m_reporting_enabled = true; }
|
|
|
|
void disable_reporting() { m_reporting_enabled = false; }
|
|
|
|
|
2023-10-27 09:47:43 +00:00
|
|
|
u64 randomized_runs() { return m_randomized_runs; }
|
|
|
|
|
2021-04-25 05:53:23 +00:00
|
|
|
private:
|
|
|
|
static TestSuite* s_global;
|
2023-03-06 13:17:01 +00:00
|
|
|
Vector<NonnullRefPtr<TestCase>> m_cases;
|
2021-04-25 05:53:23 +00:00
|
|
|
u64 m_testtime = 0;
|
|
|
|
u64 m_benchtime = 0;
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString m_suite_name;
|
2023-03-08 22:04:59 +00:00
|
|
|
u64 m_benchmark_repetitions = 1;
|
2023-10-27 09:47:43 +00:00
|
|
|
u64 m_randomized_runs = 100;
|
2021-08-29 18:55:05 +00:00
|
|
|
Function<void()> m_setup;
|
2023-10-23 22:19:04 +00:00
|
|
|
TestResult m_current_test_result = TestResult::NotRun;
|
2023-10-23 22:35:15 +00:00
|
|
|
Randomized::RandomnessSource m_randomness_source = Randomized::RandomnessSource::live();
|
2023-10-23 23:26:28 +00:00
|
|
|
bool m_reporting_enabled = true;
|
2021-04-25 05:53:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|