mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-24 10:22:05 -05:00
35c0a6c54d
As many macros as possible are moved to Macros.h, while the macros to create a test case are moved to TestCase.h. TestCase is now the only user-facing header for creating a test case. TestSuite and its helpers have moved into a .cpp file. Instead of requiring a TEST_MAIN macro to be instantiated into the test file, a TestMain.cpp file is provided instead that will be linked against each test. This has the side effect that, if we wanted to have test cases split across multiple files, it's as simple as adding them all to the same executable. The test main should be portable to kernel mode as well, so if there's a set of tests that should be run in self-test mode in kernel space, we can accomodate that. A new serenity_test CMake function streamlines adding a new test with arguments for the test source file, subdirectory under /usr/Tests to install the test application and an optional list of libraries to link against the test application. To accomodate future test where the provided TestMain.cpp is not suitable (e.g. test-js), a CUSTOM_MAIN parameter can be passed to the function to not link against the boilerplate main function.
68 lines
2.2 KiB
C++
68 lines
2.2 KiB
C++
/*
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibTest/TestCase.h>
|
|
|
|
#include <AK/Trie.h>
|
|
|
|
TEST_CASE(normal_behaviour)
|
|
{
|
|
Trie<char, String> dictionary('/', "");
|
|
constexpr static const char* data[] { "test", "example", "foo", "foobar" };
|
|
constexpr static const size_t total_chars = 18; // root (1), 'test' (4), 'example' (7), 'foo' (3), 'foobar' (3, "foo" already stored).
|
|
for (auto& entry : data) {
|
|
StringView view { entry };
|
|
auto it = view.begin();
|
|
dictionary.insert(it, view.end(), view, [](auto& parent, auto& it) -> Optional<String> { return String::formatted("{}{}", parent.metadata_value(), *it); });
|
|
}
|
|
|
|
size_t i = 0;
|
|
for ([[maybe_unused]] auto& node : dictionary)
|
|
++i;
|
|
EXPECT_EQ(i, total_chars);
|
|
|
|
for (auto& entry : data) {
|
|
StringView view { entry };
|
|
auto it = view.begin();
|
|
auto& node = dictionary.traverse_until_last_accessible_node(it, view.end());
|
|
EXPECT(it.is_end());
|
|
EXPECT(node.metadata().has_value());
|
|
EXPECT_EQ(view, node.metadata_value());
|
|
}
|
|
|
|
constexpr static const char* test_data_with_prefix_in_dict[] { "testx", "exampley", "fooa", "foobarb", "fox", "text" };
|
|
for (auto& entry : test_data_with_prefix_in_dict) {
|
|
StringView view { entry };
|
|
auto it = view.begin();
|
|
auto& node = dictionary.traverse_until_last_accessible_node(it, view.end());
|
|
EXPECT(!it.is_end());
|
|
EXPECT(node.metadata().has_value());
|
|
EXPECT(view.starts_with(node.metadata_value()));
|
|
}
|
|
}
|
|
|
|
TEST_CASE(iterate)
|
|
{
|
|
Trie<int> bunch_of_numbers { 0 };
|
|
Array<int, 64> input;
|
|
for (size_t i = 0; i < input.size(); ++i)
|
|
input[i] = i;
|
|
|
|
bunch_of_numbers.insert(input.begin(), input.end());
|
|
|
|
// Iteration order is preorder (order between adjacent nodes is not defined, but parents come before children)
|
|
// in this case, the tree is linear.
|
|
size_t i = 0;
|
|
bool is_root = true;
|
|
for (auto& node : bunch_of_numbers) {
|
|
if (is_root) {
|
|
is_root = false;
|
|
continue;
|
|
}
|
|
EXPECT_EQ(input[i], node.value());
|
|
++i;
|
|
}
|
|
}
|