mirror of
https://github.com/chylex/.NET-Community-Toolkit.git
synced 2025-08-16 05:31:46 +02:00
.github
CommunityToolkit.Common
CommunityToolkit.Diagnostics
CommunityToolkit.HighPerformance
CommunityToolkit.Mvvm
CommunityToolkit.Mvvm.SourceGenerators
build
tests
CommunityToolkit.Common.UnitTests
CommunityToolkit.Diagnostics.UnitTests
CommunityToolkit.HighPerformance.UnitTests
Buffers
Enumerables
Extensions
Test_ArrayExtensions.1D.cs
Test_ArrayExtensions.2D.cs
Test_ArrayExtensions.3D.cs
Test_ArrayPoolExtensions.cs
Test_BoolExtensions.cs
Test_HashCodeExtensions.cs
Test_IBufferWriterExtensions.cs
Test_IMemoryOwnerExtensions.cs
Test_MemoryExtensions.cs
Test_NullableExtensions.cs
Test_ReadOnlyMemoryExtensions.cs
Test_ReadOnlySpanExtensions.Count.cs
Test_ReadOnlySpanExtensions.cs
Test_SpanExtensions.cs
Test_SpinLockExtensions.cs
Test_StreamExtensions.cs
Test_StringExtensions.cs
Helpers
Memory
Streams
CommunityToolkit.HighPerformance.UnitTests.csproj
Test_Box{T}.cs
Test_NullableReadOnlyRef{T}.cs
Test_NullableRef{T}.cs
Test_ReadOnlyRef{T}.cs
Test_Ref{T}.cs
CommunityToolkit.Mvvm.DisableINotifyPropertyChanging.UnitTests
CommunityToolkit.Mvvm.ExternalAssembly
CommunityToolkit.Mvvm.Internals.UnitTests
CommunityToolkit.Mvvm.SourceGenerators.UnitTests
CommunityToolkit.Mvvm.UnitTests
.editorconfig
.git-blame-ignore-revs
.gitattributes
.gitignore
.runsettings
CODE_OF_CONDUCT.md
Contributing.md
Directory.Build.props
Directory.Build.targets
License.md
README.md
ThirdPartyNotices.txt
azure-pipelines.yml
dotnet Community Toolkit.sln
toolkit.snk
version.json
54 lines
2.2 KiB
C#
54 lines
2.2 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.Diagnostics.CodeAnalysis;
|
|
using System.Runtime.CompilerServices;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
namespace CommunityToolkit.HighPerformance.UnitTests.Extensions;
|
|
|
|
[TestClass]
|
|
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1601", Justification = "Partial test class")]
|
|
public partial class Test_ArrayExtensions
|
|
{
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_DangerousGetReference()
|
|
{
|
|
string[] tokens = "aa,bb,cc,dd,ee,ff,gg,hh,ii".Split(',');
|
|
|
|
// In all these "DangerousGetReference" tests, we need to ensure that a reference to a given
|
|
// item within an array is effectively the one corresponding to the one whe expect, which is
|
|
// either a reference to the first item if we use "DangerousGetReference", or one to the n-th
|
|
// item if we use "DangerousGetReferenceAt". So all these tests just invoke the API and then
|
|
// compare the returned reference against an existing baseline (like the built-in array indexer)
|
|
// to ensure that the two are the same. These are all managed references, so no need for pinning.
|
|
ref string r0 = ref Unsafe.AsRef(tokens.DangerousGetReference());
|
|
ref string r1 = ref Unsafe.AsRef(tokens[0]);
|
|
|
|
Assert.IsTrue(Unsafe.AreSame(ref r0, ref r1));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_DangerousGetReferenceAt_Zero()
|
|
{
|
|
string[] tokens = "aa,bb,cc,dd,ee,ff,gg,hh,ii".Split(',');
|
|
|
|
ref string r0 = ref Unsafe.AsRef(tokens.DangerousGetReference());
|
|
ref string r1 = ref Unsafe.AsRef(tokens.DangerousGetReferenceAt(0));
|
|
|
|
Assert.IsTrue(Unsafe.AreSame(ref r0, ref r1));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_DangerousGetReferenceAt_Index()
|
|
{
|
|
string[] tokens = "aa,bb,cc,dd,ee,ff,gg,hh,ii".Split(',');
|
|
|
|
ref string r0 = ref Unsafe.AsRef(tokens.DangerousGetReferenceAt(5));
|
|
ref string r1 = ref Unsafe.AsRef(tokens[5]);
|
|
|
|
Assert.IsTrue(Unsafe.AreSame(ref r0, ref r1));
|
|
}
|
|
}
|