Exemplo n.º 1
0
        private void OnBitsChanged(BitChanges changes)
        {
            if (!changes.Any())
            {
                return;
            }

            lock (_valueAccessLockObject)
            {
                this.SynchronizeBytes();

                // Enqueue the event.
                _eventQueue.Enqueue(changes);
            }

            // Execute all changed events.
            lock (_valueChangeLockObject)
            {
                while (_eventQueue.TryDequeue(out var changeValue))
                {
                    // Raise the event.
                    this.BitsChanged?.Invoke(this, new BitsChangedEventArgs(changeValue));
                }
            }
        }
        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++;
            }
        }
        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++;
            }
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="changes"> <see cref="Changes"/> </param>
 public BitsChangedEventArgs(BitChanges changes)
 {
     this.Changes = changes;
 }