summaryrefslogtreecommitdiff
path: root/Tests/AK
diff options
context:
space:
mode:
authorLenny Maiorani <lenny@colorado.edu>2021-05-19 08:54:19 -0600
committerLinus Groh <mail@linusgroh.de>2021-05-19 21:21:03 +0100
commit2b64d163cd9f25bd2951bf8721ef9325decb1b72 (patch)
tree1c58cbc0a921bd8fd52df015fe0983dd93530f3a /Tests/AK
parent4aaf8df8657b21cfd5e1744a54b2e8b071906b6c (diff)
downloadserenity-2b64d163cd9f25bd2951bf8721ef9325decb1b72.zip
Tests: static vs non-static constexpr variables
Problem: - `static` variables consume memory and sometimes are less optimizable. - `static const` variables can be `constexpr`, usually. - `static` function-local variables require an initialization check every time the function is run. Solution: - If a global `static` variable is only used in a single function then move it into the function and make it non-`static` and `constexpr`. - Make all global `static` variables `constexpr` instead of `const`. - Change function-local `static const[expr]` variables to be just `constexpr`.
Diffstat (limited to 'Tests/AK')
-rw-r--r--Tests/AK/TestTrie.cpp16
1 files changed, 7 insertions, 9 deletions
diff --git a/Tests/AK/TestTrie.cpp b/Tests/AK/TestTrie.cpp
index 50b574cced..b05bd58fb9 100644
--- a/Tests/AK/TestTrie.cpp
+++ b/Tests/AK/TestTrie.cpp
@@ -6,15 +6,15 @@
#include <LibTest/TestCase.h>
+#include <AK/StringView.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 };
+ constexpr StringView data[] { "test", "example", "foo", "foobar" };
+ constexpr size_t total_chars = 18; // root (1), 'test' (4), 'example' (7), 'foo' (3), 'foobar' (3, "foo" already stored).
+ for (auto& view : data) {
auto it = view.begin();
dictionary.insert(it, view.end(), view, [](auto& parent, auto& it) -> Optional<String> { return String::formatted("{}{}", parent.metadata_value(), *it); });
}
@@ -24,8 +24,7 @@ TEST_CASE(normal_behaviour)
++i;
EXPECT_EQ(i, total_chars);
- for (auto& entry : data) {
- StringView view { entry };
+ for (auto& view : data) {
auto it = view.begin();
auto& node = dictionary.traverse_until_last_accessible_node(it, view.end());
EXPECT(it.is_end());
@@ -33,9 +32,8 @@ TEST_CASE(normal_behaviour)
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 };
+ constexpr StringView test_data_with_prefix_in_dict[] { "testx", "exampley", "fooa", "foobarb", "fox", "text" };
+ for (auto& view : test_data_with_prefix_in_dict) {
auto it = view.begin();
auto& node = dictionary.traverse_until_last_accessible_node(it, view.end());
EXPECT(!it.is_end());