Merge branch 'master' of https://github.com/dgryski/msgpack into go

Conflicts:
	go/pack.go
This commit is contained in:
INADA Naoki 2012-05-01 01:44:27 +09:00
commit 5456814199
5 changed files with 47 additions and 20 deletions

View File

@ -1,11 +0,0 @@
include $(GOROOT)/src/Make.inc
TARG=msgpack
GOFILES=pack.go unpack.go
include $(GOROOT)/src/Make.pkg
%: install %.go
$(GC) $*.go
$(LD) -o $@ $*.$O

3
go/README Normal file
View File

@ -0,0 +1,3 @@
This builds with the new 'go' tool.
It is installable with "go get github.com/msgpack/msgpack/go".

View File

@ -1,7 +1,6 @@
package msgpack_test package msgpack
import ( import (
. "msgpack"
"testing" "testing"
"bytes" "bytes"
"reflect" "reflect"
@ -74,6 +73,35 @@ func TestPackUint8(t *testing.T) {
} }
} }
func TestPackBytes(t *testing.T) {
for _, i := range []struct {
s string
b []byte
}{
{"a", []byte("\xa1a")},
{"hello", []byte("\xa5hello")},
{"world world world", []byte("\xb1world world world")},
{"world world world world world world", []byte("\xda\x00#world world world world world world")},
} {
b := &bytes.Buffer{}
_, err := PackBytes(b, []byte(i.s))
if err != nil {
t.Error("err != nil")
}
v, _, e := Unpack(b)
if e != nil {
t.Error("err != nil")
}
if !equal(v, reflect.ValueOf([]byte(i.s))) {
t.Error("unpack(pack(%s)) != %s", i.s)
}
}
}
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} {
@ -247,7 +275,14 @@ func TestPackMap(t *testing.T) {
if err != nil { if err != nil {
t.Error("err != nil") t.Error("err != nil")
} }
if bytes.Compare(b.Bytes(), []byte{0x83, 0x00, 0x01, 0x04, 0x05, 0x02, 0x03}) != 0 {
// map ordering is no longer deterministic -- check all possible orderings :(
if bytes.Compare(b.Bytes(), []byte{0x83, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05}) != 0 &&
bytes.Compare(b.Bytes(), []byte{0x83, 0x00, 0x01, 0x04, 0x05, 0x02, 0x03}) != 0 &&
bytes.Compare(b.Bytes(), []byte{0x83, 0x02, 0x03, 0x00, 0x01, 0x04, 0x05}) != 0 &&
bytes.Compare(b.Bytes(), []byte{0x83, 0x02, 0x03, 0x04, 0x05, 0x00, 0x01}) != 0 &&
bytes.Compare(b.Bytes(), []byte{0x83, 0x04, 0x05, 0x00, 0x01, 0x02, 0x03}) != 0 &&
bytes.Compare(b.Bytes(), []byte{0x83, 0x04, 0x05, 0x02, 0x03, 0x00, 0x01}) != 0 {
t.Error("wrong output", b.Bytes()) t.Error("wrong output", b.Bytes())
} }
} }

View File

@ -2,9 +2,9 @@ package msgpack
import ( import (
"io" "io"
"os"
"reflect" "reflect"
"unsafe" "unsafe"
"syscall"
) )
// Packs a given value and writes it into the specified writer. // Packs a given value and writes it into the specified writer.
@ -59,7 +59,7 @@ func PackUint(writer io.Writer, value uint) (n int, err error) {
case 8: case 8:
return PackUint64(writer, *(*uint64)(unsafe.Pointer(&value))) return PackUint64(writer, *(*uint64)(unsafe.Pointer(&value)))
} }
return 0, syscall.ENOENT // never get here return 0, os.ErrNotExist // never get here
} }
// Packs a given value and writes it into the specified writer. // Packs a given value and writes it into the specified writer.
@ -116,7 +116,7 @@ func PackInt(writer io.Writer, value int) (n int, err error) {
case 8: case 8:
return PackInt64(writer, *(*int64)(unsafe.Pointer(&value))) return PackInt64(writer, *(*int64)(unsafe.Pointer(&value)))
} }
return 0, syscall.ENOENT // never get here return 0, os.ErrNotExist // never get here
} }
// Packs a given value and writes it into the specified writer. // Packs a given value and writes it into the specified writer.
@ -304,7 +304,7 @@ func PackUintArray(writer io.Writer, value []uint) (n int, err error) {
case 8: case 8:
return PackUint64Array(writer, *(*[]uint64)(unsafe.Pointer(&value))) return PackUint64Array(writer, *(*[]uint64)(unsafe.Pointer(&value)))
} }
return 0, syscall.ENOENT // never get here return 0, os.ErrNotExist // never get here
} }
// Packs a given value and writes it into the specified writer. // Packs a given value and writes it into the specified writer.
@ -483,7 +483,7 @@ func PackIntArray(writer io.Writer, value []int) (n int, err error) {
case 8: case 8:
return PackInt64Array(writer, *(*[]int64)(unsafe.Pointer(&value))) return PackInt64Array(writer, *(*[]int64)(unsafe.Pointer(&value)))
} }
return 0, syscall.ENOENT // never get here return 0, os.ErrNotExist // never get here
} }
// Packs a given value and writes it into the specified writer. // Packs a given value and writes it into the specified writer.

View File

@ -174,7 +174,7 @@ func unpack(reader io.Reader, reflected bool) (v reflect.Value, n int, err error
} }
nbytesread += n nbytesread += n
} else if c >= 0xa0 && c <= 0xbf { } else if c >= 0xa0 && c <= 0xbf {
data := make([]byte, c&0xf) data := make([]byte, c&0x1f)
n, e := reader.Read(data) n, e := reader.Read(data)
nbytesread += n nbytesread += n
if e != nil { if e != nil {