Skip to content

Commit b904790

Browse files
committed
Added some unit tests
1 parent 53cb75f commit b904790

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

UnitTests/UnitTests.HighPerformance.Shared/Extensions/Test_MemoryExtensions.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
using System;
66
using System.IO;
7+
using System.Runtime.CompilerServices;
8+
using System.Runtime.InteropServices;
79
using Microsoft.Toolkit.HighPerformance.Extensions;
810
using Microsoft.VisualStudio.TestTools.UnitTesting;
911

@@ -12,6 +14,109 @@ namespace UnitTests.HighPerformance.Extensions
1214
[TestClass]
1315
public class Test_MemoryExtensions
1416
{
17+
[TestCategory("MemoryExtensions")]
18+
[TestMethod]
19+
public void Test_MemoryExtensions_Cast_Empty()
20+
{
21+
Memory<byte> m1 = default;
22+
Memory<byte> mc1 = m1.Cast<byte, byte>();
23+
24+
Assert.IsTrue(mc1.IsEmpty);
25+
26+
Memory<byte> m2 = default;
27+
Memory<float> mc2 = m2.Cast<byte, float>();
28+
29+
Assert.IsTrue(mc2.IsEmpty);
30+
31+
Memory<short> m3 = default;
32+
Memory<Guid> mc3 = m3.Cast<short, Guid>();
33+
34+
Assert.IsTrue(mc3.IsEmpty);
35+
36+
Memory<byte> m4 = new byte[12].AsMemory(12);
37+
Memory<int> mc4 = m4.Cast<byte, int>();
38+
39+
Assert.IsTrue(mc4.IsEmpty);
40+
41+
Memory<byte> m5 = new byte[12].AsMemory().Slice(4).Slice(8);
42+
Memory<int> mc5 = m5.Cast<byte, int>();
43+
44+
Assert.IsTrue(mc5.IsEmpty);
45+
}
46+
47+
[TestCategory("MemoryExtensions")]
48+
[TestMethod]
49+
public void Test_MemoryExtensions_Cast_TooShort()
50+
{
51+
Memory<byte> m1 = new byte[3];
52+
Memory<int> mc1 = m1.Cast<byte, int>();
53+
54+
Assert.IsTrue(mc1.IsEmpty);
55+
56+
Memory<byte> m2 = new byte[13];
57+
Memory<float> mc2 = m2.Cast<byte, float>();
58+
59+
Assert.AreEqual(mc2.Length, 3);
60+
61+
Memory<byte> m3 = new byte[16].AsMemory(5);
62+
Memory<float> mc3 = m3.Cast<byte, float>();
63+
64+
Assert.AreEqual(mc3.Length, 2);
65+
}
66+
67+
[TestCategory("MemoryExtensions")]
68+
[TestMethod]
69+
public void Test_MemoryExtensions_FromArray_CastFromByte()
70+
{
71+
Memory<byte> memoryOfBytes = new byte[128];
72+
Memory<float> memoryOfFloats = memoryOfBytes.Cast<byte, float>();
73+
74+
Assert.AreEqual(memoryOfFloats.Length, 128 / sizeof(float));
75+
76+
Span<byte> spanOfBytes = memoryOfBytes.Span;
77+
Span<float> spanOfFloats = memoryOfFloats.Span;
78+
79+
Assert.IsTrue(Unsafe.AreSame(
80+
ref spanOfBytes[0],
81+
ref Unsafe.As<float, byte>(ref spanOfFloats[0])));
82+
}
83+
84+
[TestCategory("MemoryExtensions")]
85+
[TestMethod]
86+
public void Test_MemoryExtensions_FromArray_CastToByte()
87+
{
88+
Memory<float> memoryOfFloats = new float[128];
89+
Memory<byte> memoryOfBytes = memoryOfFloats.Cast<float, byte>();
90+
91+
Assert.AreEqual(memoryOfBytes.Length, 128 * sizeof(float));
92+
93+
Span<float> spanOfFloats = memoryOfFloats.Span;
94+
Span<byte> spanOfBytes = memoryOfBytes.Span;
95+
96+
Assert.IsTrue(Unsafe.AreSame(
97+
ref spanOfFloats[0],
98+
ref Unsafe.As<byte, float>(ref spanOfBytes[0])));
99+
}
100+
101+
[TestCategory("MemoryExtensions")]
102+
[TestMethod]
103+
public void Test_MemoryExtensions_FromArray_CastFromByteAndBack()
104+
{
105+
var data = new byte[128];
106+
Memory<byte> memoryOfBytes = data;
107+
Memory<float> memoryOfFloats = memoryOfBytes.Cast<byte, float>();
108+
Memory<byte> memoryBack = memoryOfFloats.Cast<float, byte>();
109+
110+
Assert.AreEqual(memoryOfBytes.Length, memoryBack.Length);
111+
112+
Assert.IsTrue(MemoryMarshal.TryGetArray<byte>(memoryBack, out var segment));
113+
Assert.AreSame(segment.Array!, data);
114+
Assert.AreEqual(segment.Offset, 0);
115+
Assert.AreEqual(segment.Count, data.Length);
116+
117+
Assert.IsTrue(memoryOfBytes.Equals(memoryBack));
118+
}
119+
15120
[TestCategory("MemoryExtensions")]
16121
[TestMethod]
17122
public void Test_MemoryExtensions_EmptyMemoryStream()

0 commit comments

Comments
 (0)