func TestHashMarshalling(t *testing.T) {
	h := HashObject("an object")
	hBytes, err := json.Marshal(h)
	if err != nil {
		t.Fatal(err)
	}
	var uMarH Hash
	err = uMarH.UnmarshalJSON(hBytes)
	if err != nil {
		t.Fatal(err)
	}
	if h != uMarH {
		t.Error("encoded and decoded hash do not match!")
	}
}
TestHashMarshalling checks that the marshalling of the hash type works as
expected.
package crypto
import (
	"bytes"
	"encoding/json"
	"sort"
	"strings"
	"testing"
	"github.com/NebulousLabs/fastrand"
)
type (
	TestObject struct {
		A int
		B byte
		C bool
		D string
	}
)
func TestHashing(t *testing.T) {
	to := TestObject{
		A: 12345,
		B: 5,
		C: true,
		D: "testing",
	}
	var emptyHash Hash
	h0 := HashObject(to)
	if h0 == emptyHash {
		t.Error("HashObject returned the zero hash!")
	}
	h1 := HashAll(
		int(122),
		byte(115),
		string("test"),
		to,
	)
	if h1 == emptyHash {
		t.Error("HashObject returned the zero hash!")
	}
	h2 := HashBytes(fastrand.Bytes(435))
	if h2 == emptyHash {
		t.Error("HashObject returned the zero hash!")
	}
}
func TestHashSorting(t *testing.T) {
	hashes := make([]Hash, 5)
	hashes[0][0] = 12
	hashes[1][0] = 7
	hashes[2][0] = 13
	hashes[3][0] = 14
	hashes[4][0] = 1
	sort.Sort(HashSlice(hashes))
	if hashes[0][0] != 1 {
		t.Error("bad sort")
	}
	if hashes[1][0] != 7 {
		t.Error("bad sort")
	}
	if hashes[2][0] != 12 {
		t.Error("bad sort")
	}
	if hashes[3][0] != 13 {
		t.Error("bad sort")
	}
	if hashes[4][0] != 14 {
		t.Error("bad sort")
	}
}
func TestUnitHashMarshalJSON(t *testing.T) {
	h := HashObject("an object")
	jsonBytes, err := h.MarshalJSON()
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(jsonBytes, []byte(`"`+h.String()+`"`)) {
		t.Errorf("hash %s encoded incorrectly: got %s\n", h, jsonBytes)
	}
}
func TestUnitHashUnmarshalJSON(t *testing.T) {
	invalidJSONBytes := [][]byte{
		nil,
		{},
		[]byte("\""),
		[]byte(""),
		[]byte(`"` + strings.Repeat("a", HashSize*2-1) + `"`),
		[]byte(`"` + strings.Repeat("a", HashSize*2+1) + `"`),
		[]byte(`"` + strings.Repeat("z", HashSize*2) + `"`),
		[]byte(`"` + strings.Repeat(".", HashSize*2) + `"`),
		[]byte(`"` + strings.Repeat("\n", HashSize*2) + `"`),
	}
	for _, jsonBytes := range invalidJSONBytes {
		var h Hash
		err := h.UnmarshalJSON(jsonBytes)
		if err == nil {
			t.Errorf("expected unmarshall to fail on the invalid JSON: %q\n", jsonBytes)
		}
	}
	expectedH := HashObject("an object")
	jsonBytes := []byte(`"` + expectedH.String() + `"`)
	var h Hash
	err := h.UnmarshalJSON(jsonBytes)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(h[:], expectedH[:]) {
		t.Errorf("Hash %s marshalled incorrectly: got %s\n", expectedH, h)
	}
}