Rename UnpackBits to Unpack, PacknBit to Pack(CLR type)

This commit is contained in:
Oliver Booth 2022-04-28 22:58:58 +01:00
parent 4f3dd908c6
commit ba6c400a79
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
10 changed files with 47 additions and 48 deletions

View File

@ -10,47 +10,47 @@ public class BoolListTests
public void Pack8Bit_Should_Pack_Correctly()
{
var array = new[] {true, false, true, false, true, false, true, false};
Assert.AreEqual(85, array.Pack8Bit()); // 01010101
Assert.AreEqual(85, array.PackByte()); // 01010101
}
[TestMethod]
public void Pack16Bit_Should_Pack_Correctly()
{
var array = new[] {true, false, true, false, true, false, true, false, true, true, false, true};
Assert.AreEqual(2901, array.Pack16Bit()); // 101101010101
Assert.AreEqual(2901, array.PackInt16()); // 101101010101
}
[TestMethod]
public void Pack32Bit_Should_Pack_Correctly()
{
var array = new[] {true, false, true, false, true, false, true, false, true, true, false, true};
Assert.AreEqual(2901, array.Pack32Bit()); // 101101010101
Assert.AreEqual(2901, array.PackInt32()); // 101101010101
}
[TestMethod]
public void Pack64Bit_Should_Pack_Correctly()
{
var array = new[] {true, false, true, false, true, false, true, false, true, true, false, true};
Assert.AreEqual(2901, array.Pack64Bit()); // 101101010101
Assert.AreEqual(2901, array.PackInt64()); // 101101010101
}
[TestMethod]
public void Pack_ShouldThrow_GivenLargeArray()
{
bool[] array = Enumerable.Repeat(false, 65).ToArray();
Assert.ThrowsException<ArgumentException>(() => array.Pack8Bit());
Assert.ThrowsException<ArgumentException>(() => array.Pack16Bit());
Assert.ThrowsException<ArgumentException>(() => array.Pack32Bit());
Assert.ThrowsException<ArgumentException>(() => array.Pack64Bit());
Assert.ThrowsException<ArgumentException>(() => array.PackByte());
Assert.ThrowsException<ArgumentException>(() => array.PackInt16());
Assert.ThrowsException<ArgumentException>(() => array.PackInt32());
Assert.ThrowsException<ArgumentException>(() => array.PackInt64());
}
[TestMethod]
public void Pack_ShouldThrow_GivenNull()
{
bool[]? array = null;
Assert.ThrowsException<ArgumentNullException>(() => array!.Pack8Bit());
Assert.ThrowsException<ArgumentNullException>(() => array!.Pack16Bit());
Assert.ThrowsException<ArgumentNullException>(() => array!.Pack32Bit());
Assert.ThrowsException<ArgumentNullException>(() => array!.Pack64Bit());
Assert.ThrowsException<ArgumentNullException>(() => array!.PackByte());
Assert.ThrowsException<ArgumentNullException>(() => array!.PackInt16());
Assert.ThrowsException<ArgumentNullException>(() => array!.PackInt32());
Assert.ThrowsException<ArgumentNullException>(() => array!.PackInt64());
}
}

View File

@ -9,7 +9,7 @@ public class ByteTests
[TestMethod]
public void UnpackBits_ShouldUnpackToArrayCorrectly()
{
bool[] bits = ((byte)0b11010100).UnpackBits();
bool[] bits = ((byte)0b11010100).Unpack();
Assert.AreEqual(8, bits.Length);
@ -27,7 +27,7 @@ public class ByteTests
public void UnpackBits_ShouldUnpackToSpanCorrectly()
{
Span<bool> bits = stackalloc bool[8];
((byte)0b11010100).UnpackBits(bits);
((byte)0b11010100).Unpack(bits);
Assert.IsFalse(bits[0]);
Assert.IsFalse(bits[1]);
@ -42,7 +42,7 @@ public class ByteTests
[TestMethod]
public void UnpackBits_ShouldRepackEqually()
{
Assert.AreEqual(0b11010100, ((byte)0b11010100).UnpackBits().Pack8Bit());
Assert.AreEqual(0b11010100, ((byte)0b11010100).Unpack().PackByte());
}
[TestMethod]
@ -51,7 +51,7 @@ public class ByteTests
Assert.ThrowsException<ArgumentException>(() =>
{
Span<bool> bits = stackalloc bool[0];
((byte)0b11010100).UnpackBits(bits);
((byte)0b11010100).Unpack(bits);
});
}
}

View File

