Refactor and add more regression tests.

This commit adds more test coverage for encoding.go and pulls out some existing
unit tests into separate files, to better match the current file structure. The
new encoding tests provide 100% coverage of encoding.go.