Skip to content

Commit

Permalink
unit tests, target net6.0 and net8.0, fixed size_t types
Browse files Browse the repository at this point in the history
  • Loading branch information
k-wojcik committed Jul 22, 2024
1 parent afb84d2 commit c5f1c8e
Show file tree
Hide file tree
Showing 9 changed files with 542 additions and 330 deletions.
10 changes: 5 additions & 5 deletions src/Roaring.Net/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,25 @@ public static bool roaring_bitmap_is_strict_subset(IntPtr bitmap1, IntPtr bitmap
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool roaring_bitmap_remove_run_compression(IntPtr bitmap);
[DllImport("roaring")]
public static extern int roaring_bitmap_shrink_to_fit(IntPtr bitmap);
public static extern nuint roaring_bitmap_shrink_to_fit(IntPtr bitmap);

//Serialization

[DllImport("roaring")]
public static extern int roaring_bitmap_size_in_bytes(IntPtr bitmap);
public static extern nuint roaring_bitmap_size_in_bytes(IntPtr bitmap);
[DllImport("roaring")]
public static extern int roaring_bitmap_portable_size_in_bytes(IntPtr bitmap);
public static extern nuint roaring_bitmap_portable_size_in_bytes(IntPtr bitmap);

[DllImport("roaring")]
public static extern void roaring_bitmap_to_uint32_array(IntPtr bitmap, uint[] values);

[DllImport("roaring")]
public static extern int roaring_bitmap_serialize(IntPtr bitmap, byte[] buffer);
public static extern nuint roaring_bitmap_serialize(IntPtr bitmap, byte[] buffer);
[DllImport("roaring")]
public static extern IntPtr roaring_bitmap_deserialize(byte[] buffer);

[DllImport("roaring")]
public static extern int roaring_bitmap_portable_serialize(IntPtr bitmap, byte[] buffer);
public static extern nuint roaring_bitmap_portable_serialize(IntPtr bitmap, byte[] buffer);
[DllImport("roaring")]
public static extern IntPtr roaring_bitmap_portable_deserialize(byte[] buffer);

Expand Down
2 changes: 1 addition & 1 deletion src/Roaring.Net/Roaring.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<AssemblyName>Roaring.Net</AssemblyName>
<RootNamespace>Roaring</RootNamespace>
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup>
Expand Down
12 changes: 7 additions & 5 deletions src/Roaring.Net/Roaring32/Roaring32Bitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ public unsafe class Roaring32Bitmap : IDisposable

public ulong Cardinality => NativeMethods.roaring_bitmap_get_cardinality(_pointer);
public bool IsEmpty => NativeMethods.roaring_bitmap_is_empty(_pointer);
public uint Min => NativeMethods.roaring_bitmap_minimum(_pointer);
public uint Max => NativeMethods.roaring_bitmap_maximum(_pointer);
public int SerializedBytes => NativeMethods.roaring_bitmap_size_in_bytes(_pointer);
public int PortableSerializedBytes => NativeMethods.roaring_bitmap_portable_size_in_bytes(_pointer);
public uint? Min => IsEmpty ? null : NativeMethods.roaring_bitmap_minimum(_pointer);
public uint? Max => IsEmpty ? null : NativeMethods.roaring_bitmap_maximum(_pointer);
public nuint SerializedBytes => NativeMethods.roaring_bitmap_size_in_bytes(_pointer);
public nuint PortableSerializedBytes => NativeMethods.roaring_bitmap_portable_size_in_bytes(_pointer);

//Creation/Destruction

public Roaring32Bitmap()
{
_pointer = NativeMethods.roaring_bitmap_create_with_capacity(0);
}

public Roaring32Bitmap(uint capacity)
{
_pointer = NativeMethods.roaring_bitmap_create_with_capacity(capacity);
}

private Roaring32Bitmap(IntPtr pointer)
{
_pointer = pointer;
Expand Down Expand Up @@ -190,7 +192,7 @@ public bool Optimize()
=> NativeMethods.roaring_bitmap_run_optimize(_pointer);
public bool RemoveRunCompression()
=> NativeMethods.roaring_bitmap_remove_run_compression(_pointer);
public int ShrinkToFit()
public nuint ShrinkToFit()
=> NativeMethods.roaring_bitmap_shrink_to_fit(_pointer);

//Serialization
Expand Down
9 changes: 0 additions & 9 deletions test/Roaring.Net.Test/Roaring.Net.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Roaring.Test</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Configurations>Release x64;Debug x86;Debug x64;Release x86</Configurations>
<Platforms>x64;x86</Platforms>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release x64|x64' ">
<Optimize Condition=" '$(Optimize)' == '' ">true</Optimize>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug x64|x64' ">
<DebugSymbols Condition=" '$(DebugSymbols)' == '' ">true</DebugSymbols>
<Optimize Condition=" '$(Optimize)' == '' ">false</Optimize>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../../src/Roaring.Net/Roaring.Net.csproj" />
Expand Down
32 changes: 32 additions & 0 deletions test/Roaring.Net.Test/Roaring32/ConstructorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Xunit;

namespace Roaring.Test.Roaring32;

public class ConstructorTests
{
[Fact]
public void Ctor_Default_CreatesBitmapWithZeroCapacity()
{
// Arrange
using var uut = new Roaring32Bitmap();

// Act
var actual = uut.SerializedBytes;

// Assert
Assert.True(actual > 0);
}

[Fact]
public void Ctor_WithCapacity_CreatesBitmapWithPriviedCapacity()
{
// Arrange
using var uut = new Roaring32Bitmap(1000);

// Act
var actual = uut.SerializedBytes;

// Assert
Assert.True(actual > 0);
}
}
77 changes: 33 additions & 44 deletions test/Roaring.Net.Test/Roaring32/EnumerableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,31 @@ public class EnumerableTests
public void Values_BitmapContainsValues_EnumeratesBitmap()
{
// Arrange
uint[] values = [1, 2, 3, 4, 5, 100, 1000];
using var testObject = Roaring32BitmapTestObject.GetDefault();

// Act
using var result = Roaring32Bitmap.FromValues(values);
var actual = testObject.Bitmap.Values;

// Assert
Assert.Equal(result.Values, values);
Assert.Equal(testObject.Values, actual);
}

[Fact]
public void Values_BitmapIsEmpty_BreaksEnumeration()
{
// Arrange
uint[] values = [];
using var result = Roaring32Bitmap.FromValues(values);
using var testObject = Roaring32BitmapTestObject.GetEmpty();

// Act && Assert
Assert.Empty(result.Values);
Assert.Empty(testObject.Bitmap.Values);
}

[Fact]
public void Values_Reset_ThrowsNotSupportedException()
{
// Arrange
uint[] values = [1, 2, 3, 4, 5, 100, 1000];
using var result = Roaring32Bitmap.FromValues(values);
using var enumerator = result.Values.GetEnumerator();
using var testObject = Roaring32BitmapTestObject.GetDefault();
using var enumerator = testObject.Bitmap.Values.GetEnumerator();

// Act && Assert
Assert.Throws<NotSupportedException>(() => enumerator.Reset());
Expand All @@ -47,9 +45,8 @@ public void Values_Reset_ThrowsNotSupportedException()
public void Values_ForNonGenericEnumeratorReset_ThrowsNotSupportedException()
{
// Arrange
uint[] values = [1, 2, 3, 4, 5, 100, 1000];
using var result = Roaring32Bitmap.FromValues(values);
using var enumerator = result.Values.GetEnumerator();
using var testObject = Roaring32BitmapTestObject.GetDefault();
using var enumerator = testObject.Bitmap.Values.GetEnumerator();
IEnumerator enumeratorNonGeneric = enumerator;

// Act && Assert
Expand All @@ -60,82 +57,77 @@ public void Values_ForNonGenericEnumeratorReset_ThrowsNotSupportedException()
public void Values_Current_ReturnsFirstElement()
{
// Arrange
uint[] values = [1, 2, 3, 4, 5, 100, 1000];
using var result = Roaring32Bitmap.FromValues(values);
using var enumerator = result.Values.GetEnumerator();
using var testObject = Roaring32BitmapTestObject.GetDefault();
using var enumerator = testObject.Bitmap.Values.GetEnumerator();

// Act && Assert
Assert.Equal(values[0], enumerator.Current);
Assert.Equal(testObject.Values[0], enumerator.Current);
}

[Fact]
public void Values_ForNonGenericEnumerator_Current_ReturnsFirstElement()
{
uint[] values = [1, 2, 3, 4, 5, 100, 1000];
using var result = Roaring32Bitmap.FromValues(values);
using var enumerator = result.Values.GetEnumerator();
// Arrange
using var testObject = Roaring32BitmapTestObject.GetDefault();
using var enumerator = testObject.Bitmap.Values.GetEnumerator();
IEnumerator enumeratorNonGeneric = enumerator;

// Act && Assert
Assert.Equal(values[0], enumeratorNonGeneric.Current);
Assert.Equal(testObject.Values[0], enumeratorNonGeneric.Current);
}

[Fact]
public void Values_MoveNext_ReturnsNextElement()
{
// Arrange
uint[] values = [1, 2, 3, 4, 5, 100, 1000];
using var result = Roaring32Bitmap.FromValues(values);
using var enumerator = result.Values.GetEnumerator();
using var testObject = Roaring32BitmapTestObject.GetDefault();
using var enumerator = testObject.Bitmap.Values.GetEnumerator();

// Act
enumerator.MoveNext();
enumerator.MoveNext();

// Assert
Assert.Equal(values[1], enumerator.Current);
Assert.Equal(testObject.Values[1], enumerator.Current);
}

[Fact]
public void Values_ForNonGenericEnumerator_MoveNext_ReturnsNextElement()
{
// Arrange
uint[] values = [1, 2, 3, 4, 5, 100, 1000];
using var result = Roaring32Bitmap.FromValues(values);
using var enumerator = result.Values.GetEnumerator();
using var testObject = Roaring32BitmapTestObject.GetDefault();
using var enumerator = testObject.Bitmap.Values.GetEnumerator();
IEnumerator enumeratorNonGeneric = enumerator;

// Act
enumeratorNonGeneric.MoveNext();
enumeratorNonGeneric.MoveNext();

// Assert
Assert.Equal(values[1], enumeratorNonGeneric.Current);
Assert.Equal(testObject.Values[1], enumeratorNonGeneric.Current);
}

[Fact]
public void Values_ForNonGenericEnumerable_GetEnumerator_ReturnsEnumerator()
{
// Arrange
uint[] values = [1, 2, 3, 4, 5, 100, 1000];
using var result = Roaring32Bitmap.FromValues(values);
var enumerable = (IEnumerable)result.Values;
using var testObject = Roaring32BitmapTestObject.GetDefault();
var enumerable = (IEnumerable)testObject.Bitmap.Values;

// Act
var enumerator = enumerable.GetEnumerator();
using var enumeratorDisposable = enumerator as IDisposable;

// Assert
Assert.Equal(values[0], enumerator.Current);
Assert.Equal(testObject.Values[0], enumerator.Current);
}

[Fact]
public void Values_Dispose_CurrentThrowsObjectDisposedException()
{
// Arrange
uint[] values = [1, 2, 3, 4, 5, 100, 1000];
using var result = Roaring32Bitmap.FromValues(values);
var enumerator = result.Values.GetEnumerator();
using var testObject = Roaring32BitmapTestObject.GetDefault();
var enumerator = testObject.Bitmap.Values.GetEnumerator();

// Act
enumerator.Dispose();
Expand All @@ -148,9 +140,8 @@ public void Values_Dispose_CurrentThrowsObjectDisposedException()
public void Values_Dispose_MoveNextThrowsObjectDisposedException()
{
// Arrange
uint[] values = [1, 2, 3, 4, 5, 100, 1000];
using var result = Roaring32Bitmap.FromValues(values);
var enumerator = result.Values.GetEnumerator();
using var testObject = Roaring32BitmapTestObject.GetDefault();
var enumerator = testObject.Bitmap.Values.GetEnumerator();

// Act
enumerator.Dispose();
Expand All @@ -163,9 +154,8 @@ public void Values_Dispose_MoveNextThrowsObjectDisposedException()
public void Values_DisposeTwice_IgnoresSecondDipose()
{
// Arrange
uint[] values = [1, 2, 3, 4, 5, 100, 1000];
using var result = Roaring32Bitmap.FromValues(values);
var enumerator = result.Values.GetEnumerator();
using var testObject = Roaring32BitmapTestObject.GetDefault();
var enumerator = testObject.Bitmap.Values.GetEnumerator();

// Act
enumerator.Dispose();
Expand All @@ -182,9 +172,8 @@ public void Values_Destructor_InvokesDispose()
WeakReference<IEnumerable<uint>> weakReference = null;
var dispose = () =>
{
uint[] values = [1, 2, 3, 4, 5, 100, 1000];
using var result = Roaring32Bitmap.FromValues(values);
var enumerable = result.Values;
using var testObject = Roaring32BitmapTestObject.GetDefault();
var enumerable = testObject.Bitmap.Values;
weakReference = new WeakReference<IEnumerable<uint>>(enumerable, true);
};

Expand Down
Loading

0 comments on commit c5f1c8e

Please sign in to comment.