Added a test case that reproduces the debug assertion with the old code and verifies that it is fixed with the new one.