fix bugs, add tests for snprintf