@ -9,7 +9,7 @@ public class Int16Tests
[TestMethod]
public void UnpackBits_ShouldUnpackToArrayCorrectly()
{
bool[] bits = ((short)0b11010100).UnpackBits();
bool[] bits = ((short)0b11010100).Unpack();
Assert.AreEqual(16, bits.Length);
@ -32,7 +32,7 @@ public class Int16Tests
public void UnpackBits_ShouldUnpackToSpanCorrectly()
{
Span<bool> bits = stackalloc bool[16];
((short)0b11010100).UnpackBits(bits);
((short)0b11010100).Unpack(bits);
Assert.IsFalse(bits[0]);
Assert.IsFalse(bits[1]);
@ -52,7 +52,7 @@ public class Int16Tests
[TestMethod]
public void UnpackBits_ShouldRepackEqually()
{
Assert.AreEqual(0b11010100, ((short)0b11010100).UnpackBits().Pack16Bit());
Assert.AreEqual(0b11010100, ((short)0b11010100).Unpack().PackInt16());
}
[TestMethod]
@ -61,7 +61,7 @@ public class Int16Tests
Assert.ThrowsException<ArgumentException>(() =>
{
Span<bool> bits = stackalloc bool[0];
((short)0b11010100).UnpackBits(bits);
((short)0b11010100).Unpack(bits);
});
}
}

View File

@ -9,7 +9,7 @@ public class Int32Tests
[TestMethod]
public void UnpackBits_ShouldUnpackToArrayCorrectly()
{
bool[] bits = 0b11010100.UnpackBits();
bool[] bits = 0b11010100.Unpack();
Assert.AreEqual(32, bits.Length);
@ -32,7 +32,7 @@ public class Int32Tests
public void UnpackBits_ShouldUnpackToSpanCorrectly()
{
Span<bool> bits = stackalloc bool[32];
0b11010100.UnpackBits(bits);
0b11010100.Unpack(bits);
Assert.IsFalse(bits[0]);
Assert.IsFalse(bits[1]);
@ -52,7 +52,7 @@ public class Int32Tests
[TestMethod]
public void UnpackBits_ShouldRepackEqually()
{
Assert.AreEqual(0b11010100, 0b11010100.UnpackBits().Pack32Bit());
Assert.AreEqual(0b11010100, 0b11010100.Unpack().PackInt32());
}
[TestMethod]
@ -61,7 +61,7 @@ public class Int32Tests
Assert.ThrowsException<ArgumentException>(() =>
{
Span<bool> bits = stackalloc bool[0];
0b11010100.UnpackBits(bits);
0b11010100.Unpack(bits);
});
}
}

View File

@ -1,7 +1,6 @@
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Collections;
using X10D.Linq;
namespace X10D.Tests.Collections;
@ -11,7 +10,7 @@ public class Int64Tests
[TestMethod]
public void UnpackBits_ShouldUnpackToArrayCorrectly()
{
bool[] bits = 0b11010100L.UnpackBits();
bool[] bits = 0b11010100L.Unpack();
Assert.AreEqual(64, bits.Length);
@ -37,7 +36,7 @@ public class Int64Tests
public void UnpackBits_ShouldUnpackToSpanCorrectly()
{
Span<bool> bits = stackalloc bool[64];
0b11010100L.UnpackBits(bits);
0b11010100L.Unpack(bits);
Assert.IsFalse(bits[0]);
Assert.IsFalse(bits[1]);
@ -57,7 +56,7 @@ public class Int64Tests
[TestMethod]
public void UnpackBits_ShouldRepackEqually()
{
Assert.AreEqual(0b11010100L, 0b11010100L.UnpackBits().Pack64Bit());
Assert.AreEqual(0b11010100L, 0b11010100L.Unpack().PackInt64());
}
[TestMethod]
@ -66,7 +65,7 @@ public class Int64Tests
Assert.ThrowsException<ArgumentException>(() =>
{
Span<bool> bits = stackalloc bool[0];
0b11010100L.UnpackBits(bits);
0b11010100L.Unpack(bits);
});
}
}

View File

