public void Serialization_SerializeAndDeserialize_NeverThrowsAndPropertiesAsEqual()
        {
            try
            {
                IBitCharger instance = this.CreateInstance();

                instance.SetBitAt(0, true);
                instance.SetBitAt(242, true);

                using (MemoryStream stream = new MemoryStream())
                {
                    BinaryFormatter formatter = new BinaryFormatter();

                    formatter.Serialize(stream, instance);

                    stream.Position = 0;

                    IBitCharger actual = formatter.Deserialize(stream) as IBitCharger;

                    Assert.That(actual, Is.Not.Null);
                    Assert.That(actual.Capacity, Is.EqualTo(instance.Capacity));
                    Assert.That(actual.Length, Is.EqualTo(instance.Length));
                    Assert.That(actual.Content, Is.EqualTo(instance.Content));
                }
            }
            catch (Exception error)
            {
                Assert.That(false, error.Message);
            }
        }
예제 #2
0
 /// <summary>
 /// Validates the instance of <see cref="IBitCharger"/>.
 /// </summary>
 /// <remarks>
 /// This method validates the instance of <see cref="IBitCharger"/>.
 /// </remarks>
 /// <param name="charger">
 /// The instance of <see cref="IBitCharger"/> to be validated.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// This exception is thrown in case of <paramref name="charger"/>
 /// is null.
 /// </exception>
 private static void ValidateInstance(this IBitCharger charger)
 {
     if (charger is null)
     {
         throw new ArgumentNullException(nameof(charger));
     }
 }
        public void Charge_SourceWithWrongLength_ReverseContentAsExpected(String source, String expected)
        {
            IBitCharger instance = this.CreateInstance();

            instance.Charge(source, true);

            Assert.That(instance.Content, Is.EqualTo(expected));
        }
        public void Charge_SourceMatchesEightBitRestrictionButContainsInvalidCharacters_ContentAsExpected(String source, String expected)
        {
            IBitCharger instance = this.CreateInstance();

            instance.Charge(source);

            Assert.That(instance.Content, Is.EqualTo(expected));
        }
        public void GetBytes_OffsetZeroLengthZero_ResultIsEmpty()
        {
            IBitCharger instance = this.CreateInstance();

            instance.SetBitAt(15, false);

            Assert.That(instance.GetBytes(0, 0), Is.Empty);
        }
        public void GetBytes_OffsetZeroLengthBeyondMax_ThrowsArgumentOutOfRangeException()
        {
            IBitCharger instance = this.CreateInstance();

            instance.SetBitAt(15, false);

            Assert.That(() => instance.GetBytes(0, instance.Bytes + 1), Throws.InstanceOf <ArgumentOutOfRangeException>());
        }
        public void HasBitAt_IndexIsInRange_ResultAsExpected(Int32 index, Int32 actual, Boolean expected)
        {
            IBitCharger instance = this.CreateInstance();

            instance.SetBitAt(index, true);

            Assert.That(instance.HasBitAt(actual), Is.EqualTo(expected));
        }
        public void SetBitAt_WithIndexAndEnabled_ContentAsExpected(Int32 index, String expected)
        {
            IBitCharger instance = this.CreateInstance();

            instance.SetBitAt(index, true);

            Assert.That(instance.Content, Is.EqualTo(expected));
        }
예제 #9
0
        /// <summary>
        /// Sets the bits of <paramref name="value"/> into the
        /// <paramref name="charger"/> at offset zero.
        /// </summary>
        /// <remarks>
        /// This method sets all bits of <paramref name="value"/>
        /// into the <paramref name="charger"/> at offset zero.
        /// </remarks>
        /// <param name="charger">
        /// An instance of <see cref="IBitCharger"/> to set the bits
        /// into.
        /// </param>
        /// <param name="value">
        /// The bits to set.
        /// </param>
        /// <param name="offset">
        /// The byte index to start with changing the bits.
        /// </param>
        public static void SetBytes(this IBitCharger charger, SByte value, Int32 offset)
        {
            charger.ValidateInstance();

            Byte[] array = new Byte[] { (Byte)value };

            charger.SetBytes(array, offset);
        }
        public void SetBitAt_IndexIsLessThanDefaultLength_PropertiesAsExpected()
        {
            IBitCharger instance = this.CreateInstance();

            instance.SetBitAt(7, true);

            Assert.That(instance.Length, Is.EqualTo(8));
        }
