mirror of
https://github.com/CommunityToolkit/dotnet.git
synced 2026-10-24 00:35:04 +08:00
21547f3712
* Added support for .NET Core 2.1 and 3.1 targets * Fixed T[] dangerous accessors on .NET Standard * Minor speed improvements to Array2DColumnEnumerable<T> * Updated 2D array extensions * Minor code tweaks * Added DangerousGetLookupReferenceAt<T> extension * Added BitHelper.HasLookupFlag APIs * Added BitHelper.ExtractRange and SetRange APIs * Added bool bitwise mask extensions * Added StreamExtensions class on .NET Standard 2.0 * Added IBufferWriterExtensions type * Added more Stream extensions * Added object field offset extensions * Minor code refactoring * Improved description * Switched managed count to IntPtr * Refactored GetDjb2HashCode code, minor tweaks * Unit tests fixes * Refactored Count extensions, minor tweaks * Fixed incorrect operators precedence * Added SpanHelper.GetDjb2LikeByteHash method * Minor code refactoring * Code refactoring to HashCode<T> * Minor style tweaks * Updated comments styles to follow style convention * Minor code tweaks to the Count method * More code tweaks to the Count method * Added tests for DangerousGetObjectDataByteOffset * Fixed visibility of new object extensions * Added tests for 64-bit extract/set range APIs * Added tests for bool to bitwise mask APIs * Fixed MemoryStream EOF exception type * Enabled Stream extension tests on UWP * Added StreamExtensions read/write value tests * Minor code refactoring to IBufferWriter<T> extensions * Added IBufferWriterExtensions tests * Added MemoryBufferWriter<T> type * Added IBuffer<T> interface * Added MemoryBufferWriterDebugView<T> type * Added MemoryBufferWriter<T> tests * Fixed a unit test * Fixed incorrect inlining attribute * Removed leftover partial modifier * Added missing readonly modifier for lock structs * Improved summary for the Box<T> type * Added comments for #pragma warning disable items * Added info on (x << 5) + x over x * 33 optimization * Improved XML docs for BitHelper APIs * Fixed potentially misleading test method names * Added comment for MemoryOwner<T>.Dispose test * Fixed multi-target support for string extensions * Improved IMemoryOwner.AsStream tests * Added XML remarks to the HashCode<T> type * Improved docs for ReadOnlySpanExtensions.GetDjb2HashCode<T> * Tweaked an XML link * Tweaked attributes for SpanHelper methods * Minor optimizations to SpanHelper.Count * Fixed an overflow bug in SpanHelper.Count * Speed improvements to SpanHelper.Count ~35% speedup with large collections of byte-size values, ~10% with other types * Updated .csproj description * Added float/double support to SpanHelper.Count * Added exception to the Box<T> constructor * Removed unnecessary [SuppressMessage] attributes * Removed [SuppressMessage] attributes * Changed NullableRef<T>-s exceptions to follow Nullable<T> * Added more operators to NullableRef<T> types * Added missing in modifiers in ParallelHelper.For overloads * Removed SIMD support for float/double count It lacked proper handling of NaN values and both (signed) zeros * Fixed compiler directives in SpanEnumerable<T> * Updated ReadOnlySpanEnumerable<T> type * Updated NuGet package tags * Added HighPerformance package to readme.md list * Added .NET Standard 1.4 target * Added tests for implicit NullableRef<T> operators * Removed unnecessary using directive * Added missing sealed modifiers to MemoryStream methods * Added Span<T>.IndexOf(ref T) extension * Added ReadOnlySpan<T>.IndexOf<T>(in T) extension * Added missing changes (incorrect commit) * Removed unnecessary workaround (method is inlined) * Added improved remarks for bool.ToBitwiseMask32/64 * Added internal NotNullWhen attribute when < .NET Standard 2.1 * Enabled .csproj-wide nullability annotations * Minor code style tweaks * Added [ReadOnly]NullableRef<T>.Null static properties
254 lines
8.9 KiB
C#
254 lines
8.9 KiB
C#
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Diagnostics.Contracts;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Toolkit.HighPerformance.Extensions;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
namespace UnitTests.HighPerformance.Streams
|
|
{
|
|
[TestClass]
|
|
public partial class Test_MemoryStream
|
|
{
|
|
[TestCategory("MemoryStream")]
|
|
[TestMethod]
|
|
public void Test_MemoryStream_Lifecycle()
|
|
{
|
|
Memory<byte> memory = new byte[100];
|
|
|
|
Stream stream = memory.AsStream();
|
|
|
|
Assert.IsTrue(stream.CanRead);
|
|
Assert.IsTrue(stream.CanSeek);
|
|
Assert.IsTrue(stream.CanWrite);
|
|
Assert.AreEqual(stream.Length, memory.Length);
|
|
Assert.AreEqual(stream.Position, 0);
|
|
|
|
stream.Dispose();
|
|
|
|
Assert.IsFalse(stream.CanRead);
|
|
Assert.IsFalse(stream.CanSeek);
|
|
Assert.IsFalse(stream.CanWrite);
|
|
Assert.ThrowsException<ObjectDisposedException>(() => stream.Length);
|
|
Assert.ThrowsException<ObjectDisposedException>(() => stream.Position);
|
|
}
|
|
|
|
[TestCategory("MemoryStream")]
|
|
[TestMethod]
|
|
public void Test_MemoryStream_Seek()
|
|
{
|
|
Stream stream = new byte[100].AsMemory().AsStream();
|
|
|
|
Assert.AreEqual(stream.Position, 0);
|
|
|
|
stream.Position = 42;
|
|
|
|
Assert.AreEqual(stream.Position, 42);
|
|
|
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => stream.Position = -1);
|
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => stream.Position = 120);
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => stream.Seek(-1, SeekOrigin.Begin));
|
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => stream.Seek(120, SeekOrigin.Begin));
|
|
|
|
Assert.AreEqual(stream.Position, 0);
|
|
|
|
stream.Seek(-1, SeekOrigin.End);
|
|
|
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => stream.Seek(20, SeekOrigin.End));
|
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => stream.Seek(-120, SeekOrigin.End));
|
|
|
|
Assert.AreEqual(stream.Position, stream.Length - 1);
|
|
|
|
stream.Seek(42, SeekOrigin.Begin);
|
|
stream.Seek(20, SeekOrigin.Current);
|
|
stream.Seek(-30, SeekOrigin.Current);
|
|
|
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => stream.Seek(-64, SeekOrigin.Current));
|
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => stream.Seek(80, SeekOrigin.Current));
|
|
|
|
Assert.AreEqual(stream.Position, 32);
|
|
}
|
|
|
|
[TestCategory("MemoryStream")]
|
|
[TestMethod]
|
|
public void Test_MemoryStream_ReadWrite_Array()
|
|
{
|
|
Stream stream = new byte[100].AsMemory().AsStream();
|
|
|
|
byte[] data = CreateRandomData(64);
|
|
|
|
stream.Write(data, 0, data.Length);
|
|
|
|
Assert.AreEqual(stream.Position, data.Length);
|
|
|
|
stream.Position = 0;
|
|
|
|
byte[] result = new byte[data.Length];
|
|
|
|
int bytesRead = stream.Read(result, 0, result.Length);
|
|
|
|
Assert.AreEqual(bytesRead, result.Length);
|
|
Assert.AreEqual(stream.Position, data.Length);
|
|
Assert.IsTrue(data.AsSpan().SequenceEqual(result));
|
|
|
|
Assert.ThrowsException<ArgumentNullException>(() => stream.Write(null, 0, 10));
|
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => stream.Write(data, -1, 10));
|
|
Assert.ThrowsException<ArgumentException>(() => stream.Write(data, 200, 10));
|
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() => stream.Write(data, 0, -24));
|
|
Assert.ThrowsException<ArgumentException>(() => stream.Write(data, 0, 200));
|
|
|
|
stream.Dispose();
|
|
|
|
Assert.ThrowsException<ObjectDisposedException>(() => stream.Write(data, 0, data.Length));
|
|
}
|
|
|
|
[TestCategory("MemoryStream")]
|
|
[TestMethod]
|
|
public async Task Test_MemoryStream_ReadWriteAsync_Array()
|
|
{
|
|
Stream stream = new byte[100].AsMemory().AsStream();
|
|
|
|
byte[] data = CreateRandomData(64);
|
|
|
|
await stream.WriteAsync(data, 0, data.Length);
|
|
|
|
Assert.AreEqual(stream.Position, data.Length);
|
|
|
|
stream.Position = 0;
|
|
|
|
byte[] result = new byte[data.Length];
|
|
|
|
int bytesRead = await stream.ReadAsync(result, 0, result.Length);
|
|
|
|
Assert.AreEqual(bytesRead, result.Length);
|
|
Assert.AreEqual(stream.Position, data.Length);
|
|
Assert.IsTrue(data.AsSpan().SequenceEqual(result));
|
|
|
|
await Assert.ThrowsExceptionAsync<ArgumentNullException>(() => stream.WriteAsync(null, 0, 10));
|
|
await Assert.ThrowsExceptionAsync<ArgumentOutOfRangeException>(() => stream.WriteAsync(data, -1, 10));
|
|
await Assert.ThrowsExceptionAsync<ArgumentException>(() => stream.WriteAsync(data, 200, 10));
|
|
await Assert.ThrowsExceptionAsync<ArgumentOutOfRangeException>(() => stream.WriteAsync(data, 0, -24));
|
|
await Assert.ThrowsExceptionAsync<ArgumentException>(() => stream.WriteAsync(data, 0, 200));
|
|
|
|
stream.Dispose();
|
|
|
|
await Assert.ThrowsExceptionAsync<ObjectDisposedException>(() => stream.WriteAsync(data, 0, data.Length));
|
|
}
|
|
|
|
[TestCategory("MemoryStream")]
|
|
[TestMethod]
|
|
[SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1500", Justification = "Array initialization")]
|
|
public void Test_MemoryStream_ReadWriteByte()
|
|
{
|
|
Stream stream = new byte[4].AsMemory().AsStream();
|
|
|
|
ReadOnlySpan<byte> data = stackalloc byte[] { 1, 128, 255, 32 };
|
|
|
|
foreach (var item in data.Enumerate())
|
|
{
|
|
Assert.AreEqual(stream.Position, item.Index);
|
|
|
|
stream.WriteByte(item.Value);
|
|
}
|
|
|
|
Assert.AreEqual(stream.Position, data.Length);
|
|
|
|
stream.Position = 0;
|
|
|
|
Span<byte> result = stackalloc byte[4];
|
|
|
|
foreach (ref byte value in result)
|
|
{
|
|
value = checked((byte)stream.ReadByte());
|
|
}
|
|
|
|
Assert.AreEqual(stream.Position, data.Length);
|
|
Assert.IsTrue(data.SequenceEqual(result));
|
|
|
|
Assert.ThrowsException<ArgumentException>(() => stream.WriteByte(128));
|
|
|
|
int exitCode = stream.ReadByte();
|
|
|
|
Assert.AreEqual(exitCode, -1);
|
|
}
|
|
|
|
[TestCategory("MemoryStream")]
|
|
[TestMethod]
|
|
public void Test_MemoryStream_ReadWrite_Span()
|
|
{
|
|
Stream stream = new byte[100].AsMemory().AsStream();
|
|
|
|
Memory<byte> data = CreateRandomData(64);
|
|
|
|
// This will use the extension when on .NET Standard 2.0,
|
|
// as the Stream class doesn't have Spam<T> or Memory<T>
|
|
// public APIs there. This is the case eg. on UWP as well.
|
|
stream.Write(data.Span);
|
|
|
|
Assert.AreEqual(stream.Position, data.Length);
|
|
|
|
stream.Position = 0;
|
|
|
|
Span<byte> result = new byte[data.Length];
|
|
|
|
int bytesRead = stream.Read(result);
|
|
|
|
Assert.AreEqual(bytesRead, result.Length);
|
|
Assert.AreEqual(stream.Position, data.Length);
|
|
Assert.IsTrue(data.Span.SequenceEqual(result));
|
|
}
|
|
|
|
[TestCategory("MemoryStream")]
|
|
[TestMethod]
|
|
public async Task Test_MemoryStream_ReadWriteAsync_Memory()
|
|
{
|
|
Stream stream = new byte[100].AsMemory().AsStream();
|
|
|
|
Memory<byte> data = CreateRandomData(64);
|
|
|
|
await stream.WriteAsync(data);
|
|
|
|
Assert.AreEqual(stream.Position, data.Length);
|
|
|
|
stream.Position = 0;
|
|
|
|
Memory<byte> result = new byte[data.Length];
|
|
|
|
int bytesRead = await stream.ReadAsync(result);
|
|
|
|
Assert.AreEqual(bytesRead, result.Length);
|
|
Assert.AreEqual(stream.Position, data.Length);
|
|
Assert.IsTrue(data.Span.SequenceEqual(result.Span));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a random <see cref="byte"/> array filled with random data.
|
|
/// </summary>
|
|
/// <param name="count">The number of array items to create.</param>
|
|
/// <returns>The returned random array.</returns>
|
|
[Pure]
|
|
private static byte[] CreateRandomData(int count)
|
|
{
|
|
var random = new Random(DateTime.Now.Ticks.GetHashCode());
|
|
|
|
byte[] data = new byte[count];
|
|
|
|
foreach (ref byte n in MemoryMarshal.AsBytes(data.AsSpan()))
|
|
{
|
|
n = (byte)random.Next(0, byte.MaxValue);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
}
|
|
}
|