From 4fc1daa69fbfbfa8c5751c4443327192866f7544 Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Fri, 27 Oct 2023 11:47:43 +0200 Subject: [PATCH] LibTest: Change #define-d constants into constexpr and a runtime flag MAX_GENERATED_VALUES_PER_TEST is now the --randomized_runs flag: $ ./Build/lagom/bin/TestGenerator --randomized_runs 1000 It's sometimes useful to try larger numbers for it instead of the default of 100. MAX_GEN_ATTEMPTS_PER_VALUE is now a constexpr. It's not usually needed to tweak this value; we can recompile with a different value on the rare occasion. --- Userland/Libraries/LibTest/Macros.h | 2 ++ Userland/Libraries/LibTest/TestCase.h | 23 +++++++++-------------- Userland/Libraries/LibTest/TestSuite.cpp | 10 ++++++++-- Userland/Libraries/LibTest/TestSuite.h | 6 ++++-- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/Userland/Libraries/LibTest/Macros.h b/Userland/Libraries/LibTest/Macros.h index 5f2f3cdd24..23d136f9f4 100644 --- a/Userland/Libraries/LibTest/Macros.h +++ b/Userland/Libraries/LibTest/Macros.h @@ -30,6 +30,8 @@ void set_randomness_source(Randomized::RandomnessSource); bool is_reporting_enabled(); void enable_reporting(); void disable_reporting(); + +u64 randomized_runs(); } #define EXPECT_EQ(a, b) \ diff --git a/Userland/Libraries/LibTest/TestCase.h b/Userland/Libraries/LibTest/TestCase.h index fa55210919..5e2057878b 100644 --- a/Userland/Libraries/LibTest/TestCase.h +++ b/Userland/Libraries/LibTest/TestCase.h @@ -7,22 +7,13 @@ #pragma once -#include // intentionally first -- we redefine VERIFY and friends in here -#include -#include - #include #include #include #include - -#ifndef MAX_GENERATED_VALUES_PER_TEST -# define MAX_GENERATED_VALUES_PER_TEST 100 -#endif - -#ifndef MAX_GEN_ATTEMPTS_PER_VALUE -# define MAX_GEN_ATTEMPTS_PER_VALUE 15 -#endif +#include +#include +#include namespace Test { @@ -54,8 +45,12 @@ public: static NonnullRefPtr randomized(DeprecatedString const& name, TestFunction&& test_function) { using namespace Randomized; + + constexpr u8 MAX_GEN_ATTEMPTS_PER_VALUE = 15; + TestFunction test_case_function = [test_function = move(test_function)]() { - for (u32 i = 0; i < MAX_GENERATED_VALUES_PER_TEST; ++i) { + u64 max_randomized_runs = randomized_runs(); + for (u64 i = 0; i < max_randomized_runs; ++i) { bool generated_successfully = false; u8 gen_attempt; for (gen_attempt = 0; gen_attempt < MAX_GEN_ATTEMPTS_PER_VALUE && !generated_successfully; ++gen_attempt) { @@ -100,7 +95,7 @@ public: return; } } - // MAX_GENERATED_VALUES_PER_TEST values generated, all passed the test. + // All randomized_runs() values generated + passed the test. }; return make_ref_counted(name, move(test_case_function), false); } diff --git a/Userland/Libraries/LibTest/TestSuite.cpp b/Userland/Libraries/LibTest/TestSuite.cpp index 53cd499932..632f14f8ba 100644 --- a/Userland/Libraries/LibTest/TestSuite.cpp +++ b/Userland/Libraries/LibTest/TestSuite.cpp @@ -5,10 +5,9 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include // intentionally first -- we redefine VERIFY and friends in here - #include #include +#include #include #include #include @@ -64,6 +63,12 @@ Randomized::RandomnessSource& randomness_source() return TestSuite::the().randomness_source(); } +// Declared in Macros.h +u64 randomized_runs() +{ + return TestSuite::the().randomized_runs(); +} + // Declared in TestCase.h void add_test_case_to_suite(NonnullRefPtr const& test_case) { @@ -126,6 +131,7 @@ int TestSuite::main(DeprecatedString const& suite_name, Span argumen args_parser.add_option(do_tests_only, "Only run tests.", "tests", 0); args_parser.add_option(do_benchmarks_only, "Only run benchmarks.", "bench", 0); args_parser.add_option(m_benchmark_repetitions, "Number of times to repeat each benchmark (default 1)", "benchmark_repetitions", 0, "N"); + args_parser.add_option(m_randomized_runs, "Number of times to run each RANDOMIZED_TEST_CASE (default 100)", "randomized_runs", 0, "RUNS"); args_parser.add_option(do_list_cases, "List available test cases.", "list", 0); args_parser.add_positional_argument(search_string, "Only run matching cases.", "pattern", Core::ArgsParser::Required::No); args_parser.parse(arguments); diff --git a/Userland/Libraries/LibTest/TestSuite.h b/Userland/Libraries/LibTest/TestSuite.h index f4816bb1c2..5080e39a4f 100644 --- a/Userland/Libraries/LibTest/TestSuite.h +++ b/Userland/Libraries/LibTest/TestSuite.h @@ -7,11 +7,10 @@ #pragma once -#include // intentionally first -- we redefine VERIFY and friends in here - #include #include #include +#include #include #include #include @@ -61,6 +60,8 @@ public: void enable_reporting() { m_reporting_enabled = true; } void disable_reporting() { m_reporting_enabled = false; } + u64 randomized_runs() { return m_randomized_runs; } + private: static TestSuite* s_global; Vector> m_cases; @@ -68,6 +69,7 @@ private: u64 m_benchtime = 0; DeprecatedString m_suite_name; u64 m_benchmark_repetitions = 1; + u64 m_randomized_runs = 100; Function m_setup; TestResult m_current_test_result = TestResult::NotRun; Randomized::RandomnessSource m_randomness_source = Randomized::RandomnessSource::live();