• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DemoBytom / DemoEngine / 22109264872

17 Feb 2026 05:44PM UTC coverage: 29.986% (+0.2%) from 29.787%
22109264872

push

coveralls.net

web-flow
Merge pull request #502 from DemoBytom/feature/small_fixes

Fixes #501 
Turns on several diagnostics as warnings (and thus errors):
- [CA1852](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1852)
- [CA1001](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1001)
- [CA2213](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2213)
- [CA2216](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2216)
- [CA2215](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2215)
- [CA1816](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1816)

639 of 2131 relevant lines covered (29.99%)

0.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.46
/src/Demo.Tools.Common/Collections/CircularQueue.cs
1
// Copyright © Michał Dembski and contributors.
2
// Distributed under MIT license. See LICENSE file in the root for more information.
3

4
using System.Collections;
5
using System.Collections.Concurrent;
6
using System.Diagnostics.CodeAnalysis;
7

8
namespace Demo.Tools.Common.Collections;
9

10
public sealed class CircularQueue<T>
11
    : IReadOnlyCollection<T>,
12
      ICollection,
13
      IDisposable
14
{
15
    private readonly int _capacity;
16
    private readonly ConcurrentQueue<T> _buffer;
17

18
    private readonly ReaderWriterLockSlim _lock = new();
2✔
19
    private bool _disposedValue;
20

21
    public CircularQueue(int capacity)
2✔
22
    {
23
        if (capacity < 0)
2✔
24
        {
25
            throw new InvalidOperationException($"{nameof(capacity)} cannot be negative!");
1✔
26
        }
27

28
        _capacity = capacity;
2✔
29
        _buffer = new ConcurrentQueue<T>();
2✔
30
    }
2✔
31

32
    /// <summary>
33
    /// Enqueues new element onto the queue, removing the oldest one if capacity was reached
34
    /// </summary>
35
    /// <param name="value"></param>
36
    public void Enqueue(T value)
37
    {
38
        _lock.EnterWriteLock();
2✔
39
        try
40
        {
41
            if (_buffer.Count == _capacity)
2✔
42
            {
43
                _ = _buffer.TryDequeue(out _);
1✔
44
            }
45

46
            _buffer.Enqueue(value);
2✔
47
        }
2✔
48
        finally
49
        {
50
            _lock.ExitWriteLock();
2✔
51
        }
2✔
52
    }
2✔
53

54
    /// <summary>
55
    /// Dequeues top element from the queue
56
    /// </summary>
57
    /// <returns></returns>
58
    /// <exception cref="InvalidOperationException">If can't dequeue</exception>
59
    public T Dequeue()
60
    {
61
        _lock.EnterWriteLock();
1✔
62
        try
63
        {
64
            return _buffer.TryDequeue(out var retVal)
1✔
65
                ? retVal
1✔
66
                : throw new InvalidOperationException("Dequeue failed!");
1✔
67
        }
68
        finally
69
        {
70
            _lock.ExitWriteLock();
1✔
71
        }
1✔
72
    }
1✔
73

74
    /// <summary>
75
    /// Tries to dequeue top element from the queue
76
    /// </summary>
77
    /// <param name="result"></param>
78
    /// <returns></returns>
79
    public bool TryDequeue([MaybeNullWhen(false)] out T result)
80
        => _buffer.TryDequeue(out result);
2✔
81

82
    /// <summary>
83
    /// Returns top element from the queue without dequeuing it
84
    /// </summary>
85
    /// <returns></returns>
86
    /// <exception cref="InvalidOperationException">If can't peek</exception>
87
    public T Peek()
88
        => TryPeek(out var retVal)
1✔
89
            ? retVal
1✔
90
            : throw new InvalidOperationException("Peek failed!");
1✔
91

92
    /// <summary>
93
    /// Tries to return top element from the queue without dequeuing it
94
    /// </summary>
95
    /// <param name="result"></param>
96
    /// <returns></returns>
97
    public bool TryPeek([MaybeNullWhen(false)] out T result)
98
    {
99
        _lock.EnterReadLock();
1✔
100
        try
101
        {
102
            return _buffer.TryPeek(out result);
1✔
103
        }
104
        finally
105
        {
106
            _lock.ExitReadLock();
1✔
107
        }
1✔
108
    }
1✔
109

110
    /// <summary>
111
    /// Gets the number of elements in the collection.
112
    /// </summary>
113
    /// <returns>The number of elements in the collection.</returns>
114
    public int Count => _buffer.Count;
1✔
115

116
    /// <summary>
117
    /// Gets a value indicating whether access to the <see cref="ICollection"></see> is
118
    /// synchronized (thread safe).
119
    /// </summary>
120
    /// <returns>
121
    /// true if access to the <see cref="ICollection"></see> is synchronized (thread safe);
122
    /// otherwise, false.
123
    /// </returns>
124
    public bool IsSynchronized => false;
×
125

126
    /// <summary>
127
    /// Gets an object that can be used to synchronize access to the <see cref="ICollection"></see>.
128
    /// </summary>
129
    /// <returns>An object that can be used to synchronize access to the <see cref="ICollection"></see>.</returns>
130
    object ICollection.SyncRoot => this;
×
131

132
    /// <summary>
133
    /// Copies the elements of the <see cref="CircularQueue{T}"></see> to an
134
    /// <see cref="Array"></see>, starting at a particular <see cref="Array"></see> index.
135
    /// </summary>
136
    /// <param name="array">
137
    /// The one-dimensional <see cref="Array"></see> that is the destination of the elements
138
    /// copied from <see cref="CircularQueue{T}"></see>. The <see cref="Array"></see> must have
139
    /// zero-based indexing.
140
    /// </param>
141
    /// <param name="index">The zero-based index in array at which copying begins.</param>
142
    /// <exception cref="ArgumentNullException">
143
    /// <paramref name="array">array</paramref> is null.
144
    /// </exception>
145
    /// <exception cref="ArgumentOutOfRangeException">
146
    /// <paramref name="index">index</paramref> is less than zero.
147
    /// </exception>
148
    /// <exception cref="ArgumentException">
149
    /// <paramref name="array">array</paramref> is multidimensional. -or- The number of elements
150
    /// in the source <see cref="CircularQueue{T}"></see> is greater than the available space
151
    /// from <paramref name="index">index</paramref> to the end of the destination
152
    /// <paramref name="array">array</paramref>. -or- The type of the source
153
    /// <see cref="CircularQueue{T}"></see> cannot be cast automatically to the type of the
154
    /// destination <paramref name="array">array</paramref>.
155
    /// </exception>
156
    public void CopyTo(T[] array, int index)
157
    {
158
        _lock.EnterReadLock();
1✔
159
        try
160
        {
161
            _buffer.CopyTo(array, index);
1✔
162
        }
1✔
163
        finally
164
        {
165
            _lock.ExitReadLock();
1✔
166
        }
1✔
167
    }
1✔
168

169
    public IEnumerator<T> GetEnumerator() => _buffer.GetEnumerator();
×
170

171
    IEnumerator IEnumerable.GetEnumerator() => _buffer.GetEnumerator();
×
172

173
    /// <summary>
174
    /// Copies the elements of the <see cref="CircularQueue{T}"></see> to an
175
    /// <see cref="Array"></see>, starting at a particular <see cref="Array"></see> index.
176
    /// </summary>
177
    /// <param name="array">
178
    /// The one-dimensional <see cref="Array"></see> that is the destination of the elements
179
    /// copied from <see cref="CircularQueue{T}"></see>. The <see cref="Array"></see> must have
180
    /// zero-based indexing.
181
    /// </param>
182
    /// <param name="index">The zero-based index in array at which copying begins.</param>
183
    /// <exception cref="ArgumentNullException">
184
    /// <paramref name="array">array</paramref> is null.
185
    /// </exception>
186
    /// <exception cref="ArgumentOutOfRangeException">
187
    /// <paramref name="index">index</paramref> is less than zero.
188
    /// </exception>
189
    /// <exception cref="ArgumentException">
190
    /// <paramref name="array">array</paramref> is multidimensional. -or- The number of elements
191
    /// in the source <see cref="CircularQueue{T}"></see> is greater than the available space
192
    /// from <paramref name="index">index</paramref> to the end of the destination
193
    /// <paramref name="array">array</paramref>. -or- The type of the source
194
    /// <see cref="CircularQueue{T}"></see> cannot be cast automatically to the type of the
195
    /// destination <paramref name="array">array</paramref>.
196
    /// </exception>
197
    void ICollection.CopyTo(Array array, int index) => CopyTo((T[])array, index);
×
198

199
    ~CircularQueue() => _lock?.Dispose();
×
200

201
    private void Dispose(bool disposing)
202
    {
203
        if (!_disposedValue)
1✔
204
        {
205
            if (disposing)
1✔
206
            {
207
                _lock.Dispose();
1✔
208
            }
209

210
            _disposedValue = true;
1✔
211
        }
212
    }
1✔
213

214
    public void Dispose()
215
    {
216
        // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
217
        Dispose(disposing: true);
1✔
218
        GC.SuppressFinalize(this);
1✔
219
    }
1✔
220
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc