Skip to content

Commit 63738b5

Browse files
authored
Fix bug in bitmap list where an insert at an integer border caused error (#497)
1 parent 33a7f68 commit 63738b5

File tree

2 files changed

+112
-1
lines changed

2 files changed

+112
-1
lines changed

src/FlowtideDotNet.Core/ColumnStore/Utils/BitmapList.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public unsafe class BitmapList : IReadOnlyList<bool>, IEnumerable<bool>, IDispos
9999
~((1 << 29) - 1), // 28th element with all bits except the lowest 29 bits set
100100
~((1 << 30) - 1), // 29th element with all bits except the lowest 30 bits set
101101
-2147483648, // 30th element with all bits except the lowest 31 bits set
102-
~((1 << 32) - 1)
102+
0
103103
];
104104
private IMemoryAllocator? memoryAllocator;
105105
private int _length;

tests/FlowtideDotNet.Core.Tests/ColumnStore/Utils/BitmapListTests.cs

+111
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,116 @@ public void TestSequence()
258258
Assert.True(list.Get(i));
259259
}
260260
}
261+
262+
[Fact]
263+
public void TestInsertSpecialCaseShiftLeftAtBorder()
264+
{
265+
var list = new BitmapList(new BatchMemoryManager(1));
266+
267+
for (int i = 0; i < 583; i++)
268+
{
269+
list.Unset(i);
270+
}
271+
list.Set(584);
272+
list.Unset(585);
273+
for (int i = 586; i <= 606; i++)
274+
{
275+
list.Unset(i);
276+
}
277+
278+
list.InsertAt(607, false);
279+
280+
for (int i = 0; i < 583; i++)
281+
{
282+
Assert.False(list.Get(i));
283+
}
284+
Assert.True(list.Get(584));
285+
for (int i = 585; i <= 607; i++)
286+
{
287+
Assert.False(list.Get(i));
288+
}
289+
}
290+
291+
[Fact]
292+
public void TestInsertRandomInOrder()
293+
{
294+
var list = new BitmapList(new BatchMemoryManager(1));
295+
296+
Random r = new Random(123);
297+
298+
List<bool> expected = new List<bool>();
299+
300+
for (int i = 0; i < 1_00_000; i++)
301+
{
302+
var v = r.Next(0, 2);
303+
if (v == 0)
304+
{
305+
list.InsertAt(i, false);
306+
expected.Add(false);
307+
}
308+
else
309+
{
310+
list.InsertAt(i, true);
311+
expected.Add(true);
312+
}
313+
}
314+
315+
for (int i = 0; i < expected.Count; i++)
316+
{
317+
Assert.Equal(expected[i], list.Get(i));
318+
}
319+
320+
for (int i = 0; i < 10_000; i++)
321+
{
322+
var index = r.Next(0, expected.Count);
323+
list.RemoveAt(index);
324+
expected.RemoveAt(index);
325+
}
326+
327+
for (int i = 0; i < expected.Count; i++)
328+
{
329+
Assert.Equal(expected[i], list.Get(i));
330+
}
331+
}
332+
333+
[Fact]
334+
public void TestInsertRandomRandomOrder()
335+
{
336+
var list = new BitmapList(new BatchMemoryManager(1));
337+
338+
Random r = new Random(123);
339+
340+
List<bool> expected = new List<bool>();
341+
342+
for (int i = 0; i < 1_00_000; i++)
343+
{
344+
var v = r.Next(0, 2);
345+
bool val = true;
346+
if (v == 0)
347+
{
348+
val = false;
349+
}
350+
var index = r.Next(0, expected.Count);
351+
list.InsertAt(index, val);
352+
expected.Insert(index, val);
353+
}
354+
355+
for (int i = 0; i < expected.Count; i++)
356+
{
357+
Assert.Equal(expected[i], list.Get(i));
358+
}
359+
360+
for (int i = 0; i < 10_000; i++)
361+
{
362+
var index = r.Next(0, expected.Count);
363+
list.RemoveAt(index);
364+
expected.RemoveAt(index);
365+
}
366+
367+
for (int i = 0; i < expected.Count; i++)
368+
{
369+
Assert.Equal(expected[i], list.Get(i));
370+
}
371+
}
261372
}
262373
}

0 commit comments

Comments
 (0)