func TestReadInt64FromUnsigned(t *testing.T) {
	buf := new(bytes.Buffer)
	wr := NewWriter(buf)
	rd := NewReader(buf)
	uint64s := []uint64{100000, 0, 5000, 8, 240, math.MaxUint16, math.MaxUint32, math.MaxInt64}
	uint8s := []uint8{0, 8, math.MaxInt8}
	for i, num := range uint64s {
		buf.Reset()
		err := wr.WriteUint64(num)
		if err != nil {
			t.Fatal(err)
		}
		err = wr.Flush()
		if err != nil {
			t.Fatal(err)
		}
		out, err := rd.ReadInt64()
		if err != nil {
			t.Fatal(err)
		}
		if out != int64(num) {
			t.Errorf("Test case with uint64 %d: put %d in and got %d out", i, num, out)
		}
	}
	for i, num := range uint8s {
		buf.Reset()
		err := wr.WriteUint8(num)
		if err != nil {
			t.Fatal(err)
		}
		err = wr.Flush()
		if err != nil {
			t.Fatal(err)
		}
		out, err := rd.ReadInt8()
		if err != nil {
			t.Fatal(err)
		}
		if out != int8(num) {
			t.Errorf("Test case with uint8 %d: put %d in and got %d out", i, num, out)
		}
	}
}
ReadInt64 should be able to read an unsigned integer. More tests of this feature are in tests/read_int64_unsigned_test.go.
package msgp
import (
	"bytes"
	"io"
	"math"
	"math/rand"
	"reflect"
	"testing"
	"time"
)
func TestSanity(t *testing.T) {
	if !isfixint(0) {
		t.Fatal("broken isfixint")
	}
}
func TestReadIntf(t *testing.T) {
	var testCases = []interface{}{
		float64(128.032),
		float32(9082.092),
		int64(-40),
		uint64(9082981),
		time.Now(),
		time.Unix(1523820536, 4),
		"hello!",
		[]byte("hello!"),
		map[string]interface{}{
			"thing-1": "thing-1-value",
			"thing-2": int64(800),
			"thing-3": []byte("some inner bytes..."),
			"thing-4": false,
		},
	}
	var buf bytes.Buffer
	var v interface{}
	dec := NewReader(&buf)
	enc := NewWriter(&buf)
	for i, ts := range testCases {
		buf.Reset()
		err := enc.WriteIntf(ts)
		if err != nil {
			t.Errorf("(case %d) %s", i, err)
			continue
		}
		err = enc.Flush()
		if err != nil {
			t.Fatal(err)
		}
		v, err = dec.ReadIntf()
		if err != nil {
			t.Errorf("(case %d) %s", i, err)
		}
		if tm, ok := v.(time.Time); ok {
			if !tm.Equal(v.(time.Time)) {
				t.Errorf("%v != %v", ts, v)
			}
		} else if !reflect.DeepEqual(v, ts) {
			t.Errorf("%v in; %v out", ts, v)
		}
	}
}
func TestReadMapHeader(t *testing.T) {
	cases := []uint32{0, 1, tuint16, tuint32}
	var buf bytes.Buffer
	wr := NewWriter(&buf)
	rd := NewReader(&buf)
	for i, tc := range cases {
		buf.Reset()
		err := wr.WriteMapHeader(tc)
		if err != nil {
			t.Fatal(err)
		}
		err = wr.Flush()
		if err != nil {
			t.Fatal(err)
		}
		sz, err := rd.ReadMapHeader()
		if err != nil {
			t.Errorf("(case %d) got error %s", i, err)
		}
		if sz != tc {
			t.Errorf("(case %d) wrote size %d; got size %d", i, tc, sz)
		}
	}
}
func BenchmarkReadMapHeader(b *testing.B) {
	sizes := []uint32{0, 1, tuint16, tuint32}
	data := make([]byte, 0, len(sizes)*5)
	for _, d := range sizes {
		data = AppendMapHeader(data, d)
	}
	rd := NewReader(NewEndlessReader(data, b))
	b.SetBytes(int64(len(data) / len(sizes)))
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		rd.ReadMapHeader()
	}
}
func TestReadArrayHeader(t *testing.T) {
	cases := []uint32{0, 1, tuint16, tuint32}
	var buf bytes.Buffer
	wr := NewWriter(&buf)
	rd := NewReader(&buf)
	for i, tc := range cases {
		buf.Reset()
		err := wr.WriteArrayHeader(tc)
		if err != nil {
			t.Fatal(err)
		}
		err = wr.Flush()
		if err != nil {
			t.Fatal(err)
		}
		sz, err := rd.ReadArrayHeader()
		if err != nil {
			t.Errorf("Test case %d: got error %s", i, err)
		}
		if sz != tc {
			t.Errorf("Test case %d: wrote size %d; got size %d", i, tc, sz)
		}
	}
}
func BenchmarkReadArrayHeader(b *testing.B) {
	sizes := []uint32{0, 1, tuint16, tuint32}
	data := make([]byte, 0, len(sizes)*5)
	for _, d := range sizes {
		data = AppendArrayHeader(data, d)
	}
	rd := NewReader(NewEndlessReader(data, b))
	b.ReportAllocs()
	b.SetBytes(int64(len(data) / len(sizes)))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		rd.ReadArrayHeader()
	}
}
func TestReadNil(t *testing.T) {
	var buf bytes.Buffer
	wr := NewWriter(&buf)
	rd := NewReader(&buf)
	wr.WriteNil()
	wr.Flush()
	err := rd.ReadNil()
	if err != nil {
		t.Fatal(err)
	}
}
func BenchmarkReadNil(b *testing.B) {
	data := AppendNil(nil)
	rd := NewReader(NewEndlessReader(data, b))
	b.ReportAllocs()
	b.SetBytes(1)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		err := rd.ReadNil()
		if err != nil {
			b.Fatal(err)
		}
	}
}
func TestReadFloat64(t *testing.T) {
	var buf bytes.Buffer
	wr := NewWriter(&buf)
	rd := NewReader(&buf)
	for i := 0; i < 100; i++ {
		buf.Reset()
		flt := (rand.Float64() - 0.5) * math.MaxFloat64
		err := wr.WriteFloat64(flt)
		if err != nil {
			t.Fatal(err)
		}
		err = wr.Flush()
		if err != nil {
			t.Fatal(err)
		}
		out, err := rd.ReadFloat64()
		if err != nil {
			t.Errorf("Error reading %f: %s", flt, err)
			continue
		}
		if out != flt {
			t.Errorf("Put in %f but got out %f", flt, out)
		}
	}
}
func BenchmarkReadFloat64(b *testing.B) {
	fs := []float64{rand.Float64(), rand.Float64(), rand.Float64(), rand.Float64()}
	data := make([]byte, 0, 9*len(fs))
	for _, f := range fs {
		data = AppendFloat64(data, f)
	}
	rd := NewReader(NewEndlessReader(data, b))
	b.SetBytes(9)
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_, err := rd.ReadFloat64()
		if err != nil {
			b.Fatal(err)
		}
	}
}
func TestReadFloat32(t *testing.T) {
	var buf bytes.Buffer
	wr := NewWriter(&buf)
	rd := NewReader(&buf)
	for i := 0; i < 10000; i++ {
		buf.Reset()
		flt := (rand.Float32() - 0.5) * math.MaxFloat32
		err := wr.WriteFloat32(flt)
		if err != nil {
			t.Fatal(err)
		}
		err = wr.Flush()
		if err != nil {
			t.Fatal(err)
		}
		out, err := rd.ReadFloat32()
		if err != nil {
			t.Errorf("Error reading %f: %s", flt, err)
			continue
		}
		if out != flt {
			t.Errorf("Put in %f but got out %f", flt, out)
		}
	}
}
func BenchmarkReadFloat32(b *testing.B) {
	fs := []float32{rand.Float32(), rand.Float32(), rand.Float32(), rand.Float32()}
	data := make([]byte, 0, 5*len(fs))
	for _, f := range fs {
		data = AppendFloat32(data, f)
	}
	rd := NewReader(NewEndlessReader(data, b))
	b.SetBytes(5)
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_, err := rd.ReadFloat32()
		if err != nil {
			b.Fatal(err)
		}
	}
}
func TestReadInt64(t *testing.T) {
	var buf bytes.Buffer
	wr := NewWriter(&buf)
	rd := NewReader(&buf)
	ints := []int64{-100000, -5000, -5, 0, 8, 240, int64(tuint16), int64(tuint32), int64(tuint64)}
	for i, num := range ints {
		buf.Reset()
		err := wr.WriteInt64(num)
		if err != nil {
			t.Fatal(err)
		}
		err = wr.Flush()
		if err != nil {
			t.Fatal(err)
		}
		out, err := rd.ReadInt64()
		if err != nil {
			t.Fatal(err)
		}
		if out != num {
			t.Errorf("Test case %d: put %d in and got %d out", i, num, out)
		}
	}
}