Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
67 changes: 67 additions & 0 deletions PolyShim.Tests/Net60/BitOperationsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Numerics;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.Net60;

public class BitOperationsTests
{
[Theory]
[InlineData(0, false)]
[InlineData(1, true)]
[InlineData(2, true)]
[InlineData(3, false)]
[InlineData(-4, false)]
Comment thread
Tyrrrz marked this conversation as resolved.
Resolved
public void IsPow2_Int32_Test(int value, bool expected) =>
BitOperations.IsPow2(value).Should().Be(expected);

[Theory]
[InlineData(0u, false)]
[InlineData(1u, true)]
[InlineData(1024u, true)]
[InlineData(1023u, false)]
public void IsPow2_UInt32_Test(uint value, bool expected) =>
BitOperations.IsPow2(value).Should().Be(expected);

[Theory]
[InlineData(0L, false)]
[InlineData(1L, true)]
[InlineData(2L, true)]
[InlineData(-4L, false)]
public void IsPow2_Int64_Test(long value, bool expected) =>
BitOperations.IsPow2(value).Should().Be(expected);

[Theory]
[InlineData(0ul, false)]
[InlineData(1ul, true)]
[InlineData(1024ul, true)]
[InlineData(1023ul, false)]
public void IsPow2_UInt64_Test(ulong value, bool expected) =>
BitOperations.IsPow2(value).Should().Be(expected);

[Theory]
[InlineData(0u, 0u)]
[InlineData(1u, 1u)]
[InlineData(2u, 2u)]
[InlineData(3u, 4u)]
[InlineData(5u, 8u)]
[InlineData(1000u, 1024u)]
public void RoundUpToPowerOf2_UInt32_Test(uint value, uint expected) =>
BitOperations.RoundUpToPowerOf2(value).Should().Be(expected);

[Fact]
public void RoundUpToPowerOf2_UInt32_Overflow_Test() =>
BitOperations.RoundUpToPowerOf2(uint.MaxValue).Should().Be(0u);

[Theory]
[InlineData(0ul, 0ul)]
[InlineData(1ul, 1ul)]
[InlineData(3ul, 4ul)]
[InlineData(1000ul, 1024ul)]
public void RoundUpToPowerOf2_UInt64_Test(ulong value, ulong expected) =>
BitOperations.RoundUpToPowerOf2(value).Should().Be(expected);

[Fact]
public void RoundUpToPowerOf2_UInt64_Overflow_Test() =>
BitOperations.RoundUpToPowerOf2(ulong.MaxValue).Should().Be(0ul);
}
95 changes: 95 additions & 0 deletions PolyShim.Tests/Net70/BitOperationsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Numerics;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.Net70;

