Minor bugfixes

Please review, new tests included, all tests pass
If built with -DNDEBUG, all the checks get optimized to nothing, and we get exit code 0, and "OK" printed, or am I missing something? Instead of assert, perhaps a "if (err != ...) goto FAIL;"?