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 package msgpack_test
import ( import (
. "msgpack" . "msgpack"
"testing" "testing"
"bytes" "bytes"
"reflect" "reflect"
"math" "math"
); )
func equal(lhs reflect.Value, rhs reflect.Value) bool { func equal(lhs reflect.Value, rhs reflect.Value) bool {
{ {
_rhs, ok := rhs.(*reflect.InterfaceValue) _rhs := rhs
if ok { return equal(lhs, _rhs.Elem()) } if _rhs.Kind() == reflect.Interface {
} return equal(lhs, _rhs.Elem())
switch _lhs := lhs.(type) { }
case *reflect.InterfaceValue: }
return equal(_lhs.Elem(), rhs) switch _lhs := lhs; _lhs.Kind() {
case *reflect.BoolValue: case reflect.Interface:
_rhs, ok := rhs.(*reflect.BoolValue) return equal(_lhs.Elem(), rhs)
return ok && _lhs.Get() == _rhs.Get() case reflect.Bool:
case *reflect.UintValue: _rhs := rhs
_rhs, ok := rhs.(*reflect.UintValue) return _rhs.Kind() == reflect.Bool &&
return ok && _lhs.Get() == _rhs.Get() _lhs.Bool() == _rhs.Bool()
case *reflect.IntValue: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
_rhs, ok := rhs.(*reflect.IntValue) _rhs := rhs
return ok && _lhs.Get() == _rhs.Get() 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) &&
case *reflect.FloatValue: _lhs.Uint() == _rhs.Uint()
_rhs, ok := rhs.(*reflect.FloatValue) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return ok && _lhs.Get() == _rhs.Get() _rhs := rhs
case reflect.ArrayOrSliceValue: return (_rhs.Kind() == reflect.Int || _rhs.Kind() == reflect.Int8 || _rhs.Kind() == reflect.Int16 || _rhs.Kind() == reflect.Int32 || _rhs.Kind() == reflect.Int64) &&
_rhs := rhs.(reflect.ArrayOrSliceValue) _lhs.Int() == _rhs.Int()
if _lhs.Len() != _rhs.Len() { return false; } case reflect.Float32, reflect.Float64:
for i := 0; i < _lhs.Len(); i++ { _rhs := rhs
if !equal(_lhs.Elem(i), _rhs.Elem(i)) { return false; } return (_rhs.Kind() == reflect.Float32 || _rhs.Kind() == reflect.Float64) &&
} _lhs.Float() == _rhs.Float()
return true; case reflect.Array, reflect.Slice:
case *reflect.MapValue: _rhs := rhs
_rhs := rhs.(*reflect.MapValue) if _lhs.Len() != _rhs.Len() {
if _lhs.Len() != _rhs.Len() { return false; } return false
for _, k := range _lhs.Keys() { }
lv, rv := _lhs.Elem(k), _rhs.Elem(k) for i := 0; i < _lhs.Len(); i++ {
if lv == nil || rv == nil || !equal(lv, rv) { return false; } if !equal(_lhs.Index(i), _rhs.Index(i)) {
} return false
return true; }
} }
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) { func TestPackUint8(t *testing.T) {
b := &bytes.Buffer{} b := &bytes.Buffer{}
for _, i := range []uint8 { 0, 1, 2, 125, 126, 127, 128, 253, 254, 255 } { for _, i := range []uint8{0, 1, 2, 125, 126, 127, 128, 253, 254, 255} {
_, err := PackUint8(b, i) _, err := PackUint8(b, i)
if err != nil { t.Error("err != nil") } 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()) } }
}
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) { func TestPackUint16(t *testing.T) {
b := &bytes.Buffer{} b := &bytes.Buffer{}
for _, i := range []uint16 { 0, 1, 2, 125, 126, 127, 128, 253, 254, 255, 256, 65533, 65534, 65535 } { for _, i := range []uint16{0, 1, 2, 125, 126, 127, 128, 253, 254, 255, 256, 65533, 65534, 65535} {
_, err := PackUint16(b, i) _, err := PackUint16(b, i)
if err != nil { t.Error("err != nil") } 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()) } }
}
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) { func TestPackUint32(t *testing.T) {
b := &bytes.Buffer{} 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 } { 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) _, err := PackUint32(b, i)
if err != nil { t.Error("err != nil") } 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()) } }
}
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) { func TestPackUint64(t *testing.T) {
b := &bytes.Buffer{} 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 } { 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) _, err := PackUint64(b, i)
if err != nil { t.Error("err != nil") } 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()) } }
}
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) { func TestPackInt8(t *testing.T) {
b := &bytes.Buffer{} b := &bytes.Buffer{}
for _, i := range []int8 { -128, -127, -34, -33, -32, -31, 0, 1, 126, 127 } { for _, i := range []int8{-128, -127, -34, -33, -32, -31, 0, 1, 126, 127} {
_, err := PackInt8(b, i) _, err := PackInt8(b, i)
if err != nil { t.Error("err != nil") } 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()) } }
}
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) { func TestPackInt16(t *testing.T) {
b := &bytes.Buffer{} 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 } { 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) _, err := PackInt16(b, i)
if err != nil { t.Error("err != nil") } 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()) } }
}
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) { func TestPackInt32(t *testing.T) {
b := &bytes.Buffer{} 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 } { 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) _, err := PackInt32(b, i)
if err != nil { t.Error("err != nil") } 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()) } }
}
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) { func TestPackInt64(t *testing.T) {
b := &bytes.Buffer{} 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 } { 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) _, err := PackInt64(b, i)
if err != nil { t.Error("err != nil") } 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()) } }
}
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) { func TestPackNil(t *testing.T) {
b := &bytes.Buffer{} b := &bytes.Buffer{}
nbyteswrite, err := PackNil(b) nbyteswrite, err := PackNil(b)
if nbyteswrite != 1 { t.Error("nbyteswrite != 1") } if nbyteswrite != 1 {
if err != nil { t.Error("err != nil") } t.Error("nbyteswrite != 1")
if bytes.Compare(b.Bytes(), []byte { 0xc0 }) != 0 { t.Error("wrong output", b.Bytes()) } }
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) { func TestPackBool(t *testing.T) {
b := &bytes.Buffer{} b := &bytes.Buffer{}
for _, i := range []bool { false, true } { for _, i := range []bool{false, true} {
nbyteswrite, err := PackBool(b, i) nbyteswrite, err := PackBool(b, i)
if nbyteswrite != 1 { t.Error("nbyteswrite != 1") } if nbyteswrite != 1 {
if err != nil { t.Error("err != nil") } t.Error("nbyteswrite != 1")
} }
if bytes.Compare(b.Bytes(), []byte { 0xc2, 0xc3 }) != 0 { t.Error("wrong output", b.Bytes()) } 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) { func TestPackInt32Array(t *testing.T) {
b := &bytes.Buffer{} b := &bytes.Buffer{}
_, err := PackInt32Array(b, []int32 {}) _, err := PackInt32Array(b, []int32{})
if err != nil { t.Error("err != nil") } if err != nil {
_, err = PackInt32Array(b, []int32 { 0 }) t.Error("err != nil")
if err != nil { t.Error("err != nil") } }
_, err = PackInt32Array(b, []int32 { 0, 1 }) _, err = PackInt32Array(b, []int32{0})
if err != nil { t.Error("err != nil") } if err != nil {
_, err = PackInt32Array(b, []int32 { 0, 1, 2 }) t.Error("err != nil")
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()) } _, 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) { func TestPackArray(t *testing.T) {
b := &bytes.Buffer{} b := &bytes.Buffer{}
_, err := PackArray(b, reflect.NewValue([]int32 {}).(reflect.ArrayOrSliceValue)) _, err := PackArray(b, reflect.ValueOf([]int32{}))
if err != nil { t.Error("err != nil") } if err != nil {
_, err = PackArray(b, reflect.NewValue([]int32 { 0 }).(reflect.ArrayOrSliceValue)) t.Error("err != nil")
if err != nil { t.Error("err != nil") } }
_, err = PackArray(b, reflect.NewValue([]int32 { 0, 1 }).(reflect.ArrayOrSliceValue)) _, err = PackArray(b, reflect.ValueOf([]int32{0}))
if err != nil { t.Error("err != nil") } if err != nil {
_, err = PackArray(b, reflect.NewValue([]int32 { 0, 1, 2 }).(reflect.ArrayOrSliceValue)) t.Error("err != nil")
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()) } _, 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) { func TestPackMap(t *testing.T) {
b := &bytes.Buffer{} b := &bytes.Buffer{}
_, err := PackMap(b, reflect.NewValue(map[int] int { 0: 1, 2: 3, 4: 5 }).(*reflect.MapValue)) _, err := PackMap(b, reflect.ValueOf(map[int]int{0: 1, 2: 3, 4: 5}))
if err != nil { t.Error("err != nil") } if err != nil {
if bytes.Compare(b.Bytes(), []byte { 0x83, 0x00, 0x01, 0x04, 0x05, 0x02, 0x03 }) != 0 { t.Error("wrong output", b.Bytes()) } 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) { func TestPack(t *testing.T) {
b := &bytes.Buffer{} b := &bytes.Buffer{}
for _, i := range [](interface{}) { nil, false, true, 0, 1, 2, 3, 127, -32, -1, -33, 128 } { for _, i := range [](interface{}){nil, false, true, 0, 1, 2, 3, 127, -32, -1, -33, 128} {
_, err := Pack(b, i) _, err := Pack(b, i)
if err != nil { t.Error("err != nil") } 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") } }
}
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) { 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 }) 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) } } { 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) retval, _, e := Unpack(b)
if e != nil { t.Error("err != nil") } if e != nil {
if !equal(reflect.NewValue(retval.Interface()), reflect.NewValue(v)) { t.Errorf("%s != %s", retval.Interface(), v) } 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) { 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 }) 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{}) { 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) } { 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) retval, _, e := Unpack(b)
if e != nil { t.Error("err != nil") } if e != nil {
if retval.Interface() != v { t.Errorf("%u != %u", retval.Interface(), v) } t.Error("err != nil")
} }
if retval.Interface() != v {
t.Errorf("%u != %u", retval.Interface(), v)
}
}
} }
func TestUnpackFloat(t *testing.T) { 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 }) 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) } { 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) retval, _, e := Unpack(b)
if e != nil { t.Error("err != nil") } if e != nil {
isnan := false t.Error("err != nil")
if _v, ok := v.(float64); ok { }
isnan = math.IsNaN(_v) isnan := false
} else if _v, ok := v.(float32); ok { if _v, ok := v.(float64); ok {
isnan = math.IsNaN(float64(_v)) isnan = math.IsNaN(_v)
} } else if _v, ok := v.(float32); ok {
if isnan { isnan = math.IsNaN(float64(_v))
if retval.Interface() == v { t.Errorf("[NaN] %u == %u", retval.Interface(), v) } }
} else { if isnan {
if retval.Interface() != v { t.Errorf("%u != %u", retval.Interface(), v) } 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 package msgpack
import ( import (
"io" "io"
"os" "os"
"unsafe" "unsafe"
"strconv" "strconv"
"reflect" "reflect"
) )
func readByte(reader io.Reader) (v uint8, err os.Error) { func readByte(reader io.Reader) (v uint8, err os.Error) {
data := [1]byte{} data := [1]byte{}
_, e := reader.Read(data[0:]) _, e := reader.Read(data[0:])
if e != nil { return 0, e } if e != nil {
return data[0], nil return 0, e
}
return data[0], nil
} }
func readUint16(reader io.Reader) (v uint16, n int, err os.Error) { func readUint16(reader io.Reader) (v uint16, n int, err os.Error) {
data := [2]byte{} data := [2]byte{}
n, e := reader.Read(data[0:]) n, e := reader.Read(data[0:])
if e != nil { return 0, n, e } if e != nil {
return (uint16(data[0]) << 8) | uint16(data[1]), n, 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) { func readUint32(reader io.Reader) (v uint32, n int, err os.Error) {
data := [4]byte{} data := [4]byte{}
n, e := reader.Read(data[0:]) n, e := reader.Read(data[0:])
if e != nil { return 0, n, e } if e != nil {
return (uint32(data[0]) << 24) | (uint32(data[1]) << 16) | (uint32(data[2]) << 8) | uint32(data[3]), n, 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) { func readUint64(reader io.Reader) (v uint64, n int, err os.Error) {
data := [8]byte{} data := [8]byte{}
n, e := reader.Read(data[0:]) n, e := reader.Read(data[0:])
if e != nil { return 0, n, e } if e != nil {
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 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) { func readInt16(reader io.Reader) (v int16, n int, err os.Error) {
data := [2]byte{} data := [2]byte{}
n, e := reader.Read(data[0:]) n, e := reader.Read(data[0:])
if e != nil { return 0, n, e } if e != nil {
return (int16(data[0]) << 8) | int16(data[1]), n, 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) { func readInt32(reader io.Reader) (v int32, n int, err os.Error) {
data := [4]byte{} data := [4]byte{}
n, e := reader.Read(data[0:]) n, e := reader.Read(data[0:])
if e != nil { return 0, n, e } if e != nil {
return (int32(data[0]) << 24) | (int32(data[1]) << 16) | (int32(data[2]) << 8) | int32(data[3]), n, 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) { func readInt64(reader io.Reader) (v int64, n int, err os.Error) {
data := [8]byte{} data := [8]byte{}
n, e := reader.Read(data[0:]) n, e := reader.Read(data[0:])
if e != nil { return 0, n, e } if e != nil {
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 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) { func unpackArray(reader io.Reader, nelems uint) (v reflect.Value, n int, err os.Error) {
retval := make([]interface{}, nelems) retval := make([]interface{}, nelems)
nbytesread := 0 nbytesread := 0
var i uint var i uint
for i = 0; i < nelems; i++ { for i = 0; i < nelems; i++ {
v, n, e := Unpack(reader) v, n, e := Unpack(reader)
nbytesread += n nbytesread += n
if e != nil { return nil, nbytesread, e } if e != nil {
retval[i] = v.Interface() return reflect.Value{}, nbytesread, e
} }
return reflect.NewValue(retval), nbytesread, nil 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) { func unpackArrayReflected(reader io.Reader, nelems uint) (v reflect.Value, n int, err os.Error) {
retval := make([]reflect.Value, nelems) retval := make([]reflect.Value, nelems)
nbytesread := 0 nbytesread := 0
var i uint var i uint
for i = 0; i < nelems; i++ { for i = 0; i < nelems; i++ {
v, n, e := UnpackReflected(reader) v, n, e := UnpackReflected(reader)
nbytesread += n nbytesread += n
if e != nil { return nil, nbytesread, e } if e != nil {
retval[i] = v return reflect.Value{}, nbytesread, e
} }
return reflect.NewValue(retval), nbytesread, nil retval[i] = v
}
return reflect.ValueOf(retval), nbytesread, nil
} }
func unpackMap(reader io.Reader, nelems uint) (v reflect.Value, n int, err os.Error) { func unpackMap(reader io.Reader, nelems uint) (v reflect.Value, n int, err os.Error) {
retval := make(map [interface{}] interface{}) retval := make(map[interface{}]interface{})
nbytesread := 0 nbytesread := 0
var i uint var i uint
for i = 0; i < nelems; i++ { for i = 0; i < nelems; i++ {
k, n, e := Unpack(reader) k, n, e := Unpack(reader)
nbytesread += n nbytesread += n
if e != nil { return nil, nbytesread, e } if e != nil {
v, n, e := Unpack(reader) return reflect.Value{}, nbytesread, e
nbytesread += n }
if e != nil { return nil, nbytesread, e } v, n, e := Unpack(reader)
retval[k.Interface()] = v.Interface() nbytesread += n
} if e != nil {
return reflect.NewValue(retval), nbytesread, 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) { func unpackMapReflected(reader io.Reader, nelems uint) (v reflect.Value, n int, err os.Error) {
retval := make(map [reflect.Value] reflect.Value) retval := make(map[interface{}]reflect.Value)
nbytesread := 0 nbytesread := 0
var i uint var i uint
for i = 0; i < nelems; i++ { for i = 0; i < nelems; i++ {
k, n, e := UnpackReflected(reader) k, n, e := UnpackReflected(reader)
nbytesread += n nbytesread += n
if e != nil { return nil, nbytesread, e } if e != nil {
v, n, e := UnpackReflected(reader) return reflect.Value{}, nbytesread, e
nbytesread += n }
if e != nil { return nil, nbytesread, e } v, n, e := UnpackReflected(reader)
retval[k] = v nbytesread += n
} if e != nil {
return reflect.NewValue(retval), nbytesread, 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) { func unpack(reader io.Reader, reflected bool) (v reflect.Value, n int, err os.Error) {
var retval reflect.Value var retval reflect.Value
var nbytesread int = 0 var nbytesread int = 0
c, e := readByte(reader) c, e := readByte(reader)
if e != nil { return nil, 0, e } if e != nil {
nbytesread += 1 return reflect.Value{}, 0, e
if c < 0x80 || c >= 0xe0 { }
retval = reflect.NewValue(int8(c)) nbytesread += 1
} else if c >= 0x80 && c <= 0x8f { if c < 0x80 || c >= 0xe0 {
if reflected { retval = reflect.ValueOf(int8(c))
retval, n, e = unpackMapReflected(reader, uint(c & 0xf)) } else if c >= 0x80 && c <= 0x8f {
} else { if reflected {
retval, n, e = unpackMap(reader, uint(c & 0xf)) retval, n, e = unpackMapReflected(reader, uint(c&0xf))
} } else {
nbytesread += n retval, n, e = unpackMap(reader, uint(c&0xf))
if e != nil { return nil, nbytesread, e } }
nbytesread += n nbytesread += n
} else if c >= 0x90 && c <= 0x9f { if e != nil {
if reflected { return reflect.Value{}, nbytesread, e
retval, n, e = unpackArrayReflected(reader, uint(c & 0xf)) }
} else { nbytesread += n
retval, n, e = unpackArray(reader, uint(c & 0xf)) } else if c >= 0x90 && c <= 0x9f {
} if reflected {
nbytesread += n retval, n, e = unpackArrayReflected(reader, uint(c&0xf))
if e != nil { return nil, nbytesread, e } } else {
nbytesread += n retval, n, e = unpackArray(reader, uint(c&0xf))
} else if c >= 0xa0 && c <= 0xbf { }
data := make([]byte, c & 0xf); nbytesread += n
n, e := reader.Read(data) if e != nil {
nbytesread += n return reflect.Value{}, nbytesread, e
if e != nil { return nil, nbytesread, e } }
retval = reflect.NewValue(data) nbytesread += n
} else { } else if c >= 0xa0 && c <= 0xbf {
switch c { data := make([]byte, c&0xf)
case 0xc0: retval = reflect.NewValue(nil) n, e := reader.Read(data)
case 0xc2: retval = reflect.NewValue(false) nbytesread += n
case 0xc3: retval = reflect.NewValue(true) if e != nil {
case 0xca: return reflect.Value{}, nbytesread, e
data, n, e := readUint32(reader) }
nbytesread += n retval = reflect.ValueOf(data)
if e != nil { return nil, nbytesread, e } } else {
retval = reflect.NewValue(*(*float32)(unsafe.Pointer(&data))) switch c {
case 0xcb: case 0xc0:
data, n, e := readUint64(reader) retval = reflect.ValueOf(nil)
nbytesread += n case 0xc2:
if e != nil { return nil, nbytesread, e } retval = reflect.ValueOf(false)
retval = reflect.NewValue(*(*float64)(unsafe.Pointer(&data))) case 0xc3:
case 0xcc: retval = reflect.ValueOf(true)
data, e := readByte(reader) case 0xca:
if e != nil { return nil, nbytesread, e } data, n, e := readUint32(reader)
retval = reflect.NewValue(uint8(data)) nbytesread += n
nbytesread += 1 if e != nil {
case 0xcd: return reflect.Value{}, nbytesread, e
data, n, e := readUint16(reader) }
nbytesread += n retval = reflect.ValueOf(*(*float32)(unsafe.Pointer(&data)))
if e != nil { return nil, nbytesread, e } case 0xcb:
retval = reflect.NewValue(data) data, n, e := readUint64(reader)
case 0xce: nbytesread += n
data, n, e := readUint32(reader) if e != nil {
nbytesread += n return reflect.Value{}, nbytesread, e
if e != nil { return nil, nbytesread, e } }
retval = reflect.NewValue(data) retval = reflect.ValueOf(*(*float64)(unsafe.Pointer(&data)))
case 0xcf: case 0xcc:
data, n, e := readUint64(reader) data, e := readByte(reader)
nbytesread += n if e != nil {
if e != nil { return nil, nbytesread, e } return reflect.Value{}, nbytesread, e
retval = reflect.NewValue(data) }
case 0xd0: retval = reflect.ValueOf(uint8(data))
data, e := readByte(reader) nbytesread += 1
if e != nil { return nil, nbytesread, e } case 0xcd:
retval = reflect.NewValue(int8(data)) data, n, e := readUint16(reader)
nbytesread += 1 nbytesread += n
case 0xd1: if e != nil {
data, n, e := readInt16(reader) return reflect.Value{}, nbytesread, e
nbytesread += n }
if e != nil { return nil, nbytesread, e } retval = reflect.ValueOf(data)
retval = reflect.NewValue(data) case 0xce:
case 0xd2: data, n, e := readUint32(reader)
data, n, e := readInt32(reader) nbytesread += n
nbytesread += n if e != nil {
if e != nil { return nil, nbytesread, e } return reflect.Value{}, nbytesread, e
retval = reflect.NewValue(data) }
case 0xd3: retval = reflect.ValueOf(data)
data, n, e := readInt64(reader) case 0xcf:
nbytesread += n data, n, e := readUint64(reader)
if e != nil { return nil, nbytesread, e } nbytesread += n
retval = reflect.NewValue(data) if e != nil {
case 0xda: return reflect.Value{}, nbytesread, e
nbytestoread, n, e := readUint16(reader) }
nbytesread += n retval = reflect.ValueOf(data)
if e != nil { return nil, nbytesread, e } case 0xd0:
data := make([]byte, nbytestoread) data, e := readByte(reader)
n, e = reader.Read(data) if e != nil {
nbytesread += n return reflect.Value{}, nbytesread, e
if e != nil { return nil, nbytesread, e } }
retval = reflect.NewValue(data) retval = reflect.ValueOf(int8(data))
case 0xdb: nbytesread += 1
nbytestoread, n, e := readUint32(reader) case 0xd1:
nbytesread += n data, n, e := readInt16(reader)
if e != nil { return nil, nbytesread, e } nbytesread += n
data := make([]byte, nbytestoread) if e != nil {
n, e = reader.Read(data) return reflect.Value{}, nbytesread, e
nbytesread += n }
if e != nil { return nil, nbytesread, e } retval = reflect.ValueOf(data)
retval = reflect.NewValue(data) case 0xd2:
case 0xdc: data, n, e := readInt32(reader)
nelemstoread, n, e := readUint16(reader) nbytesread += n
nbytesread += n if e != nil {
if e != nil { return nil, nbytesread, e } return reflect.Value{}, nbytesread, e
if reflected { }
retval, n, e = unpackArrayReflected(reader, uint(nelemstoread)) retval = reflect.ValueOf(data)
} else { case 0xd3:
retval, n, e = unpackArray(reader, uint(nelemstoread)) data, n, e := readInt64(reader)
} nbytesread += n
nbytesread += n if e != nil {
if e != nil { return nil, nbytesread, e } return reflect.Value{}, nbytesread, e
case 0xdd: }
nelemstoread, n, e := readUint32(reader) retval = reflect.ValueOf(data)
nbytesread += n case 0xda:
if e != nil { return nil, nbytesread, e } nbytestoread, n, e := readUint16(reader)
if reflected { nbytesread += n
retval, n, e = unpackArrayReflected(reader, uint(nelemstoread)) if e != nil {
} else { return reflect.Value{}, nbytesread, e
retval, n, e = unpackArray(reader, uint(nelemstoread)) }
} data := make([]byte, nbytestoread)
nbytesread += n n, e = reader.Read(data)
if e != nil { return nil, nbytesread, e } nbytesread += n
case 0xde: if e != nil {
nelemstoread, n, e := readUint16(reader) return reflect.Value{}, nbytesread, e
nbytesread += n }
if e != nil { return nil, nbytesread, e } retval = reflect.ValueOf(data)
if reflected { case 0xdb:
retval, n, e = unpackMapReflected(reader, uint(nelemstoread)) nbytestoread, n, e := readUint32(reader)
} else { nbytesread += n
retval, n, e = unpackMap(reader, uint(nelemstoread)) if e != nil {
} return reflect.Value{}, nbytesread, e
nbytesread += n }
if e != nil { return nil, nbytesread, e } data := make([]byte, nbytestoread)
case 0xdf: n, e = reader.Read(data)
nelemstoread, n, e := readUint32(reader) nbytesread += n
nbytesread += n if e != nil {
if e != nil { return nil, nbytesread, e } return reflect.Value{}, nbytesread, e
if reflected { }
retval, n, e = unpackMapReflected(reader, uint(nelemstoread)) retval = reflect.ValueOf(data)
} else { case 0xdc:
retval, n, e = unpackMap(reader, uint(nelemstoread)) nelemstoread, n, e := readUint16(reader)
} nbytesread += n
nbytesread += n if e != nil {
if e != nil { return nil, nbytesread, e } return reflect.Value{}, nbytesread, e
default: }
panic("unsupported code: " + strconv.Itoa(int(c))) if reflected {
} retval, n, e = unpackArrayReflected(reader, uint(nelemstoread))
} } else {
return retval, nbytesread, nil 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. // Reads a value from the reader, unpack and returns it.
func Unpack(reader io.Reader) (v reflect.Value, n int, err os.Error) { 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 // 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 // value is an array or map, leaves the elements wrapped by corresponding
// wrapper objects defined in reflect package. // wrapper objects defined in reflect package.
func UnpackReflected(reader io.Reader) (v reflect.Value, n int, err os.Error) { func UnpackReflected(reader io.Reader) (v reflect.Value, n int, err os.Error) {
return unpack(reader, true) return unpack(reader, true)
} }