예제 #11
0
        /// <summary>
        /// Sets the bits of <paramref name="value"/> into the
        /// <paramref name="charger"/> at offset zero.
        /// </summary>
        /// <remarks>
        /// This method sets all bits of <paramref name="value"/>
        /// into the <paramref name="charger"/> at offset zero.
        /// </remarks>
        /// <param name="charger">
        /// An instance of <see cref="IBitCharger"/> to set the bits
        /// into.
        /// </param>
        /// <param name="value">
        /// The bits to set.
        /// </param>
        /// <param name="offset">
        /// The byte index to start with changing the bits.
        /// </param>
        public static void SetBytes(this IBitCharger charger, UInt64 value, Int32 offset)
        {
            charger.ValidateInstance();

            Byte[] array = BitConverter.GetBytes(value);

            charger.SetBytes(array, offset);
        }
예제 #12
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]);
        }
예제 #13
0
        public void ToUInt64_UInt64OverloadValueAndLengthInstanceIsNull_ThrowsArgumentNullException()
        {
            IBitCharger instance = null;

            Int32 offset = 42;

            Assert.That(() => instance.ToUInt64(offset), Throws.ArgumentNullException);
        }
예제 #14
0
        /// <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));
        }
예제 #15
0
        public void SetBytes_UInt64OverloadOnlyValueInstanceIsNull_ThrowsArgumentNullException()
        {
            IBitCharger instance = null;

            UInt64 value = 42;

            Assert.That(() => instance.SetBytes(value), Throws.ArgumentNullException);
        }
예제 #16
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));
        }
예제 #17
0
        public void SetBytes_UInt64OverloadValueAndLengthInstanceIsNull_ThrowsArgumentNullException()
        {
            IBitCharger instance = null;

            UInt64 value  = 42;
            Int32  offset = 42;

            Assert.That(() => instance.SetBytes(value, offset), Throws.ArgumentNullException);
        }
        public void SetBitAt_IndexIsGreaterThanDefaultLength_PropertiesAsExpected()
        {
            IBitCharger instance = this.CreateInstance();

            instance.SetBitAt(135, true);

            Assert.That(instance.Capacity, Is.EqualTo(264));
            Assert.That(instance.Length, Is.EqualTo(136));
        }
        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));
        }
예제 #20
0
        public void ToUInt64_OnlyValue_ResultAsExpected()
        {
            IBitCharger instance = this.CreateInstance();

            UInt64 expected = 3026418949640084127; // LE: 0x9FDACE020000002A => 10011111 11011010 11001110 00000010 00000000 00000000 00000000 00101010

            Byte[] array = new Byte[] { 0x9F, 0xDA, 0xCE, 0x02, 0x00, 0x00, 0x00, 0x2A };

            instance.SetBytes(array);

            Assert.That(instance.ToUInt64(), Is.EqualTo(expected));
        }
예제 #21
0
        public void SetBytes_UInt16OverloadOnlyValue_ResultAsExpected()
        {
            IBitCharger instance = this.CreateInstance();

            UInt16 value = 4711; // LE: 0x6712 => 01100111 00010010

            String expected = "0110011100010010";

            instance.SetBytes(value);

            Assert.That(instance.Content, Is.EqualTo(expected));
        }
예제 #22
0
        public void SetBytes_SByteOverloadOnlyValue_ResultAsExpected()
        {
            IBitCharger instance = this.CreateInstance();

            SByte value = 42; // LE: 0x2A => 00101010

            String expected = "00101010";

            instance.SetBytes(value);

            Assert.That(instance.Content, Is.EqualTo(expected));
        }
