Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Containers.Test/RingBufferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,29 @@ public void Resize_NonPositiveLength_Throws()
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => buffer.Resize(-1));
}

[TestMethod]
public void Resample_LargeBufferToSameLength_KeepsContents()
{
const int length = 50_000;
RingBuffer<int> buffer = new(Enumerable.Range(0, length), length);

buffer.Resample(length);

Assert.AreSequenceEqual(Enumerable.Range(0, length), buffer);
}

[TestMethod]
public void Resample_UpsampleToMillionsOfElements_Succeeds()
{
RingBuffer<int> buffer = new(Enumerable.Range(0, 1000), 1000);

buffer.Resample(3_000_000);

Assert.HasCount(3_000_000, buffer);
Assert.AreEqual(0, buffer.Front());
Assert.AreEqual(999, buffer.Back());
}

[TestMethod]
public void Resample_NonPositiveLength_Throws()
{
Expand Down
3 changes: 2 additions & 1 deletion Containers/RingBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ public void Resample(int length)
for (int i = 0; i < length; i++)
{
// Map the new index to the old data range
double oldIndex = i * (oldCount - 1) / (double)Math.Max(length - 1, 1);
// Multiply in double: the int product overflows once length * oldCount passes int.MaxValue
double oldIndex = (double)i * (oldCount - 1) / Math.Max(length - 1, 1);
int index = (int)Math.Round(oldIndex);

// Ensure we don't go out of bounds
Expand Down
Loading