-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpscRingBufferTests.cs
More file actions
158 lines (135 loc) · 3.43 KB
/
Copy pathSpscRingBufferTests.cs
File metadata and controls
158 lines (135 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) ktsu.dev
// All rights reserved.
// Licensed under the MIT license.
namespace ktsu.Containers.Tests;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class SpscRingBufferTests
{
[TestMethod]
public void Constructor_NonPositiveCapacity_Throws()
{
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => new SpscRingBuffer<int>(0));
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => new SpscRingBuffer<int>(-1));
}
[TestMethod]
public void Capacity_IsAtLeastRequested()
{
SpscRingBuffer<int> buffer = new(5);
Assert.IsTrue(buffer.Capacity >= 5, "Usable capacity must be at least the requested amount.");
}
[TestMethod]
public void NewBuffer_IsEmpty()
{
SpscRingBuffer<int> buffer = new(4);
Assert.IsTrue(buffer.IsEmpty);
Assert.AreEqual(0, buffer.Count);
Assert.IsFalse(buffer.TryDequeue(out _));
}
[TestMethod]
public void EnqueueDequeue_PreservesFifoOrder()
{
SpscRingBuffer<int> buffer = new(8);
for (int i = 0; i < 5; i++)
{
Assert.IsTrue(buffer.TryEnqueue(i));
}
for (int i = 0; i < 5; i++)
{
Assert.IsTrue(buffer.TryDequeue(out int value));
Assert.AreEqual(i, value);
}
Assert.IsTrue(buffer.IsEmpty);
}
[TestMethod]
public void TryEnqueue_WhenFull_ReturnsFalse()
{
SpscRingBuffer<int> buffer = new(4);
int enqueued = 0;
while (buffer.TryEnqueue(enqueued))
{
enqueued++;
}
Assert.IsTrue(enqueued >= 4, "Should accept at least the requested capacity before reporting full.");
Assert.IsFalse(buffer.TryEnqueue(999));
}
[TestMethod]
public void TryPeek_DoesNotRemoveElement()
{
SpscRingBuffer<int> buffer = new(4);
buffer.TryEnqueue(42);
Assert.IsTrue(buffer.TryPeek(out int peeked));
Assert.AreEqual(42, peeked);
Assert.AreEqual(1, buffer.Count);
Assert.IsTrue(buffer.TryDequeue(out int dequeued));
Assert.AreEqual(42, dequeued);
}
[TestMethod]
public void WrapAround_ReusesSlotsCorrectly()
{
SpscRingBuffer<int> buffer = new(4);
// Cycle through many more items than capacity to force repeated wraparound.
for (int i = 0; i < 1000; i++)
{
Assert.IsTrue(buffer.TryEnqueue(i));
Assert.IsTrue(buffer.TryDequeue(out int value));
Assert.AreEqual(i, value);
}
Assert.IsTrue(buffer.IsEmpty);
}
[TestMethod]
public async Task ConcurrentProducerConsumer_TransfersAllItemsInOrder()
{
const int itemCount = 1_000_000;
SpscRingBuffer<int> buffer = new(1024);
Task producer = Task.Run(() =>
{
int produced = 0;
while (produced < itemCount)
{
if (buffer.TryEnqueue(produced))
{
produced++;
}
else
{
Thread.SpinWait(1);
}
}
});
Task<bool> consumer = Task.Run(() =>
{
int expected = 0;
while (expected < itemCount)
{
if (buffer.TryDequeue(out int value))
{
if (value != expected)
{
return false;
}
expected++;
}
else
{
Thread.SpinWait(1);
}
}
return true;
});
await Task.WhenAll(producer, consumer).ConfigureAwait(false);
Assert.IsTrue(await consumer.ConfigureAwait(false), "All items must be received exactly once and in order.");
Assert.IsTrue(buffer.IsEmpty);
}
[TestMethod]
public void Dequeue_ReferenceType_ReleasesReference()
{
SpscRingBuffer<string> buffer = new(4);
buffer.TryEnqueue("hello");
Assert.IsTrue(buffer.TryDequeue(out string? value));
Assert.AreEqual("hello", value);
Assert.IsTrue(buffer.IsEmpty);
}
}