public void Given_a_bits_list_then_msb_is_first() { //Given var bits = new Bits(new[] { true, true, true, true, false, false, false, false }); Assert.Equal(new byte[] { 0xF0 }, bits.AsBytes()); }
private static sbyte[] CreateBitCompactedArray(ShortArray type, object array, int offsetBytes) { Type componentType = array.GetType().GetElementType(); bool isPrimitiveByteArray = componentType.Equals(Byte.TYPE); bool isByteArray = componentType.Equals(typeof(Byte)) || isPrimitiveByteArray; int arrayLength = Array.getLength(array); int requiredBits = isByteArray ? (sizeof(sbyte) * 8) : type.calculateRequiredBitsForArray(array, arrayLength); int totalBits = requiredBits * arrayLength; int bitsUsedInLastByte = totalBits % 8; bitsUsedInLastByte = bitsUsedInLastByte == 0 ? 8 : bitsUsedInLastByte; if (isByteArray) { return(CreateBitCompactedByteArray(type, isPrimitiveByteArray, array, bitsUsedInLastByte, requiredBits, offsetBytes)); } else { int numberOfBytes = (totalBits - 1) / 8 + 1; numberOfBytes += NUMBER_HEADER_SIZE; // type + rest + requiredBits header. TODO no need to use full bytes Bits bits = Bits.bits(numberOfBytes); bits.Put(( sbyte )type.intValue()); bits.Put(( sbyte )bitsUsedInLastByte); bits.Put(( sbyte )requiredBits); type.writeAll(array, arrayLength, requiredBits, bits); return(bits.AsBytes(offsetBytes)); } }
public void Given_a_byte_list_then_it_roundtrips() { //Given byte[] bytes = new byte[] { 0xF0, 0x01, 0x02, 0x03 }; var bits = new Bits(bytes); Assert.Equal(bytes, bits.AsBytes()); }
public void Given_a_bits_list_then_it_roundtrips() { //Given byte[] bytes = new byte[] { 0xF0, 0x01, 0x02, 0x03 }; var bits1 = new Bits(bytes); var bits2 = new Bits(bits1.AsEnumerable()); Assert.Equal(bytes, bits2.AsBytes()); }