8
8
using System . Runtime . InteropServices ;
9
9
using Microsoft . Toolkit . HighPerformance . Helpers ;
10
10
using Microsoft . VisualStudio . TestTools . UnitTesting ;
11
+ using UnitTests . HighPerformance . Shared . Buffers . Internals ;
11
12
12
13
namespace UnitTests . HighPerformance . Helpers
13
14
{
@@ -67,19 +68,23 @@ public void Test_HashCodeOfT_VectorUnsupportedTypes_TestRepeat()
67
68
[ TestMethod ]
68
69
public void Test_HashCodeOfT_ManagedType_TestRepeat ( )
69
70
{
70
- var random = new Random ( ) ;
71
+ var localTestCounts = TestCounts . Slice ( 0 , 8 ) ;
72
+
73
+ // Only rent a single array of the maximum necessary size, to save space
74
+ string [ ] data = new string [ localTestCounts [ localTestCounts . Length - 1 ] ] ;
71
75
72
- foreach ( var count in TestCounts . Slice ( 0 , 8 ) )
76
+ var random = new Random ( ) ;
77
+ foreach ( ref string text in data . AsSpan ( ) )
73
78
{
74
- string [ ] data = new string [ count ] ;
79
+ text = random . NextDouble ( ) . ToString ( "E" ) ;
80
+ }
75
81
76
- foreach ( ref string text in data . AsSpan ( ) )
77
- {
78
- text = random . NextDouble ( ) . ToString ( "E" ) ;
79
- }
82
+ foreach ( var count in localTestCounts )
83
+ {
84
+ Span < string > iterationData = data . AsSpan ( ) . Slice ( 0 , count ) ;
80
85
81
- int hash1 = HashCode < string > . Combine ( data ) ;
82
- int hash2 = HashCode < string > . Combine ( data ) ;
86
+ int hash1 = HashCode < string > . Combine ( iterationData ) ;
87
+ int hash2 = HashCode < string > . Combine ( iterationData ) ;
83
88
84
89
Assert . AreEqual ( hash1 , hash2 , $ "Failed { typeof ( string ) } test with count { count } : got { hash1 } and then { hash2 } ") ;
85
90
}
@@ -95,10 +100,10 @@ private static void TestForType<T>()
95
100
{
96
101
foreach ( var count in TestCounts )
97
102
{
98
- T [ ] data = CreateRandomData < T > ( count ) ;
103
+ using UnmanagedSpanOwner < T > data = CreateRandomData < T > ( count ) ;
99
104
100
- int hash1 = HashCode < T > . Combine ( data ) ;
101
- int hash2 = HashCode < T > . Combine ( data ) ;
105
+ int hash1 = HashCode < T > . Combine ( data . Span ) ;
106
+ int hash2 = HashCode < T > . Combine ( data . Span ) ;
102
107
103
108
Assert . AreEqual ( hash1 , hash2 , $ "Failed { typeof ( T ) } test with count { count } : got { hash1 } and then { hash2 } ") ;
104
109
}
@@ -111,14 +116,14 @@ private static void TestForType<T>()
111
116
/// <param name="count">The number of array items to create.</param>
112
117
/// <returns>An array of random <typeparamref name="T"/> elements.</returns>
113
118
[ Pure ]
114
- private static T [ ] CreateRandomData < T > ( int count )
119
+ private static UnmanagedSpanOwner < T > CreateRandomData < T > ( int count )
115
120
where T : unmanaged
116
121
{
117
122
var random = new Random ( count ) ;
118
123
119
- T [ ] data = new T [ count ] ;
124
+ UnmanagedSpanOwner < T > data = new UnmanagedSpanOwner < T > ( count ) ;
120
125
121
- foreach ( ref byte n in MemoryMarshal . AsBytes ( data . AsSpan ( ) ) )
126
+ foreach ( ref byte n in MemoryMarshal . AsBytes ( data . Span ) )
122
127
{
123
128
n = ( byte ) random . Next ( 0 , byte . MaxValue ) ;
124
129
}
0 commit comments