public class BitOperationsTests
{
[Theory]
[InlineData(0, false)]
[InlineData(1, true)]
[InlineData(2, true)]
[InlineData(3, false)]
public void IsPow2_IntPtr_Test(int value, bool expected) =>
BitOperations.IsPow2((IntPtr)value).Should().Be(expected);

[Theory]
[InlineData(0u, false)]
[InlineData(1u, true)]
[InlineData(1024u, true)]
[InlineData(1023u, false)]
public void IsPow2_UIntPtr_Test(uint value, bool expected) =>
BitOperations.IsPow2((UIntPtr)value).Should().Be(expected);

[Theory]
[InlineData(0u, 0u)]
[InlineData(3u, 4u)]
[InlineData(1000u, 1024u)]
public void RoundUpToPowerOf2_UIntPtr_Test(uint value, uint expected) =>
BitOperations.RoundUpToPowerOf2((UIntPtr)value).Should().Be((UIntPtr)expected);

[Fact]
public void LeadingZeroCount_UIntPtr_Test()
{
// Arrange
var bitWidth = UIntPtr.Size * 8;

// Act & assert
BitOperations.LeadingZeroCount((UIntPtr)0u).Should().Be(bitWidth);
BitOperations.LeadingZeroCount((UIntPtr)1u).Should().Be(bitWidth - 1);
}

[Theory]
[InlineData(1u, 0)]
[InlineData(16u, 4)]
public void Log2_UIntPtr_Test(uint value, int expected) =>
BitOperations.Log2((UIntPtr)value).Should().Be(expected);

[Theory]
[InlineData(0u, 0)]
[InlineData(7u, 3)]
public void PopCount_UIntPtr_Test(uint value, int expected) =>
BitOperations.PopCount((UIntPtr)value).Should().Be(expected);

[Fact]
public void TrailingZeroCount_IntPtr_Test()
{
// Arrange
var bitWidth = UIntPtr.Size * 8;

// Act & assert
BitOperations.TrailingZeroCount((IntPtr)0).Should().Be(bitWidth);
BitOperations.TrailingZeroCount((IntPtr)8).Should().Be(3);
}

[Fact]
public void TrailingZeroCount_UIntPtr_Test()
{
// Arrange
var bitWidth = UIntPtr.Size * 8;

// Act & assert
BitOperations.TrailingZeroCount((UIntPtr)0u).Should().Be(bitWidth);
BitOperations.TrailingZeroCount((UIntPtr)8u).Should().Be(3);
}

[Fact]
public void RotateLeft_UIntPtr_Test() =>
BitOperations.RotateLeft((UIntPtr)0b_1000_0000u, 1).Should().Be((UIntPtr)0b_1_0000_0000u);

[Fact]
public void RotateRight_UIntPtr_Test() =>
BitOperations.RotateRight((UIntPtr)0b_0000_0010u, 1).Should().Be((UIntPtr)0b_0000_0001u);

[Fact]
public void RotateRight_UIntPtr_FullCircle_Test()
{
// Arrange
var bitWidth = UIntPtr.Size * 8;

// Act & assert
BitOperations.RotateRight((UIntPtr)1u, bitWidth).Should().Be((UIntPtr)1u);
}
}
76 changes: 76 additions & 0 deletions PolyShim.Tests/Net80/BitOperationsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Numerics;
using System.Text;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.Net80;

public class BitOperationsTests
{
[Fact]
public void Crc32C_Byte_KnownCheckValue_Test()
{
// Arrange
var data = Encoding.ASCII.GetBytes("123456789");

// Act
var crc = uint.MaxValue;
foreach (var b in data)
crc = BitOperations.Crc32C(crc, b);
crc = ~crc;

// Assert
// https://reveng.sourceforge.io/crc-catalogue/17plus.htm#crc.cat.crc-32-iscsi
crc.Should().Be(0xE3069283u);
}

[Fact]
public void Crc32C_UInt16_MatchesByteByByte_Test()
{
// Arrange
ushort data = 0x1234;

// Act
var viaUInt16 = BitOperations.Crc32C(0u, data);

var viaBytes = BitOperations.Crc32C(0u, (byte)data);
viaBytes = BitOperations.Crc32C(viaBytes, (byte)(data >> 8));

// Assert
viaUInt16.Should().Be(viaBytes);
}

[Fact]
public void Crc32C_UInt32_MatchesByteByByte_Test()
{
// Arrange
uint data = 0x1234_5678u;

// Act
var viaUInt32 = BitOperations.Crc32C(0u, data);

var viaBytes = BitOperations.Crc32C(0u, (byte)data);
viaBytes = BitOperations.Crc32C(viaBytes, (byte)(data >> 8));
viaBytes = BitOperations.Crc32C(viaBytes, (byte)(data >> 16));
viaBytes = BitOperations.Crc32C(viaBytes, (byte)(data >> 24));

// Assert
viaUInt32.Should().Be(viaBytes);
}

[Fact]
public void Crc32C_UInt64_MatchesUInt32Halves_Test()
{
// Arrange
ulong data = 0x1234_5678_9ABC_DEF0ul;

// Act
var viaUInt64 = BitOperations.Crc32C(0u, data);

var viaUInt32Halves = BitOperations.Crc32C(0u, (uint)data);
viaUInt32Halves = BitOperations.Crc32C(viaUInt32Halves, (uint)(data >> 32));

// Assert
viaUInt64.Should().Be(viaUInt32Halves);
}
}
119 changes: 119 additions & 0 deletions PolyShim.Tests/NetCore30/BitOperationsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System.Numerics;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.NetCore30;

