mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 05:05:05 +00:00
Merge branch 'Nibbles' of Common/Utils into dev
This commit is contained in:
@@ -3,41 +3,53 @@ using NUnit.Framework;
|
|||||||
|
|
||||||
namespace ICD.Common.Utils.Tests.Extensions
|
namespace ICD.Common.Utils.Tests.Extensions
|
||||||
{
|
{
|
||||||
[TestFixture]
|
[TestFixture]
|
||||||
public sealed class ByteExtensionsTest
|
public sealed class ByteExtensionsTest
|
||||||
{
|
{
|
||||||
[TestCase(false, 0, 0)]
|
[TestCase(false, 0, 0)]
|
||||||
[TestCase(true, 1, 0)]
|
[TestCase(true, 1, 0)]
|
||||||
[TestCase(false, 247, 3)]
|
[TestCase(false, 247, 3)]
|
||||||
[TestCase(true, 255, 3)]
|
[TestCase(true, 255, 3)]
|
||||||
public void GetBitTest(bool expected, byte value, int index)
|
public void GetBitTest(bool expected, byte value, int index)
|
||||||
{
|
{
|
||||||
Assert.AreEqual(expected, value.GetBit(index));
|
Assert.AreEqual(expected, value.GetBit(index));
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase(1, 0, 0, true)]
|
[TestCase(1, 0, 0, true)]
|
||||||
[TestCase(0, 1, 0, false)]
|
[TestCase(0, 1, 0, false)]
|
||||||
[TestCase(247, 255, 3, false)]
|
[TestCase(247, 255, 3, false)]
|
||||||
[TestCase(255, 247, 3, true)]
|
[TestCase(255, 247, 3, true)]
|
||||||
public void SetBitTest(byte expected, byte value, int index, bool bitValue)
|
public void SetBitTest(byte expected, byte value, int index, bool bitValue)
|
||||||
{
|
{
|
||||||
Assert.AreEqual(expected, value.SetBit(index, bitValue));
|
Assert.AreEqual(expected, value.SetBit(index, bitValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase(1, 0, 0)]
|
[TestCase(1, 0, 0)]
|
||||||
[TestCase(1, 1, 0)]
|
[TestCase(1, 1, 0)]
|
||||||
[TestCase(255, 247, 3)]
|
[TestCase(255, 247, 3)]
|
||||||
public void SetBitOnTest(byte expected, byte value, int index)
|
public void SetBitOnTest(byte expected, byte value, int index)
|
||||||
{
|
{
|
||||||
Assert.AreEqual(expected, value.SetBitOn(index));
|
Assert.AreEqual(expected, value.SetBitOn(index));
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase(0, 0, 0)]
|
[TestCase(0, 0, 0)]
|
||||||
[TestCase(0, 1, 0)]
|
[TestCase(0, 1, 0)]
|
||||||
[TestCase(247, 255, 3)]
|
[TestCase(247, 255, 3)]
|
||||||
public void SetBitOffTest(byte expected, byte value, int index)
|
public void SetBitOffTest(byte expected, byte value, int index)
|
||||||
{
|
{
|
||||||
Assert.AreEqual(expected, value.SetBitOff(index));
|
Assert.AreEqual(expected, value.SetBitOff(index));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
[TestCase(0xFF, 0x0F)]
|
||||||
|
public void GetLower4BitsTest(byte b, byte expected)
|
||||||
|
{
|
||||||
|
Assert.AreEqual(expected, b.GetLower4Bits());
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase(0xFF, 0xF0)]
|
||||||
|
public void GetUpper4BitsTest(byte b, byte expected)
|
||||||
|
{
|
||||||
|
Assert.AreEqual(expected, b.GetUpper4Bits());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,22 +7,20 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
public static bool GetBit(this byte b, int n)
|
public static bool GetBit(this byte b, int n)
|
||||||
{
|
{
|
||||||
if (n > 7 || n < 0)
|
if (n > 7 || n < 0)
|
||||||
throw new ArgumentException();
|
throw new ArgumentOutOfRangeException("n");
|
||||||
|
|
||||||
return (b & (1 << n)) == (1 << n);
|
return (b & (1 << n)) == (1 << n);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte SetBit(this byte b, int n, bool v)
|
public static byte SetBit(this byte b, int n, bool v)
|
||||||
{
|
{
|
||||||
if (v)
|
return v ? SetBitOn(b, n) : SetBitOff(b, n);
|
||||||
return SetBitOn(b, n);
|
|
||||||
return SetBitOff(b, n);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte SetBitOn(this byte b, int n)
|
public static byte SetBitOn(this byte b, int n)
|
||||||
{
|
{
|
||||||
if (n > 7 || n < 0)
|
if (n > 7 || n < 0)
|
||||||
throw new ArgumentException();
|
throw new ArgumentOutOfRangeException("n");
|
||||||
|
|
||||||
return (byte)(b | (1 << n));
|
return (byte)(b | (1 << n));
|
||||||
}
|
}
|
||||||
@@ -30,9 +28,19 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
public static byte SetBitOff(this byte b, int n)
|
public static byte SetBitOff(this byte b, int n)
|
||||||
{
|
{
|
||||||
if (n > 7 || n < 0)
|
if (n > 7 || n < 0)
|
||||||
throw new ArgumentException();
|
throw new ArgumentOutOfRangeException("n");
|
||||||
|
|
||||||
return (byte)(b & ~(1 << n));
|
return (byte)(b & ~(1 << n));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte GetLower4Bits(this byte b)
|
||||||
|
{
|
||||||
|
return (byte)(b & 15);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte GetUpper4Bits(this byte b)
|
||||||
|
{
|
||||||
|
return (byte)(b >> 4);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user