Cosmetic changes to test framework

Re-order function definitions to group them more logically, and add a
few more comments.  No functional change whatsoever.