88using System . Runtime . InteropServices ;
99using Microsoft . Toolkit . HighPerformance . Helpers ;
1010using Microsoft . VisualStudio . TestTools . UnitTesting ;
11+ using UnitTests . HighPerformance . Shared . Buffers . Internals ;
1112
1213namespace UnitTests . HighPerformance . Helpers
1314{
@@ -67,19 +68,23 @@ public void Test_HashCodeOfT_VectorUnsupportedTypes_TestRepeat()
6768 [ TestMethod ]
6869 public void Test_HashCodeOfT_ManagedType_TestRepeat ( )
6970 {
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 ] ] ;
7175
72- foreach ( var count in TestCounts . Slice ( 0 , 8 ) )
76+ var random = new Random ( ) ;
77+ foreach ( ref string text in data . AsSpan ( ) )
7378 {
74- string [ ] data = new string [ count ] ;
79+ text = random . NextDouble ( ) . ToString ( "E" ) ;
80+ }
7581
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 ) ;
8085
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 ) ;
8388
8489 Assert . AreEqual ( hash1 , hash2 , $ "Failed { typeof ( string ) } test with count { count } : got { hash1 } and then { hash2 } ") ;
8590 }
@@ -95,10 +100,10 @@ private static void TestForType<T>()
95100 {
96101 foreach ( var count in TestCounts )
97102 {
98- T [ ] data = CreateRandomData < T > ( count ) ;
103+ using UnmanagedSpanOwner < T > data = CreateRandomData < T > ( count ) ;
99104
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 ) ;
102107
103108 Assert . AreEqual ( hash1 , hash2 , $ "Failed { typeof ( T ) } test with count { count } : got { hash1 } and then { hash2 } ") ;
104109 }
@@ -111,14 +116,14 @@ private static void TestForType<T>()
111116 /// <param name="count">The number of array items to create.</param>
112117 /// <returns>An array of random <typeparamref name="T"/> elements.</returns>
113118 [ Pure ]
114- private static T [ ] CreateRandomData < T > ( int count )
119+ private static UnmanagedSpanOwner < T > CreateRandomData < T > ( int count )
115120 where T : unmanaged
116121 {
117122 var random = new Random ( count ) ;
118123
119- T [ ] data = new T [ count ] ;
124+ UnmanagedSpanOwner < T > data = new UnmanagedSpanOwner < T > ( count ) ;
120125
121- foreach ( ref byte n in MemoryMarshal . AsBytes ( data . AsSpan ( ) ) )
126+ foreach ( ref byte n in MemoryMarshal . AsBytes ( data . Span ) )
122127 {
123128 n = ( byte ) random . Next ( 0 , byte . MaxValue ) ;
124129 }
0 commit comments