@ -1,4 +1,4 @@
namespace X10D.Collections;
namespace X10D.Collections;
/// <summary>
/// Collection-related extension methods for <see cref="IReadOnlyList{T}" /> of <see cref="bool" />.
@ -13,7 +13,7 @@ public static class BoolListExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException"><paramref name="source" /> contains more than 8 elements.</exception>
/// <author>Alpha Anar</author>
public static byte Pack8Bit(this IReadOnlyList<bool> source)
public static byte PackByte(this IReadOnlyList<bool> source)
{
if (source is null)
{
@ -42,7 +42,7 @@ public static class BoolListExtensions
/// <returns>A 16-bit signed integer containing the packed booleans.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException"><paramref name="source" /> contains more than 16 elements.</exception>
public static short Pack16Bit(this IReadOnlyList<bool> source)
public static short PackInt16(this IReadOnlyList<bool> source)
{
if (source is null)
{
@ -71,7 +71,7 @@ public static class BoolListExtensions
/// <returns>A 32-bit signed integer containing the packed booleans.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException"><paramref name="source" /> contains more than 32 elements.</exception>
public static int Pack32Bit(this IReadOnlyList<bool> source)
public static int PackInt32(this IReadOnlyList<bool> source)
{
if (source is null)
{
@ -100,7 +100,7 @@ public static class BoolListExtensions
/// <returns>A 64-bit signed integer containing the packed booleans.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException"><paramref name="source" /> contains more than 64 elements.</exception>
public static long Pack64Bit(this IReadOnlyList<bool> source)
public static long PackInt64(this IReadOnlyList<bool> source)
{
if (source is null)
{

View File

@ -12,10 +12,10 @@ public static class ByteExtensions
/// </summary>
/// <param name="value">The value to unpack.</param>
/// <returns>An array of <see cref="bool" /> with length 8.</returns>
public static bool[] UnpackBits(this byte value)
public static bool[] Unpack(this byte value)
{
Span<bool> buffer = stackalloc bool[Size];
value.UnpackBits(buffer);
value.Unpack(buffer);
return buffer.ToArray();
}
@ -26,7 +26,7 @@ public static class ByteExtensions
/// <param name="destination">When this method returns, contains the unpacked booleans from <paramref name="value" />.</param>
/// <exception cref="ArgumentException"><paramref name="destination" /> is not large enough to contain the result.</exception>
/// <author>Alpha Anar</author>
public static void UnpackBits(this byte value, Span<bool> destination)
public static void Unpack(this byte value, Span<bool> destination)
{
if (destination.Length < Size)
{

View File

@ -12,10 +12,10 @@ public static class Int16Extensions
/// </summary>
/// <param name="value">The value to unpack.</param>
/// <returns>An array of <see cref="bool" /> with length 16.</returns>
public static bool[] UnpackBits(this short value)
public static bool[] Unpack(this short value)
{
Span<bool> buffer = stackalloc bool[Size];
value.UnpackBits(buffer);
value.Unpack(buffer);
return buffer.ToArray();
}
@ -25,13 +25,13 @@ public static class Int16Extensions
/// <param name="value">The value to unpack.</param>
/// <param name="destination">When this method returns, contains the unpacked booleans from <paramref name="value" />.</param>
/// <exception cref="ArgumentException"><paramref name="destination" /> is not large enough to contain the result.</exception>
public static void UnpackBits(this short value, Span<bool> destination)
public static void Unpack(this short value, Span<bool> destination)
{
if (destination.Length < Size)
{
throw new ArgumentException($"Destination must be at least {Size} in length.", nameof(destination));
}
for (var index = 0; index < Size; index++)
{
destination[index] = (value & (1 << index)) != 0;

View File

@ -12,10 +12,10 @@ public static class Int32Extensions
/// </summary>
/// <param name="value">The value to unpack.</param>
/// <returns>An array of <see cref="bool" /> with length 32.</returns>
public static bool[] UnpackBits(this int value)
public static bool[] Unpack(this int value)
{
Span<bool> buffer = stackalloc bool[Size];
value.UnpackBits(buffer);
value.Unpack(buffer);
return buffer.ToArray();
}
@ -25,13 +25,13 @@ public static class Int32Extensions
/// <param name="value">The value to unpack.</param>
/// <param name="destination">When this method returns, contains the unpacked booleans from <paramref name="value" />.</param>
/// <exception cref="ArgumentException"><paramref name="destination" /> is not large enough to contain the result.</exception>
public static void UnpackBits(this int value, Span<bool> destination)
public static void Unpack(this int value, Span<bool> destination)
{
if (destination.Length < Size)
{
throw new ArgumentException($"Destination must be at least {Size} in length.", nameof(destination));
}
for (var index = 0; index < Size; index++)
{
destination[index] = (value & (1 << index)) != 0;

View File

@ -12,10 +12,10 @@ public static class Int64Extensions
/// </summary>
/// <param name="value">The value to unpack.</param>
/// <returns>An array of <see cref="bool" /> with length 64.</returns>
public static bool[] UnpackBits(this long value)
public static bool[] Unpack(this long value)
{
Span<bool> buffer = stackalloc bool[Size];
value.UnpackBits(buffer);
value.Unpack(buffer);
return buffer.ToArray();
}
@ -25,7 +25,7 @@ public static class Int64Extensions
/// <param name="value">The value to unpack.</param>
/// <param name="destination">When this method returns, contains the unpacked booleans from <paramref name="value" />.</param>
/// <exception cref="ArgumentException"><paramref name="destination" /> is not large enough to contain the result.</exception>
public static void UnpackBits(this long value, Span<bool> destination)
public static void Unpack(this long value, Span<bool> destination)
{
if (destination.Length < Size)
{