summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkleines Filmröllchen <malu.bertsch@gmail.com>2022-01-12 22:33:43 +0100
committerLinus Groh <mail@linusgroh.de>2022-01-13 11:17:44 +0100
commit594bbbf020b2bca56f6778dacbd31a422a3a18e9 (patch)
treee130042d4fa8047bf30630aa7b197bf1251f8a1f
parent1d144ed6fcc1768a6cce7408bfb633ed7849d847 (diff)
downloadserenity-594bbbf020b2bca56f6778dacbd31a422a3a18e9.zip
Tests: Test FixedArray completely
Except for tangential accessors such as data(), there is no more feature of FixedArray that is untested after this large expansion of its test cases. These tests, with the help of the new NoAllocationGuard, also test the allocation contract that was fixated in the last commit. Hopefully this builds confidence in future Kernel uses of FixedArray as well as its establishment in the real-time parts of the audio subsystem. I'm excited :^)
-rw-r--r--AK/NoAllocationGuard.h2
-rw-r--r--Tests/AK/TestFixedArray.cpp59
2 files changed, 58 insertions, 3 deletions
diff --git a/AK/NoAllocationGuard.h b/AK/NoAllocationGuard.h
index 19356541dd..e009644196 100644
--- a/AK/NoAllocationGuard.h
+++ b/AK/NoAllocationGuard.h
@@ -58,7 +58,7 @@ private:
#endif
}
- bool m_allocation_enabled_previously;
+ bool m_allocation_enabled_previously { true };
};
}
diff --git a/Tests/AK/TestFixedArray.cpp b/Tests/AK/TestFixedArray.cpp
index 872f97767b..b0c6ca8f31 100644
--- a/Tests/AK/TestFixedArray.cpp
+++ b/Tests/AK/TestFixedArray.cpp
@@ -4,14 +4,16 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <LibTest/TestCase.h>
#include <LibTest/TestSuite.h>
#include <AK/FixedArray.h>
-#include <AK/String.h>
+#include <AK/NoAllocationGuard.h>
TEST_CASE(construct)
{
- EXPECT(FixedArray<int>().size() == 0);
+ EXPECT_EQ(FixedArray<int>().size(), 0u);
+ EXPECT_EQ(FixedArray<int>::must_create_but_fixme_should_propagate_errors(1985).size(), 1985u);
}
TEST_CASE(ints)
@@ -24,3 +26,56 @@ TEST_CASE(ints)
EXPECT_EQ(ints[1], 1);
EXPECT_EQ(ints[2], 2);
}
+
+TEST_CASE(swap)
+{
+ FixedArray<int> first = FixedArray<int>::must_create_but_fixme_should_propagate_errors(4);
+ FixedArray<int> second = FixedArray<int>::must_create_but_fixme_should_propagate_errors(5);
+ first[3] = 1;
+ second[3] = 2;
+ first.swap(second);
+ EXPECT_EQ(first.size(), 5u);
+ EXPECT_EQ(second.size(), 4u);
+ EXPECT_EQ(first[3], 2);
+ EXPECT_EQ(second[3], 1);
+}
+
+TEST_CASE(move)
+{
+ FixedArray<int> moved_from_array = FixedArray<int>::must_create_but_fixme_should_propagate_errors(6);
+ FixedArray<int> moved_to_array(move(moved_from_array));
+ EXPECT_EQ(moved_to_array.size(), 6u);
+ EXPECT_EQ(moved_from_array.size(), 0u);
+}
+
+TEST_CASE(no_allocation)
+{
+ FixedArray<int> array = FixedArray<int>::must_create_but_fixme_should_propagate_errors(5);
+ EXPECT_NO_CRASH("Assigments", [&] {
+ NoAllocationGuard guard;
+ array[0] = 0;
+ array[1] = 1;
+ array[2] = 2;
+ array[4] = array[1];
+ array[3] = array[0] + array[2];
+ return Test::Crash::Failure::DidNotCrash;
+ });
+
+ EXPECT_NO_CRASH("Move", [&] {
+ FixedArray<int> moved_from_array = FixedArray<int>::must_create_but_fixme_should_propagate_errors(6);
+ NoAllocationGuard guard;
+ FixedArray<int> moved_to_array(move(moved_from_array));
+ // We need to ensure that this destructor runs before the FixedArray destructor.
+ guard.~NoAllocationGuard();
+ return Test::Crash::Failure::DidNotCrash;
+ });
+
+ EXPECT_NO_CRASH("Swap", [&] {
+ FixedArray<int> target_for_swapping;
+ {
+ NoAllocationGuard guard;
+ array.swap(target_for_swapping);
+ }
+ return Test::Crash::Failure::DidNotCrash;
+ });
+}