From f565eeef748bbc79306aae6255f9c85c4aee9025 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Fri, 17 Mar 2023 07:32:04 +1000 Subject: [PATCH] Add test case for mem callback removing itself --- .../MemoryCallbackSystemTests.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/BizHawk.Tests/Emulation.Common/Base Implementations/MemoryCallbackSystemTests.cs b/src/BizHawk.Tests/Emulation.Common/Base Implementations/MemoryCallbackSystemTests.cs index 2fb35ddc8e..5cacef19ab 100644 --- a/src/BizHawk.Tests/Emulation.Common/Base Implementations/MemoryCallbackSystemTests.cs +++ b/src/BizHawk.Tests/Emulation.Common/Base Implementations/MemoryCallbackSystemTests.cs @@ -257,6 +257,37 @@ namespace BizHawk.Tests.Emulation.Common "Callback list is incorrect"); } + [TestMethod] + public void TestRemovingSelfWithinCallback() + { + MemoryCallback callback1 = new(ScopeA, MemoryCallbackType.Read, "Callback 1", _testCallbacks.Callback1, null, null); + + MemoryCallback? callback2 = null; + var callback2invoked = false; + MemoryCallbackDelegate callback = (_, _, _) => + { + callback2invoked = true; + _memoryCallbackSystem.Remove(callback2!.Callback); + }; + + callback2 = new(ScopeA, MemoryCallbackType.Read, "Callback 2", callback, null, null); + MemoryCallback callback3 = new(ScopeA, MemoryCallbackType.Read, "Callback 3", _testCallbacks.Callback3, null, null); + + _memoryCallbackSystem.Add(callback1); + _memoryCallbackSystem.Add(callback2); + _memoryCallbackSystem.Add(callback3); + + _memoryCallbackSystem.CallMemoryCallbacks(0, 0, (uint) MemoryCallbackFlags.AccessRead, ScopeA); + + Assert.AreEqual(1, _testCallbacks.Callback1Invocations.Count, "Callback 1 not invoked correctly"); + Assert.IsTrue(callback2invoked, "Callback 2 not invoked"); + Assert.AreEqual(1, _testCallbacks.Callback3Invocations.Count, "Callback 3 not invoked correctly"); + CollectionAssert.AreEqual( + new[] { callback1, callback3 }, + _memoryCallbackSystem.ToList(), + "Callback list is incorrect"); + } + private sealed class TestCallbackHolder { public List<(uint Address, uint Value, uint Flags)> Callback1Invocations { get; } = new();