public void Change_Notification_Is_Raised()
        {
            // Arrange
            BitChanges changes       = null;
            var        bitCollection = new BitCollection(false, Enumerable.Range(0, 10).Select(index => false).ToArray());

            bitCollection.BitsChanged += (sender, args) => changes = args.Changes;
            var newData            = Enumerable.Range(0, 5).Select(index => true).Concat(Enumerable.Range(0, 5).Select(index => false)).ToArray();
            var targetChangesCount = newData.Count(boolean => boolean == true);

            // Act
            bitCollection.TransferValuesFrom(newData, 0);

            // Assert
            Assert.That(changes.Count, Is.EqualTo(targetChangesCount));
            var position = uint.MinValue;

            foreach (var change in changes.OrderBy(change => change.Key))
            {
                Assert.True(change.Key == position);
                Assert.False(change.Value.OldValue, $"Old value was not false at position: {position}.");
                Assert.True(change.Value.NewValue, $"New value was not true at position: {position}.");
                position++;
            }
        }
        public void Change_Notification_Is_Raised_If_Dynamically_Truncated()
        {
            // Arrange
            BitChanges changes = null;
            var        modifiableBitCollection = new BitCollection(true, Enumerable.Range(0, 10).Select(index => false).ToArray());

            modifiableBitCollection.BitsChanged += (sender, args) => changes = args.Changes;
            var newData            = Enumerable.Range(0, (int)(modifiableBitCollection.Length / 2)).Select(index => false).ToArray();  //! Make this smaller.
            var targetChangesCount = newData.Length;

            // Act
            modifiableBitCollection.TransferValuesFrom(newData, 0);

            // Assert
            Assert.That(changes.Count, Is.EqualTo(targetChangesCount));
            var position = targetChangesCount;

            foreach (var change in changes.OrderBy(change => change.Key))
            {
                Assert.True(change.Key == position);
                Assert.False(change.Value.OldValue, $"Old value was not false at position: {position}.");
                Assert.Null(change.Value.NewValue, $"New value was not null at position: {position}.");
                position++;
            }
        }
 /// <summary>
 /// Writes the <paramref name="data"/> beginning at <paramref name="start"/>.
 /// </summary>
 /// <param name="data"> The <see cref="BitCollection"/> to write. </param>
 /// <param name="start"> The starting position. </param>
 /// <returns> <c>True</c> on success, otherwise <c>False</c>. </returns>
 internal bool Write(BitCollection data, uint start)
 {
     lock (_lock)
     {
         this.BitCollection.TransferValuesFrom(data, start);
     }
     return(true);
 }
 /// <inheritdoc />
 /// <exception cref="ArgumentOutOfRangeException"> Is thrown if the <paramref name="data"/> could not be converted back into the enumeration. </exception>
 public override TEnum ConvertFromData(BitCollection data)
 {
     // Check if the underlying enumeration type has such a numerical value.
     if (TryConvertToEnum(data, out object numerical, out TEnum enumeration))
     {
         // YES: Return the enumeration value.
         return(enumeration);
     }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="initialData"> Initial data for this region. </param>
        internal DataRegion(byte[] initialData)
        {
            // Save parameters.

            // Initialize fields.
            _lock = new object();
            this.BitCollection = new BitCollection(false, initialData);
        }
        public void Same_Are_Not_Equal()
        {
            // Arrange
            var first  = new BitCollection(false, Enumerable.Range(0, 10).Select(index => (byte)index).ToArray());
            var second = new BitCollection(false, Enumerable.Range(10, 10).Select(index => (byte)index).ToArray());

            // Act
            var areEqual = first.Equals(second);

            // Assert
            Assert.False(areEqual);
        }
        public void Size_Is_Not_Increased()
        {
            // Arrange
            var fixedBitCollection = new BitCollection(false, Enumerable.Range(0, 5).Select(index => false).ToArray());
            var newData            = Enumerable.Range(0, (int)(fixedBitCollection.Length * 2)).Select(index => false).ToArray();   //! Make this larger.
            var targetLength       = fixedBitCollection.Length;

            // Act
            fixedBitCollection.TransferValuesFrom(newData, 0);

            // Assert
            Assert.That(fixedBitCollection.Length, Is.EqualTo(targetLength));
        }
        public void New_Data_Is_Transferred()
        {
            // Arrange
            var bitCollection = new BitCollection(false, Enumerable.Range(0, 5).Select(index => false).ToArray());
            var newData       = Enumerable.Range(0, (int)(bitCollection.Length)).Select(index => true).ToArray();
            var targetLength  = newData.Length;

            // Act
            bitCollection.TransferValuesFrom(newData, 0);

            // Assert
            Assert.True(((bool[])bitCollection).SequenceEqual(newData));
        }
        public void Size_Is_Decreased()
        {
            // Arrange
            var modifiableBitCollection = new BitCollection(true, Enumerable.Range(0, 10).Select(index => false).ToArray());
            var newData      = Enumerable.Range(0, (int)(modifiableBitCollection.Length / 2)).Select(index => false).ToArray();        //! Make this smaller.
            var targetLength = newData.Length;

            // Act
            modifiableBitCollection.TransferValuesFrom(newData, 0);

            // Assert
            Assert.That(modifiableBitCollection.Length, Is.EqualTo(targetLength));
        }
        public void Clones_Are_Equal_But_Not_Same()
        {
            // Arrange
            var bitCollection = new BitCollection(false, Enumerable.Range(0, 10).Select(index => (byte)index).ToArray());
            var clone         = bitCollection.Clone();

            // Act
            var areEqual = bitCollection.Equals(clone);

            // Assert
            Assert.True(areEqual);
            Assert.That(clone, Is.Not.SameAs(bitCollection));
        }
Beispiel #11
0
        public void ReadBits()
        {
            var bitsItem = new BitsPlcItem(dataBlock: this.Data.Datablock, position: 1, bitPosition: BitPosition.X1, bitAmount: 5);

            base.ExecuteTest
            (
                async(plc) =>
            {
                var result = await plc.ReadItemAsync(bitsItem);
                Assert.True(bitsItem.Value == result);

                // Get the target value.
                var booleans         = DataConverter.ToBooleans(this.Data.TargetBytes);
                var relevantBooleans = booleans.Skip(bitsItem.Position * 8 + ((byte)bitsItem.BitPosition)).Take((int)bitsItem.Value.Length).ToArray();
                var target           = new BitCollection(false, relevantBooleans);

                Assert.True(result == target);
            }
            );
        }
        public void Change_Tracking_Is_Executed_Fast()
        {
            // Arrange
            var changes            = 10000;
            var initialData        = Enumerable.Range(0, changes).Select(index => false).ToArray();
            var modifiedData       = Enumerable.Range(0, changes).Select(index => true).ToArray();
            var largeBitCollection = new BitCollection(false, initialData);
            var stopwatch          = new Stopwatch();

            // Act
            stopwatch.Start();
            largeBitCollection.TransferValuesFrom(modifiedData);
            stopwatch.Stop();

            // Assert
            var elapsed = stopwatch.ElapsedMilliseconds;

            Console.WriteLine($"Change tracking of {changes} changes took {elapsed}ms.");
            Assert.That(elapsed, Is.LessThanOrEqualTo(20));
            Assert.True(((bool[])largeBitCollection).All(b => true));
        }
        public void WriteBits()
        {
            var allFalse    = new BitCollection(false, new[] { false, false, false, false, false, false });
            var allTrue     = new BitCollection(false, new[] { true, true, true, true, true, true });
            var partialTrue = new BitCollection(false, new[] { true, true, true, true });
            var mixed       = new BitCollection(false, new[] { false, false, true, true, true, true, false, false });

            var fullItem         = new BitsPlcItem(dataBlock: this.Data.Datablock, position: this.Data.StartOfModifiableBytes, BitPosition.X0, bitAmount: 8);
            var writeItem        = new BitsPlcItem(dataBlock: this.Data.Datablock, position: this.Data.StartOfModifiableBytes, BitPosition.X1, initialValue: allTrue);
            var partialWriteItem = new BitsPlcItem(dataBlock: this.Data.Datablock, position: this.Data.StartOfModifiableBytes, BitPosition.X2, initialValue: partialTrue);

            base.ExecuteTest
            (
                async(plc) =>
            {
                // Clear the whole byte.
                fullItem.Value.SetAllBitsTo(false);
                await plc.WriteItemAsync(fullItem);

                var result = await plc.WriteItemWithValidationAsync(writeItem);
                Assert.True(result);

                // Set everything to false.
                writeItem.Value.SetAllBitsTo(false);
                result = await plc.WriteItemWithValidationAsync(writeItem);
                Assert.True(result);

                // Make only a part true.
                result = await plc.WriteItemAsync(partialWriteItem);
                Assert.True(result);

                // Read everything and compare those two.
                var mirror = await plc.ReadItemAsync(fullItem);
                Assert.True(mirror.SequenceEqual(mixed));
            }
            );
        }
Beispiel #14
0
 /// <inheritdoc />
 public override byte[] ConvertFromData(BitCollection data)
 {
     return(data);
 }
Beispiel #15
0
 /// <inheritdoc />
 public override uint ConvertFromData(BitCollection data)
 {
     return(DataConverter.ToUInt32(data, Endianness));
 }
Beispiel #16
0
 /// <inheritdoc />
 public override bool ConvertFromData(BitCollection data)
 {
     return(data[0]);
 }
Beispiel #17
0
 /// <inheritdoc />
 public override short ConvertFromData(BitCollection data)
 {
     return(DataConverter.ToInt16(data, Endianness));
 }
Beispiel #18
0
 /// <inheritdoc />
 /// <summary>
 /// Constructor with initial value.
 /// </summary>
 public BitsPlcItem(PlcItemType type, ushort dataBlock, ushort position, BitPosition bitPosition, BitCollection initialValue, string identifier = default)
     : base(type, dataBlock, position, bitPosition, initialValue, identifier)
 {
 }
 /// <inheritdoc />
 public override long ConvertFromData(BitCollection data)
 {
     return(DataConverter.ToInt64(data, Endianness));
 }