Skip to content

Commit f565eee

Browse files
committed
Add test case for mem callback removing itself
1 parent f021159 commit f565eee

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/BizHawk.Tests/Emulation.Common/Base Implementations/MemoryCallbackSystemTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,37 @@ public void TestRemovingCallbackWithinCallback()
257257
"Callback list is incorrect");
258258
}
259259

260+
[TestMethod]
261+
public void TestRemovingSelfWithinCallback()
262+
{
263+
MemoryCallback callback1 = new(ScopeA, MemoryCallbackType.Read, "Callback 1", _testCallbacks.Callback1, null, null);
264+
265+
MemoryCallback? callback2 = null;
266+
var callback2invoked = false;
267+
MemoryCallbackDelegate callback = (_, _, _) =>
268+
{
269+
callback2invoked = true;
270+
_memoryCallbackSystem.Remove(callback2!.Callback);
271+
};
272+
273+
callback2 = new(ScopeA, MemoryCallbackType.Read, "Callback 2", callback, null, null);
274+
MemoryCallback callback3 = new(ScopeA, MemoryCallbackType.Read, "Callback 3", _testCallbacks.Callback3, null, null);
275+
276+
_memoryCallbackSystem.Add(callback1);
277+
_memoryCallbackSystem.Add(callback2);
278+
_memoryCallbackSystem.Add(callback3);
279+
280+
_memoryCallbackSystem.CallMemoryCallbacks(0, 0, (uint) MemoryCallbackFlags.AccessRead, ScopeA);
281+
282+
Assert.AreEqual(1, _testCallbacks.Callback1Invocations.Count, "Callback 1 not invoked correctly");
283+
Assert.IsTrue(callback2invoked, "Callback 2 not invoked");
284+
Assert.AreEqual(1, _testCallbacks.Callback3Invocations.Count, "Callback 3 not invoked correctly");
285+
CollectionAssert.AreEqual(
286+
new[] { callback1, callback3 },
287+
_memoryCallbackSystem.ToList(),
288+
"Callback list is incorrect");
289+
}
290+
260291
private sealed class TestCallbackHolder
261292
{
262293
public List<(uint Address, uint Value, uint Flags)> Callback1Invocations { get; } = new();

0 commit comments

Comments
 (0)