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

AK: Make some tweaks in TestSuite.h.

This commit is contained in:
asynts 2020-08-21 18:52:28 +02:00 committed by Andreas Kling
parent 207b9774e0
commit d2121ab7c7

View file

@ -28,21 +28,33 @@
#define AK_TEST_SUITE #define AK_TEST_SUITE
#include <stdio.h> #define ASSERT(x) \
{ \
#define ASSERT(x) \ if (!(x)) \
if (!(x)) { \ fprintf(stderr, "\033[31;1mFAIL\033[0m: %s:%d: ASSERT(%s) failed\n", __FILE__, __LINE__, #x); \
fprintf(stderr, "\033[33;1mASSERT\033[0m: " #x "\n"); \
} }
#define ASSERT_NOT_REACHED() fprintf(stderr, "\033[31;1mASSERT_NOT_REACHED\033[0m\n"); #define RELEASE_ASSERT(x) \
#define RELEASE_ASSERT ASSERT { \
#define TODO ASSERT_NOT_REACHED if (!(x)) \
fprintf(stderr, "\033[31;1mFAIL\033[0m: %s:%d: RELEASE_ASSERT(%s) failed\n", __FILE__, __LINE__, #x); \
}
#define ASSERT_NOT_REACHED() \
fprintf(stderr, "\033[31;1mFAIL\033[0m: %s:%d: ASSERT_NOT_REACHED() called\n", __FILE__, __LINE__);
#define TODO \
fprintf(stderr, "\033[31;1mFAIL\033[0m: %s:%d: TODO() called\n", __FILE__, __LINE__);
#include <stdio.h>
#include <AK/Function.h> #include <AK/Function.h>
#include <AK/LogStream.h>
#include <AK/NonnullRefPtrVector.h> #include <AK/NonnullRefPtrVector.h>
#include <AK/String.h> #include <AK/String.h>
#include <LibCore/ArgsParser.h>
#include <sys/time.h> #include <sys/time.h>
namespace AK { namespace AK {
@ -68,7 +80,7 @@ private:
struct timeval m_started; struct timeval m_started;
}; };
typedef AK::Function<void()> TestFunction; using TestFunction = AK::Function<void()>;
class TestCase : public RefCounted<TestCase> { class TestCase : public RefCounted<TestCase> {
public: public:
@ -100,11 +112,12 @@ public:
static void release() static void release()
{ {
delete s_global; if (s_global)
delete s_global;
s_global = nullptr; s_global = nullptr;
} }
void run(const NonnullRefPtrVector<TestCase>& tests); void run(const NonnullRefPtrVector<TestCase>&);
void main(const String& suite_name, int argc, char** argv); void main(const String& suite_name, int argc, char** argv);
NonnullRefPtrVector<TestCase> find_cases(const String& search, bool find_tests, bool find_benchmarks); NonnullRefPtrVector<TestCase> find_cases(const String& search, bool find_tests, bool find_benchmarks);
void add_case(const NonnullRefPtr<TestCase>& test_case) void add_case(const NonnullRefPtr<TestCase>& test_case)
@ -115,47 +128,40 @@ public:
private: private:
static TestSuite* s_global; static TestSuite* s_global;
NonnullRefPtrVector<TestCase> m_cases; NonnullRefPtrVector<TestCase> m_cases;
uint64_t m_testtime = 0; u64 m_testtime = 0;
uint64_t m_benchtime = 0; u64 m_benchtime = 0;
String m_suite_name; String m_suite_name;
}; };
void TestSuite::main(const String& suite_name, int argc, char** argv) void TestSuite::main(const String& suite_name, int argc, char** argv)
{ {
m_suite_name = suite_name; m_suite_name = suite_name;
bool find_tests = true;
bool find_benchmarks = true;
String search_string; Core::ArgsParser args_parser;
for (int i = 1; i < argc; i++) {
if (!String(argv[i]).starts_with("--")) { bool do_tests_only = false;
search_string = argv[i]; bool do_benchmarks_only = false;
} else if (String(argv[i]) == String("--bench")) { bool do_list_cases = false;
find_tests = false; const char* search_string = "*";
} else if (String(argv[i]) == String("--test")) {
find_benchmarks = false; args_parser.add_option(do_tests_only, "Only run tests.", "tests", 0);
} else if (String(argv[i]) == String("--help")) { args_parser.add_option(do_benchmarks_only, "Only run benchmarks.", "bench", 0);
dbg() << "Available tests for " << suite_name << ":"; args_parser.add_option(do_list_cases, "List avaliable test cases.", "list", 0);
const auto& tests = find_cases("*", true, false); args_parser.add_positional_argument(search_string, "Only run matching cases.", "pattern", Core::ArgsParser::Required::No);
for (const auto& t : tests) { args_parser.parse(argc, argv);
dbg() << "\t" << t.name();
} const auto& matching_tests = find_cases(search_string, !do_benchmarks_only, !do_tests_only);
dbg() << "Available benchmarks for " << suite_name << ":";
const auto& benches = find_cases("*", false, true); if (do_list_cases) {
for (const auto& t : benches) { out() << "Avaliable cases for " << suite_name << ":";
dbg() << "\t" << t.name(); for (const auto& test : matching_tests) {
} out() << " " << test.name();
exit(0);
} }
} } else {
out() << "Running " << matching_tests.size() << " cases out of " << m_cases.size();
const auto& matches = find_cases(search_string, find_tests, find_benchmarks); run(matching_tests);
if (matches.size() == 0) {
dbg() << "0 matches when searching for " << search_string << " (out of " << m_cases.size() << ")";
exit(1);
} }
dbg() << "Running " << matches.size() << " cases out of " << m_cases.size();
run(matches);
} }
NonnullRefPtrVector<TestCase> TestSuite::find_cases(const String& search, bool find_tests, bool find_benchmarks) NonnullRefPtrVector<TestCase> TestSuite::find_cases(const String& search, bool find_tests, bool find_benchmarks)
@ -180,15 +186,21 @@ NonnullRefPtrVector<TestCase> TestSuite::find_cases(const String& search, bool f
void TestSuite::run(const NonnullRefPtrVector<TestCase>& tests) void TestSuite::run(const NonnullRefPtrVector<TestCase>& tests)
{ {
int test_count = 0; size_t test_count = 0;
int benchmark_count = 0; size_t benchmark_count = 0;
TestElapsedTimer global_timer; TestElapsedTimer global_timer;
for (const auto& t : tests) { for (const auto& t : tests) {
dbg() << "START Running " << (t.is_benchmark() ? "benchmark" : "test") << " " << t.name(); const auto test_type = t.is_benchmark() ? "benchmark" : "test";
dbg() << "START Running " << test_type << " " << t.name();
TestElapsedTimer timer; TestElapsedTimer timer;
t.func()(); t.func()();
auto time = timer.elapsed_milliseconds(); const auto time = timer.elapsed_milliseconds();
fprintf(stderr, "\033[32;1mPASS\033[0m: %d ms running %s %s\n", (int)time, (t.is_benchmark() ? "benchmark" : "test"), t.name().characters());
warn() << "\033[32;1mPASS\033[0m: " << time << " ms running " << test_type << " " << t.name();
if (t.is_benchmark()) { if (t.is_benchmark()) {
m_benchtime += time; m_benchtime += time;
benchmark_count++; benchmark_count++;
@ -197,8 +209,9 @@ void TestSuite::run(const NonnullRefPtrVector<TestCase>& tests)
test_count++; test_count++;
} }
} }
dbg() << "Finished " << test_count << " tests and " << benchmark_count << " benchmarks in " << (int)global_timer.elapsed_milliseconds() << " ms ("
<< (int)m_testtime << " tests, " << (int)m_benchtime << " benchmarks, " << int(global_timer.elapsed_milliseconds() - (m_testtime + m_benchtime)) << " other)"; dbg() << "Finished " << test_count << " tests and " << benchmark_count << " benchmarks in " << global_timer.elapsed_milliseconds() << " ms ("
<< m_testtime << " tests, " << m_benchtime << " benchmarks, " << (global_timer.elapsed_milliseconds() - (m_testtime + m_benchtime)) << " other)";
} }
} }
@ -206,57 +219,52 @@ void TestSuite::run(const NonnullRefPtrVector<TestCase>& tests)
using AK::TestCase; using AK::TestCase;
using AK::TestSuite; using AK::TestSuite;
#define xstr(s) ___str(s) #define __TESTCASE_FUNC(x) __test_##x
#define ___str(s) #s #define __TESTCASE_TYPE(x) __TestCase_##x
#define TESTCASE_TYPE_NAME(x) TestCase_##x #define TEST_CASE(x) \
static void __TESTCASE_FUNC(x)(); \
struct __TESTCASE_TYPE(x) { \
__TESTCASE_TYPE(x) \
() { TestSuite::the().add_case(adopt(*new TestCase(#x, __TESTCASE_FUNC(x), false))); } \
}; \
static struct __TESTCASE_TYPE(x) __TESTCASE_TYPE(x); \
static void __TESTCASE_FUNC(x)()
/*! Define a test case function. */ #define __BENCHMARK_FUNC(x) __benchmark_##x
#define TEST_CASE(x) \ #define __BENCHMARK_TYPE(x) __BenchmarkCase_##x
static void x(); \
struct TESTCASE_TYPE_NAME(x) { \
TESTCASE_TYPE_NAME(x) \
() { TestSuite::the().add_case(adopt(*new TestCase(___str(x), x, false))); } \
}; \
static struct TESTCASE_TYPE_NAME(x) TESTCASE_TYPE_NAME(x); \
static void x()
#define BENCHMARK_TYPE_NAME(x) TestCase_##x #define BENCHMARK_CASE(x) \
static void __BENCHMARK_FUNC(x)(); \
struct __BENCHMARK_TYPE(x) { \
__BENCHMARK_TYPE(x) \
() { TestSuite::the().add_case(adopt(*new TestCase(#x, __BENCHMARK_FUNC(x), true))); } \
}; \
static struct __BENCHMARK_TYPE(x) __BENCHMARK_TYPE(x); \
static void __BENCHMARK_FUNC(x)()
#define BENCHMARK_CASE(x) \ #define TEST_MAIN(x) \
static void x(); \ TestSuite* TestSuite::s_global = nullptr; \
struct BENCHMARK_TYPE_NAME(x) { \ template<size_t N> \
BENCHMARK_TYPE_NAME(x) \ constexpr size_t compiletime_lenof(const char(&)[N]) \
() { TestSuite::the().add_case(adopt(*new TestCase(___str(x), x, true))); } \ { \
}; \ return N - 1; \
static struct BENCHMARK_TYPE_NAME(x) BENCHMARK_TYPE_NAME(x); \ } \
static void x() int main(int argc, char** argv) \
{ \
/*! Define the main function of the testsuite. All TEST_CASE functions will be executed. */ static_assert(compiletime_lenof(#x) != 0, "Set SuiteName"); \
#define TEST_MAIN(SuiteName) \ TestSuite::the().main(#x, argc, argv); \
TestSuite* TestSuite::s_global = nullptr; \ TestSuite::release(); \
template<size_t N> \
constexpr size_t compiletime_lenof(const char(&)[N]) \
{ \
return N - 1; \
} \
int main(int argc, char** argv) \
{ \
static_assert(compiletime_lenof(___str(SuiteName)) != 0, "Set SuiteName"); \
TestSuite::the().main(___str(SuiteName), argc, argv); \
TestSuite::release(); \
return 0; \
} }
#define assertEqual(one, two) \ #define EXPECT_EQ(a, b) \
do { \ { \
auto ___aev1 = one; \ if ((a) != (b)) \
auto ___aev2 = two; \ warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT_EQ(" #a ", " #b ") failed"; \
if (___aev1 != ___aev2) { \ }
dbg() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": assertEqual(" ___str(one) ", " ___str(two) ") failed"; \
} \
} while (0)
#define EXPECT_EQ(one, two) assertEqual(one, two) #define EXPECT(x) \
{ \
#define EXPECT(one) assertEqual(one, true) if (!(x)) \
warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT(" #x ") failed"; \
}