Added assertions to check conditions for exiting while loop

in _raisePermanenceToThreshold method

fixes #1322 
Can we do this change in another issue. I'm not sure of the rationale for this. In addition any changes like this need a corresponding change to the C implementation so that the two implementations remain identical.