This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
WindowsRuntimeBufferExtensionsTests.cs
463 lines (399 loc) · 23 KB
/
WindowsRuntimeBufferExtensionsTests.cs
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.IO;
using Windows.Storage.Streams;
using Xunit;
namespace System.Runtime.InteropServices.WindowsRuntime.Tests
{
public class WindowsRuntimeBufferExtensionsTests
{
public static IEnumerable<object[]> AsBuffer_TestData()
{
yield return new object[] { new byte[0], 0, 0, 0 };
yield return new object[] { new byte[] { 1, 2, 3 }, 2, 0, 0 };
yield return new object[] { new byte[] { 1, 2, 3 }, 2, 0, 1 };
yield return new object[] { new byte[] { 1, 2, 3 }, 0, 3, 3 };
yield return new object[] { new byte[] { 1, 2, 3 }, 1, 2, 2 };
}
[Theory]
[MemberData(nameof(AsBuffer_TestData))]
public void AsBuffer_Buffer_ReturnsExpected(byte[] source, int offset, int length, int capacity)
{
if (capacity == length)
{
if (offset == 0 && length == source.Length)
{
Verify(WindowsRuntimeBufferExtensions.AsBuffer(source), source, offset, length, capacity);
}
Verify(WindowsRuntimeBufferExtensions.AsBuffer(source, offset, length), source, offset, length, capacity);
}
Verify(WindowsRuntimeBufferExtensions.AsBuffer(source, offset, length, capacity), source, offset, length, capacity);
}
[Fact]
public void AsBuffer_NullBuffer_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("source", () => WindowsRuntimeBufferExtensions.AsBuffer(null));
AssertExtensions.Throws<ArgumentNullException>("source", () => WindowsRuntimeBufferExtensions.AsBuffer(null, 0, 0));
AssertExtensions.Throws<ArgumentNullException>("source", () => WindowsRuntimeBufferExtensions.AsBuffer(null, 0, 0, 0));
}
[Fact]
public void AsBuffer_NegativeOffset_ThrowsArgumentOutOfRangeException()
{
AssertExtensions.Throws<ArgumentOutOfRangeException>("offset", () => WindowsRuntimeBufferExtensions.AsBuffer(new byte[0], -1, 0));
AssertExtensions.Throws<ArgumentOutOfRangeException>("offset", () => WindowsRuntimeBufferExtensions.AsBuffer(new byte[0], -1, 0, 0));
}
[Fact]
public void AsBuffer_NegativeLength_ThrowsArgumentOutOfRangeException()
{
AssertExtensions.Throws<ArgumentOutOfRangeException>("length", () => WindowsRuntimeBufferExtensions.AsBuffer(new byte[0], 0, -1));
AssertExtensions.Throws<ArgumentOutOfRangeException>("length", () => WindowsRuntimeBufferExtensions.AsBuffer(new byte[0], 0, -1, 0));
}
[Theory]
[InlineData(new byte[0], 0, 1, 0)]
[InlineData(new byte[0], 1, 0, 0)]
[InlineData(new byte[] { 0, 0 }, 1, 2, 0)]
[InlineData(new byte[] { 0, 0 }, int.MaxValue, 0, 0)]
[InlineData(new byte[] { 0, 0 }, 0, 0, 3)]
[InlineData(new byte[] { 0, 0 }, 0, 0, int.MaxValue)]
[InlineData(new byte[] { 0, 0 }, 0, 2, 1)]
public void AsBuffer_InvalidOffsetLengthCapacity_ThrowsArgumentException(byte[] data, int offset, int length, int capacity)
{
if (capacity == 0)
{
AssertExtensions.Throws<ArgumentException>(null, () => WindowsRuntimeBufferExtensions.AsBuffer(data, offset, length));
}
AssertExtensions.Throws<ArgumentException>(null, () => WindowsRuntimeBufferExtensions.AsBuffer(data, offset, length, capacity));
}
[Fact]
public void AsBuffer_NegativeCapacity_ThrowsArgumentOutOfRangeException()
{
AssertExtensions.Throws<ArgumentOutOfRangeException>("capacity", () => WindowsRuntimeBufferExtensions.AsBuffer(new byte[0], 0, 0, -1));
}
public static IEnumerable<object[]> AsStream_TestData()
{
yield return new object[] { new byte[0].AsBuffer(), new byte[0] };
yield return new object[] { new byte[] { 1, 2, 3 }.AsBuffer(), new byte[] { 1, 2, 3 } };
yield return new object[] { new byte[] { 1, 2, 3 }.AsBuffer(1, 2), new byte[] { 2, 3 } };
}
[Theory]
[MemberData(nameof(AsStream_TestData))]
public void AsStream_Buffer_Success(IBuffer buffer, byte[] expected)
{
using (MemoryStream stream = (MemoryStream)buffer.AsStream())
{
Assert.Equal(expected.Length, stream.Length);
Assert.True(stream.CanWrite);
Assert.Equal(expected, stream.ToArray());
}
}
[Fact]
public void AsStream_NullBuffer_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("source", () => WindowsRuntimeBufferExtensions.AsStream(null));
}
[Fact]
public void AsStream_CustomBuffer_ThrowsInvalidCastException()
{
Assert.Throws<InvalidCastException>(() => WindowsRuntimeBufferExtensions.AsStream(new CustomBuffer()));
}
public static IEnumerable<object[]> CopyTo_TestData()
{
yield return new object[] { new byte[] { 1, 2, 3 }, 0, new byte[] { 2, 3, 4 }, 0, 3, new byte[] { 1, 2, 3 } };
yield return new object[] { new byte[] { 1, 2, 3, 4, 5 }, 3, new byte[] { 255, 254, 253, 252 }, 2, 1, new byte[] { 255, 254, 4, 252 } };
yield return new object[] { new byte[] { 1, 2, 3, 4, 5 }, 0, new byte[] { 255, 254, 253, 252 }, 0, 0, new byte[] { 255, 254, 253, 252 } };
}
[Theory]
[MemberData(nameof(CopyTo_TestData))]
public void CopyTo_Buffer_Success(byte[] source, int sourceIndex, byte[] destination, int destinationIndex, int count, byte[] expected)
{
byte[] Clone(byte[] array) => (byte[])array.Clone();
IBuffer Buffer(byte[] array) => Clone(array).AsBuffer();
if (sourceIndex == 0 && destinationIndex == 0 && count == source.Length)
{
// CopyTo(byte[], IBuffer)
byte[] source1 = Clone(source);
IBuffer destination1 = Buffer(destination);
WindowsRuntimeBufferExtensions.CopyTo(source1, destination1);
Assert.Equal(expected, destination1.ToArray());
// CopyTo(IBuffer, byte[])
IBuffer source2 = Buffer(source);
byte[] destination2 = Clone(destination);
WindowsRuntimeBufferExtensions.CopyTo(source2, destination2);
Assert.Equal(expected, destination2);
// CopyTo(IBuffer, IBuffer)
IBuffer source3 = Buffer(source);
IBuffer destination3 = Buffer(destination);
WindowsRuntimeBufferExtensions.CopyTo(source3, destination3);
Assert.Equal(expected, destination3.ToArray());
}
// CopyTo(byte[], int, IBuffer, int, int)
byte[] source4 = Clone(source);
IBuffer destination4 = Buffer(destination);
WindowsRuntimeBufferExtensions.CopyTo(source4, sourceIndex, destination4, (uint)destinationIndex, count);
Assert.Equal(expected, destination4.ToArray());
// CopyTo(IBuffer, int, byte[], int, int)
IBuffer source5 = Buffer(source);
byte[] destination5 = Clone(destination);
WindowsRuntimeBufferExtensions.CopyTo(source5, (uint)sourceIndex, destination5, destinationIndex, count);
Assert.Equal(expected, destination5);
// CopyTo(IBuffer, int, IBuffer, int, int)
IBuffer source6 = Buffer(source);
IBuffer destination6 = Buffer(destination);
WindowsRuntimeBufferExtensions.CopyTo(source6, (uint)sourceIndex, destination6, (uint)destinationIndex, (uint)count);
Assert.Equal(expected, destination6.ToArray());
}
[Fact]
public void CopyTo_NullSource_ThrowsArgumentNullException()
{
IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(new byte[0]);
AssertExtensions.Throws<ArgumentNullException>("source", () => WindowsRuntimeBufferExtensions.CopyTo((byte[])null, buffer));
AssertExtensions.Throws<ArgumentNullException>("source", () => WindowsRuntimeBufferExtensions.CopyTo(null, new byte[0]));
AssertExtensions.Throws<ArgumentNullException>("source", () => WindowsRuntimeBufferExtensions.CopyTo((IBuffer)null, buffer));
AssertExtensions.Throws<ArgumentNullException>("source", () => WindowsRuntimeBufferExtensions.CopyTo(null, 0, buffer, 0, 0));
AssertExtensions.Throws<ArgumentNullException>("source", () => WindowsRuntimeBufferExtensions.CopyTo(null, 0, new byte[0], 0, 0));
AssertExtensions.Throws<ArgumentNullException>("source", () => WindowsRuntimeBufferExtensions.CopyTo((IBuffer)null, 0, buffer, 0, 0));
}
[Fact]
public void CopyTo_NullDestination_ThrowsArgumentNullException()
{
IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(new byte[0]);
AssertExtensions.Throws<ArgumentNullException>("destination", () => WindowsRuntimeBufferExtensions.CopyTo(new byte[0], null));
AssertExtensions.Throws<ArgumentNullException>("destination", () => WindowsRuntimeBufferExtensions.CopyTo(buffer, (IBuffer)null));
AssertExtensions.Throws<ArgumentNullException>("destination", () => WindowsRuntimeBufferExtensions.CopyTo(buffer, (byte[])null));
AssertExtensions.Throws<ArgumentNullException>("destination", () => WindowsRuntimeBufferExtensions.CopyTo(new byte[0], 0, null, 0, 0));
AssertExtensions.Throws<ArgumentNullException>("destination", () => WindowsRuntimeBufferExtensions.CopyTo(buffer, 0, null, 0, 0));
AssertExtensions.Throws<ArgumentNullException>("destination", () => WindowsRuntimeBufferExtensions.CopyTo(buffer, 0, (IBuffer)null, 0, 0));
}
[Fact]
public void CopyTo_NegativeSourceIndex_ThrowsArgumentOutOfRangeException()
{
IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(new byte[0]);
AssertExtensions.Throws<ArgumentOutOfRangeException>("sourceIndex", () => WindowsRuntimeBufferExtensions.CopyTo(new byte[0], -1, buffer, 0, 0));
}
[Fact]
public void CopyTo_NegativeDestinationIndex_ThrowsArgumentOutOfRangeException()
{
IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(new byte[0]);
AssertExtensions.Throws<ArgumentOutOfRangeException>("destinationIndex", () => WindowsRuntimeBufferExtensions.CopyTo(buffer, 0, new byte[0], -1, 0));
}
[Fact]
public void CopyTo_LargeSourceIndex_ThrowsArgumentException()
{
IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(new byte[0]);
AssertExtensions.Throws<ArgumentException>("sourceIndex", () => WindowsRuntimeBufferExtensions.CopyTo(new byte[0], 1, buffer, 0, 0));
AssertExtensions.Throws<ArgumentException>(null, () => WindowsRuntimeBufferExtensions.CopyTo(buffer, 1, buffer, 0, 0));
AssertExtensions.Throws<ArgumentException>(null, () => WindowsRuntimeBufferExtensions.CopyTo(buffer, 1, new byte[0], 0, 0));
}
[Fact]
public void CopyTo_NegativeCount_ThrowsArgumentOutOfRangeException()
{
IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(new byte[0]);
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => WindowsRuntimeBufferExtensions.CopyTo(new byte[0], 0, buffer, 0, -1));
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => WindowsRuntimeBufferExtensions.CopyTo(buffer, 0, new byte[0], 0, -1));
}
[Theory]
[InlineData(new byte[0], 0, 0)]
[InlineData(new byte[0], 0, 1)]
[InlineData(new byte[] { 0, 0 }, 2, 0)]
[InlineData(new byte[] { 0, 0 }, 1, 2)]
public void CopyTo_InvalidSourceIndexCount_ThrowsArgumentException(byte[] bytes, uint sourceIndex, uint count)
{
IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(bytes);
AssertExtensions.Throws<ArgumentException>(sourceIndex >= bytes.Length ? "sourceIndex" : null, () => WindowsRuntimeBufferExtensions.CopyTo(bytes, (int)sourceIndex, buffer, 0, (int)count));
AssertExtensions.Throws<ArgumentException>(null, () => WindowsRuntimeBufferExtensions.CopyTo(buffer, sourceIndex, buffer, 0, count));
AssertExtensions.Throws<ArgumentException>(null, () => WindowsRuntimeBufferExtensions.CopyTo(buffer, sourceIndex, new byte[0], 0, (int)count));
}
[Theory]
[InlineData(new byte[] { 0, 0 }, 2, 1)]
[InlineData(new byte[] { 0, 0 }, 1, 2)]
public void CopyTo_InvalidDestinationIndexCount_ThrowsArgumentException(byte[] bytes, uint destinationIndex, uint count)
{
IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(bytes);
AssertExtensions.Throws<ArgumentException>(null, () => WindowsRuntimeBufferExtensions.CopyTo(new byte[10], 0, buffer, destinationIndex, (int)count));
AssertExtensions.Throws<ArgumentException>(null, () => WindowsRuntimeBufferExtensions.CopyTo(new byte[10].AsBuffer(), 0, bytes, (int)destinationIndex, (int)count));
AssertExtensions.Throws<ArgumentException>(null, () => WindowsRuntimeBufferExtensions.CopyTo(new byte[10].AsBuffer(), 0, buffer, destinationIndex, count));
}
[Fact]
public void CopyTo_CustomBuffer_ThrowsInvalidCastException()
{
Assert.Throws<InvalidCastException>(() => WindowsRuntimeBufferExtensions.CopyTo(new byte[10], new CustomBuffer()));
Assert.Throws<InvalidCastException>(() => WindowsRuntimeBufferExtensions.CopyTo(new CustomBuffer(), new byte[10]));
Assert.Throws<InvalidCastException>(() => WindowsRuntimeBufferExtensions.CopyTo(new CustomBuffer(), new CustomBuffer()));
Assert.Throws<InvalidCastException>(() => WindowsRuntimeBufferExtensions.CopyTo(new byte[10], 0, new CustomBuffer(), 0, 0));
Assert.Throws<InvalidCastException>(() => WindowsRuntimeBufferExtensions.CopyTo(new CustomBuffer(), 0, new byte[10], 0, 0));
Assert.Throws<InvalidCastException>(() => WindowsRuntimeBufferExtensions.CopyTo(new CustomBuffer(), 0, new CustomBuffer(), 0, 0));
Assert.Throws<InvalidCastException>(() => WindowsRuntimeBufferExtensions.CopyTo(new CustomBuffer(), 0, new byte[10].AsBuffer(), 0, 0));
Assert.Throws<InvalidCastException>(() => WindowsRuntimeBufferExtensions.CopyTo(new byte[10].AsBuffer(), 0, new CustomBuffer(), 0, 0));
}
public static IEnumerable<object[]> IsSameData_TestData()
{
byte[] data = new byte[] { 1, 2, 3 };
IBuffer buffer = data.AsBuffer();
yield return new object[] { buffer, buffer, true };
yield return new object[] { buffer, data.AsBuffer(), true };
yield return new object[] { buffer, new byte[] { 1, 3, 3 }.AsBuffer(), false };
yield return new object[] { buffer, new CustomBuffer(), false };
yield return new object[] { buffer, null, false };
}
[Theory]
[MemberData(nameof(IsSameData_TestData))]
public void IsSameData_Buffer_ReturnsExpected(IBuffer buffer, IBuffer other, bool expected)
{
Assert.Equal(expected, WindowsRuntimeBufferExtensions.IsSameData(buffer, other));
}
[Fact]
public void IsSameData_CustomBuffer_ThrowsInvalidCastException()
{
Assert.Throws<InvalidCastException>(() => WindowsRuntimeBufferExtensions.IsSameData(new CustomBuffer(), new CustomBuffer()));
}
[Fact]
public void IsSameData_NullBuffer_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("buffer", () => WindowsRuntimeBufferExtensions.IsSameData(null, new byte[0].AsBuffer()));
}
[Fact]
public void GetByte_NullBuffer_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("source", () => WindowsRuntimeBufferExtensions.GetByte(null, 0));
}
[Theory]
[InlineData(1)]
[InlineData(2)]
public void GetByte_InvalidOffset_ThrowsArgumentException(uint byteOffset)
{
IBuffer buffer = new byte[1].AsBuffer();
AssertExtensions.Throws<ArgumentException>("byteOffset", () => WindowsRuntimeBufferExtensions.GetByte(buffer, byteOffset));
}
[Fact]
public void GetByte_CustomBuffer_ThrowsInvalidCastExceptionException()
{
Assert.Throws<InvalidCastException>(() => WindowsRuntimeBufferExtensions.GetByte(new CustomBuffer(), 2));
}
public static IEnumerable<object[]> GetWindowsRuntimeBuffer_TestData()
{
yield return new object[] { new byte[] { 1, 2, 3 }, 0, 3 };
yield return new object[] { new byte[] { 1, 2, 3 }, 1, 2 };
yield return new object[] { new byte[] { 1, 2, 3 }, 3, 0 };
}
[Theory]
[MemberData(nameof(GetWindowsRuntimeBuffer_TestData))]
public void GetWindowsRuntimeBuffer_Stream_Success(byte[] bytes, int positionInStream, int length)
{
using (var stream = new MemoryStream())
{
stream.Write(bytes, 0, bytes.Length);
stream.Position = 0;
if (positionInStream == 0 && length == bytes.Length)
{
Verify(WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(stream), bytes, positionInStream, length, stream.Capacity);
}
stream.Position = 0;
Verify(WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(stream, positionInStream, length), bytes, positionInStream, length, length);
}
}
[Fact]
public void GetWindowsRuntimeBuffer_NullStream_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("underlyingStream", () => WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(null));
AssertExtensions.Throws<ArgumentNullException>("underlyingStream", () => WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(null, 0, 0));
}
[Fact]
public void GetWindowsRuntimeBuffer_NonWritableStream_ThrowsUnauthorizedAccessException()
{
var memoryStream = new MemoryStream(new byte[10], false);
Assert.Throws<UnauthorizedAccessException>(() => WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream));
Assert.Throws<UnauthorizedAccessException>(() => WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, 0));
}
[Fact]
public void GetWindowsRuntimeBuffer_NegativePositionInStream_ThrowsArgumentOufOfRangeException()
{
var memoryStream = new MemoryStream();
AssertExtensions.Throws<ArgumentOutOfRangeException>("positionInStream", () => WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, -1, 0));
}
[Fact]
public void GetWindowsRuntimeBuffer_NegativeLength_ThrowsArgumentOufOfRangeException()
{
var memoryStream = new MemoryStream();
AssertExtensions.Throws<ArgumentOutOfRangeException>("length", () => WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, -1));
}
[Fact]
public void GetWindowsRuntimeBuffer_EmptyStream_ThrowsArgumentException()
{
using (var stream = new MemoryStream())
{
Assert.Throws<ArgumentException>(null, () => WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(stream, 0, 0));
}
}
[Theory]
[InlineData(1)]
[InlineData(2)]
public void GetWindowsRuntimeBuffer_PositonInStreamGreaterOrEqualToCapacity_ThrowsArgumentException(int positionInStream)
{
var memoryStream = new MemoryStream(1);
AssertExtensions.Throws<ArgumentException>(null, () => WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, positionInStream, 0));
}
[Fact]
public void GetWindowsRuntimeBuffer_BufferWithLengthGreaterThanIntMax_Throws()
{
var memoryStream = new SubMemoryStream(10);
WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, 0);
}
public static IEnumerable<object[]> ToArray_TestData()
{
yield return new object[] { new byte[] { 1, 2, 3 }, 0, 3, new byte[] { 1, 2, 3 } };
yield return new object[] { new byte[] { 1, 2, 3 }, 2, 0, new byte[0] };
yield return new object[] { new byte[] { 1, 2, 3 }, 1, 2, new byte[] { 2, 3 } };
}
[Theory]
[MemberData(nameof(ToArray_TestData))]
public void ToArray_Buffer_ReturnsExpected(byte[] buffer, uint index, int count, byte[] expected)
{
if (index == 0 && count == buffer.Length)
{
Assert.Equal(expected, buffer.AsBuffer().ToArray());
}
Assert.Equal(expected, buffer.AsBuffer().ToArray(index, count));
}
[Fact]
public void ToArray_NullBuffer_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("source", () => WindowsRuntimeBufferExtensions.ToArray(null));
AssertExtensions.Throws<ArgumentNullException>("source", () => WindowsRuntimeBufferExtensions.ToArray(null, 0, 0));
}
[Fact]
public void ToArray_NegativeCount_ThrowsArgumentOutOfRangeException()
{
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => WindowsRuntimeBufferExtensions.ToArray(new byte[0].AsBuffer(), 0, -1));
}
[Theory]
[InlineData(new byte[0], 0, 0)]
[InlineData(new byte[] { 0 }, 1, 0)]
[InlineData(new byte[] { 0, 0, 0 }, 0, 4)]
[InlineData(new byte[] { 0, 0, 0 }, 1, 3)]
public void ToArray_InvalidIndexCount_ThrowsArgumentException(byte[] buffer, uint index, int count)
{
AssertExtensions.Throws<ArgumentException>(null, () => WindowsRuntimeBufferExtensions.ToArray(buffer.AsBuffer(), index, count));
}
private static void Verify(IBuffer buffer, byte[] source, int offset, int length, int capacity)
{
Assert.Equal(length, (int)buffer.Length);
Assert.Equal(capacity, (int)buffer.Capacity);
for (uint i = 0; i < length; i++)
{
Assert.Equal(source[i + offset], buffer.GetByte(i));
}
}
private class SubMemoryStream : MemoryStream
{
public SubMemoryStream(int capacity) : base(capacity) { }
public override long Length => long.MaxValue;
}
private class CustomBuffer : IBuffer
{
public uint Capacity => 10;
public uint Length { get; set; } = 10;
}
}
}