// 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 CommunityToolkit.HighPerformance.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; using CommunityToolkit.HighPerformance.UnitTests.Buffers.Internals; namespace CommunityToolkit.HighPerformance.UnitTests.Helpers; public partial class Test_ParallelHelper { [TestMethod] public void Test_ParallelHelper_ForEach_Ref() { foreach (int count in TestForCounts) { using UnmanagedSpanOwner data = CreateRandomData(count); using UnmanagedSpanOwner copy = new(count); data.GetSpan().CopyTo(copy.GetSpan()); foreach (ref int n in copy.GetSpan()) { n = unchecked(n * 397); } ParallelHelper.ForEach(data.Memory, new Multiplier(397)); Span dataSpan = data.GetSpan(); Span copySpan = copy.GetSpan(); for (int i = 0; i < data.Length; i++) { if (dataSpan[i] != copySpan[i]) { Assert.Fail($"Item #{i} was not a match, was {dataSpan[i]} instead of {copySpan[i]}"); } } } } /// /// A type implementing to multiply array elements. /// private readonly struct Multiplier : IRefAction { private readonly int factor; public Multiplier(int factor) => this.factor = factor; /// public void Invoke(ref int i) => i = unchecked(i * this.factor); } }