Factor common encode test code and make consistent.

Also add an additional test for marshaling to nil pointer in addition to
the existing tests for mashaling to a nil interface and a pointer to a nil
interface.