예제 #23
0
        public void ToUInt32_OnlyValue_ResultAsExpected()
        {
            IBitCharger instance = this.CreateInstance();

            UInt32 expected = 47110815; // LE: 0x9FDACE02 => 10011111 11011010 11001110 00000010

            Byte[] array = new Byte[] { 0x9F, 0xDA, 0xCE, 0x02 };

            instance.SetBytes(array);

            Assert.That(instance.ToUInt32(), Is.EqualTo(expected));
        }
예제 #24
0
        public void ToUInt16_OnlyValue_ResultAsExpected()
        {
            IBitCharger instance = this.CreateInstance();

            UInt16 expected = 4711; // LE: 0x6712 => 01100111 00010010

            Byte[] array = new Byte[] { 0x67, 0x12 };

            instance.SetBytes(array);

            Assert.That(instance.ToUInt16(), Is.EqualTo(expected));
        }
예제 #25
0
        public void SetBytes_UInt64OverloadOnlyValue_ResultAsExpected()
        {
            IBitCharger instance = this.CreateInstance();

            UInt64 value = 47110815; // LE: 0x9FDACE0200000000 => 10011111 11011010 11001110 00000010 00000000 00000000 00000000 00000000

            String expected = "1001111111011010110011100000001000000000000000000000000000000000";

            instance.SetBytes(value);

            Assert.That(instance.Content, Is.EqualTo(expected));
        }
예제 #26
0
        public void ToByte_OnlyValue_ResultAsExpected()
        {
            IBitCharger instance = this.CreateInstance();

            Byte expected = 42; // LE: 0x2A => 00101010

            Byte[] array = new Byte[] { 0x2A };

            instance.SetBytes(array);

            Assert.That(instance.ToByte(), Is.EqualTo(expected));
        }
예제 #27
0
        public void SetBytes_Int64OverloadValueAndOffset_ResultAsExpected()
        {
            IBitCharger instance = this.CreateInstance();

            Int64 value  = 47110815; // LE: 0x9FDACE0200000000 => 10011111 11011010 11001110 00000010 00000000 00000000 00000000 00000000
            Int32 offset = 2;

            String expected = "10011111110110101100111000000010000000000000000000000000000000000000000000000000";

            instance.SetBytes(value, offset);

            Assert.That(instance.Content, Is.EqualTo(expected));
        }
예제 #28
0
        public void ToInt64_ValueAndOffset_ResultAsExpected()
        {
            IBitCharger instance = this.CreateInstance();

            Int64 expected = 3026418949640084127; // LE: 0x9FDACE020000002A => 10011111 11011010 11001110 00000010 00000000 00000000 00000000 00101010
            Int32 offset   = 2;

            Byte[] array = new Byte[] { 0x9F, 0xDA, 0xCE, 0x02, 0x00, 0x00, 0x00, 0x2A };

            instance.SetBytes(array, offset);

            Assert.That(instance.ToInt64(offset), Is.EqualTo(expected));
        }
예제 #29
0
        public void ToInt32_ValueAndOffset_ResultAsExpected()
        {
            IBitCharger instance = this.CreateInstance();

            Int32 expected = 47110815; // LE: 0x9FDACE02 => 10011111 11011010 11001110 00000010
            Int32 offset   = 2;

            Byte[] array = new Byte[] { 0x9F, 0xDA, 0xCE, 0x02 };

            instance.SetBytes(array, offset);

            Assert.That(instance.ToInt32(offset), Is.EqualTo(expected));
        }
예제 #30
0
        public void SetBytes_SByteOverloadValueAndOffset_ResultAsExpected()
        {
            IBitCharger instance = this.CreateInstance();

            SByte value  = 42; // LE: 0x2A => 00101010
            Int32 offset = 2;

            String expected = "001010100000000000000000";

            instance.SetBytes(value, offset);

            Assert.That(instance.Content, Is.EqualTo(expected));
        }