Skip to content

Commit 4f1f2f2

Browse files
author
Nikita Andreev
committed
v1.0.0
1 parent 26454d6 commit 4f1f2f2

File tree

7 files changed

+469
-1
lines changed

7 files changed

+469
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
11+
<PackageReference Include="NUnit" Version="3.13.1" />
12+
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
13+
<PackageReference Include="coverlet.collector" Version="3.0.2" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\NetBitOps\NetBitOps.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using NetBitOps;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace BitOpsTest {
10+
public class GetFirstSetBitTests {
11+
[SetUp]
12+
public void Setup() {
13+
}
14+
15+
/// <summary>
16+
/// All 0x80 and 0x1 (the last and the first bits of byte) combinations for each byte for 64-bit ulong type.
17+
/// </summary>
18+
private static IEnumerable<ulong> TestULongsSource =
19+
Enumerable.Range(0, sizeof(ulong) + 1).Reverse().SelectMany(
20+
index1 => Enumerable.Range(0, sizeof(ulong) + 1).Reverse().Select(index2 => (index1 != sizeof(ulong) ? unchecked((ulong)0x80) << (index1 * 8) : 0) | (index2 != sizeof(ulong) ? unchecked((ulong)0x1) << (index2 * 8) : 0))
21+
);
22+
23+
[Test]
24+
[Description("Test BitOps.GetFirstSetBit for ulong type")]
25+
[TestCaseSource(nameof(TestULongsSource))]
26+
public void ULongTest(ulong n) {
27+
Assert.AreEqual(TrueGetFirstSetBit(n), BitOps.GetFirstSetBit(n));
28+
}
29+
30+
/// <summary>
31+
/// All 0x80 and 0x1 (the last and the first bits of byte) combinations for each byte for 32-bit uint type.
32+
/// </summary>
33+
private static IEnumerable<uint> TestUIntegersSource =
34+
Enumerable.Range(0, sizeof(uint) + 1).Reverse().SelectMany(
35+
index1 => Enumerable.Range(0, sizeof(uint) + 1).Reverse().Select(index2 => (index1 != sizeof(uint) ? unchecked((uint)0x80) << (index1 * 8) : 0) | (index2 != sizeof(uint) ? unchecked((uint)0x1) << (index2 * 8) : 0))
36+
);
37+
38+
39+
40+
[Test]
41+
[Description("Test BitOps.GetFirstSetBit for uint type")]
42+
[TestCaseSource(nameof(TestUIntegersSource))]
43+
public void UIntTest(uint n) {
44+
Assert.AreEqual(TrueGetFirstSetBit(n), BitOps.GetFirstSetBit(n));
45+
}
46+
47+
48+
/// <summary>
49+
/// All 0x80 and 0x1 (the last and the first bits of byte) combinations for each byte for 16-bit ushort type.
50+
/// </summary>
51+
private static IEnumerable<ushort> TestUShortsSource =
52+
Enumerable.Range(0, sizeof(ushort) + 1).Reverse().SelectMany(
53+
index1 => Enumerable.Range(0, sizeof(ushort) + 1).Reverse().Select(index2 => (ushort)((index1 != sizeof(ushort) ? unchecked((ushort)0x80) << (index1 * 8) : 0) | (index2 != sizeof(ushort) ? unchecked((ushort)0x1) << (index2 * 8) : 0)))
54+
);
55+
56+
57+
58+
[Test]
59+
[Description("Test BitOps.GetFirstSetBit for ushort type")]
60+
[TestCaseSource(nameof(TestUShortsSource))]
61+
public void UShortTest(ushort n) {
62+
Assert.AreEqual(TrueGetFirstSetBit(n), BitOps.GetFirstSetBit(n));
63+
}
64+
65+
/*--------------------------------Byte--------------------------------*/
66+
/// <summary>
67+
/// All 8-bit byte type values.
68+
/// </summary>
69+
private static IEnumerable<byte> TestByteSource = Enumerable.Range(0, 2 << (sizeof(byte) * 8)).Select(n => (byte)n);
70+
71+
[Test]
72+
[Description("Test BitOps.GetFirstSetBit for byte type")]
73+
[TestCaseSource(nameof(TestByteSource))]
74+
public void ByteTest(byte n) {
75+
Assert.AreEqual(TrueGetFirstSetBit(n), BitOps.GetFirstSetBit(n));
76+
}
77+
78+
static int TrueGetFirstSetBit(ulong n) {
79+
int counter = 0;
80+
while(n != 0) {
81+
if((n & 0b1) != 0)
82+
return counter;
83+
n >>= 1;
84+
counter++;
85+
}
86+
return -1;
87+
}
88+
}
89+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using NetBitOps;
2+
using NUnit.Framework;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace BitOpsTest {
7+
public class GetLastSetBitTests {
8+
[SetUp]
9+
public void Setup() {
10+
}
11+
12+
/*--------------------------------ULong--------------------------------*/
13+
/// <summary>
14+
/// All 0x80 and 0x1 (the last and the first bits of byte) combinations for each byte for 64-bit ulong type.
15+
/// </summary>
16+
private static IEnumerable<ulong> TestULongSource =
17+
Enumerable.Range(0, sizeof(ulong) + 1).Reverse().SelectMany(
18+
index1 => Enumerable.Range(0, sizeof(ulong) + 1).Reverse().Select(index2 => (index1 != sizeof(ulong) ? unchecked((ulong)0x80) << (index1 * 8) : 0) | (index2 != sizeof(ulong) ? unchecked((ulong)0x1) << (index2 * 8) : 0))
19+
);
20+
21+
[Test]
22+
[Description("Test BitOps.GetLastSetBit for ulong type")]
23+
[TestCaseSource(nameof(TestULongSource))]
24+
public void ULongTest(ulong n) {
25+
Assert.AreEqual(TrueGetLastSetBit(n), BitOps.GetLastSetBit(n));
26+
}
27+
28+
29+
/*--------------------------------UInt--------------------------------*/
30+
/// <summary>
31+
/// All 0x80 and 0x1 (the last and the first bits of byte) combinations for each byte for 32-bit uint type.
32+
/// </summary>
33+
private static IEnumerable<uint> TestUIntegerSource =
34+
Enumerable.Range(0, sizeof(uint) + 1).Reverse().SelectMany(
35+
index1 => Enumerable.Range(0, sizeof(uint) + 1).Reverse().Select(index2 => (index1 != sizeof(uint) ? unchecked((uint)0x80) << (index1 * 8) : 0) | (index2 != sizeof(uint) ? unchecked((uint)0x1) << (index2 * 8) : 0))
36+
);
37+
38+
[Test]
39+
[Description("Test BitOps.GetLastSetBit for uint type")]
40+
[TestCaseSource(nameof(TestUIntegerSource))]
41+
public void UIntTest(uint n) {
42+
Assert.AreEqual(TrueGetLastSetBit(n), BitOps.GetLastSetBit(n));
43+
}
44+
45+
46+
/*--------------------------------UShort--------------------------------*/
47+
/// <summary>
48+
/// All 0x80 and 0x1 (the last and the first bits of byte) combinations for each byte for 16-bit ushort type.
49+
/// </summary>
50+
private static IEnumerable<ushort> TestUShortSource =
51+
Enumerable.Range(0, sizeof(ushort) + 1).Reverse().SelectMany(
52+
index1 => Enumerable.Range(0, sizeof(ushort) + 1).Reverse().Select(index2 => (ushort)((index1 != sizeof(ushort) ? unchecked((ushort)0x80) << (index1 * 8) : 0) | (index2 != sizeof(ushort) ? unchecked((ushort)0x1) << (index2 * 8) : 0)))
53+
);
54+
55+
[Test]
56+
[Description("Test BitOps.GetLastSetBit for ushort type")]
57+
[TestCaseSource(nameof(TestUShortSource))]
58+
public void UShortTest(ushort n) {
59+
Assert.AreEqual(TrueGetLastSetBit(n), BitOps.GetLastSetBit(n));
60+
}
61+
62+
63+
/*--------------------------------Byte--------------------------------*/
64+
/// <summary>
65+
/// All 8-bit byte type values.
66+
/// </summary>
67+
private static IEnumerable<byte> TestByteSource = Enumerable.Range(0, 2 << (sizeof(byte) * 8)).Select(n => (byte)n);
68+
69+
[Test]
70+
[Description("Test BitOps.GetLastSetBit for byte type")]
71+
[TestCaseSource(nameof(TestByteSource))]
72+
public void ByteTest(byte n) {
73+
Assert.AreEqual(TrueGetLastSetBit(n), BitOps.GetLastSetBit(n));
74+
}
75+
76+
static int TrueGetLastSetBit(ulong n) {
77+
int counter = -1;
78+
while(n != 0) {
79+
n >>= 1;
80+
counter++;
81+
}
82+
return counter;
83+
}
84+
85+
}
86+
}

NetBitOps/NetBitOps.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31729.503
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitOpsTest", "BitOpsTest\BitOpsTest.csproj", "{466CAF6C-7277-4E38-9ED5-050BCE8267E2}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetBitOps", "NetBitOps\NetBitOps.csproj", "{1D5C7B3D-0A95-4C39-A162-DE687520CCC0}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{466CAF6C-7277-4E38-9ED5-050BCE8267E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{466CAF6C-7277-4E38-9ED5-050BCE8267E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{466CAF6C-7277-4E38-9ED5-050BCE8267E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{466CAF6C-7277-4E38-9ED5-050BCE8267E2}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{1D5C7B3D-0A95-4C39-A162-DE687520CCC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{1D5C7B3D-0A95-4C39-A162-DE687520CCC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{1D5C7B3D-0A95-4C39-A162-DE687520CCC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{1D5C7B3D-0A95-4C39-A162-DE687520CCC0}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {0FF0C6F0-F70F-49B3-9754-3FD395D2C586}
30+
EndGlobalSection
31+
EndGlobal

0 commit comments

Comments
 (0)