public class BitOperationsTests
{
[Theory]
[InlineData(0b_0000_0000u, 0)]
[InlineData(0b_0000_0001u, 1)]
[InlineData(0b_0000_0111u, 3)]
[InlineData(0b_1111_1111u, 8)]
[InlineData(uint.MaxValue, 32)]
public void PopCount_UInt32_Test(uint value, int expected) =>
BitOperations.PopCount(value).Should().Be(expected);

[Theory]
[InlineData(0ul, 0)]
[InlineData(1ul, 1)]
[InlineData(0b_0000_0111ul, 3)]
[InlineData(ulong.MaxValue, 64)]
public void PopCount_UInt64_Test(ulong value, int expected) =>
BitOperations.PopCount(value).Should().Be(expected);

[Theory]
[InlineData(0u, 32)]
[InlineData(1u, 31)]
[InlineData(uint.MaxValue, 0)]
[InlineData(0b_1000_0000_0000_0000_0000_0000_0000_0000u, 0)]
public void LeadingZeroCount_UInt32_Test(uint value, int expected) =>
BitOperations.LeadingZeroCount(value).Should().Be(expected);

[Theory]
[InlineData(0ul, 64)]
[InlineData(1ul, 63)]
[InlineData(ulong.MaxValue, 0)]
public void LeadingZeroCount_UInt64_Test(ulong value, int expected) =>
BitOperations.LeadingZeroCount(value).Should().Be(expected);

[Theory]
[InlineData(0u, 0)]
[InlineData(1u, 0)]
[InlineData(2u, 1)]
[InlineData(15u, 3)]
[InlineData(16u, 4)]
[InlineData(uint.MaxValue, 31)]
public void Log2_UInt32_Test(uint value, int expected) =>
BitOperations.Log2(value).Should().Be(expected);

[Theory]
[InlineData(0ul, 0)]
[InlineData(1ul, 0)]
[InlineData(1024ul, 10)]
[InlineData(ulong.MaxValue, 63)]
public void Log2_UInt64_Test(ulong value, int expected) =>
BitOperations.Log2(value).Should().Be(expected);

[Theory]
[InlineData(0, 32)]
[InlineData(1, 0)]
[InlineData(8, 3)]
[InlineData(-1, 0)]
public void TrailingZeroCount_Int32_Test(int value, int expected) =>
BitOperations.TrailingZeroCount(value).Should().Be(expected);

[Theory]
[InlineData(0u, 32)]
[InlineData(1u, 0)]
[InlineData(8u, 3)]
[InlineData(uint.MaxValue, 0)]
public void TrailingZeroCount_UInt32_Test(uint value, int expected) =>
BitOperations.TrailingZeroCount(value).Should().Be(expected);

[Theory]
[InlineData(0L, 64)]
[InlineData(1L, 0)]
[InlineData(-1L, 0)]
public void TrailingZeroCount_Int64_Test(long value, int expected) =>
BitOperations.TrailingZeroCount(value).Should().Be(expected);

[Theory]
[InlineData(0ul, 64)]
[InlineData(1ul, 0)]
[InlineData(ulong.MaxValue, 0)]
public void TrailingZeroCount_UInt64_Test(ulong value, int expected) =>
BitOperations.TrailingZeroCount(value).Should().Be(expected);

[Fact]
public void RotateLeft_UInt32_Test() =>
BitOperations.RotateLeft(0b_1000_0000u, 1).Should().Be(0b_1_0000_0000u);

[Fact]
public void RotateLeft_UInt32_Overflow_Test() =>
BitOperations
.RotateLeft(0b_1000_0000_0000_0000_0000_0000_0000_0000u, 1)
.Should()
.Be(0b_0000_0000_0000_0000_0000_0000_0000_0001u);

[Fact]
public void RotateLeft_UInt64_Test() =>
BitOperations.RotateLeft(0b_1000_0000ul, 1).Should().Be(0b_1_0000_0000ul);

[Fact]
public void RotateRight_UInt32_Test() =>
BitOperations
.RotateRight(0b_0000_0001u, 1)
.Should()
.Be(0b_1000_0000_0000_0000_0000_0000_0000_0000u);

[Fact]
public void RotateRight_UInt64_Test() =>
BitOperations
.RotateRight(0b_0000_0001ul, 1)
.Should()
.Be(
0b_1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000ul
);
}
Loading
Loading