The following code is being executed in a highly threaded environment.
1. Why does the Debug.Assert statement sometimes fail?
2. What can be done to stop this from occurring?
public class Warehouse
{
private int stockCount = 0;
public void DecrementStock ()
{
if ( stockCount > 0 )
stockCount--;
Debug.Assert ( stockCount >= 0 )
}
public void IncrementStock()
{
stockCount ++;
}
}
The answer is a beauty:
Remove the code in decrementing the stock… this would make the condition of Debug.Assert to always true.
What a great answer - if in doubt, delete the code. As long as the Assert validates then the system must be correct. Why, even the unit tests would all work then. Genius!!
Personally, I think this is a simply genius solution to solve this problem. Why are you bagging it?
ReplyDeleteI think the question itself is a bug, please read your code carefully!
ReplyDeleteWhy would it matter? The class is pointless, there is never any output from it.
ReplyDeleteAnd what magnificent unit tests were you planning on running?
ReplyDelete[TestFixture]
public WarehouseUnitTests
{
[Test]
public void WarehouseTest
{
Warehouse warehouse = new Warehouse();
warehouse.DecrementStock();
Assert.?!?!??!?!?!
}
}
Hahahhaahahah!
By the way, your blog pages don't work properly on Safari. The comment links go disappearing when you put the mouse pointer over them.
ReplyDelete