summaryrefslogtreecommitdiff
path: root/Tests
diff options
context:
space:
mode:
authorAndrew Kaster <andrewdkaster@gmail.com>2021-05-15 00:12:50 -0600
committerLinus Groh <mail@linusgroh.de>2021-05-16 21:58:14 +0100
commit73adbb319cacea04ac52e2f51be72260dd4c0f87 (patch)
treec4197fe0728893a94d2b2d12ac459d88681b7d54 /Tests
parentb9d65da5c89a2bd42e6108d9bf8eab14b5f19566 (diff)
downloadserenity-73adbb319cacea04ac52e2f51be72260dd4c0f87.zip
AK: Don't read past the end in BitmapView::count_in_range()
The current code is factored such that reads to the entirety of the last byte should be dropped. This was relying on the fact that last would be one past the end in that case. Instead of actually reading that byte when it's completely out of bounds of the bitmask, just skip reads that would be invalid. Add more tests to make sure that the behavior is correct for byte aligned reads of byte aligned bitmaps.
Diffstat (limited to 'Tests')
-rw-r--r--Tests/AK/TestBitmap.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/Tests/AK/TestBitmap.cpp b/Tests/AK/TestBitmap.cpp
index b4326dcd54..464e81c0c8 100644
--- a/Tests/AK/TestBitmap.cpp
+++ b/Tests/AK/TestBitmap.cpp
@@ -248,3 +248,31 @@ TEST_CASE(count_in_range)
test_with_value(true);
test_with_value(false);
}
+
+TEST_CASE(byte_aligned_access)
+{
+ {
+ Bitmap bitmap(16, true);
+ EXPECT_EQ(bitmap.count_in_range(0, 16, true), 16u);
+ EXPECT_EQ(bitmap.count_in_range(8, 8, true), 8u);
+ EXPECT_EQ(bitmap.count_in_range(0, 8, true), 8u);
+ EXPECT_EQ(bitmap.count_in_range(4, 8, true), 8u);
+ }
+ {
+ Bitmap bitmap(16, false);
+ bitmap.set_range(4, 8, true);
+ EXPECT_EQ(bitmap.count_in_range(0, 16, true), 8u);
+ EXPECT_EQ(bitmap.count_in_range(8, 8, true), 4u);
+ EXPECT_EQ(bitmap.count_in_range(0, 8, true), 4u);
+ EXPECT_EQ(bitmap.count_in_range(4, 8, true), 8u);
+ }
+ {
+ Bitmap bitmap(8, false);
+ bitmap.set(2, true);
+ bitmap.set(4, true);
+ EXPECT_EQ(bitmap.count_in_range(0, 2, true), 0u);
+ EXPECT_EQ(bitmap.count_in_range(0, 4, true), 1u);
+ EXPECT_EQ(bitmap.count_in_range(0, 8, true), 2u);
+ EXPECT_EQ(bitmap.count_in_range(4, 4, true), 1u);
+ }
+}