From acea7b2e728787a0d83bbf83c8c1f042d2c32e7e Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Mon, 3 Jun 2024 10:15:45 +0800 Subject: + plugins project --- .../Collections/BagTests.cs | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Collections/BagTests.cs (limited to 'Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Collections/BagTests.cs') diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Collections/BagTests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Collections/BagTests.cs new file mode 100644 index 0000000..f89b45b --- /dev/null +++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Collections/BagTests.cs @@ -0,0 +1,48 @@ +using MonoGame.Extended.Collections; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Runtime; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using Xunit; + +namespace MonoGame.Extended.Tests.Collections +{ + public class BagTests + { + [Fact] + public void Bag_Enumeration_Does_Not_Allocate() + { + var bag = new Bag(); + for (int i = 0; i < 100; i++) bag.Add(i); + // ensure we have plenty of memory and that the heap only increases for the duration of this test + Assert.True(GC.TryStartNoGCRegion(Unsafe.SizeOf.BagEnumerator>() * 1000)); + var heapSize = GC.GetAllocatedBytesForCurrentThread(); + + // this should NOT allocate + foreach (int i in bag) + { + // assert methods cause the NoGCRegion to fail, so do this manually + if (GC.GetAllocatedBytesForCurrentThread() != heapSize) + Assert.True(false); + } + + // sanity check: this SHOULD allocate + foreach (int _ in (IEnumerable)bag) + { + // assert methods cause the NoGCRegion to fail, so do this manually + if (GC.GetAllocatedBytesForCurrentThread() == heapSize) + Assert.True(false); + } + + // Wrap in if statement due to exception thrown when running test through + // cake build script or when debugging test script manually. + if(GCSettings.LatencyMode == GCLatencyMode.NoGCRegion) + { + GC.EndNoGCRegion(); + } + } + + } +} -- cgit v1.1-26-g67d0