8
8
using System . Numerics ;
9
9
using System . Threading . Tasks ;
10
10
using Microsoft . DotNet . RemoteExecutor ;
11
+ using Microsoft . DotNet . XUnitExtensions ;
11
12
using Xunit ;
12
13
13
14
namespace System . Buffers . ArrayPool . Tests
@@ -240,15 +241,11 @@ public static void NewDefaultArrayPoolWithSmallBufferSizeRoundsToOurSmallestSupp
240
241
}
241
242
242
243
[ Fact ]
243
- public static void ReturningABufferGreaterThanMaxSizeDoesNotThrow ( )
244
+ public static void ReturningToCreatePoolABufferGreaterThanMaxSizeDoesNotThrow ( )
244
245
{
245
246
ArrayPool < byte > pool = ArrayPool < byte > . Create ( maxArrayLength : 16 , maxArraysPerBucket : 1 ) ;
246
247
byte [ ] rented = pool . Rent ( 32 ) ;
247
248
pool . Return ( rented ) ;
248
-
249
- ArrayPool < byte > . Shared . Return ( new byte [ 3 * 1024 * 1024 ] ) ;
250
- ArrayPool < char > . Shared . Return ( new char [ 3 * 1024 * 1024 ] ) ;
251
- ArrayPool < string > . Shared . Return ( new string [ 3 * 1024 * 1024 ] ) ;
252
249
}
253
250
254
251
[ Fact ]
@@ -292,11 +289,11 @@ public static void CanRentManySizedBuffers(ArrayPool<byte> pool)
292
289
[ InlineData ( 1024 , 1024 ) ]
293
290
[ InlineData ( 4096 , 4096 ) ]
294
291
[ InlineData ( 1024 * 1024 , 1024 * 1024 ) ]
295
- [ InlineData ( 1024 * 1024 + 1 , 1024 * 1024 + 1 ) ]
292
+ [ InlineData ( 1024 * 1024 + 1 , 1024 * 1024 * 2 ) ]
296
293
[ InlineData ( 1024 * 1024 * 2 , 1024 * 1024 * 2 ) ]
297
294
public static void RentingSpecificLengthsYieldsExpectedLengths ( int requestedMinimum , int expectedLength )
298
295
{
299
- foreach ( ArrayPool < byte > pool in new [ ] { ArrayPool < byte > . Create ( ) , ArrayPool < byte > . Shared } )
296
+ foreach ( ArrayPool < byte > pool in new [ ] { ArrayPool < byte > . Create ( ( int ) BitOperations . RoundUpToPowerOf2 ( ( uint ) requestedMinimum ) , 1 ) , ArrayPool < byte > . Shared } )
300
297
{
301
298
byte [ ] buffer1 = pool . Rent ( requestedMinimum ) ;
302
299
byte [ ] buffer2 = pool . Rent ( requestedMinimum ) ;
@@ -313,7 +310,7 @@ public static void RentingSpecificLengthsYieldsExpectedLengths(int requestedMini
313
310
pool . Return ( buffer1 ) ;
314
311
}
315
312
316
- foreach ( ArrayPool < char > pool in new [ ] { ArrayPool < char > . Create ( ) , ArrayPool < char > . Shared } )
313
+ foreach ( ArrayPool < char > pool in new [ ] { ArrayPool < char > . Create ( ( int ) BitOperations . RoundUpToPowerOf2 ( ( uint ) requestedMinimum ) , 1 ) , ArrayPool < char > . Shared } )
317
314
{
318
315
char [ ] buffer1 = pool . Rent ( requestedMinimum ) ;
319
316
char [ ] buffer2 = pool . Rent ( requestedMinimum ) ;
@@ -330,7 +327,7 @@ public static void RentingSpecificLengthsYieldsExpectedLengths(int requestedMini
330
327
pool . Return ( buffer1 ) ;
331
328
}
332
329
333
- foreach ( ArrayPool < string > pool in new [ ] { ArrayPool < string > . Create ( ) , ArrayPool < string > . Shared } )
330
+ foreach ( ArrayPool < string > pool in new [ ] { ArrayPool < string > . Create ( ( int ) BitOperations . RoundUpToPowerOf2 ( ( uint ) requestedMinimum ) , 1 ) , ArrayPool < string > . Shared } )
334
331
{
335
332
string [ ] buffer1 = pool . Rent ( requestedMinimum ) ;
336
333
string [ ] buffer2 = pool . Rent ( requestedMinimum ) ;
@@ -348,6 +345,38 @@ public static void RentingSpecificLengthsYieldsExpectedLengths(int requestedMini
348
345
}
349
346
}
350
347
348
+ [ ConditionalTheory ( typeof ( PlatformDetection ) , nameof ( PlatformDetection . Is64BitProcess ) ) ]
349
+ [ InlineData ( 1024 * 1024 * 1024 - 1 , true ) ]
350
+ [ InlineData ( 1024 * 1024 * 1024 , true ) ]
351
+ [ InlineData ( 1024 * 1024 * 1024 + 1 , false ) ]
352
+ [ InlineData ( 0X7FFFFFC7 /* Array.MaxLength */ , false ) ]
353
+ [ OuterLoop ]
354
+ public static void RentingGiganticArraySucceeds ( int length , bool expectPooled )
355
+ {
356
+ var options = new RemoteInvokeOptions ( ) ;
357
+ options . StartInfo . UseShellExecute = false ;
358
+ options . StartInfo . EnvironmentVariables . Add ( TrimSwitchName , "false" ) ;
359
+
360
+ RemoteExecutor . Invoke ( ( lengthStr , expectPooledStr ) =>
361
+ {
362
+ int length = int . Parse ( lengthStr ) ;
363
+ byte [ ] array ;
364
+ try
365
+ {
366
+ array = ArrayPool < byte > . Shared . Rent ( length ) ;
367
+ }
368
+ catch ( OutOfMemoryException )
369
+ {
370
+ return ;
371
+ }
372
+
373
+ Assert . InRange ( array . Length , length , int . MaxValue ) ;
374
+ ArrayPool < byte > . Shared . Return ( array ) ;
375
+
376
+ Assert . Equal ( bool . Parse ( expectPooledStr ) , ReferenceEquals ( array , ArrayPool < byte > . Shared . Rent ( length ) ) ) ;
377
+ } , length . ToString ( ) , expectPooled . ToString ( ) , options ) . Dispose ( ) ;
378
+ }
379
+
351
380
[ Fact ]
352
381
public static void RentingAfterPoolExhaustionReturnsSizeForCorrespondingBucket_SmallerThanLimit ( )
353
382
{
0 commit comments