public void SetBitsGlobalOffsetTest() { var offset = 24; var temp = MagicVector.Take(5).ToArray(); BufferPrimitives.SetBits(temp, 0, ref offset, 2, 2); Assert.AreEqual(new byte[] { 0x01, 0x23, 0x45, 0xA7, 0x89 }, temp); BufferPrimitives.SetBits(temp, 0, ref offset, 2, 3); Assert.AreEqual(new byte[] { 0x01, 0x23, 0x45, 0xB7, 0x89 }, temp); BufferPrimitives.SetBits(temp, 0, ref offset, 2, 3); Assert.AreEqual(new byte[] { 0x01, 0x23, 0x45, 0xBF, 0x89 }, temp); BufferPrimitives.SetBits(temp, 0, ref offset, 2, 0); Assert.AreEqual(new byte[] { 0x01, 0x23, 0x45, 0xBC, 0x89 }, temp); offset = 28; temp = MagicVector.Take(5).ToArray(); BufferPrimitives.SetBits(temp, 0, ref offset, 3, 6); Assert.AreEqual(new byte[] { 0x01, 0x23, 0x45, 0x6D, 0x89 }, temp); BufferPrimitives.SetBits(temp, 0, ref offset, 3, 7); Assert.AreEqual(new byte[] { 0x01, 0x23, 0x45, 0x6D, 0xC9 }, temp); BufferPrimitives.SetBits(temp, 0, ref offset, 3, 5); Assert.AreEqual(new byte[] { 0x01, 0x23, 0x45, 0x6D, 0xE9 }, temp); BufferPrimitives.SetBits(temp, 0, ref offset, 3, 7); Assert.AreEqual(new byte[] { 0x01, 0x23, 0x45, 0x6D, 0xEF }, temp); temp = MagicVector.Take(5).ToArray(); BufferPrimitives.SetBits(temp, 0, 28, 8, 0xEF); Assert.AreEqual(new byte[] { 0x01, 0x23, 0x45, 0x6E, 0xF9 }, temp); BufferPrimitives.SetBits(temp, 0, 20, 16, 0xCDEF); Assert.AreEqual(new byte[] { 0x01, 0x23, 0x4C, 0xDE, 0xF9 }, temp); BufferPrimitives.SetBits(temp, 0, 12, 24, 0xABCDEF); Assert.AreEqual(new byte[] { 0x01, 0x2A, 0xBC, 0xDE, 0xF9 }, temp); BufferPrimitives.SetBits(temp, 0, 4, 32, 0x89ABCDEF); Assert.AreEqual(new byte[] { 0x08, 0x9A, 0xBC, 0xDE, 0xF9 }, temp); }
public void SetSignedBitsTest() { var temp = MagicVector.Take(5).ToArray(); BufferPrimitives.SetBits(temp, 1, 2, 4, unchecked ((ulong)-1)); Assert.AreEqual(new byte[] { 0x01, 0x3F, 0x45, 0x67, 0x89 }, temp); BufferPrimitives.SetBits(temp, 2, 1, 6, unchecked ((ulong)-22)); Assert.AreEqual(new byte[] { 0x01, 0x3F, 0x55, 0x67, 0x89 }, temp); Assert.AreEqual(1, BufferPrimitives.NormalizeLong(BufferPrimitives.GetBits(temp, 0, 0, 8), 8)); Assert.AreEqual(1, BufferPrimitives.NormalizeLong(BufferPrimitives.GetBits(temp, 0, 6, 2), 2)); Assert.AreEqual(-1, BufferPrimitives.NormalizeLong(BufferPrimitives.GetBits(temp, 1, 2, 4), 4)); Assert.AreEqual(-22, BufferPrimitives.NormalizeLong(BufferPrimitives.GetBits(temp, 2, 1, 6), 6)); }
public void ByteAlignedTest() { var temp = new byte[MagicVector.Length]; var reference = 0ul; var referenceLe = 0ul; var shift = 0; for (var i = 0; i < 8; ++i) { reference = reference << 8 | MagicVector[i]; referenceLe |= (ulong)MagicVector[i] << shift; shift += 8; Array.Clear(temp, 0, temp.Length); BufferPrimitives.SetVarious(temp, 0, reference, i + 1); CollectionAssert.AreEqual(MagicVector.Take(i + 1), temp.Take(i + 1)); Assert.AreEqual(reference, BufferPrimitives.GetVarious(temp, 0, i + 1)); Array.Clear(temp, 0, temp.Length); BufferPrimitives.SetVariousLe(temp, 0, referenceLe, i + 1); CollectionAssert.AreEqual(MagicVector.Take(i + 1), temp.Take(i + 1)); Assert.AreEqual(referenceLe, BufferPrimitives.GetVariousLe(temp, 0, i + 1)); } }