fixed author and updated to r58 or higher

This commit is contained in:
Brian Ketelsen 2011-07-21 12:33:44 -04:00
parent bcad8d4c4c
commit 682d25b551
3 changed files with 1227 additions and 868 deletions

View File

@ -1,218 +1,318 @@
package msgpack_test
import (
. "msgpack"
"testing"
"bytes"
"reflect"
"math"
);
. "msgpack"
"testing"
"bytes"
"reflect"
"math"
)
func equal(lhs reflect.Value, rhs reflect.Value) bool {
{
_rhs, ok := rhs.(*reflect.InterfaceValue)
if ok { return equal(lhs, _rhs.Elem()) }
}
switch _lhs := lhs.(type) {
case *reflect.InterfaceValue:
return equal(_lhs.Elem(), rhs)
case *reflect.BoolValue:
_rhs, ok := rhs.(*reflect.BoolValue)
return ok && _lhs.Get() == _rhs.Get()
case *reflect.UintValue:
_rhs, ok := rhs.(*reflect.UintValue)
return ok && _lhs.Get() == _rhs.Get()
case *reflect.IntValue:
_rhs, ok := rhs.(*reflect.IntValue)
return ok && _lhs.Get() == _rhs.Get()
case *reflect.FloatValue:
_rhs, ok := rhs.(*reflect.FloatValue)
return ok && _lhs.Get() == _rhs.Get()
case reflect.ArrayOrSliceValue:
_rhs := rhs.(reflect.ArrayOrSliceValue)
if _lhs.Len() != _rhs.Len() { return false; }
for i := 0; i < _lhs.Len(); i++ {
if !equal(_lhs.Elem(i), _rhs.Elem(i)) { return false; }
}
return true;
case *reflect.MapValue:
_rhs := rhs.(*reflect.MapValue)
if _lhs.Len() != _rhs.Len() { return false; }
for _, k := range _lhs.Keys() {
lv, rv := _lhs.Elem(k), _rhs.Elem(k)
if lv == nil || rv == nil || !equal(lv, rv) { return false; }
}
return true;
}
return false;
{
_rhs := rhs
if _rhs.Kind() == reflect.Interface {
return equal(lhs, _rhs.Elem())
}
}
switch _lhs := lhs; _lhs.Kind() {
case reflect.Interface:
return equal(_lhs.Elem(), rhs)
case reflect.Bool:
_rhs := rhs
return _rhs.Kind() == reflect.Bool &&
_lhs.Bool() == _rhs.Bool()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
_rhs := rhs
return (_rhs.Kind() == reflect.Uint || _rhs.Kind() == reflect.Uint8 || _rhs.Kind() == reflect.Uint16 || _rhs.Kind() == reflect.Uint32 || _rhs.Kind() == reflect.Uint64 || _rhs.Kind() == reflect.Uintptr) &&
_lhs.Uint() == _rhs.Uint()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
_rhs := rhs
return (_rhs.Kind() == reflect.Int || _rhs.Kind() == reflect.Int8 || _rhs.Kind() == reflect.Int16 || _rhs.Kind() == reflect.Int32 || _rhs.Kind() == reflect.Int64) &&
_lhs.Int() == _rhs.Int()
case reflect.Float32, reflect.Float64:
_rhs := rhs
return (_rhs.Kind() == reflect.Float32 || _rhs.Kind() == reflect.Float64) &&
_lhs.Float() == _rhs.Float()
case reflect.Array, reflect.Slice:
_rhs := rhs
if _lhs.Len() != _rhs.Len() {
return false
}
for i := 0; i < _lhs.Len(); i++ {
if !equal(_lhs.Index(i), _rhs.Index(i)) {
return false
}
}
return true
case reflect.Map:
_rhs := rhs
if _lhs.Len() != _rhs.Len() {
return false
}
for _, k := range _lhs.MapKeys() {
lv, rv := _lhs.MapIndex(k), _rhs.MapIndex(k)
if !lv.IsValid() || !rv.IsValid() || !equal(lv, rv) {
return false
}
}
return true
}
return false
}
func TestPackUint8(t *testing.T) {
b := &bytes.Buffer{}
for _, i := range []uint8 { 0, 1, 2, 125, 126, 127, 128, 253, 254, 255 } {
_, err := PackUint8(b, i)
if err != nil { t.Error("err != nil") }
}
if bytes.Compare(b.Bytes(), []byte { 0x00, 0x01, 0x02, 0x7d, 0x7e, 0x7f, 0xcc, 0x80, 0xcc, 0xfd, 0xcc, 0xfe, 0xcc, 0xff }) != 0 { t.Error("wrong output", b.Bytes()) }
b := &bytes.Buffer{}
for _, i := range []uint8{0, 1, 2, 125, 126, 127, 128, 253, 254, 255} {
_, err := PackUint8(b, i)
if err != nil {
t.Error("err != nil")
}
}
if bytes.Compare(b.Bytes(), []byte{0x00, 0x01, 0x02, 0x7d, 0x7e, 0x7f, 0xcc, 0x80, 0xcc, 0xfd, 0xcc, 0xfe, 0xcc, 0xff}) != 0 {
t.Error("wrong output", b.Bytes())
}
}
func TestPackUint16(t *testing.T) {
b := &bytes.Buffer{}
for _, i := range []uint16 { 0, 1, 2, 125, 126, 127, 128, 253, 254, 255, 256, 65533, 65534, 65535 } {
_, err := PackUint16(b, i)
if err != nil { t.Error("err != nil") }
}
if bytes.Compare(b.Bytes(), []byte { 0x00, 0x01, 0x02, 0x7d, 0x7e, 0x7f, 0xcc, 0x80, 0xcc, 0xfd, 0xcc, 0xfe, 0xcc, 0xff, 0xcd, 0x01, 0x00, 0xcd, 0xff, 0xfd, 0xcd, 0xff, 0xfe, 0xcd, 0xff, 0xff }) != 0 { t.Error("wrong output", b.Bytes()) }
b := &bytes.Buffer{}
for _, i := range []uint16{0, 1, 2, 125, 126, 127, 128, 253, 254, 255, 256, 65533, 65534, 65535} {
_, err := PackUint16(b, i)
if err != nil {
t.Error("err != nil")
}
}
if bytes.Compare(b.Bytes(), []byte{0x00, 0x01, 0x02, 0x7d, 0x7e, 0x7f, 0xcc, 0x80, 0xcc, 0xfd, 0xcc, 0xfe, 0xcc, 0xff, 0xcd, 0x01, 0x00, 0xcd, 0xff, 0xfd, 0xcd, 0xff, 0xfe, 0xcd, 0xff, 0xff}) != 0 {
t.Error("wrong output", b.Bytes())
}
}
func TestPackUint32(t *testing.T) {
b := &bytes.Buffer{}
for _, i := range []uint32 { 0, 1, 2, 125, 126, 127, 128, 253, 254, 255, 256, 65533, 65534, 65535, 65536, 4294967293, 4294967294, 4294967295 } {
_, err := PackUint32(b, i)
if err != nil { t.Error("err != nil") }
}
if bytes.Compare(b.Bytes(), []byte { 0x00, 0x01, 0x02, 0x7d, 0x7e, 0x7f, 0xcc, 0x80, 0xcc, 0xfd, 0xcc, 0xfe, 0xcc, 0xff, 0xcd, 0x01, 0x00, 0xcd, 0xff, 0xfd, 0xcd, 0xff, 0xfe, 0xcd, 0xff, 0xff, 0xce, 0x00, 0x01, 0x00, 0x00, 0xce, 0xff, 0xff, 0xff, 0xfd, 0xce, 0xff, 0xff, 0xff, 0xfe, 0xce, 0xff, 0xff, 0xff, 0xff }) != 0 { t.Error("wrong output", b.Bytes()) }
b := &bytes.Buffer{}
for _, i := range []uint32{0, 1, 2, 125, 126, 127, 128, 253, 254, 255, 256, 65533, 65534, 65535, 65536, 4294967293, 4294967294, 4294967295} {
_, err := PackUint32(b, i)
if err != nil {
t.Error("err != nil")
}
}
if bytes.Compare(b.Bytes(), []byte{0x00, 0x01, 0x02, 0x7d, 0x7e, 0x7f, 0xcc, 0x80, 0xcc, 0xfd, 0xcc, 0xfe, 0xcc, 0xff, 0xcd, 0x01, 0x00, 0xcd, 0xff, 0xfd, 0xcd, 0xff, 0xfe, 0xcd, 0xff, 0xff, 0xce, 0x00, 0x01, 0x00, 0x00, 0xce, 0xff, 0xff, 0xff, 0xfd, 0xce, 0xff, 0xff, 0xff, 0xfe, 0xce, 0xff, 0xff, 0xff, 0xff}) != 0 {
t.Error("wrong output", b.Bytes())
}
}
func TestPackUint64(t *testing.T) {
b := &bytes.Buffer{}
for _, i := range []uint64 { 0, 1, 2, 125, 126, 127, 128, 253, 254, 255, 256, 65533, 65534, 65535, 65536, 4294967293, 4294967294, 4294967295, 4294967296, 18446744073709551613, 18446744073709551614, 18446744073709551615 } {
_, err := PackUint64(b, i)
if err != nil { t.Error("err != nil") }
}
if bytes.Compare(b.Bytes(), []byte { 0x00, 0x01, 0x02, 0x7d, 0x7e, 0x7f, 0xcc, 0x80, 0xcc, 0xfd, 0xcc, 0xfe, 0xcc, 0xff, 0xcd, 0x01, 0x00, 0xcd, 0xff, 0xfd, 0xcd, 0xff, 0xfe, 0xcd, 0xff, 0xff, 0xce, 0x00, 0x01, 0x00, 0x00, 0xce, 0xff, 0xff, 0xff, 0xfd, 0xce, 0xff, 0xff, 0xff, 0xfe, 0xce, 0xff, 0xff, 0xff, 0xff, 0xcf, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }) != 0 { t.Error("wrong output", b.Bytes()) }
b := &bytes.Buffer{}
for _, i := range []uint64{0, 1, 2, 125, 126, 127, 128, 253, 254, 255, 256, 65533, 65534, 65535, 65536, 4294967293, 4294967294, 4294967295, 4294967296, 18446744073709551613, 18446744073709551614, 18446744073709551615} {
_, err := PackUint64(b, i)
if err != nil {
t.Error("err != nil")
}
}
if bytes.Compare(b.Bytes(), []byte{0x00, 0x01, 0x02, 0x7d, 0x7e, 0x7f, 0xcc, 0x80, 0xcc, 0xfd, 0xcc, 0xfe, 0xcc, 0xff, 0xcd, 0x01, 0x00, 0xcd, 0xff, 0xfd, 0xcd, 0xff, 0xfe, 0xcd, 0xff, 0xff, 0xce, 0x00, 0x01, 0x00, 0x00, 0xce, 0xff, 0xff, 0xff, 0xfd, 0xce, 0xff, 0xff, 0xff, 0xfe, 0xce, 0xff, 0xff, 0xff, 0xff, 0xcf, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}) != 0 {
t.Error("wrong output", b.Bytes())
}
}
func TestPackInt8(t *testing.T) {
b := &bytes.Buffer{}
for _, i := range []int8 { -128, -127, -34, -33, -32, -31, 0, 1, 126, 127 } {
_, err := PackInt8(b, i)
if err != nil { t.Error("err != nil") }
}
if bytes.Compare(b.Bytes(), []byte { 0xd0, 0x80, 0xd0, 0x81, 0xd0, 0xde, 0xd0, 0xdf, 0xe0, 0xe1, 0x00, 0x01, 0x7e, 0x7f }) != 0 { t.Error("wrong output", b.Bytes()) }
b := &bytes.Buffer{}
for _, i := range []int8{-128, -127, -34, -33, -32, -31, 0, 1, 126, 127} {
_, err := PackInt8(b, i)
if err != nil {
t.Error("err != nil")
}
}
if bytes.Compare(b.Bytes(), []byte{0xd0, 0x80, 0xd0, 0x81, 0xd0, 0xde, 0xd0, 0xdf, 0xe0, 0xe1, 0x00, 0x01, 0x7e, 0x7f}) != 0 {
t.Error("wrong output", b.Bytes())
}
}
func TestPackInt16(t *testing.T) {
b := &bytes.Buffer{}
for _, i := range []int16 { -32768, -32767, -131, -130, -129, -128, -127, -34, -33, -32, -31, 0, 1, 126, 127, 128, 129, 130, 32765, 32766, 32767 } {
_, err := PackInt16(b, i)
if err != nil { t.Error("err != nil") }
}
if bytes.Compare(b.Bytes(), []byte { 0xd1, 0x80, 0x00, 0xd1, 0x80, 0x01, 0xd1, 0xff, 0x7d, 0xd1, 0xff, 0x7e, 0xd1, 0xff, 0x7f, 0xd0, 0x80, 0xd0, 0x81, 0xd0, 0xde, 0xd0, 0xdf, 0xe0, 0xe1, 0x00, 0x01, 0x7e, 0x7f, 0xd1, 0x00, 0x80, 0xd1, 0x00, 0x81, 0xd1, 0x00, 0x82, 0xd1, 0x7f, 0xfd, 0xd1, 0x7f, 0xfe, 0xd1, 0x7f, 0xff }) != 0 { t.Error("wrong output", b.Bytes()) }
b := &bytes.Buffer{}
for _, i := range []int16{-32768, -32767, -131, -130, -129, -128, -127, -34, -33, -32, -31, 0, 1, 126, 127, 128, 129, 130, 32765, 32766, 32767} {
_, err := PackInt16(b, i)
if err != nil {
t.Error("err != nil")
}
}
if bytes.Compare(b.Bytes(), []byte{0xd1, 0x80, 0x00, 0xd1, 0x80, 0x01, 0xd1, 0xff, 0x7d, 0xd1, 0xff, 0x7e, 0xd1, 0xff, 0x7f, 0xd0, 0x80, 0xd0, 0x81, 0xd0, 0xde, 0xd0, 0xdf, 0xe0, 0xe1, 0x00, 0x01, 0x7e, 0x7f, 0xd1, 0x00, 0x80, 0xd1, 0x00, 0x81, 0xd1, 0x00, 0x82, 0xd1, 0x7f, 0xfd, 0xd1, 0x7f, 0xfe, 0xd1, 0x7f, 0xff}) != 0 {
t.Error("wrong output", b.Bytes())
}
}
func TestPackInt32(t *testing.T) {
b := &bytes.Buffer{}
for _, i := range []int32 { -2147483648, -2147483647, -2147483646, -32771, -32770, -32769, -32768, -32767, -131, -130, -129, -128, -127, -34, -33, -32, -31, 0, 1, 126, 127, 128, 129, 130, 32765, 32766, 32767, 32768, 32769, 32770, 2147483645, 2147483646, 2147483647 } {
_, err := PackInt32(b, i)
if err != nil { t.Error("err != nil") }
}
if bytes.Compare(b.Bytes(), []byte { 0xd2, 0x80, 0x00, 0x00, 0x00, 0xd2, 0x80, 0x00, 0x00, 0x01, 0xd2, 0x80, 0x00, 0x00, 0x02, 0xd2, 0xff, 0xff, 0x7f, 0xfd, 0xd2, 0xff, 0xff, 0x7f, 0xfe, 0xd2, 0xff, 0xff, 0x7f, 0xff, 0xd1, 0x80, 0x00, 0xd1, 0x80, 0x01, 0xd1, 0xff, 0x7d, 0xd1, 0xff, 0x7e, 0xd1, 0xff, 0x7f, 0xd0, 0x80, 0xd0, 0x81, 0xd0, 0xde, 0xd0, 0xdf, 0xe0, 0xe1, 0x00, 0x01, 0x7e, 0x7f, 0xd1, 0x00, 0x80, 0xd1, 0x00, 0x81, 0xd1, 0x00, 0x82, 0xd1, 0x7f, 0xfd, 0xd1, 0x7f, 0xfe, 0xd1, 0x7f, 0xff, 0xd2, 0x00, 0x00, 0x80, 0x00, 0xd2, 0x00, 0x00, 0x80, 0x01, 0xd2, 0x00, 0x00, 0x80, 0x02, 0xd2, 0x7f, 0xff, 0xff, 0xfd, 0xd2, 0x7f, 0xff, 0xff, 0xfe, 0xd2, 0x7f, 0xff, 0xff, 0xff }) != 0 { t.Error("wrong output", b.Bytes()) }
b := &bytes.Buffer{}
for _, i := range []int32{-2147483648, -2147483647, -2147483646, -32771, -32770, -32769, -32768, -32767, -131, -130, -129, -128, -127, -34, -33, -32, -31, 0, 1, 126, 127, 128, 129, 130, 32765, 32766, 32767, 32768, 32769, 32770, 2147483645, 2147483646, 2147483647} {
_, err := PackInt32(b, i)
if err != nil {
t.Error("err != nil")
}
}
if bytes.Compare(b.Bytes(), []byte{0xd2, 0x80, 0x00, 0x00, 0x00, 0xd2, 0x80, 0x00, 0x00, 0x01, 0xd2, 0x80, 0x00, 0x00, 0x02, 0xd2, 0xff, 0xff, 0x7f, 0xfd, 0xd2, 0xff, 0xff, 0x7f, 0xfe, 0xd2, 0xff, 0xff, 0x7f, 0xff, 0xd1, 0x80, 0x00, 0xd1, 0x80, 0x01, 0xd1, 0xff, 0x7d, 0xd1, 0xff, 0x7e, 0xd1, 0xff, 0x7f, 0xd0, 0x80, 0xd0, 0x81, 0xd0, 0xde, 0xd0, 0xdf, 0xe0, 0xe1, 0x00, 0x01, 0x7e, 0x7f, 0xd1, 0x00, 0x80, 0xd1, 0x00, 0x81, 0xd1, 0x00, 0x82, 0xd1, 0x7f, 0xfd, 0xd1, 0x7f, 0xfe, 0xd1, 0x7f, 0xff, 0xd2, 0x00, 0x00, 0x80, 0x00, 0xd2, 0x00, 0x00, 0x80, 0x01, 0xd2, 0x00, 0x00, 0x80, 0x02, 0xd2, 0x7f, 0xff, 0xff, 0xfd, 0xd2, 0x7f, 0xff, 0xff, 0xfe, 0xd2, 0x7f, 0xff, 0xff, 0xff}) != 0 {
t.Error("wrong output", b.Bytes())
}
}
func TestPackInt64(t *testing.T) {
b := &bytes.Buffer{}
for _, i := range []int64 { -9223372036854775808, -9223372036854775807, -9223372036854775806, -2147483651, -2147483650, -2147483649, -2147483648, -2147483647, -2147483646, -32771, -32770, -32769, -32768, -32767, -131, -130, -129, -128, -127, -34, -33, -32, -31, 0, 1, 126, 127, 128, 129, 130, 32765, 32766, 32767, 32768, 32769, 32770, 2147483645, 2147483646, 2147483647, 2147483648, 2147483649, 2147483650, 4294967296, 4294967297, 4294967298 } {
_, err := PackInt64(b, i)
if err != nil { t.Error("err != nil") }
}
if bytes.Compare(b.Bytes(), []byte { 0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xd3, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xfd, 0xd3, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xfe, 0xd3, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0xd2, 0x80, 0x00, 0x00, 0x00, 0xd2, 0x80, 0x00, 0x00, 0x01, 0xd2, 0x80, 0x00, 0x00, 0x02, 0xd2, 0xff, 0xff, 0x7f, 0xfd, 0xd2, 0xff, 0xff, 0x7f, 0xfe, 0xd2, 0xff, 0xff, 0x7f, 0xff, 0xd1, 0x80, 0x00, 0xd1, 0x80, 0x01, 0xd1, 0xff, 0x7d, 0xd1, 0xff, 0x7e, 0xd1, 0xff, 0x7f, 0xd0, 0x80, 0xd0, 0x81, 0xd0, 0xde, 0xd0, 0xdf, 0xe0, 0xe1, 0x00, 0x01, 0x7e, 0x7f, 0xd1, 0x00, 0x80, 0xd1, 0x00, 0x81, 0xd1, 0x00, 0x82, 0xd1, 0x7f, 0xfd, 0xd1, 0x7f, 0xfe, 0xd1, 0x7f, 0xff, 0xd2, 0x00, 0x00, 0x80, 0x00, 0xd2, 0x00, 0x00, 0x80, 0x01, 0xd2, 0x00, 0x00, 0x80, 0x02, 0xd2, 0x7f, 0xff, 0xff, 0xfd, 0xd2, 0x7f, 0xff, 0xff, 0xfe, 0xd2, 0x7f, 0xff, 0xff, 0xff, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x01, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x02, 0xd3, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0xd3, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02 }) != 0 { t.Error("wrong output", b.Bytes()) }
b := &bytes.Buffer{}
for _, i := range []int64{-9223372036854775808, -9223372036854775807, -9223372036854775806, -2147483651, -2147483650, -2147483649, -2147483648, -2147483647, -2147483646, -32771, -32770, -32769, -32768, -32767, -131, -130, -129, -128, -127, -34, -33, -32, -31, 0, 1, 126, 127, 128, 129, 130, 32765, 32766, 32767, 32768, 32769, 32770, 2147483645, 2147483646, 2147483647, 2147483648, 2147483649, 2147483650, 4294967296, 4294967297, 4294967298} {
_, err := PackInt64(b, i)
if err != nil {
t.Error("err != nil")
}
}
if bytes.Compare(b.Bytes(), []byte{0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xd3, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xfd, 0xd3, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xfe, 0xd3, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0xd2, 0x80, 0x00, 0x00, 0x00, 0xd2, 0x80, 0x00, 0x00, 0x01, 0xd2, 0x80, 0x00, 0x00, 0x02, 0xd2, 0xff, 0xff, 0x7f, 0xfd, 0xd2, 0xff, 0xff, 0x7f, 0xfe, 0xd2, 0xff, 0xff, 0x7f, 0xff, 0xd1, 0x80, 0x00, 0xd1, 0x80, 0x01, 0xd1, 0xff, 0x7d, 0xd1, 0xff, 0x7e, 0xd1, 0xff, 0x7f, 0xd0, 0x80, 0xd0, 0x81, 0xd0, 0xde, 0xd0, 0xdf, 0xe0, 0xe1, 0x00, 0x01, 0x7e, 0x7f, 0xd1, 0x00, 0x80, 0xd1, 0x00, 0x81, 0xd1, 0x00, 0x82, 0xd1, 0x7f, 0xfd, 0xd1, 0x7f, 0xfe, 0xd1, 0x7f, 0xff, 0xd2, 0x00, 0x00, 0x80, 0x00, 0xd2, 0x00, 0x00, 0x80, 0x01, 0xd2, 0x00, 0x00, 0x80, 0x02, 0xd2, 0x7f, 0xff, 0xff, 0xfd, 0xd2, 0x7f, 0xff, 0xff, 0xfe, 0xd2, 0x7f, 0xff, 0xff, 0xff, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x01, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x02, 0xd3, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0xd3, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02}) != 0 {
t.Error("wrong output", b.Bytes())
}
}
func TestPackNil(t *testing.T) {
b := &bytes.Buffer{}
nbyteswrite, err := PackNil(b)
if nbyteswrite != 1 { t.Error("nbyteswrite != 1") }
if err != nil { t.Error("err != nil") }
if bytes.Compare(b.Bytes(), []byte { 0xc0 }) != 0 { t.Error("wrong output", b.Bytes()) }
b := &bytes.Buffer{}
nbyteswrite, err := PackNil(b)
if nbyteswrite != 1 {
t.Error("nbyteswrite != 1")
}
if err != nil {
t.Error("err != nil")
}
if bytes.Compare(b.Bytes(), []byte{0xc0}) != 0 {
t.Error("wrong output", b.Bytes())
}
}
func TestPackBool(t *testing.T) {
b := &bytes.Buffer{}
for _, i := range []bool { false, true } {
nbyteswrite, err := PackBool(b, i)
if nbyteswrite != 1 { t.Error("nbyteswrite != 1") }
if err != nil { t.Error("err != nil") }
}
if bytes.Compare(b.Bytes(), []byte { 0xc2, 0xc3 }) != 0 { t.Error("wrong output", b.Bytes()) }
b := &bytes.Buffer{}
for _, i := range []bool{false, true} {
nbyteswrite, err := PackBool(b, i)
if nbyteswrite != 1 {
t.Error("nbyteswrite != 1")
}
if err != nil {
t.Error("err != nil")
}
}
if bytes.Compare(b.Bytes(), []byte{0xc2, 0xc3}) != 0 {
t.Error("wrong output", b.Bytes())
}
}
func TestPackInt32Array(t *testing.T) {
b := &bytes.Buffer{}
_, err := PackInt32Array(b, []int32 {})
if err != nil { t.Error("err != nil") }
_, err = PackInt32Array(b, []int32 { 0 })
if err != nil { t.Error("err != nil") }
_, err = PackInt32Array(b, []int32 { 0, 1 })
if err != nil { t.Error("err != nil") }
_, err = PackInt32Array(b, []int32 { 0, 1, 2 })
if err != nil { t.Error("err != nil") }
if bytes.Compare(b.Bytes(), []byte { 0x90, 0x91, 0x00, 0x92, 0x00, 0x01, 0x93, 0x00, 0x01, 0x02 }) != 0 { t.Error("wrong output", b.Bytes()) }
b := &bytes.Buffer{}
_, err := PackInt32Array(b, []int32{})
if err != nil {
t.Error("err != nil")
}
_, err = PackInt32Array(b, []int32{0})
if err != nil {
t.Error("err != nil")
}
_, err = PackInt32Array(b, []int32{0, 1})
if err != nil {
t.Error("err != nil")
}
_, err = PackInt32Array(b, []int32{0, 1, 2})
if err != nil {
t.Error("err != nil")
}
if bytes.Compare(b.Bytes(), []byte{0x90, 0x91, 0x00, 0x92, 0x00, 0x01, 0x93, 0x00, 0x01, 0x02}) != 0 {
t.Error("wrong output", b.Bytes())
}
}
func TestPackArray(t *testing.T) {
b := &bytes.Buffer{}
_, err := PackArray(b, reflect.NewValue([]int32 {}).(reflect.ArrayOrSliceValue))
if err != nil { t.Error("err != nil") }
_, err = PackArray(b, reflect.NewValue([]int32 { 0 }).(reflect.ArrayOrSliceValue))
if err != nil { t.Error("err != nil") }
_, err = PackArray(b, reflect.NewValue([]int32 { 0, 1 }).(reflect.ArrayOrSliceValue))
if err != nil { t.Error("err != nil") }
_, err = PackArray(b, reflect.NewValue([]int32 { 0, 1, 2 }).(reflect.ArrayOrSliceValue))
if err != nil { t.Error("err != nil") }
if bytes.Compare(b.Bytes(), []byte { 0x90, 0x91, 0x00, 0x92, 0x00, 0x01, 0x93, 0x00, 0x01, 0x02 }) != 0 { t.Error("wrong output", b.Bytes()) }
b := &bytes.Buffer{}
_, err := PackArray(b, reflect.ValueOf([]int32{}))
if err != nil {
t.Error("err != nil")
}
_, err = PackArray(b, reflect.ValueOf([]int32{0}))
if err != nil {
t.Error("err != nil")
}
_, err = PackArray(b, reflect.ValueOf([]int32{0, 1}))
if err != nil {
t.Error("err != nil")
}
_, err = PackArray(b, reflect.ValueOf([]int32{0, 1, 2}))
if err != nil {
t.Error("err != nil")
}
if bytes.Compare(b.Bytes(), []byte{0x90, 0x91, 0x00, 0x92, 0x00, 0x01, 0x93, 0x00, 0x01, 0x02}) != 0 {
t.Error("wrong output", b.Bytes())
}
}
func TestPackMap(t *testing.T) {
b := &bytes.Buffer{}
_, err := PackMap(b, reflect.NewValue(map[int] int { 0: 1, 2: 3, 4: 5 }).(*reflect.MapValue))
if err != nil { t.Error("err != nil") }
if bytes.Compare(b.Bytes(), []byte { 0x83, 0x00, 0x01, 0x04, 0x05, 0x02, 0x03 }) != 0 { t.Error("wrong output", b.Bytes()) }
b := &bytes.Buffer{}
_, err := PackMap(b, reflect.ValueOf(map[int]int{0: 1, 2: 3, 4: 5}))
if err != nil {
t.Error("err != nil")
}
if bytes.Compare(b.Bytes(), []byte{0x83, 0x00, 0x01, 0x04, 0x05, 0x02, 0x03}) != 0 {
t.Error("wrong output", b.Bytes())
}
}
func TestPack(t *testing.T) {
b := &bytes.Buffer{}
for _, i := range [](interface{}) { nil, false, true, 0, 1, 2, 3, 127, -32, -1, -33, 128 } {
_, err := Pack(b, i)
if err != nil { t.Error("err != nil") }
}
if bytes.Compare(b.Bytes(), []byte { 0xc0, 0xc2, 0xc3, 0x00, 0x01, 0x02, 0x03, 0x7f, 0xf0, 0xff, 0xd0, 0xef, 0xd1, 0x00, 0x80 }) == 0 { t.Error("wrong output") }
b := &bytes.Buffer{}
for _, i := range [](interface{}){nil, false, true, 0, 1, 2, 3, 127, -32, -1, -33, 128} {
_, err := Pack(b, i)
if err != nil {
t.Error("err != nil")
}
}
if bytes.Compare(b.Bytes(), []byte{0xc0, 0xc2, 0xc3, 0x00, 0x01, 0x02, 0x03, 0x7f, 0xf0, 0xff, 0xd0, 0xef, 0xd1, 0x00, 0x80}) == 0 {
t.Error("wrong output")
}
}
func TestUnpackArray(t *testing.T) {
b := bytes.NewBuffer([]byte { 0x90, 0x91, 0x00, 0x92, 0x00, 0x01, 0x93, 0xd1, 0x00, 0x00, 0xd2, 0x00, 0x00, 0x00, 0x01, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xdc, 0x00, 0x02, 0x00, 0x01, 0xdd, 0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03 })
for _, v := range [](interface{}) { [](interface{}) {}, [](interface{}) { int8(0) }, [](interface{}) { int8(0), int8(1) }, [](interface{}) { int16(0), int32(1), int64(2) }, [](interface{}){ int8(0), int8(1) }, [](interface{}) { int8(0), int8(1), int8(2), int8(3) } } {
retval, _, e := Unpack(b)
if e != nil { t.Error("err != nil") }
if !equal(reflect.NewValue(retval.Interface()), reflect.NewValue(v)) { t.Errorf("%s != %s", retval.Interface(), v) }
}
b := bytes.NewBuffer([]byte{0x90, 0x91, 0x00, 0x92, 0x00, 0x01, 0x93, 0xd1, 0x00, 0x00, 0xd2, 0x00, 0x00, 0x00, 0x01, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xdc, 0x00, 0x02, 0x00, 0x01, 0xdd, 0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03})
for _, v := range [](interface{}){[](interface{}){}, [](interface{}){int8(0)}, [](interface{}){int8(0), int8(1)}, [](interface{}){int16(0), int32(1), int64(2)}, [](interface{}){int8(0), int8(1)}, [](interface{}){int8(0), int8(1), int8(2), int8(3)}} {
retval, _, e := Unpack(b)
if e != nil {
t.Error("err != nil")
}
if !equal(reflect.ValueOf(retval.Interface()), reflect.ValueOf(v)) {
t.Errorf("%s != %s", retval.Interface(), v)
}
}
}
func TestUnpackInt(t *testing.T) {
b := bytes.NewBuffer([]byte { 0xff, 0xe0, 0x00, 0x01, 0x02, 0x7d, 0x7e, 0x7f, 0xd0, 0x01, 0xd0, 0x80, 0xd0, 0xff, 0xcc, 0x80, 0xcc, 0xfd, 0xcc, 0xfe, 0xcc, 0xff, 0xd1, 0x00, 0x00, 0xd1, 0x7f, 0xff, 0xd1, 0xff, 0xff, 0xcd, 0x80, 0x00, 0xcd, 0xff, 0xff, 0xd2, 0x7f, 0xff, 0xff, 0xff, 0xce, 0x7f, 0xff, 0xff, 0xff, 0xd3, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff })
for _, v := range [](interface{}) {
int8(-1), int8(-32), int8(0), int8(1), int8(2), int8(125), int8(126), int8(127), int8(1), int8(-128), int8(-1), uint8(128), uint8(253), uint8(254), uint8(255), int16(0), int16(32767), int16(-1), uint16(32768), uint16(65535), int32(2147483647), uint32(2147483647), int64(9223372036854775807), uint64(18446744073709551615) } {
retval, _, e := Unpack(b)
if e != nil { t.Error("err != nil") }
if retval.Interface() != v { t.Errorf("%u != %u", retval.Interface(), v) }
}
b := bytes.NewBuffer([]byte{0xff, 0xe0, 0x00, 0x01, 0x02, 0x7d, 0x7e, 0x7f, 0xd0, 0x01, 0xd0, 0x80, 0xd0, 0xff, 0xcc, 0x80, 0xcc, 0xfd, 0xcc, 0xfe, 0xcc, 0xff, 0xd1, 0x00, 0x00, 0xd1, 0x7f, 0xff, 0xd1, 0xff, 0xff, 0xcd, 0x80, 0x00, 0xcd, 0xff, 0xff, 0xd2, 0x7f, 0xff, 0xff, 0xff, 0xce, 0x7f, 0xff, 0xff, 0xff, 0xd3, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff})
for _, v := range [](interface{}){
int8(-1), int8(-32), int8(0), int8(1), int8(2), int8(125), int8(126), int8(127), int8(1), int8(-128), int8(-1), uint8(128), uint8(253), uint8(254), uint8(255), int16(0), int16(32767), int16(-1), uint16(32768), uint16(65535), int32(2147483647), uint32(2147483647), int64(9223372036854775807), uint64(18446744073709551615)} {
retval, _, e := Unpack(b)
if e != nil {
t.Error("err != nil")
}
if retval.Interface() != v {
t.Errorf("%u != %u", retval.Interface(), v)
}
}
}
func TestUnpackFloat(t *testing.T) {
b := bytes.NewBuffer([]byte { 0xca, 0x3d, 0xcc, 0xcc, 0xcd, 0xca, 0x3e, 0x4c, 0xcc, 0xcd, 0xca, 0xbd, 0xcc, 0xcc, 0xcd, 0xca, 0xbe, 0x4c, 0xcc, 0xcd, 0xca, 0x7f, 0x80, 0x00, 0x00, 0xca, 0xff, 0x80, 0x00, 0x00, 0xca, 0xff, 0xc0, 0x00, 0x0, 0xcb, 0x3f, 0xb9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xcb, 0x3f, 0xc9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xcb, 0xbf, 0xb9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xcb, 0xbf, 0xc9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xcb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcb, 0xcb, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcb, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })
for _, v := range [](interface{}) { float32(.1), float32(.2), float32(-.1), float32(-.2), float32(math.Inf(1)), float32(math.Inf(-1)), float32(math.NaN()), float64(.1), float64(.2), float64(-.1), float64(-.2) } {
retval, _, e := Unpack(b)
if e != nil { t.Error("err != nil") }
isnan := false
if _v, ok := v.(float64); ok {
isnan = math.IsNaN(_v)
} else if _v, ok := v.(float32); ok {
isnan = math.IsNaN(float64(_v))
}
if isnan {
if retval.Interface() == v { t.Errorf("[NaN] %u == %u", retval.Interface(), v) }
} else {
if retval.Interface() != v { t.Errorf("%u != %u", retval.Interface(), v) }
}
}
b := bytes.NewBuffer([]byte{0xca, 0x3d, 0xcc, 0xcc, 0xcd, 0xca, 0x3e, 0x4c, 0xcc, 0xcd, 0xca, 0xbd, 0xcc, 0xcc, 0xcd, 0xca, 0xbe, 0x4c, 0xcc, 0xcd, 0xca, 0x7f, 0x80, 0x00, 0x00, 0xca, 0xff, 0x80, 0x00, 0x00, 0xca, 0xff, 0xc0, 0x00, 0x0, 0xcb, 0x3f, 0xb9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xcb, 0x3f, 0xc9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xcb, 0xbf, 0xb9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xcb, 0xbf, 0xc9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xcb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcb, 0xcb, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcb, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
for _, v := range [](interface{}){float32(.1), float32(.2), float32(-.1), float32(-.2), float32(math.Inf(1)), float32(math.Inf(-1)), float32(math.NaN()), float64(.1), float64(.2), float64(-.1), float64(-.2)} {
retval, _, e := Unpack(b)
if e != nil {
t.Error("err != nil")
}
isnan := false
if _v, ok := v.(float64); ok {
isnan = math.IsNaN(_v)
} else if _v, ok := v.(float32); ok {
isnan = math.IsNaN(float64(_v))
}
if isnan {
if retval.Interface() == v {
t.Errorf("[NaN] %u == %u", retval.Interface(), v)
}
} else {
if retval.Interface() != v {
t.Errorf("%u != %u", retval.Interface(), v)
}
}
}
}

1121
go/pack.go

File diff suppressed because it is too large Load Diff

View File

@ -1,288 +1,366 @@
package msgpack
import (
"io"
"os"
"unsafe"
"strconv"
"reflect"
"io"
"os"
"unsafe"
"strconv"
"reflect"
)
func readByte(reader io.Reader) (v uint8, err os.Error) {
data := [1]byte{}
_, e := reader.Read(data[0:])
if e != nil { return 0, e }
return data[0], nil
data := [1]byte{}
_, e := reader.Read(data[0:])
if e != nil {
return 0, e
}
return data[0], nil
}
func readUint16(reader io.Reader) (v uint16, n int, err os.Error) {
data := [2]byte{}
n, e := reader.Read(data[0:])
if e != nil { return 0, n, e }
return (uint16(data[0]) << 8) | uint16(data[1]), n, nil
data := [2]byte{}
n, e := reader.Read(data[0:])
if e != nil {
return 0, n, e
}
return (uint16(data[0]) << 8) | uint16(data[1]), n, nil
}
func readUint32(reader io.Reader) (v uint32, n int, err os.Error) {
data := [4]byte{}
n, e := reader.Read(data[0:])
if e != nil { return 0, n, e }
return (uint32(data[0]) << 24) | (uint32(data[1]) << 16) | (uint32(data[2]) << 8) | uint32(data[3]), n, nil
data := [4]byte{}
n, e := reader.Read(data[0:])
if e != nil {
return 0, n, e
}
return (uint32(data[0]) << 24) | (uint32(data[1]) << 16) | (uint32(data[2]) << 8) | uint32(data[3]), n, nil
}
func readUint64(reader io.Reader) (v uint64, n int, err os.Error) {
data := [8]byte{}
n, e := reader.Read(data[0:])
if e != nil { return 0, n, e }
return (uint64(data[0]) << 56) | (uint64(data[1]) << 48) | (uint64(data[2]) << 40) | (uint64(data[3]) << 32) | (uint64(data[4]) << 24) | (uint64(data[5]) << 16) | (uint64(data[6]) << 8) | uint64(data[7]), n, nil
data := [8]byte{}
n, e := reader.Read(data[0:])
if e != nil {
return 0, n, e
}
return (uint64(data[0]) << 56) | (uint64(data[1]) << 48) | (uint64(data[2]) << 40) | (uint64(data[3]) << 32) | (uint64(data[4]) << 24) | (uint64(data[5]) << 16) | (uint64(data[6]) << 8) | uint64(data[7]), n, nil
}
func readInt16(reader io.Reader) (v int16, n int, err os.Error) {
data := [2]byte{}
n, e := reader.Read(data[0:])
if e != nil { return 0, n, e }
return (int16(data[0]) << 8) | int16(data[1]), n, nil
data := [2]byte{}
n, e := reader.Read(data[0:])
if e != nil {
return 0, n, e
}
return (int16(data[0]) << 8) | int16(data[1]), n, nil
}
func readInt32(reader io.Reader) (v int32, n int, err os.Error) {
data := [4]byte{}
n, e := reader.Read(data[0:])
if e != nil { return 0, n, e }
return (int32(data[0]) << 24) | (int32(data[1]) << 16) | (int32(data[2]) << 8) | int32(data[3]), n, nil
data := [4]byte{}
n, e := reader.Read(data[0:])
if e != nil {
return 0, n, e
}
return (int32(data[0]) << 24) | (int32(data[1]) << 16) | (int32(data[2]) << 8) | int32(data[3]), n, nil
}
func readInt64(reader io.Reader) (v int64, n int, err os.Error) {
data := [8]byte{}
n, e := reader.Read(data[0:])
if e != nil { return 0, n, e }
return (int64(data[0]) << 56) | (int64(data[1]) << 48) | (int64(data[2]) << 40) | (int64(data[3]) << 32) | (int64(data[4]) << 24) | (int64(data[5]) << 16) | (int64(data[6]) << 8) | int64(data[7]), n, nil
data := [8]byte{}
n, e := reader.Read(data[0:])
if e != nil {
return 0, n, e
}
return (int64(data[0]) << 56) | (int64(data[1]) << 48) | (int64(data[2]) << 40) | (int64(data[3]) << 32) | (int64(data[4]) << 24) | (int64(data[5]) << 16) | (int64(data[6]) << 8) | int64(data[7]), n, nil
}
func unpackArray(reader io.Reader, nelems uint) (v reflect.Value, n int, err os.Error) {
retval := make([]interface{}, nelems)
nbytesread := 0
var i uint
for i = 0; i < nelems; i++ {
v, n, e := Unpack(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
retval[i] = v.Interface()
}
return reflect.NewValue(retval), nbytesread, nil
retval := make([]interface{}, nelems)
nbytesread := 0
var i uint
for i = 0; i < nelems; i++ {
v, n, e := Unpack(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval[i] = v.Interface()
}
return reflect.ValueOf(retval), nbytesread, nil
}
func unpackArrayReflected(reader io.Reader, nelems uint) (v reflect.Value, n int, err os.Error) {
retval := make([]reflect.Value, nelems)
nbytesread := 0
var i uint
for i = 0; i < nelems; i++ {
v, n, e := UnpackReflected(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
retval[i] = v
}
return reflect.NewValue(retval), nbytesread, nil
retval := make([]reflect.Value, nelems)
nbytesread := 0
var i uint
for i = 0; i < nelems; i++ {
v, n, e := UnpackReflected(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval[i] = v
}
return reflect.ValueOf(retval), nbytesread, nil
}
func unpackMap(reader io.Reader, nelems uint) (v reflect.Value, n int, err os.Error) {
retval := make(map [interface{}] interface{})
nbytesread := 0
var i uint
for i = 0; i < nelems; i++ {
k, n, e := Unpack(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
v, n, e := Unpack(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
retval[k.Interface()] = v.Interface()
}
return reflect.NewValue(retval), nbytesread, nil
retval := make(map[interface{}]interface{})
nbytesread := 0
var i uint
for i = 0; i < nelems; i++ {
k, n, e := Unpack(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
v, n, e := Unpack(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval[k.Interface()] = v.Interface()
}
return reflect.ValueOf(retval), nbytesread, nil
}
func unpackMapReflected(reader io.Reader, nelems uint) (v reflect.Value, n int, err os.Error) {
retval := make(map [reflect.Value] reflect.Value)
nbytesread := 0
var i uint
for i = 0; i < nelems; i++ {
k, n, e := UnpackReflected(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
v, n, e := UnpackReflected(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
retval[k] = v
}
return reflect.NewValue(retval), nbytesread, nil
retval := make(map[interface{}]reflect.Value)
nbytesread := 0
var i uint
for i = 0; i < nelems; i++ {
k, n, e := UnpackReflected(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
v, n, e := UnpackReflected(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval[k] = v
}
return reflect.ValueOf(retval), nbytesread, nil
}
func unpack(reader io.Reader, reflected bool) (v reflect.Value, n int, err os.Error) {
var retval reflect.Value
var nbytesread int = 0
var retval reflect.Value
var nbytesread int = 0
c, e := readByte(reader)
if e != nil { return nil, 0, e }
nbytesread += 1
if c < 0x80 || c >= 0xe0 {
retval = reflect.NewValue(int8(c))
} else if c >= 0x80 && c <= 0x8f {
if reflected {
retval, n, e = unpackMapReflected(reader, uint(c & 0xf))
} else {
retval, n, e = unpackMap(reader, uint(c & 0xf))
}
nbytesread += n
if e != nil { return nil, nbytesread, e }
nbytesread += n
} else if c >= 0x90 && c <= 0x9f {
if reflected {
retval, n, e = unpackArrayReflected(reader, uint(c & 0xf))
} else {
retval, n, e = unpackArray(reader, uint(c & 0xf))
}
nbytesread += n
if e != nil { return nil, nbytesread, e }
nbytesread += n
} else if c >= 0xa0 && c <= 0xbf {
data := make([]byte, c & 0xf);
n, e := reader.Read(data)
nbytesread += n
if e != nil { return nil, nbytesread, e }
retval = reflect.NewValue(data)
} else {
switch c {
case 0xc0: retval = reflect.NewValue(nil)
case 0xc2: retval = reflect.NewValue(false)
case 0xc3: retval = reflect.NewValue(true)
case 0xca:
data, n, e := readUint32(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
retval = reflect.NewValue(*(*float32)(unsafe.Pointer(&data)))
case 0xcb:
data, n, e := readUint64(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
retval = reflect.NewValue(*(*float64)(unsafe.Pointer(&data)))
case 0xcc:
data, e := readByte(reader)
if e != nil { return nil, nbytesread, e }
retval = reflect.NewValue(uint8(data))
nbytesread += 1
case 0xcd:
data, n, e := readUint16(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
retval = reflect.NewValue(data)
case 0xce:
data, n, e := readUint32(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
retval = reflect.NewValue(data)
case 0xcf:
data, n, e := readUint64(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
retval = reflect.NewValue(data)
case 0xd0:
data, e := readByte(reader)
if e != nil { return nil, nbytesread, e }
retval = reflect.NewValue(int8(data))
nbytesread += 1
case 0xd1:
data, n, e := readInt16(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
retval = reflect.NewValue(data)
case 0xd2:
data, n, e := readInt32(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
retval = reflect.NewValue(data)
case 0xd3:
data, n, e := readInt64(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
retval = reflect.NewValue(data)
case 0xda:
nbytestoread, n, e := readUint16(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
data := make([]byte, nbytestoread)
n, e = reader.Read(data)
nbytesread += n
if e != nil { return nil, nbytesread, e }
retval = reflect.NewValue(data)
case 0xdb:
nbytestoread, n, e := readUint32(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
data := make([]byte, nbytestoread)
n, e = reader.Read(data)
nbytesread += n
if e != nil { return nil, nbytesread, e }
retval = reflect.NewValue(data)
case 0xdc:
nelemstoread, n, e := readUint16(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
if reflected {
retval, n, e = unpackArrayReflected(reader, uint(nelemstoread))
} else {
retval, n, e = unpackArray(reader, uint(nelemstoread))
}
nbytesread += n
if e != nil { return nil, nbytesread, e }
case 0xdd:
nelemstoread, n, e := readUint32(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
if reflected {
retval, n, e = unpackArrayReflected(reader, uint(nelemstoread))
} else {
retval, n, e = unpackArray(reader, uint(nelemstoread))
}
nbytesread += n
if e != nil { return nil, nbytesread, e }
case 0xde:
nelemstoread, n, e := readUint16(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
if reflected {
retval, n, e = unpackMapReflected(reader, uint(nelemstoread))
} else {
retval, n, e = unpackMap(reader, uint(nelemstoread))
}
nbytesread += n
if e != nil { return nil, nbytesread, e }
case 0xdf:
nelemstoread, n, e := readUint32(reader)
nbytesread += n
if e != nil { return nil, nbytesread, e }
if reflected {
retval, n, e = unpackMapReflected(reader, uint(nelemstoread))
} else {
retval, n, e = unpackMap(reader, uint(nelemstoread))
}
nbytesread += n
if e != nil { return nil, nbytesread, e }
default:
panic("unsupported code: " + strconv.Itoa(int(c)))
}
}
return retval, nbytesread, nil
c, e := readByte(reader)
if e != nil {
return reflect.Value{}, 0, e
}
nbytesread += 1
if c < 0x80 || c >= 0xe0 {
retval = reflect.ValueOf(int8(c))
} else if c >= 0x80 && c <= 0x8f {
if reflected {
retval, n, e = unpackMapReflected(reader, uint(c&0xf))
} else {
retval, n, e = unpackMap(reader, uint(c&0xf))
}
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
nbytesread += n
} else if c >= 0x90 && c <= 0x9f {
if reflected {
retval, n, e = unpackArrayReflected(reader, uint(c&0xf))
} else {
retval, n, e = unpackArray(reader, uint(c&0xf))
}
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
nbytesread += n
} else if c >= 0xa0 && c <= 0xbf {
data := make([]byte, c&0xf)
n, e := reader.Read(data)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
} else {
switch c {
case 0xc0:
retval = reflect.ValueOf(nil)
case 0xc2:
retval = reflect.ValueOf(false)
case 0xc3:
retval = reflect.ValueOf(true)
case 0xca:
data, n, e := readUint32(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(*(*float32)(unsafe.Pointer(&data)))
case 0xcb:
data, n, e := readUint64(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(*(*float64)(unsafe.Pointer(&data)))
case 0xcc:
data, e := readByte(reader)
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(uint8(data))
nbytesread += 1
case 0xcd:
data, n, e := readUint16(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
case 0xce:
data, n, e := readUint32(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
case 0xcf:
data, n, e := readUint64(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
case 0xd0:
data, e := readByte(reader)
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(int8(data))
nbytesread += 1
case 0xd1:
data, n, e := readInt16(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
case 0xd2:
data, n, e := readInt32(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
case 0xd3:
data, n, e := readInt64(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
case 0xda:
nbytestoread, n, e := readUint16(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
data := make([]byte, nbytestoread)
n, e = reader.Read(data)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
case 0xdb:
nbytestoread, n, e := readUint32(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
data := make([]byte, nbytestoread)
n, e = reader.Read(data)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
retval = reflect.ValueOf(data)
case 0xdc:
nelemstoread, n, e := readUint16(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
if reflected {
retval, n, e = unpackArrayReflected(reader, uint(nelemstoread))
} else {
retval, n, e = unpackArray(reader, uint(nelemstoread))
}
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
case 0xdd:
nelemstoread, n, e := readUint32(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
if reflected {
retval, n, e = unpackArrayReflected(reader, uint(nelemstoread))
} else {
retval, n, e = unpackArray(reader, uint(nelemstoread))
}
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
case 0xde:
nelemstoread, n, e := readUint16(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
if reflected {
retval, n, e = unpackMapReflected(reader, uint(nelemstoread))
} else {
retval, n, e = unpackMap(reader, uint(nelemstoread))
}
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
case 0xdf:
nelemstoread, n, e := readUint32(reader)
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
if reflected {
retval, n, e = unpackMapReflected(reader, uint(nelemstoread))
} else {
retval, n, e = unpackMap(reader, uint(nelemstoread))
}
nbytesread += n
if e != nil {
return reflect.Value{}, nbytesread, e
}
default:
panic("unsupported code: " + strconv.Itoa(int(c)))
}
}
return retval, nbytesread, nil
}
// Reads a value from the reader, unpack and returns it.
func Unpack(reader io.Reader) (v reflect.Value, n int, err os.Error) {
return unpack(reader, false)
return unpack(reader, false)
}
// Reads unpack a value from the reader, unpack and returns it. When the
// value is an array or map, leaves the elements wrapped by corresponding
// wrapper objects defined in reflect package.
func UnpackReflected(reader io.Reader) (v reflect.Value, n int, err os.Error) {
return unpack(reader, true)
return unpack(reader, true)
}