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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
using NUnit.Framework;
using Ryujinx.Common.Extensions;
using Ryujinx.Memory;
using System;
using System.Buffers;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Ryujinx.Tests.Common.Extensions
{
public class SequenceReaderExtensionsTests
{
[TestCase(null)]
[TestCase(sizeof(int) + 1)]
public void GetRefOrRefToCopy_ReadsMultiSegmentedSequenceSuccessfully(int? maxSegmentSize)
{
// Arrange
MyUnmanagedStruct[] originalStructs = EnumerateNewUnmanagedStructs().Take(3).ToArray();
ReadOnlySequence<byte> sequence =
CreateSegmentedByteSequence(originalStructs, maxSegmentSize ?? Unsafe.SizeOf<MyUnmanagedStruct>());
var sequenceReader = new SequenceReader<byte>(sequence);
foreach (var original in originalStructs)
{
// Act
ref readonly MyUnmanagedStruct read = ref sequenceReader.GetRefOrRefToCopy<MyUnmanagedStruct>(out _);
// Assert
MyUnmanagedStruct.Assert(Assert.AreEqual, original, read);
}
}
[Test]
public void GetRefOrRefToCopy_FragmentedSequenceReturnsRefToCopy()
{
// Arrange
MyUnmanagedStruct[] originalStructs = EnumerateNewUnmanagedStructs().Take(1).ToArray();
ReadOnlySequence<byte> sequence = CreateSegmentedByteSequence(originalStructs, 3);
var sequenceReader = new SequenceReader<byte>(sequence);
foreach (var original in originalStructs)
{
// Act
ref readonly MyUnmanagedStruct read = ref sequenceReader.GetRefOrRefToCopy<MyUnmanagedStruct>(out var copy);
// Assert
MyUnmanagedStruct.Assert(Assert.AreEqual, original, read);
MyUnmanagedStruct.Assert(Assert.AreEqual, read, copy);
}
}
[Test]
public void GetRefOrRefToCopy_ContiguousSequenceReturnsRefToBuffer()
{
// Arrange
MyUnmanagedStruct[] originalStructs = EnumerateNewUnmanagedStructs().Take(1).ToArray();
ReadOnlySequence<byte> sequence = CreateSegmentedByteSequence(originalStructs, int.MaxValue);
var sequenceReader = new SequenceReader<byte>(sequence);
foreach (var original in originalStructs)
{
// Act
ref readonly MyUnmanagedStruct read = ref sequenceReader.GetRefOrRefToCopy<MyUnmanagedStruct>(out var copy);
// Assert
MyUnmanagedStruct.Assert(Assert.AreEqual, original, read);
MyUnmanagedStruct.Assert(Assert.AreNotEqual, read, copy);
}
}
[Test]
public void GetRefOrRefToCopy_ThrowsWhenNotEnoughData()
{
// Arrange
MyUnmanagedStruct[] originalStructs = EnumerateNewUnmanagedStructs().Take(1).ToArray();
ReadOnlySequence<byte> sequence = CreateSegmentedByteSequence(originalStructs, int.MaxValue);
// Act/Assert
Assert.Throws<ArgumentOutOfRangeException>(() =>
{
var sequenceReader = new SequenceReader<byte>(sequence);
sequenceReader.Advance(1);
ref readonly MyUnmanagedStruct result = ref sequenceReader.GetRefOrRefToCopy<MyUnmanagedStruct>(out _);
});
}
[Test]
public void ReadLittleEndian_Int32_RoundTripsSuccessfully()
{
// Arrange
const int TestValue = 0x1234abcd;
byte[] buffer = new byte[sizeof(int)];
BinaryPrimitives.WriteInt32LittleEndian(buffer.AsSpan(), TestValue);
var sequenceReader = new SequenceReader<byte>(new ReadOnlySequence<byte>(buffer));
// Act
sequenceReader.ReadLittleEndian(out int roundTrippedValue);
// Assert
Assert.AreEqual(TestValue, roundTrippedValue);
}
[Test]
public void ReadLittleEndian_Int32_ResultIsNotBigEndian()
{
// Arrange
const int TestValue = 0x1234abcd;
byte[] buffer = new byte[sizeof(int)];
BinaryPrimitives.WriteInt32BigEndian(buffer.AsSpan(), TestValue);
var sequenceReader = new SequenceReader<byte>(new ReadOnlySequence<byte>(buffer));
// Act
sequenceReader.ReadLittleEndian(out int roundTrippedValue);
// Assert
Assert.AreNotEqual(TestValue, roundTrippedValue);
}
[Test]
public void ReadLittleEndian_Int32_ThrowsWhenNotEnoughData()
{
// Arrange
const int TestValue = 0x1234abcd;
byte[] buffer = new byte[sizeof(int)];
BinaryPrimitives.WriteInt32BigEndian(buffer.AsSpan(), TestValue);
// Act/Assert
Assert.Throws<ArgumentOutOfRangeException>(() =>
{
var sequenceReader = new SequenceReader<byte>(new ReadOnlySequence<byte>(buffer));
sequenceReader.Advance(1);
sequenceReader.ReadLittleEndian(out int roundTrippedValue);
});
}
[Test]
public void ReadUnmanaged_ContiguousSequence_Succeeds()
=> ReadUnmanaged_Succeeds(int.MaxValue);
[Test]
public void ReadUnmanaged_FragmentedSequence_Succeeds()
=> ReadUnmanaged_Succeeds(sizeof(int) + 1);
[Test]
public void ReadUnmanaged_ThrowsWhenNotEnoughData()
{
// Arrange
MyUnmanagedStruct[] originalStructs = EnumerateNewUnmanagedStructs().Take(1).ToArray();
ReadOnlySequence<byte> sequence = CreateSegmentedByteSequence(originalStructs, int.MaxValue);
// Act/Assert
Assert.Throws<ArgumentOutOfRangeException>(() =>
{
var sequenceReader = new SequenceReader<byte>(sequence);
sequenceReader.Advance(1);
sequenceReader.ReadUnmanaged(out MyUnmanagedStruct read);
});
}
[Test]
public void SetConsumed_ContiguousSequence_SucceedsWhenValid()
=> SetConsumed_SucceedsWhenValid(int.MaxValue);
[Test]
public void SetConsumed_FragmentedSequence_SucceedsWhenValid()
=> SetConsumed_SucceedsWhenValid(sizeof(int) + 1);
[Test]
public void SetConsumed_ThrowsWhenBeyondActualLength()
{
const int StructCount = 2;
// Arrange
MyUnmanagedStruct[] originalStructs = EnumerateNewUnmanagedStructs().Take(StructCount).ToArray();
ReadOnlySequence<byte> sequence = CreateSegmentedByteSequence(originalStructs, MyUnmanagedStruct.SizeOf);
Assert.Throws<ArgumentOutOfRangeException>(() =>
{
var sequenceReader = new SequenceReader<byte>(sequence);
sequenceReader.SetConsumed(MyUnmanagedStruct.SizeOf * StructCount + 1);
});
}
private static void ReadUnmanaged_Succeeds(int maxSegmentLength)
{
// Arrange
MyUnmanagedStruct[] originalStructs = EnumerateNewUnmanagedStructs().Take(3).ToArray();
ReadOnlySequence<byte> sequence = CreateSegmentedByteSequence(originalStructs, maxSegmentLength);
var sequenceReader = new SequenceReader<byte>(sequence);
foreach (var original in originalStructs)
{
// Act
sequenceReader.ReadUnmanaged(out MyUnmanagedStruct read);
// Assert
MyUnmanagedStruct.Assert(Assert.AreEqual, original, read);
}
}
private static void SetConsumed_SucceedsWhenValid(int maxSegmentLength)
{
// Arrange
MyUnmanagedStruct[] originalStructs = EnumerateNewUnmanagedStructs().Take(2).ToArray();
ReadOnlySequence<byte> sequence = CreateSegmentedByteSequence(originalStructs, maxSegmentLength);
var sequenceReader = new SequenceReader<byte>(sequence);
static void SetConsumedAndAssert(scoped ref SequenceReader<byte> sequenceReader, long consumed)
{
sequenceReader.SetConsumed(consumed);
Assert.AreEqual(consumed, sequenceReader.Consumed);
}
// Act/Assert
ref readonly MyUnmanagedStruct struct0A = ref sequenceReader.GetRefOrRefToCopy<MyUnmanagedStruct>(out _);
Assert.AreEqual(sequenceReader.Consumed, MyUnmanagedStruct.SizeOf);
SetConsumedAndAssert(ref sequenceReader, 0);
ref readonly MyUnmanagedStruct struct0B = ref sequenceReader.GetRefOrRefToCopy<MyUnmanagedStruct>(out _);
MyUnmanagedStruct.Assert(Assert.AreEqual, struct0A, struct0B);
SetConsumedAndAssert(ref sequenceReader, 1);
SetConsumedAndAssert(ref sequenceReader, MyUnmanagedStruct.SizeOf);
ref readonly MyUnmanagedStruct struct1A = ref sequenceReader.GetRefOrRefToCopy<MyUnmanagedStruct>(out _);
SetConsumedAndAssert(ref sequenceReader, MyUnmanagedStruct.SizeOf);
ref readonly MyUnmanagedStruct struct1B = ref sequenceReader.GetRefOrRefToCopy<MyUnmanagedStruct>(out _);
MyUnmanagedStruct.Assert(Assert.AreEqual, struct1A, struct1B);
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct MyUnmanagedStruct
{
public int BehaviourSize;
public int MemoryPoolsSize;
public short VoicesSize;
public int VoiceResourcesSize;
public short EffectsSize;
public int RenderInfoSize;
public unsafe fixed byte Reserved[16];
public static readonly int SizeOf = Unsafe.SizeOf<MyUnmanagedStruct>();
public static unsafe MyUnmanagedStruct Generate(Random rng)
{
const int BaseInt32Value = 0x1234abcd;
const short BaseInt16Value = 0x5678;
var result = new MyUnmanagedStruct
{
BehaviourSize = BaseInt32Value ^ rng.Next(),
MemoryPoolsSize = BaseInt32Value ^ rng.Next(),
VoicesSize = (short)(BaseInt16Value ^ rng.Next()),
VoiceResourcesSize = BaseInt32Value ^ rng.Next(),
EffectsSize = (short)(BaseInt16Value ^ rng.Next()),
RenderInfoSize = BaseInt32Value ^ rng.Next(),
};
Unsafe.Write(result.Reserved, rng.NextInt64());
return result;
}
public static unsafe void Assert(Action<object, object> assert, in MyUnmanagedStruct expected, in MyUnmanagedStruct actual)
{
assert(expected.BehaviourSize, actual.BehaviourSize);
assert(expected.MemoryPoolsSize, actual.MemoryPoolsSize);
assert(expected.VoicesSize, actual.VoicesSize);
assert(expected.VoiceResourcesSize, actual.VoiceResourcesSize);
assert(expected.EffectsSize, actual.EffectsSize);
assert(expected.RenderInfoSize, actual.RenderInfoSize);
fixed (void* expectedReservedPtr = expected.Reserved)
fixed (void* actualReservedPtr = actual.Reserved)
{
long expectedReservedLong = Unsafe.Read<long>(expectedReservedPtr);
long actualReservedLong = Unsafe.Read<long>(actualReservedPtr);
assert(expectedReservedLong, actualReservedLong);
}
}
}
private static IEnumerable<MyUnmanagedStruct> EnumerateNewUnmanagedStructs()
{
var rng = new Random(0);
while (true)
{
yield return MyUnmanagedStruct.Generate(rng);
}
}
private static ReadOnlySequence<byte> CreateSegmentedByteSequence<T>(T[] array, int maxSegmentLength) where T : unmanaged
{
byte[] arrayBytes = MemoryMarshal.AsBytes(array.AsSpan()).ToArray();
var memory = new Memory<byte>(arrayBytes);
int index = 0;
BytesReadOnlySequenceSegment first = null, last = null;
while (index < memory.Length)
{
int nextSegmentLength = Math.Min(maxSegmentLength, memory.Length - index);
var nextSegment = memory.Slice(index, nextSegmentLength);
if (first == null)
{
first = last = new BytesReadOnlySequenceSegment(nextSegment);
}
else
{
last = last.Append(nextSegment);
}
index += nextSegmentLength;
}
return new ReadOnlySequence<byte>(first, 0, last, (int)(memory.Length - last.RunningIndex));
}
}
}
|