public void GetBytes_OffsetPlusLengthBeyondMax_ThrowsArgumentOutOfRangeException() { IBitCharger instance = this.CreateInstance(); instance.SetBitAt(15, false); Assert.That(() => instance.GetBytes(1, 2), Throws.InstanceOf <ArgumentOutOfRangeException>()); Assert.That(() => instance.GetBytes(2, 1), Throws.InstanceOf <ArgumentOutOfRangeException>()); }
/// <summary> /// Gets all bytes from the charger and converts them into /// <see cref="UInt64"/>. /// </summary> /// <remarks> /// This method gets all bytes from the charger and converts /// them into <see cref="UInt64"/>. /// </remarks> /// <param name="charger"> /// An instance of <see cref="IBitCharger"/> to set the bits /// </param> /// <param name="offset"> /// The byte index to get all affected bits from. /// </param> /// <returns> /// A value of type <see cref="UInt64"/> representing all applied /// bits. /// </returns> public static UInt64 ToUInt64(this IBitCharger charger, Int32 offset) { charger.ValidateInstance(); Byte[] array = charger.GetBytes(offset, sizeof(UInt64)); return(BitConverter.ToUInt64(array, 0)); }
/// <summary> /// Gets all bytes from the charger and converts them into /// <see cref="Int16"/>. /// </summary> /// <remarks> /// This method gets all bytes from the charger and converts /// them into <see cref="Int16"/>. /// </remarks> /// <param name="charger"> /// An instance of <see cref="IBitCharger"/> to set the bits /// </param> /// <param name="offset"> /// The byte index to get all affected bits from. /// </param> /// <returns> /// A value of type <see cref="Int16"/> representing all applied /// bits. /// </returns> public static Int16 ToInt16(this IBitCharger charger, Int32 offset) { charger.ValidateInstance(); Byte[] array = charger.GetBytes(offset, sizeof(Int16)); return(BitConverter.ToInt16(array, 0)); }
/// <summary> /// Gets all bytes from the charger and converts them into /// <see cref="Byte"/>. /// </summary> /// <remarks> /// This method gets all bytes from the charger and converts /// them into <see cref="Byte"/>. /// </remarks> /// <param name="charger"> /// An instance of <see cref="IBitCharger"/> to set the bits /// </param> /// <param name="offset"> /// The byte index to get all affected bits from. /// </param> /// <returns> /// A value of type <see cref="Byte"/> representing all applied /// bits. /// </returns> public static Byte ToByte(this IBitCharger charger, Int32 offset) { charger.ValidateInstance(); Byte[] array = charger.GetBytes(offset, sizeof(Byte)); return(array[0]); }
public void GetBytes_OffsetZeroLengthZero_ResultIsEmpty() { IBitCharger instance = this.CreateInstance(); instance.SetBitAt(15, false); Assert.That(instance.GetBytes(0, 0), Is.Empty); }
public void GetBytes_SourceInt32OffsetZeroBE_ResultAsExpected() { IBitCharger instance = this.CreateInstance(); this.byteOrder.SetupGet(x => x.IsLittleEndian).Returns(false); Int32 expected = 47110815; // BE: 0x02CEDA9F => 00000010 11001110 11011010 10011111 // BE: 0x9F => 10011111 instance.SetBitAt(0, true); instance.SetBitAt(1, true); instance.SetBitAt(2, true); instance.SetBitAt(3, true); instance.SetBitAt(4, true); instance.SetBitAt(5, false); instance.SetBitAt(6, false); instance.SetBitAt(7, true); // BE: 0xDA => 11011010 instance.SetBitAt(8, false); instance.SetBitAt(9, true); instance.SetBitAt(10, false); instance.SetBitAt(11, true); instance.SetBitAt(12, true); instance.SetBitAt(13, false); instance.SetBitAt(14, true); instance.SetBitAt(15, true); // BE: 0xCE => 11001110 instance.SetBitAt(16, false); instance.SetBitAt(17, true); instance.SetBitAt(18, true); instance.SetBitAt(19, true); instance.SetBitAt(20, false); instance.SetBitAt(21, false); instance.SetBitAt(22, true); instance.SetBitAt(23, true); // BE: 0x02 => 00000010 instance.SetBitAt(24, false); instance.SetBitAt(25, true); instance.SetBitAt(26, false); instance.SetBitAt(27, false); instance.SetBitAt(28, false); instance.SetBitAt(29, false); instance.SetBitAt(30, false); instance.SetBitAt(31, false); Byte[] result = instance.GetBytes(0, sizeof(Int32)); Int32 actual = BitConverter.ToInt32(result, 0); Assert.That(actual, Is.EqualTo(expected)); }
public void GetBytes_OffsetZeroLengthUnused_ResultAsExpected() { IBitCharger instance = this.CreateInstance(); // 10101100 (0xAC, 172) instance.SetBitAt(2, true); instance.SetBitAt(3, true); instance.SetBitAt(5, true); instance.SetBitAt(7, true); Byte[] actual = instance.GetBytes(0); Assert.That(actual.Length, Is.EqualTo(1)); Assert.That(actual[0], Is.EqualTo(172)); }
public void GetBytes_OffsetZeroLengthOne_ResultAsExpected() { IBitCharger instance = this.CreateInstance(); // 10101100 (0xAC, 172) instance.SetBitAt(2, true); instance.SetBitAt(3, true); instance.SetBitAt(5, true); instance.SetBitAt(7, true); // 11110000 (0xF0, 240) instance.SetBitAt(12, true); instance.SetBitAt(13, true); instance.SetBitAt(14, true); instance.SetBitAt(15, true); Byte[] actual = instance.GetBytes(0, 2); Assert.That(actual.Length, Is.EqualTo(2)); Assert.That(actual[0], Is.EqualTo(240)); Assert.That(actual[1], Is.EqualTo(172)); }
public void GetBytes_OffsetZeroLengthBelowMin_ThrowsArgumentOutOfRangeException() { IBitCharger instance = this.CreateInstance(); Assert.That(() => instance.GetBytes(0, -1), Throws.InstanceOf <ArgumentOutOfRangeException>()); }