Skip to content

Commit

Permalink
Fix bug in bitmap list where an insert at an integer border caused er…
Browse files Browse the repository at this point in the history
…ror (#497)
  • Loading branch information
Ulimo authored Aug 30, 2024
1 parent 33a7f68 commit 63738b5
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/FlowtideDotNet.Core/ColumnStore/Utils/BitmapList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public unsafe class BitmapList : IReadOnlyList<bool>, IEnumerable<bool>, IDispos
~((1 << 29) - 1), // 28th element with all bits except the lowest 29 bits set
~((1 << 30) - 1), // 29th element with all bits except the lowest 30 bits set
-2147483648, // 30th element with all bits except the lowest 31 bits set
~((1 << 32) - 1)
0
];
private IMemoryAllocator? memoryAllocator;
private int _length;
Expand Down
111 changes: 111 additions & 0 deletions tests/FlowtideDotNet.Core.Tests/ColumnStore/Utils/BitmapListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,116 @@ public void TestSequence()
Assert.True(list.Get(i));
}
}

[Fact]
public void TestInsertSpecialCaseShiftLeftAtBorder()
{
var list = new BitmapList(new BatchMemoryManager(1));

for (int i = 0; i < 583; i++)
{
list.Unset(i);
}
list.Set(584);
list.Unset(585);
for (int i = 586; i <= 606; i++)
{
list.Unset(i);
}

list.InsertAt(607, false);

for (int i = 0; i < 583; i++)
{
Assert.False(list.Get(i));
}
Assert.True(list.Get(584));
for (int i = 585; i <= 607; i++)
{
Assert.False(list.Get(i));
}
}

[Fact]
public void TestInsertRandomInOrder()
{
var list = new BitmapList(new BatchMemoryManager(1));

Random r = new Random(123);

List<bool> expected = new List<bool>();

for (int i = 0; i < 1_00_000; i++)
{
var v = r.Next(0, 2);
if (v == 0)
{
list.InsertAt(i, false);
expected.Add(false);
}
else
{
list.InsertAt(i, true);
expected.Add(true);
}
}

for (int i = 0; i < expected.Count; i++)
{
Assert.Equal(expected[i], list.Get(i));
}

for (int i = 0; i < 10_000; i++)
{
var index = r.Next(0, expected.Count);
list.RemoveAt(index);
expected.RemoveAt(index);
}

for (int i = 0; i < expected.Count; i++)
{
Assert.Equal(expected[i], list.Get(i));
}
}

[Fact]
public void TestInsertRandomRandomOrder()
{
var list = new BitmapList(new BatchMemoryManager(1));

Random r = new Random(123);

List<bool> expected = new List<bool>();

for (int i = 0; i < 1_00_000; i++)
{
var v = r.Next(0, 2);
bool val = true;
if (v == 0)
{
val = false;
}
var index = r.Next(0, expected.Count);
list.InsertAt(index, val);
expected.Insert(index, val);
}

for (int i = 0; i < expected.Count; i++)
{
Assert.Equal(expected[i], list.Get(i));
}

for (int i = 0; i < 10_000; i++)
{
var index = r.Next(0, expected.Count);
list.RemoveAt(index);
expected.RemoveAt(index);
}

for (int i = 0; i < expected.Count; i++)
{
Assert.Equal(expected[i], list.Get(i));
}
}
}
}

0 comments on commit 63738b5

Please sign in to comment.