2009-06-29 16:15:07 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gob
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:35:38 -07:00
|
|
|
"bytes"
|
|
|
|
"math"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"unsafe"
|
2009-06-29 16:15:07 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Guarantee encoding format by comparing some encodings to hand-written values
|
|
|
|
type EncodeT struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
x uint64
|
|
|
|
b []byte
|
2009-06-29 16:15:07 -06:00
|
|
|
}
|
2009-11-05 15:53:42 -07:00
|
|
|
|
|
|
|
var encodeT = []EncodeT{
|
|
|
|
EncodeT{0x00, []byte{0x00}},
|
|
|
|
EncodeT{0x0F, []byte{0x0F}},
|
|
|
|
EncodeT{0xFF, []byte{0xFF, 0xFF}},
|
|
|
|
EncodeT{0xFFFF, []byte{0xFE, 0xFF, 0xFF}},
|
|
|
|
EncodeT{0xFFFFFF, []byte{0xFD, 0xFF, 0xFF, 0xFF}},
|
|
|
|
EncodeT{0xFFFFFFFF, []byte{0xFC, 0xFF, 0xFF, 0xFF, 0xFF}},
|
|
|
|
EncodeT{0xFFFFFFFFFF, []byte{0xFB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
|
|
|
|
EncodeT{0xFFFFFFFFFFFF, []byte{0xFA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
|
|
|
|
EncodeT{0xFFFFFFFFFFFFFF, []byte{0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
|
|
|
|
EncodeT{0xFFFFFFFFFFFFFFFF, []byte{0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
|
|
|
|
EncodeT{0x1111, []byte{0xFE, 0x11, 0x11}},
|
|
|
|
EncodeT{0x1111111111111111, []byte{0xF8, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}},
|
|
|
|
EncodeT{0x8888888888888888, []byte{0xF8, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88}},
|
2009-11-09 22:13:17 -07:00
|
|
|
EncodeT{1 << 63, []byte{0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
|
2009-06-29 16:15:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test basic encode/decode routines for unsigned integers
|
|
|
|
func TestUintCodec(t *testing.T) {
|
2009-12-15 16:35:38 -07:00
|
|
|
b := new(bytes.Buffer)
|
|
|
|
encState := new(encoderState)
|
|
|
|
encState.b = b
|
2009-09-15 10:41:59 -06:00
|
|
|
for _, tt := range encodeT {
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
encodeUint(encState, tt.x)
|
2009-06-30 17:20:31 -06:00
|
|
|
if encState.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("encodeUint:", tt.x, encState.err)
|
2009-06-29 16:15:07 -06:00
|
|
|
}
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(tt.b, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("encodeUint: %#x encode: expected % x got % x", tt.x, tt.b, b.Bytes())
|
2009-06-29 16:15:07 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
decState := newDecodeState(b)
|
2009-11-09 22:13:17 -07:00
|
|
|
for u := uint64(0); ; u = (u + 1) * 7 {
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
encodeUint(encState, u)
|
2009-06-30 17:20:31 -06:00
|
|
|
if encState.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("encodeUint:", u, encState.err)
|
2009-06-29 16:15:07 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
v := decodeUint(decState)
|
2009-06-30 17:20:31 -06:00
|
|
|
if decState.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("DecodeUint:", u, decState.err)
|
2009-06-29 16:15:07 -06:00
|
|
|
}
|
|
|
|
if u != v {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("Encode/Decode: sent %#x received %#x\n", u, v)
|
2009-06-29 16:15:07 -06:00
|
|
|
}
|
2009-11-05 15:53:42 -07:00
|
|
|
if u&(1<<63) != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2009-06-29 16:15:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyInt(i int64, t *testing.T) {
|
2009-12-15 16:35:38 -07:00
|
|
|
var b = new(bytes.Buffer)
|
|
|
|
encState := new(encoderState)
|
|
|
|
encState.b = b
|
|
|
|
encodeInt(encState, i)
|
2009-06-30 17:20:31 -06:00
|
|
|
if encState.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("encodeInt:", i, encState.err)
|
2009-06-29 16:15:07 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
decState := newDecodeState(b)
|
|
|
|
decState.buf = make([]byte, 8)
|
|
|
|
j := decodeInt(decState)
|
2009-06-30 17:20:31 -06:00
|
|
|
if decState.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("DecodeInt:", i, decState.err)
|
2009-06-29 16:15:07 -06:00
|
|
|
}
|
|
|
|
if i != j {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("Encode/Decode: sent %#x received %#x\n", uint64(i), uint64(j))
|
2009-06-29 16:15:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test basic encode/decode routines for signed integers
|
|
|
|
func TestIntCodec(t *testing.T) {
|
2009-11-09 22:13:17 -07:00
|
|
|
for u := uint64(0); ; u = (u + 1) * 7 {
|
2009-06-29 16:15:07 -06:00
|
|
|
// Do positive and negative values
|
2009-12-15 16:35:38 -07:00
|
|
|
i := int64(u)
|
|
|
|
verifyInt(i, t)
|
|
|
|
verifyInt(-i, t)
|
|
|
|
verifyInt(^i, t)
|
2009-11-05 15:53:42 -07:00
|
|
|
if u&(1<<63) != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2009-06-29 16:15:07 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
verifyInt(-1<<63, t) // a tricky case
|
2009-06-29 16:15:07 -06:00
|
|
|
}
|
2009-06-30 16:37:46 -06:00
|
|
|
|
2009-07-28 18:20:19 -06:00
|
|
|
// The result of encoding a true boolean with field number 7
|
|
|
|
var boolResult = []byte{0x07, 0x01}
|
|
|
|
// The result of encoding a number 17 with field number 7
|
2009-11-09 22:13:17 -07:00
|
|
|
var signedResult = []byte{0x07, 2 * 17}
|
2009-07-28 18:20:19 -06:00
|
|
|
var unsignedResult = []byte{0x07, 17}
|
|
|
|
var floatResult = []byte{0x07, 0xFE, 0x31, 0x40}
|
2009-07-02 12:21:01 -06:00
|
|
|
// The result of encoding "hello" with field number 6
|
2009-07-28 18:20:19 -06:00
|
|
|
var bytesResult = []byte{0x07, 0x05, 'h', 'e', 'l', 'l', 'o'}
|
2009-07-01 19:25:13 -06:00
|
|
|
|
2009-07-15 17:10:17 -06:00
|
|
|
func newencoderState(b *bytes.Buffer) *encoderState {
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
state := new(encoderState)
|
|
|
|
state.b = b
|
|
|
|
state.fieldnum = -1
|
|
|
|
return state
|
2009-07-01 19:25:13 -06:00
|
|
|
}
|
2009-06-30 16:37:46 -06:00
|
|
|
|
|
|
|
// Test instruction execution for encoding.
|
|
|
|
// Do not run the machine yet; instead do individual instructions crafted by hand.
|
|
|
|
func TestScalarEncInstructions(t *testing.T) {
|
2009-12-15 16:35:38 -07:00
|
|
|
var b = new(bytes.Buffer)
|
2009-06-30 16:37:46 -06:00
|
|
|
|
|
|
|
// bool
|
|
|
|
{
|
2009-12-15 16:35:38 -07:00
|
|
|
data := struct{ a bool }{true}
|
|
|
|
instr := &encInstr{encBool, 6, 0, 0}
|
|
|
|
state := newencoderState(b)
|
|
|
|
instr.op(instr, state, unsafe.Pointer(&data))
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(boolResult, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("bool enc instructions: expected % x got % x", boolResult, b.Bytes())
|
2009-06-30 16:37:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// int
|
|
|
|
{
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
data := struct{ a int }{17}
|
|
|
|
instr := &encInstr{encInt, 6, 0, 0}
|
|
|
|
state := newencoderState(b)
|
|
|
|
instr.op(instr, state, unsafe.Pointer(&data))
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(signedResult, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("int enc instructions: expected % x got % x", signedResult, b.Bytes())
|
2009-06-30 16:37:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uint
|
|
|
|
{
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
data := struct{ a uint }{17}
|
|
|
|
instr := &encInstr{encUint, 6, 0, 0}
|
|
|
|
state := newencoderState(b)
|
|
|
|
instr.op(instr, state, unsafe.Pointer(&data))
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(unsignedResult, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("uint enc instructions: expected % x got % x", unsignedResult, b.Bytes())
|
2009-06-30 16:37:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// int8
|
|
|
|
{
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
data := struct{ a int8 }{17}
|
|
|
|
instr := &encInstr{encInt8, 6, 0, 0}
|
|
|
|
state := newencoderState(b)
|
|
|
|
instr.op(instr, state, unsafe.Pointer(&data))
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(signedResult, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("int8 enc instructions: expected % x got % x", signedResult, b.Bytes())
|
2009-06-30 16:37:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uint8
|
|
|
|
{
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
data := struct{ a uint8 }{17}
|
|
|
|
instr := &encInstr{encUint8, 6, 0, 0}
|
|
|
|
state := newencoderState(b)
|
|
|
|
instr.op(instr, state, unsafe.Pointer(&data))
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(unsignedResult, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("uint8 enc instructions: expected % x got % x", unsignedResult, b.Bytes())
|
2009-06-30 16:37:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// int16
|
|
|
|
{
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
data := struct{ a int16 }{17}
|
|
|
|
instr := &encInstr{encInt16, 6, 0, 0}
|
|
|
|
state := newencoderState(b)
|
|
|
|
instr.op(instr, state, unsafe.Pointer(&data))
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(signedResult, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("int16 enc instructions: expected % x got % x", signedResult, b.Bytes())
|
2009-06-30 16:37:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uint16
|
|
|
|
{
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
data := struct{ a uint16 }{17}
|
|
|
|
instr := &encInstr{encUint16, 6, 0, 0}
|
|
|
|
state := newencoderState(b)
|
|
|
|
instr.op(instr, state, unsafe.Pointer(&data))
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(unsignedResult, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("uint16 enc instructions: expected % x got % x", unsignedResult, b.Bytes())
|
2009-06-30 16:37:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// int32
|
|
|
|
{
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
data := struct{ a int32 }{17}
|
|
|
|
instr := &encInstr{encInt32, 6, 0, 0}
|
|
|
|
state := newencoderState(b)
|
|
|
|
instr.op(instr, state, unsafe.Pointer(&data))
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(signedResult, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("int32 enc instructions: expected % x got % x", signedResult, b.Bytes())
|
2009-06-30 16:37:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uint32
|
|
|
|
{
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
data := struct{ a uint32 }{17}
|
|
|
|
instr := &encInstr{encUint32, 6, 0, 0}
|
|
|
|
state := newencoderState(b)
|
|
|
|
instr.op(instr, state, unsafe.Pointer(&data))
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(unsignedResult, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("uint32 enc instructions: expected % x got % x", unsignedResult, b.Bytes())
|
2009-06-30 16:37:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// int64
|
|
|
|
{
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
data := struct{ a int64 }{17}
|
|
|
|
instr := &encInstr{encInt64, 6, 0, 0}
|
|
|
|
state := newencoderState(b)
|
|
|
|
instr.op(instr, state, unsafe.Pointer(&data))
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(signedResult, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("int64 enc instructions: expected % x got % x", signedResult, b.Bytes())
|
2009-06-30 16:37:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uint64
|
|
|
|
{
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
data := struct{ a uint64 }{17}
|
|
|
|
instr := &encInstr{encUint64, 6, 0, 0}
|
|
|
|
state := newencoderState(b)
|
|
|
|
instr.op(instr, state, unsafe.Pointer(&data))
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(unsignedResult, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("uint64 enc instructions: expected % x got % x", unsignedResult, b.Bytes())
|
2009-06-30 16:37:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// float
|
|
|
|
{
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
data := struct{ a float }{17}
|
|
|
|
instr := &encInstr{encFloat, 6, 0, 0}
|
|
|
|
state := newencoderState(b)
|
|
|
|
instr.op(instr, state, unsafe.Pointer(&data))
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(floatResult, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("float enc instructions: expected % x got % x", floatResult, b.Bytes())
|
2009-06-30 16:37:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// float32
|
|
|
|
{
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
data := struct{ a float32 }{17}
|
|
|
|
instr := &encInstr{encFloat32, 6, 0, 0}
|
|
|
|
state := newencoderState(b)
|
|
|
|
instr.op(instr, state, unsafe.Pointer(&data))
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(floatResult, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("float32 enc instructions: expected % x got % x", floatResult, b.Bytes())
|
2009-06-30 16:37:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// float64
|
|
|
|
{
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
data := struct{ a float64 }{17}
|
|
|
|
instr := &encInstr{encFloat64, 6, 0, 0}
|
|
|
|
state := newencoderState(b)
|
|
|
|
instr.op(instr, state, unsafe.Pointer(&data))
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(floatResult, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("float64 enc instructions: expected % x got % x", floatResult, b.Bytes())
|
2009-06-30 16:37:46 -06:00
|
|
|
}
|
|
|
|
}
|
2009-07-02 12:21:01 -06:00
|
|
|
|
|
|
|
// bytes == []uint8
|
|
|
|
{
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
2010-02-25 17:01:29 -07:00
|
|
|
data := struct{ a []byte }{[]byte("hello")}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &encInstr{encUint8Array, 6, 0, 0}
|
|
|
|
state := newencoderState(b)
|
|
|
|
instr.op(instr, state, unsafe.Pointer(&data))
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(bytesResult, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("bytes enc instructions: expected % x got % x", bytesResult, b.Bytes())
|
2009-07-02 12:21:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// string
|
|
|
|
{
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
data := struct{ a string }{"hello"}
|
|
|
|
instr := &encInstr{encString, 6, 0, 0}
|
|
|
|
state := newencoderState(b)
|
|
|
|
instr.op(instr, state, unsafe.Pointer(&data))
|
2009-09-16 16:15:00 -06:00
|
|
|
if !bytes.Equal(bytesResult, b.Bytes()) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("string enc instructions: expected % x got % x", bytesResult, b.Bytes())
|
2009-07-02 12:21:01 -06:00
|
|
|
}
|
|
|
|
}
|
2009-06-30 16:37:46 -06:00
|
|
|
}
|
2009-06-30 18:59:41 -06:00
|
|
|
|
2009-07-15 17:10:17 -06:00
|
|
|
func execDec(typ string, instr *decInstr, state *decodeState, t *testing.T, p unsafe.Pointer) {
|
2009-12-15 16:35:38 -07:00
|
|
|
v := int(decodeUint(state))
|
2009-06-30 18:59:41 -06:00
|
|
|
if state.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Fatalf("decoding %s field: %v", typ, state.err)
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
2009-11-09 22:13:17 -07:00
|
|
|
if v+state.fieldnum != 6 {
|
|
|
|
t.Fatalf("decoding field number %d, got %d", 6, v+state.fieldnum)
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr.op(instr, state, decIndirect(p, instr.indir))
|
|
|
|
state.fieldnum = 6
|
2009-07-01 19:25:13 -06:00
|
|
|
}
|
|
|
|
|
2009-07-28 18:20:19 -06:00
|
|
|
func newDecodeStateFromData(data []byte) *decodeState {
|
2009-12-15 16:35:38 -07:00
|
|
|
state := newDecodeState(bytes.NewBuffer(data))
|
|
|
|
state.fieldnum = -1
|
|
|
|
return state
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test instruction execution for decoding.
|
|
|
|
// Do not run the machine yet; instead do individual instructions crafted by hand.
|
|
|
|
func TestScalarDecInstructions(t *testing.T) {
|
2009-12-15 16:35:38 -07:00
|
|
|
ovfl := os.ErrorString("overflow")
|
2009-06-30 18:59:41 -06:00
|
|
|
|
|
|
|
// bool
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a bool
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decBool, 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(boolResult)
|
|
|
|
execDec("bool", instr, state, t, unsafe.Pointer(&data))
|
2009-06-30 18:59:41 -06:00
|
|
|
if data.a != true {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("bool a = %v not true", data.a)
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// int
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a int
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decOpMap[valueKind(data.a)], 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(signedResult)
|
|
|
|
execDec("int", instr, state, t, unsafe.Pointer(&data))
|
2009-06-30 18:59:41 -06:00
|
|
|
if data.a != 17 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("int a = %v not 17", data.a)
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uint
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a uint
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decOpMap[valueKind(data.a)], 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(unsignedResult)
|
|
|
|
execDec("uint", instr, state, t, unsafe.Pointer(&data))
|
2009-06-30 18:59:41 -06:00
|
|
|
if data.a != 17 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("uint a = %v not 17", data.a)
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// int8
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a int8
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decInt8, 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(signedResult)
|
|
|
|
execDec("int8", instr, state, t, unsafe.Pointer(&data))
|
2009-06-30 18:59:41 -06:00
|
|
|
if data.a != 17 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("int8 a = %v not 17", data.a)
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uint8
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a uint8
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decUint8, 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(unsignedResult)
|
|
|
|
execDec("uint8", instr, state, t, unsafe.Pointer(&data))
|
2009-06-30 18:59:41 -06:00
|
|
|
if data.a != 17 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("uint8 a = %v not 17", data.a)
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// int16
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a int16
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decInt16, 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(signedResult)
|
|
|
|
execDec("int16", instr, state, t, unsafe.Pointer(&data))
|
2009-06-30 18:59:41 -06:00
|
|
|
if data.a != 17 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("int16 a = %v not 17", data.a)
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uint16
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a uint16
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decUint16, 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(unsignedResult)
|
|
|
|
execDec("uint16", instr, state, t, unsafe.Pointer(&data))
|
2009-06-30 18:59:41 -06:00
|
|
|
if data.a != 17 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("uint16 a = %v not 17", data.a)
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// int32
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a int32
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decInt32, 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(signedResult)
|
|
|
|
execDec("int32", instr, state, t, unsafe.Pointer(&data))
|
2009-06-30 18:59:41 -06:00
|
|
|
if data.a != 17 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("int32 a = %v not 17", data.a)
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uint32
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a uint32
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decUint32, 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(unsignedResult)
|
|
|
|
execDec("uint32", instr, state, t, unsafe.Pointer(&data))
|
2009-06-30 18:59:41 -06:00
|
|
|
if data.a != 17 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("uint32 a = %v not 17", data.a)
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uintptr
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a uintptr
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decOpMap[valueKind(data.a)], 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(unsignedResult)
|
|
|
|
execDec("uintptr", instr, state, t, unsafe.Pointer(&data))
|
2009-07-28 13:59:39 -06:00
|
|
|
if data.a != 17 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("uintptr a = %v not 17", data.a)
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// int64
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a int64
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decInt64, 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(signedResult)
|
|
|
|
execDec("int64", instr, state, t, unsafe.Pointer(&data))
|
2009-06-30 18:59:41 -06:00
|
|
|
if data.a != 17 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("int64 a = %v not 17", data.a)
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uint64
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a uint64
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decUint64, 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(unsignedResult)
|
|
|
|
execDec("uint64", instr, state, t, unsafe.Pointer(&data))
|
2009-06-30 18:59:41 -06:00
|
|
|
if data.a != 17 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("uint64 a = %v not 17", data.a)
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// float
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a float
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decOpMap[valueKind(data.a)], 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(floatResult)
|
|
|
|
execDec("float", instr, state, t, unsafe.Pointer(&data))
|
2009-06-30 18:59:41 -06:00
|
|
|
if data.a != 17 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("float a = %v not 17", data.a)
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// float32
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a float32
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decFloat32, 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(floatResult)
|
|
|
|
execDec("float32", instr, state, t, unsafe.Pointer(&data))
|
2009-06-30 18:59:41 -06:00
|
|
|
if data.a != 17 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("float32 a = %v not 17", data.a)
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// float64
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a float64
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decFloat64, 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(floatResult)
|
|
|
|
execDec("float64", instr, state, t, unsafe.Pointer(&data))
|
2009-06-30 18:59:41 -06:00
|
|
|
if data.a != 17 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("float64 a = %v not 17", data.a)
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
|
|
|
}
|
2009-07-02 12:21:01 -06:00
|
|
|
|
|
|
|
// bytes == []uint8
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a []byte
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decUint8Array, 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(bytesResult)
|
|
|
|
execDec("bytes", instr, state, t, unsafe.Pointer(&data))
|
2009-07-02 12:21:01 -06:00
|
|
|
if string(data.a) != "hello" {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf(`bytes a = %q not "hello"`, string(data.a))
|
2009-07-02 12:21:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// string
|
|
|
|
{
|
2009-11-05 15:53:42 -07:00
|
|
|
var data struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a string
|
2009-11-05 15:53:42 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
instr := &decInstr{decString, 6, 0, 0, ovfl}
|
|
|
|
state := newDecodeStateFromData(bytesResult)
|
|
|
|
execDec("bytes", instr, state, t, unsafe.Pointer(&data))
|
2009-07-02 12:21:01 -06:00
|
|
|
if data.a != "hello" {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf(`bytes a = %q not "hello"`, data.a)
|
2009-07-02 12:21:01 -06:00
|
|
|
}
|
|
|
|
}
|
2009-06-30 18:59:41 -06:00
|
|
|
}
|
2009-07-01 19:25:13 -06:00
|
|
|
|
2009-07-02 19:02:42 -06:00
|
|
|
func TestEndToEnd(t *testing.T) {
|
2009-07-02 14:43:47 -06:00
|
|
|
type T2 struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
t string
|
2009-07-02 14:43:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
s1 := "string1"
|
|
|
|
s2 := "string2"
|
2009-07-02 09:21:42 -06:00
|
|
|
type T1 struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a, b, c int
|
|
|
|
n *[3]float
|
|
|
|
strs *[2]string
|
|
|
|
int64s *[]int64
|
|
|
|
s string
|
|
|
|
y []byte
|
|
|
|
t *T2
|
2009-07-02 12:21:01 -06:00
|
|
|
}
|
|
|
|
t1 := &T1{
|
2010-03-02 14:46:51 -07:00
|
|
|
a: 17,
|
|
|
|
b: 18,
|
|
|
|
c: -5,
|
|
|
|
n: &[3]float{1.5, 2.5, 3.5},
|
|
|
|
strs: &[2]string{s1, s2},
|
2009-07-02 19:02:42 -06:00
|
|
|
int64s: &[]int64{77, 89, 123412342134},
|
2010-03-02 14:46:51 -07:00
|
|
|
s: "Now is the time",
|
|
|
|
y: []byte("hello, sailor"),
|
|
|
|
t: &T2{"this is T2"},
|
2009-12-15 16:35:38 -07:00
|
|
|
}
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
err := NewEncoder(b).Encode(t1)
|
2009-11-17 00:32:30 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error("encode:", err)
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
var _t1 T1
|
|
|
|
err = NewDecoder(b).Decode(&_t1)
|
2009-11-17 00:32:30 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("decode:", err)
|
|
|
|
}
|
2009-07-01 19:25:13 -06:00
|
|
|
if !reflect.DeepEqual(t1, &_t1) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("encode expected %v got %v", *t1, _t1)
|
2009-07-01 19:25:13 -06:00
|
|
|
}
|
|
|
|
}
|
2009-07-02 09:21:42 -06:00
|
|
|
|
2009-07-28 13:59:39 -06:00
|
|
|
func TestOverflow(t *testing.T) {
|
|
|
|
type inputT struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
maxi int64
|
|
|
|
mini int64
|
|
|
|
maxu uint64
|
|
|
|
maxf float64
|
|
|
|
minf float64
|
|
|
|
}
|
|
|
|
var it inputT
|
|
|
|
var err os.Error
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
enc := NewEncoder(b)
|
|
|
|
dec := NewDecoder(b)
|
2009-07-28 13:59:39 -06:00
|
|
|
|
|
|
|
// int8
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
2009-11-05 15:53:42 -07:00
|
|
|
it = inputT{
|
2009-07-28 13:59:39 -06:00
|
|
|
maxi: math.MaxInt8 + 1,
|
2009-12-15 16:35:38 -07:00
|
|
|
}
|
2009-07-28 13:59:39 -06:00
|
|
|
type outi8 struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
maxi int8
|
|
|
|
mini int8
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
var o1 outi8
|
|
|
|
enc.Encode(it)
|
|
|
|
err = dec.Decode(&o1)
|
2009-07-28 13:59:39 -06:00
|
|
|
if err == nil || err.String() != `value for "maxi" out of range` {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("wrong overflow error for int8:", err)
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
2009-11-05 15:53:42 -07:00
|
|
|
it = inputT{
|
2009-07-28 13:59:39 -06:00
|
|
|
mini: math.MinInt8 - 1,
|
2009-12-15 16:35:38 -07:00
|
|
|
}
|
|
|
|
b.Reset()
|
|
|
|
enc.Encode(it)
|
|
|
|
err = dec.Decode(&o1)
|
2009-07-28 13:59:39 -06:00
|
|
|
if err == nil || err.String() != `value for "mini" out of range` {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("wrong underflow error for int8:", err)
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// int16
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
2009-11-05 15:53:42 -07:00
|
|
|
it = inputT{
|
2009-07-28 13:59:39 -06:00
|
|
|
maxi: math.MaxInt16 + 1,
|
2009-12-15 16:35:38 -07:00
|
|
|
}
|
2009-07-28 13:59:39 -06:00
|
|
|
type outi16 struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
maxi int16
|
|
|
|
mini int16
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
var o2 outi16
|
|
|
|
enc.Encode(it)
|
|
|
|
err = dec.Decode(&o2)
|
2009-07-28 13:59:39 -06:00
|
|
|
if err == nil || err.String() != `value for "maxi" out of range` {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("wrong overflow error for int16:", err)
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
2009-11-05 15:53:42 -07:00
|
|
|
it = inputT{
|
2009-07-28 13:59:39 -06:00
|
|
|
mini: math.MinInt16 - 1,
|
2009-12-15 16:35:38 -07:00
|
|
|
}
|
|
|
|
b.Reset()
|
|
|
|
enc.Encode(it)
|
|
|
|
err = dec.Decode(&o2)
|
2009-07-28 13:59:39 -06:00
|
|
|
if err == nil || err.String() != `value for "mini" out of range` {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("wrong underflow error for int16:", err)
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// int32
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
2009-11-05 15:53:42 -07:00
|
|
|
it = inputT{
|
2009-07-28 13:59:39 -06:00
|
|
|
maxi: math.MaxInt32 + 1,
|
2009-12-15 16:35:38 -07:00
|
|
|
}
|
2009-07-28 13:59:39 -06:00
|
|
|
type outi32 struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
maxi int32
|
|
|
|
mini int32
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
var o3 outi32
|
|
|
|
enc.Encode(it)
|
|
|
|
err = dec.Decode(&o3)
|
2009-07-28 13:59:39 -06:00
|
|
|
if err == nil || err.String() != `value for "maxi" out of range` {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("wrong overflow error for int32:", err)
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
2009-11-05 15:53:42 -07:00
|
|
|
it = inputT{
|
2009-07-28 13:59:39 -06:00
|
|
|
mini: math.MinInt32 - 1,
|
2009-12-15 16:35:38 -07:00
|
|
|
}
|
|
|
|
b.Reset()
|
|
|
|
enc.Encode(it)
|
|
|
|
err = dec.Decode(&o3)
|
2009-07-28 13:59:39 -06:00
|
|
|
if err == nil || err.String() != `value for "mini" out of range` {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("wrong underflow error for int32:", err)
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// uint8
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
2009-11-05 15:53:42 -07:00
|
|
|
it = inputT{
|
2009-07-28 13:59:39 -06:00
|
|
|
maxu: math.MaxUint8 + 1,
|
2009-12-15 16:35:38 -07:00
|
|
|
}
|
2009-07-28 13:59:39 -06:00
|
|
|
type outu8 struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
maxu uint8
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
var o4 outu8
|
|
|
|
enc.Encode(it)
|
|
|
|
err = dec.Decode(&o4)
|
2009-07-28 13:59:39 -06:00
|
|
|
if err == nil || err.String() != `value for "maxu" out of range` {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("wrong overflow error for uint8:", err)
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// uint16
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
2009-11-05 15:53:42 -07:00
|
|
|
it = inputT{
|
2009-07-28 13:59:39 -06:00
|
|
|
maxu: math.MaxUint16 + 1,
|
2009-12-15 16:35:38 -07:00
|
|
|
}
|
2009-07-28 13:59:39 -06:00
|
|
|
type outu16 struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
maxu uint16
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
var o5 outu16
|
|
|
|
enc.Encode(it)
|
|
|
|
err = dec.Decode(&o5)
|
2009-07-28 13:59:39 -06:00
|
|
|
if err == nil || err.String() != `value for "maxu" out of range` {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("wrong overflow error for uint16:", err)
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// uint32
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
2009-11-05 15:53:42 -07:00
|
|
|
it = inputT{
|
2009-07-28 13:59:39 -06:00
|
|
|
maxu: math.MaxUint32 + 1,
|
2009-12-15 16:35:38 -07:00
|
|
|
}
|
2009-07-28 13:59:39 -06:00
|
|
|
type outu32 struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
maxu uint32
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
var o6 outu32
|
|
|
|
enc.Encode(it)
|
|
|
|
err = dec.Decode(&o6)
|
2009-07-28 13:59:39 -06:00
|
|
|
if err == nil || err.String() != `value for "maxu" out of range` {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("wrong overflow error for uint32:", err)
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// float32
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
2009-11-05 15:53:42 -07:00
|
|
|
it = inputT{
|
2009-07-28 13:59:39 -06:00
|
|
|
maxf: math.MaxFloat32 * 2,
|
2009-12-15 16:35:38 -07:00
|
|
|
}
|
2009-07-28 13:59:39 -06:00
|
|
|
type outf32 struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
maxf float32
|
|
|
|
minf float32
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
var o7 outf32
|
|
|
|
enc.Encode(it)
|
|
|
|
err = dec.Decode(&o7)
|
2009-07-28 13:59:39 -06:00
|
|
|
if err == nil || err.String() != `value for "maxf" out of range` {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("wrong overflow error for float32:", err)
|
2009-07-28 13:59:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-09 15:33:43 -06:00
|
|
|
func TestNesting(t *testing.T) {
|
|
|
|
type RT struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a string
|
|
|
|
next *RT
|
|
|
|
}
|
|
|
|
rt := new(RT)
|
|
|
|
rt.a = "level1"
|
|
|
|
rt.next = new(RT)
|
|
|
|
rt.next.a = "level2"
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
NewEncoder(b).Encode(rt)
|
|
|
|
var drt RT
|
|
|
|
dec := NewDecoder(b)
|
|
|
|
err := dec.Decode(&drt)
|
2009-11-17 00:32:30 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("decoder error:", err)
|
|
|
|
}
|
2009-07-09 15:33:43 -06:00
|
|
|
if drt.a != rt.a {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("nesting: encode expected %v got %v", *rt, drt)
|
2009-07-09 15:33:43 -06:00
|
|
|
}
|
|
|
|
if drt.next == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("nesting: recursion failed")
|
2009-07-09 15:33:43 -06:00
|
|
|
}
|
|
|
|
if drt.next.a != rt.next.a {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("nesting: encode expected %v got %v", *rt.next, *drt.next)
|
2009-07-09 15:33:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-02 09:21:42 -06:00
|
|
|
// These three structures have the same data with different indirections
|
|
|
|
type T0 struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a int
|
|
|
|
b int
|
|
|
|
c int
|
|
|
|
d int
|
2009-07-02 09:21:42 -06:00
|
|
|
}
|
|
|
|
type T1 struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a int
|
|
|
|
b *int
|
|
|
|
c **int
|
|
|
|
d ***int
|
2009-07-02 09:21:42 -06:00
|
|
|
}
|
|
|
|
type T2 struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a ***int
|
|
|
|
b **int
|
|
|
|
c *int
|
|
|
|
d int
|
2009-07-02 09:21:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoIndirection(t *testing.T) {
|
2009-07-02 10:22:38 -06:00
|
|
|
// First transfer t1 into t0
|
2009-12-15 16:35:38 -07:00
|
|
|
var t1 T1
|
|
|
|
t1.a = 17
|
|
|
|
t1.b = new(int)
|
|
|
|
*t1.b = 177
|
|
|
|
t1.c = new(*int)
|
|
|
|
*t1.c = new(int)
|
|
|
|
**t1.c = 1777
|
|
|
|
t1.d = new(**int)
|
|
|
|
*t1.d = new(*int)
|
|
|
|
**t1.d = new(int)
|
|
|
|
***t1.d = 17777
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
enc := NewEncoder(b)
|
|
|
|
enc.Encode(t1)
|
|
|
|
dec := NewDecoder(b)
|
|
|
|
var t0 T0
|
|
|
|
dec.Decode(&t0)
|
2009-07-02 09:21:42 -06:00
|
|
|
if t0.a != 17 || t0.b != 177 || t0.c != 1777 || t0.d != 17777 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("t1->t0: expected {17 177 1777 17777}; got %v", t0)
|
2009-07-02 09:21:42 -06:00
|
|
|
}
|
|
|
|
|
2009-07-02 10:22:38 -06:00
|
|
|
// Now transfer t2 into t0
|
2009-12-15 16:35:38 -07:00
|
|
|
var t2 T2
|
|
|
|
t2.d = 17777
|
|
|
|
t2.c = new(int)
|
|
|
|
*t2.c = 1777
|
|
|
|
t2.b = new(*int)
|
|
|
|
*t2.b = new(int)
|
|
|
|
**t2.b = 177
|
|
|
|
t2.a = new(**int)
|
|
|
|
*t2.a = new(*int)
|
|
|
|
**t2.a = new(int)
|
|
|
|
***t2.a = 17
|
|
|
|
b.Reset()
|
|
|
|
enc.Encode(t2)
|
|
|
|
t0 = T0{}
|
|
|
|
dec.Decode(&t0)
|
2009-07-02 09:21:42 -06:00
|
|
|
if t0.a != 17 || t0.b != 177 || t0.c != 1777 || t0.d != 17777 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("t2->t0 expected {17 177 1777 17777}; got %v", t0)
|
2009-07-02 09:21:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now transfer t0 into t1
|
2009-12-15 16:35:38 -07:00
|
|
|
t0 = T0{17, 177, 1777, 17777}
|
|
|
|
b.Reset()
|
|
|
|
enc.Encode(t0)
|
|
|
|
t1 = T1{}
|
|
|
|
dec.Decode(&t1)
|
2009-07-02 09:21:42 -06:00
|
|
|
if t1.a != 17 || *t1.b != 177 || **t1.c != 1777 || ***t1.d != 17777 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("t0->t1 expected {17 177 1777 17777}; got {%d %d %d %d}", t1.a, *t1.b, **t1.c, ***t1.d)
|
2009-07-02 09:21:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now transfer t0 into t2
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
enc.Encode(t0)
|
|
|
|
t2 = T2{}
|
|
|
|
dec.Decode(&t2)
|
2009-07-02 09:21:42 -06:00
|
|
|
if ***t2.a != 17 || **t2.b != 177 || *t2.c != 1777 || t2.d != 17777 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("t0->t2 expected {17 177 1777 17777}; got {%d %d %d %d}", ***t2.a, **t2.b, *t2.c, t2.d)
|
2009-07-02 09:21:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now do t2 again but without pre-allocated pointers.
|
2009-12-15 16:35:38 -07:00
|
|
|
b.Reset()
|
|
|
|
enc.Encode(t0)
|
|
|
|
***t2.a = 0
|
|
|
|
**t2.b = 0
|
|
|
|
*t2.c = 0
|
|
|
|
t2.d = 0
|
|
|
|
dec.Decode(&t2)
|
2009-07-02 09:21:42 -06:00
|
|
|
if ***t2.a != 17 || **t2.b != 177 || *t2.c != 1777 || t2.d != 17777 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("t0->t2 expected {17 177 1777 17777}; got {%d %d %d %d}", ***t2.a, **t2.b, *t2.c, t2.d)
|
2009-07-02 09:21:42 -06:00
|
|
|
}
|
|
|
|
}
|
2009-07-16 18:55:16 -06:00
|
|
|
|
|
|
|
type RT0 struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a int
|
|
|
|
b string
|
|
|
|
c float
|
2009-07-16 18:55:16 -06:00
|
|
|
}
|
|
|
|
type RT1 struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
c float
|
|
|
|
b string
|
|
|
|
a int
|
|
|
|
notSet string
|
2009-07-16 18:55:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReorderedFields(t *testing.T) {
|
2009-12-15 16:35:38 -07:00
|
|
|
var rt0 RT0
|
|
|
|
rt0.a = 17
|
|
|
|
rt0.b = "hello"
|
|
|
|
rt0.c = 3.14159
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
NewEncoder(b).Encode(rt0)
|
|
|
|
dec := NewDecoder(b)
|
|
|
|
var rt1 RT1
|
2009-07-16 18:55:16 -06:00
|
|
|
// Wire type is RT0, local type is RT1.
|
2009-12-15 16:35:38 -07:00
|
|
|
err := dec.Decode(&rt1)
|
2009-11-17 00:32:30 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error("decode error:", err)
|
|
|
|
}
|
2009-07-16 18:55:16 -06:00
|
|
|
if rt0.a != rt1.a || rt0.b != rt1.b || rt0.c != rt1.c {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("rt1->rt0: expected %v; got %v", rt0, rt1)
|
2009-07-16 18:55:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Like an RT0 but with fields we'll ignore on the decode side.
|
|
|
|
type IT0 struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
a int64
|
|
|
|
b string
|
|
|
|
ignore_d []int
|
|
|
|
ignore_e [3]float
|
|
|
|
ignore_f bool
|
|
|
|
ignore_g string
|
|
|
|
ignore_h []byte
|
|
|
|
ignore_i *RT1
|
|
|
|
c float
|
2009-07-16 18:55:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIgnoredFields(t *testing.T) {
|
2009-12-15 16:35:38 -07:00
|
|
|
var it0 IT0
|
|
|
|
it0.a = 17
|
|
|
|
it0.b = "hello"
|
|
|
|
it0.c = 3.14159
|
|
|
|
it0.ignore_d = []int{1, 2, 3}
|
|
|
|
it0.ignore_e[0] = 1.0
|
|
|
|
it0.ignore_e[1] = 2.0
|
|
|
|
it0.ignore_e[2] = 3.0
|
|
|
|
it0.ignore_f = true
|
|
|
|
it0.ignore_g = "pay no attention"
|
2010-02-25 17:01:29 -07:00
|
|
|
it0.ignore_h = []byte("to the curtain")
|
2009-12-15 16:35:38 -07:00
|
|
|
it0.ignore_i = &RT1{3.1, "hi", 7, "hello"}
|
|
|
|
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
NewEncoder(b).Encode(it0)
|
|
|
|
dec := NewDecoder(b)
|
|
|
|
var rt1 RT1
|
2009-07-16 18:55:16 -06:00
|
|
|
// Wire type is IT0, local type is RT1.
|
2009-12-15 16:35:38 -07:00
|
|
|
err := dec.Decode(&rt1)
|
2009-07-17 12:38:31 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("error: ", err)
|
2009-07-17 12:38:31 -06:00
|
|
|
}
|
2009-07-16 18:55:16 -06:00
|
|
|
if int(it0.a) != rt1.a || it0.b != rt1.b || it0.c != rt1.c {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("rt1->rt0: expected %v; got %v", it0, rt1)
|
2009-07-16 18:55:16 -06:00
|
|
|
}
|
|
|
|
}
|
2009-07-29 18:24:25 -06:00
|
|
|
|
|
|
|
type Bad0 struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
inter interface{}
|
|
|
|
c float
|
2009-07-29 18:24:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidField(t *testing.T) {
|
2009-12-15 16:35:38 -07:00
|
|
|
var bad0 Bad0
|
|
|
|
bad0.inter = 17
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
err := encode(b, &bad0)
|
2009-07-29 18:24:25 -06:00
|
|
|
if err == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("expected error; got none")
|
2009-07-29 18:24:25 -06:00
|
|
|
} else if strings.Index(err.String(), "interface") < 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("expected type error; got", err)
|
2009-07-29 18:24:25 -06:00
|
|
|
}
|
|
|
|
}
|