-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathTest_SpanOwner{T}.cs
More file actions
93 lines (73 loc) · 2.93 KB
/
Test_SpanOwner{T}.cs
File metadata and controls
93 lines (73 loc) · 2.93 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
// 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;
using System.Linq;
using CommunityToolkit.HighPerformance.Buffers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CommunityToolkit.HighPerformance.UnitTests.Buffers.Internals;
namespace CommunityToolkit.HighPerformance.UnitTests.Buffers;
[TestClass]
public class Test_SpanOwnerOfT
{
[TestMethod]
public void Test_SpanOwnerOfT_AllocateAndGetMemoryAndSpan()
{
using SpanOwner<int> buffer = SpanOwner<int>.Allocate(127);
Assert.IsTrue(buffer.Length == 127);
Assert.IsTrue(buffer.Span.Length == 127);
buffer.Span.Fill(42);
Assert.IsTrue(buffer.Span.ToArray().All(i => i == 42));
}
[TestMethod]
public void Test_SpanOwnerOfT_AllocateFromCustomPoolAndGetMemoryAndSpan()
{
TrackingArrayPool<int>? pool = new();
using (SpanOwner<int> buffer = SpanOwner<int>.Allocate(127, pool))
{
Assert.AreEqual(1, pool.RentedArrays.Count);
Assert.IsTrue(buffer.Length == 127);
Assert.IsTrue(buffer.Span.Length == 127);
buffer.Span.Fill(42);
Assert.IsTrue(buffer.Span.ToArray().All(i => i == 42));
}
Assert.AreEqual(0, pool.RentedArrays.Count);
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Test_SpanOwnerOfT_InvalidRequestedSize()
{
using SpanOwner<int> buffer = SpanOwner<int>.Allocate(-1);
Assert.Fail("You shouldn't be here");
}
[TestMethod]
public void Test_SpanOwnerOfT_PooledBuffersAndClear()
{
using (SpanOwner<int> buffer = SpanOwner<int>.Allocate(127))
{
buffer.Span.Fill(42);
}
using (SpanOwner<int> buffer = SpanOwner<int>.Allocate(127))
{
Assert.IsTrue(buffer.Span.ToArray().All(i => i == 42));
}
using (SpanOwner<int> buffer = SpanOwner<int>.Allocate(127, AllocationMode.Clear))
{
Assert.IsTrue(buffer.Span.ToArray().All(i => i == 0));
}
}
[TestMethod]
public void Test_SpanOwnerOfT_AllocateAndGetArray()
{
using SpanOwner<int> buffer = SpanOwner<int>.Allocate(127);
ArraySegment<int> segment = buffer.DangerousGetArray();
// See comments in the MemoryOwner<T> tests about this. The main difference
// here is that we don't do the disposed checks, as SpanOwner<T> is optimized
// with the assumption that usages after dispose are undefined behavior. This
// is all documented in the XML docs for the SpanOwner<T> type.
Assert.IsNotNull(segment.Array);
Assert.IsTrue(segment.Array.Length >= buffer.Length);
Assert.AreEqual(0, segment.Offset);
Assert.AreEqual(buffer.Length, segment.Count);
}
}