Skip to content

Commit

Permalink
fix build and add batch tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhathcock committed Jan 2, 2025
1 parent 9e3c47b commit 79d7ab9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ public class SizeBatchingChannelReader<T>(
int batchSize,
bool singleReader,
bool syncCont = false
) : BatchingChannelReader<T, Batch<T>>(source, batchSize, singleReader, syncCont)
) : BatchingChannelReader<T, Batch<T>>(x => new(x), source, batchSize, singleReader, syncCont)
where T : IHasSize
{
protected override Batch<T> CreateBatch(int capacity) => new(capacity);

protected override void TrimBatch(Batch<T> batch) => batch.TrimExcess();
protected override void TrimBatch(ref Batch<T> batch, bool isVerifiedFull)
{
if (!isVerifiedFull)
{
batch.TrimExcess();
}
}

protected override void AddBatchItem(Batch<T> batch, T item) => batch.Add(item);

Expand Down
44 changes: 44 additions & 0 deletions tests/Speckle.Sdk.Tests.Unit/Serialisation/BatchTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using NUnit.Framework;
using Shouldly;
using Speckle.Sdk.Serialisation.V2.Send;

namespace Speckle.Sdk.Tests.Unit.Serialisation;

[TestFixture]
public class BatchTests
{
private class BatchItem : IHasSize
{
public BatchItem(int size)
{
Size = size;
}

public int Size { get; }
}

[Test]
public void TestBatchSize_Calc()
{
var batch = new Batch<BatchItem>(4);
batch.Add(new BatchItem(1));
batch.Size.ShouldBe(1);
batch.Add(new BatchItem(2));
batch.Size.ShouldBe(3);
}

[Test]
public void TestBatchSize_Trim()
{
var batch = new Batch<BatchItem>(4);
batch.Add(new BatchItem(1));
batch.Add(new BatchItem(2));
batch.Size.ShouldBe(3);

batch.Items.Capacity.ShouldBe(4);
batch.TrimExcess();

batch.Items.Capacity.ShouldBe(2);
batch.Size.ShouldBe(3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Speckle.Sdk.Tests.Unit.Serialisation;

[TestFixture]
public class SimpleRoundTripTests
{
private IOperations _operations;
Expand Down

0 comments on commit 79d7ab9

Please sign in to comment.