-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use GC.AllocateUninitializedArray in MemoryStream.ToArray (#50692)
* Use GC.AllocateUninitializedArray in MemoryStream.ToArray * Apply suggestions from code review Co-authored-by: Stephen Toub <[email protected]> Co-authored-by: Stephen Toub <[email protected]>
- Loading branch information
1 parent
84bf5bc
commit c5eeedb
Showing
3 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/libraries/System.IO/tests/MemoryStream/MemoryStream.ToArrayTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
using System; | ||
using System.IO; | ||
using System.Collections.Generic; | ||
using System.Security.Cryptography; | ||
|
||
namespace System.IO.Tests | ||
{ | ||
public class MemoryStream_ToArrayTests | ||
{ | ||
[Theory] | ||
[MemberData(nameof(GetArraysVariedBySize))] | ||
public static void ToArray_ZeroOffset(byte[] array) | ||
{ | ||
var stream = new MemoryStream(); | ||
stream.Write(array); | ||
var newArray = stream.ToArray(); | ||
|
||
Assert.Equal(array.Length, newArray.Length); | ||
Assert.Equal(array, newArray); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(GetArraysVariedBySize))] | ||
public static void ToArray_Offset(byte[] array) | ||
{ | ||
int index = 0; | ||
int count = array.Length; | ||
|
||
if (count > 3) | ||
{ | ||
// Trim some off each end | ||
index = 1; | ||
count -= 3; | ||
} | ||
|
||
var stream = new MemoryStream(array, index, count); | ||
var newArray = stream.ToArray(); | ||
|
||
Assert.Equal(count, newArray.Length); | ||
Assert.True(array.AsSpan(index, count).SequenceEqual(newArray)); | ||
} | ||
|
||
public static IEnumerable<object[]> GetArraysVariedBySize() | ||
{ | ||
yield return new object[] { RandomNumberGenerator.GetBytes(0) }; | ||
yield return new object[] { RandomNumberGenerator.GetBytes(1) }; | ||
yield return new object[] { RandomNumberGenerator.GetBytes(2) }; | ||
yield return new object[] { RandomNumberGenerator.GetBytes(256) }; | ||
yield return new object[] { RandomNumberGenerator.GetBytes(512) }; | ||
yield return new object[] { RandomNumberGenerator.GetBytes(1024) }; | ||
yield return new object[] { RandomNumberGenerator.GetBytes(2047) }; | ||
yield return new object[] { RandomNumberGenerator.GetBytes(2048) }; | ||
yield return new object[] { RandomNumberGenerator.GetBytes(2049) }; | ||
yield return new object[] { RandomNumberGenerator.GetBytes(2100) }; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters