gofmt-ify gob

(the one-line struct types used in composite literals will become
one line again in another cleanup round; don't worry about them now)

R=r
http://go/go-review/1016056
This commit is contained in:
Robert Griesemer 2009-11-05 14:53:42 -08:00
parent 790c9b59d6
commit f65e42d039
3 changed files with 328 additions and 245 deletions

View file

@ -19,21 +19,22 @@ type EncodeT struct {
x uint64; x uint64;
b []byte; b []byte;
} }
var encodeT = []EncodeT {
EncodeT{ 0x00, []byte{0x00} }, var encodeT = []EncodeT{
EncodeT{ 0x0F, []byte{0x0F} }, EncodeT{0x00, []byte{0x00}},
EncodeT{ 0xFF, []byte{0xFF, 0xFF} }, EncodeT{0x0F, []byte{0x0F}},
EncodeT{ 0xFFFF, []byte{0xFE, 0xFF, 0xFF} }, EncodeT{0xFF, []byte{0xFF, 0xFF}},
EncodeT{ 0xFFFFFF, []byte{0xFD, 0xFF, 0xFF, 0xFF} }, EncodeT{0xFFFF, []byte{0xFE, 0xFF, 0xFF}},
EncodeT{ 0xFFFFFFFF, []byte{0xFC, 0xFF, 0xFF, 0xFF, 0xFF} }, EncodeT{0xFFFFFF, []byte{0xFD, 0xFF, 0xFF, 0xFF}},
EncodeT{ 0xFFFFFFFFFF, []byte{0xFB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} }, EncodeT{0xFFFFFFFF, []byte{0xFC, 0xFF, 0xFF, 0xFF, 0xFF}},
EncodeT{ 0xFFFFFFFFFFFF, []byte{0xFA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} }, EncodeT{0xFFFFFFFFFF, []byte{0xFB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
EncodeT{ 0xFFFFFFFFFFFFFF, []byte{0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} }, EncodeT{0xFFFFFFFFFFFF, []byte{0xFA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
EncodeT{ 0xFFFFFFFFFFFFFFFF, []byte{0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} }, EncodeT{0xFFFFFFFFFFFFFF, []byte{0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
EncodeT{ 0x1111, []byte{0xFE, 0x11, 0x11} }, EncodeT{0xFFFFFFFFFFFFFFFF, []byte{0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
EncodeT{ 0x1111111111111111, []byte{0xF8, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11} }, EncodeT{0x1111, []byte{0xFE, 0x11, 0x11}},
EncodeT{ 0x8888888888888888, []byte{0xF8, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88} }, EncodeT{0x1111111111111111, []byte{0xF8, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}},
EncodeT{ 1<<63, []byte{0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} }, EncodeT{0x8888888888888888, []byte{0xF8, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88}},
EncodeT{1<<63, []byte{0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
} }
@ -46,28 +47,28 @@ func TestUintCodec(t *testing.T) {
b.Reset(); b.Reset();
encodeUint(encState, tt.x); encodeUint(encState, tt.x);
if encState.err != nil { if encState.err != nil {
t.Error("encodeUint:", tt.x, encState.err) t.Error("encodeUint:", tt.x, encState.err);
} }
if !bytes.Equal(tt.b, b.Bytes()) { if !bytes.Equal(tt.b, b.Bytes()) {
t.Errorf("encodeUint: %#x encode: expected % x got % x", tt.x, tt.b, b.Bytes()) t.Errorf("encodeUint: %#x encode: expected % x got % x", tt.x, tt.b, b.Bytes());
} }
} }
decState := newDecodeState(b); decState := newDecodeState(b);
for u := uint64(0); ; u = (u+1) * 7 { for u := uint64(0); ; u = (u+1)*7 {
b.Reset(); b.Reset();
encodeUint(encState, u); encodeUint(encState, u);
if encState.err != nil { if encState.err != nil {
t.Error("encodeUint:", u, encState.err) t.Error("encodeUint:", u, encState.err);
} }
v := decodeUint(decState); v := decodeUint(decState);
if decState.err != nil { if decState.err != nil {
t.Error("DecodeUint:", u, decState.err) t.Error("DecodeUint:", u, decState.err);
} }
if u != v { if u != v {
t.Errorf("Encode/Decode: sent %#x received %#x\n", u, v) t.Errorf("Encode/Decode: sent %#x received %#x\n", u, v);
} }
if u & (1<<63) != 0 { if u&(1<<63) != 0 {
break break;
} }
} }
} }
@ -78,32 +79,32 @@ func verifyInt(i int64, t *testing.T) {
encState.b = b; encState.b = b;
encodeInt(encState, i); encodeInt(encState, i);
if encState.err != nil { if encState.err != nil {
t.Error("encodeInt:", i, encState.err) t.Error("encodeInt:", i, encState.err);
} }
decState := newDecodeState(b); decState := newDecodeState(b);
decState.buf = make([]byte, 8); decState.buf = make([]byte, 8);
j := decodeInt(decState); j := decodeInt(decState);
if decState.err != nil { if decState.err != nil {
t.Error("DecodeInt:", i, decState.err) t.Error("DecodeInt:", i, decState.err);
} }
if i != j { if i != j {
t.Errorf("Encode/Decode: sent %#x received %#x\n", uint64(i), uint64(j)) t.Errorf("Encode/Decode: sent %#x received %#x\n", uint64(i), uint64(j));
} }
} }
// Test basic encode/decode routines for signed integers // Test basic encode/decode routines for signed integers
func TestIntCodec(t *testing.T) { func TestIntCodec(t *testing.T) {
for u := uint64(0); ; u = (u+1) * 7 { for u := uint64(0); ; u = (u+1)*7 {
// Do positive and negative values // Do positive and negative values
i := int64(u); i := int64(u);
verifyInt(i, t); verifyInt(i, t);
verifyInt(-i, t); verifyInt(-i, t);
verifyInt(^i, t); verifyInt(^i, t);
if u & (1<<63) != 0 { if u&(1<<63) != 0 {
break break;
} }
} }
verifyInt(-1<<63, t); // a tricky case verifyInt(-1 << 63, t); // a tricky case
} }
// The result of encoding a true boolean with field number 7 // The result of encoding a true boolean with field number 7
@ -130,192 +131,224 @@ func TestScalarEncInstructions(t *testing.T) {
// bool // bool
{ {
data := struct { a bool } { true }; data := struct {
instr := &encInstr{ encBool, 6, 0, 0 }; a bool;
}{true};
instr := &encInstr{encBool, 6, 0, 0};
state := newencoderState(b); state := newencoderState(b);
instr.op(instr, state, unsafe.Pointer(&data)); instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(boolResult, b.Bytes()) { if !bytes.Equal(boolResult, b.Bytes()) {
t.Errorf("bool enc instructions: expected % x got % x", boolResult, b.Bytes()) t.Errorf("bool enc instructions: expected % x got % x", boolResult, b.Bytes());
} }
} }
// int // int
{ {
b.Reset(); b.Reset();
data := struct { a int } { 17 }; data := struct {
instr := &encInstr{ encInt, 6, 0, 0 }; a int;
}{17};
instr := &encInstr{encInt, 6, 0, 0};
state := newencoderState(b); state := newencoderState(b);
instr.op(instr, state, unsafe.Pointer(&data)); instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(signedResult, b.Bytes()) { if !bytes.Equal(signedResult, b.Bytes()) {
t.Errorf("int enc instructions: expected % x got % x", signedResult, b.Bytes()) t.Errorf("int enc instructions: expected % x got % x", signedResult, b.Bytes());
} }
} }
// uint // uint
{ {
b.Reset(); b.Reset();
data := struct { a uint } { 17 }; data := struct {
instr := &encInstr{ encUint, 6, 0, 0 }; a uint;
}{17};
instr := &encInstr{encUint, 6, 0, 0};
state := newencoderState(b); state := newencoderState(b);
instr.op(instr, state, unsafe.Pointer(&data)); instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(unsignedResult, b.Bytes()) { if !bytes.Equal(unsignedResult, b.Bytes()) {
t.Errorf("uint enc instructions: expected % x got % x", unsignedResult, b.Bytes()) t.Errorf("uint enc instructions: expected % x got % x", unsignedResult, b.Bytes());
} }
} }
// int8 // int8
{ {
b.Reset(); b.Reset();
data := struct { a int8 } { 17 }; data := struct {
instr := &encInstr{ encInt8, 6, 0, 0 }; a int8;
}{17};
instr := &encInstr{encInt8, 6, 0, 0};
state := newencoderState(b); state := newencoderState(b);
instr.op(instr, state, unsafe.Pointer(&data)); instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(signedResult, b.Bytes()) { if !bytes.Equal(signedResult, b.Bytes()) {
t.Errorf("int8 enc instructions: expected % x got % x", signedResult, b.Bytes()) t.Errorf("int8 enc instructions: expected % x got % x", signedResult, b.Bytes());
} }
} }
// uint8 // uint8
{ {
b.Reset(); b.Reset();
data := struct { a uint8 } { 17 }; data := struct {
instr := &encInstr{ encUint8, 6, 0, 0 }; a uint8;
}{17};
instr := &encInstr{encUint8, 6, 0, 0};
state := newencoderState(b); state := newencoderState(b);
instr.op(instr, state, unsafe.Pointer(&data)); instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(unsignedResult, b.Bytes()) { if !bytes.Equal(unsignedResult, b.Bytes()) {
t.Errorf("uint8 enc instructions: expected % x got % x", unsignedResult, b.Bytes()) t.Errorf("uint8 enc instructions: expected % x got % x", unsignedResult, b.Bytes());
} }
} }
// int16 // int16
{ {
b.Reset(); b.Reset();
data := struct { a int16 } { 17 }; data := struct {
instr := &encInstr{ encInt16, 6, 0, 0 }; a int16;
}{17};
instr := &encInstr{encInt16, 6, 0, 0};
state := newencoderState(b); state := newencoderState(b);
instr.op(instr, state, unsafe.Pointer(&data)); instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(signedResult, b.Bytes()) { if !bytes.Equal(signedResult, b.Bytes()) {
t.Errorf("int16 enc instructions: expected % x got % x", signedResult, b.Bytes()) t.Errorf("int16 enc instructions: expected % x got % x", signedResult, b.Bytes());
} }
} }
// uint16 // uint16
{ {
b.Reset(); b.Reset();
data := struct { a uint16 } { 17 }; data := struct {
instr := &encInstr{ encUint16, 6, 0, 0 }; a uint16;
}{17};
instr := &encInstr{encUint16, 6, 0, 0};
state := newencoderState(b); state := newencoderState(b);
instr.op(instr, state, unsafe.Pointer(&data)); instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(unsignedResult, b.Bytes()) { if !bytes.Equal(unsignedResult, b.Bytes()) {
t.Errorf("uint16 enc instructions: expected % x got % x", unsignedResult, b.Bytes()) t.Errorf("uint16 enc instructions: expected % x got % x", unsignedResult, b.Bytes());
} }
} }
// int32 // int32
{ {
b.Reset(); b.Reset();
data := struct { a int32 } { 17 }; data := struct {
instr := &encInstr{ encInt32, 6, 0, 0 }; a int32;
}{17};
instr := &encInstr{encInt32, 6, 0, 0};
state := newencoderState(b); state := newencoderState(b);
instr.op(instr, state, unsafe.Pointer(&data)); instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(signedResult, b.Bytes()) { if !bytes.Equal(signedResult, b.Bytes()) {
t.Errorf("int32 enc instructions: expected % x got % x", signedResult, b.Bytes()) t.Errorf("int32 enc instructions: expected % x got % x", signedResult, b.Bytes());
} }
} }
// uint32 // uint32
{ {
b.Reset(); b.Reset();
data := struct { a uint32 } { 17 }; data := struct {
instr := &encInstr{ encUint32, 6, 0, 0 }; a uint32;
}{17};
instr := &encInstr{encUint32, 6, 0, 0};
state := newencoderState(b); state := newencoderState(b);
instr.op(instr, state, unsafe.Pointer(&data)); instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(unsignedResult, b.Bytes()) { if !bytes.Equal(unsignedResult, b.Bytes()) {
t.Errorf("uint32 enc instructions: expected % x got % x", unsignedResult, b.Bytes()) t.Errorf("uint32 enc instructions: expected % x got % x", unsignedResult, b.Bytes());
} }
} }
// int64 // int64
{ {
b.Reset(); b.Reset();
data := struct { a int64 } { 17 }; data := struct {
instr := &encInstr{ encInt64, 6, 0, 0 }; a int64;
}{17};
instr := &encInstr{encInt64, 6, 0, 0};
state := newencoderState(b); state := newencoderState(b);
instr.op(instr, state, unsafe.Pointer(&data)); instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(signedResult, b.Bytes()) { if !bytes.Equal(signedResult, b.Bytes()) {
t.Errorf("int64 enc instructions: expected % x got % x", signedResult, b.Bytes()) t.Errorf("int64 enc instructions: expected % x got % x", signedResult, b.Bytes());
} }
} }
// uint64 // uint64
{ {
b.Reset(); b.Reset();
data := struct { a uint64 } { 17 }; data := struct {
instr := &encInstr{ encUint64, 6, 0, 0 }; a uint64;
}{17};
instr := &encInstr{encUint64, 6, 0, 0};
state := newencoderState(b); state := newencoderState(b);
instr.op(instr, state, unsafe.Pointer(&data)); instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(unsignedResult, b.Bytes()) { if !bytes.Equal(unsignedResult, b.Bytes()) {
t.Errorf("uint64 enc instructions: expected % x got % x", unsignedResult, b.Bytes()) t.Errorf("uint64 enc instructions: expected % x got % x", unsignedResult, b.Bytes());
} }
} }
// float // float
{ {
b.Reset(); b.Reset();
data := struct { a float } { 17 }; data := struct {
instr := &encInstr{ encFloat, 6, 0, 0 }; a float;
}{17};
instr := &encInstr{encFloat, 6, 0, 0};
state := newencoderState(b); state := newencoderState(b);
instr.op(instr, state, unsafe.Pointer(&data)); instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(floatResult, b.Bytes()) { if !bytes.Equal(floatResult, b.Bytes()) {
t.Errorf("float enc instructions: expected % x got % x", floatResult, b.Bytes()) t.Errorf("float enc instructions: expected % x got % x", floatResult, b.Bytes());
} }
} }
// float32 // float32
{ {
b.Reset(); b.Reset();
data := struct { a float32 } { 17 }; data := struct {
instr := &encInstr{ encFloat32, 6, 0, 0 }; a float32;
}{17};
instr := &encInstr{encFloat32, 6, 0, 0};
state := newencoderState(b); state := newencoderState(b);
instr.op(instr, state, unsafe.Pointer(&data)); instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(floatResult, b.Bytes()) { if !bytes.Equal(floatResult, b.Bytes()) {
t.Errorf("float32 enc instructions: expected % x got % x", floatResult, b.Bytes()) t.Errorf("float32 enc instructions: expected % x got % x", floatResult, b.Bytes());
} }
} }
// float64 // float64
{ {
b.Reset(); b.Reset();
data := struct { a float64 } { 17 }; data := struct {
instr := &encInstr{ encFloat64, 6, 0, 0 }; a float64;
}{17};
instr := &encInstr{encFloat64, 6, 0, 0};
state := newencoderState(b); state := newencoderState(b);
instr.op(instr, state, unsafe.Pointer(&data)); instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(floatResult, b.Bytes()) { if !bytes.Equal(floatResult, b.Bytes()) {
t.Errorf("float64 enc instructions: expected % x got % x", floatResult, b.Bytes()) t.Errorf("float64 enc instructions: expected % x got % x", floatResult, b.Bytes());
} }
} }
// bytes == []uint8 // bytes == []uint8
{ {
b.Reset(); b.Reset();
data := struct { a []byte } { strings.Bytes("hello") }; data := struct {
instr := &encInstr{ encUint8Array, 6, 0, 0 }; a []byte;
}{strings.Bytes("hello")};
instr := &encInstr{encUint8Array, 6, 0, 0};
state := newencoderState(b); state := newencoderState(b);
instr.op(instr, state, unsafe.Pointer(&data)); instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(bytesResult, b.Bytes()) { if !bytes.Equal(bytesResult, b.Bytes()) {
t.Errorf("bytes enc instructions: expected % x got % x", bytesResult, b.Bytes()) t.Errorf("bytes enc instructions: expected % x got % x", bytesResult, b.Bytes());
} }
} }
// string // string
{ {
b.Reset(); b.Reset();
data := struct { a string } { "hello" }; data := struct {
instr := &encInstr{ encString, 6, 0, 0 }; a string;
}{"hello"};
instr := &encInstr{encString, 6, 0, 0};
state := newencoderState(b); state := newencoderState(b);
instr.op(instr, state, unsafe.Pointer(&data)); instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(bytesResult, b.Bytes()) { if !bytes.Equal(bytesResult, b.Bytes()) {
t.Errorf("string enc instructions: expected % x got % x", bytesResult, b.Bytes()) t.Errorf("string enc instructions: expected % x got % x", bytesResult, b.Bytes());
} }
} }
} }
@ -345,199 +378,233 @@ func TestScalarDecInstructions(t *testing.T) {
// bool // bool
{ {
var data struct { a bool }; var data struct {
instr := &decInstr{ decBool, 6, 0, 0, ovfl }; a bool;
}
instr := &decInstr{decBool, 6, 0, 0, ovfl};
state := newDecodeStateFromData(boolResult); state := newDecodeStateFromData(boolResult);
execDec("bool", instr, state, t, unsafe.Pointer(&data)); execDec("bool", instr, state, t, unsafe.Pointer(&data));
if data.a != true { if data.a != true {
t.Errorf("bool a = %v not true", data.a) t.Errorf("bool a = %v not true", data.a);
} }
} }
// int // int
{ {
var data struct { a int }; var data struct {
instr := &decInstr{ decOpMap[valueKind(data.a)], 6, 0, 0, ovfl }; a int;
}
instr := &decInstr{decOpMap[valueKind(data.a)], 6, 0, 0, ovfl};
state := newDecodeStateFromData(signedResult); state := newDecodeStateFromData(signedResult);
execDec("int", instr, state, t, unsafe.Pointer(&data)); execDec("int", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 { if data.a != 17 {
t.Errorf("int a = %v not 17", data.a) t.Errorf("int a = %v not 17", data.a);
} }
} }
// uint // uint
{ {
var data struct { a uint }; var data struct {
instr := &decInstr{ decOpMap[valueKind(data.a)], 6, 0, 0, ovfl }; a uint;
}
instr := &decInstr{decOpMap[valueKind(data.a)], 6, 0, 0, ovfl};
state := newDecodeStateFromData(unsignedResult); state := newDecodeStateFromData(unsignedResult);
execDec("uint", instr, state, t, unsafe.Pointer(&data)); execDec("uint", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 { if data.a != 17 {
t.Errorf("uint a = %v not 17", data.a) t.Errorf("uint a = %v not 17", data.a);
} }
} }
// int8 // int8
{ {
var data struct { a int8 }; var data struct {
instr := &decInstr{ decInt8, 6, 0, 0, ovfl }; a int8;
}
instr := &decInstr{decInt8, 6, 0, 0, ovfl};
state := newDecodeStateFromData(signedResult); state := newDecodeStateFromData(signedResult);
execDec("int8", instr, state, t, unsafe.Pointer(&data)); execDec("int8", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 { if data.a != 17 {
t.Errorf("int8 a = %v not 17", data.a) t.Errorf("int8 a = %v not 17", data.a);
} }
} }
// uint8 // uint8
{ {
var data struct { a uint8 }; var data struct {
instr := &decInstr{ decUint8, 6, 0, 0, ovfl }; a uint8;
}
instr := &decInstr{decUint8, 6, 0, 0, ovfl};
state := newDecodeStateFromData(unsignedResult); state := newDecodeStateFromData(unsignedResult);
execDec("uint8", instr, state, t, unsafe.Pointer(&data)); execDec("uint8", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 { if data.a != 17 {
t.Errorf("uint8 a = %v not 17", data.a) t.Errorf("uint8 a = %v not 17", data.a);
} }
} }
// int16 // int16
{ {
var data struct { a int16 }; var data struct {
instr := &decInstr{ decInt16, 6, 0, 0, ovfl }; a int16;
}
instr := &decInstr{decInt16, 6, 0, 0, ovfl};
state := newDecodeStateFromData(signedResult); state := newDecodeStateFromData(signedResult);
execDec("int16", instr, state, t, unsafe.Pointer(&data)); execDec("int16", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 { if data.a != 17 {
t.Errorf("int16 a = %v not 17", data.a) t.Errorf("int16 a = %v not 17", data.a);
} }
} }
// uint16 // uint16
{ {
var data struct { a uint16 }; var data struct {
instr := &decInstr{ decUint16, 6, 0, 0, ovfl }; a uint16;
}
instr := &decInstr{decUint16, 6, 0, 0, ovfl};
state := newDecodeStateFromData(unsignedResult); state := newDecodeStateFromData(unsignedResult);
execDec("uint16", instr, state, t, unsafe.Pointer(&data)); execDec("uint16", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 { if data.a != 17 {
t.Errorf("uint16 a = %v not 17", data.a) t.Errorf("uint16 a = %v not 17", data.a);
} }
} }
// int32 // int32
{ {
var data struct { a int32 }; var data struct {
instr := &decInstr{ decInt32, 6, 0, 0, ovfl }; a int32;
}
instr := &decInstr{decInt32, 6, 0, 0, ovfl};
state := newDecodeStateFromData(signedResult); state := newDecodeStateFromData(signedResult);
execDec("int32", instr, state, t, unsafe.Pointer(&data)); execDec("int32", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 { if data.a != 17 {
t.Errorf("int32 a = %v not 17", data.a) t.Errorf("int32 a = %v not 17", data.a);
} }
} }
// uint32 // uint32
{ {
var data struct { a uint32 }; var data struct {
instr := &decInstr{ decUint32, 6, 0, 0, ovfl }; a uint32;
}
instr := &decInstr{decUint32, 6, 0, 0, ovfl};
state := newDecodeStateFromData(unsignedResult); state := newDecodeStateFromData(unsignedResult);
execDec("uint32", instr, state, t, unsafe.Pointer(&data)); execDec("uint32", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 { if data.a != 17 {
t.Errorf("uint32 a = %v not 17", data.a) t.Errorf("uint32 a = %v not 17", data.a);
} }
} }
// uintptr // uintptr
{ {
var data struct { a uintptr }; var data struct {
instr := &decInstr{ decOpMap[valueKind(data.a)], 6, 0, 0, ovfl }; a uintptr;
}
instr := &decInstr{decOpMap[valueKind(data.a)], 6, 0, 0, ovfl};
state := newDecodeStateFromData(unsignedResult); state := newDecodeStateFromData(unsignedResult);
execDec("uintptr", instr, state, t, unsafe.Pointer(&data)); execDec("uintptr", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 { if data.a != 17 {
t.Errorf("uintptr a = %v not 17", data.a) t.Errorf("uintptr a = %v not 17", data.a);
} }
} }
// int64 // int64
{ {
var data struct { a int64 }; var data struct {
instr := &decInstr{ decInt64, 6, 0, 0, ovfl }; a int64;
}
instr := &decInstr{decInt64, 6, 0, 0, ovfl};
state := newDecodeStateFromData(signedResult); state := newDecodeStateFromData(signedResult);
execDec("int64", instr, state, t, unsafe.Pointer(&data)); execDec("int64", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 { if data.a != 17 {
t.Errorf("int64 a = %v not 17", data.a) t.Errorf("int64 a = %v not 17", data.a);
} }
} }
// uint64 // uint64
{ {
var data struct { a uint64 }; var data struct {
instr := &decInstr{ decUint64, 6, 0, 0, ovfl }; a uint64;
}
instr := &decInstr{decUint64, 6, 0, 0, ovfl};
state := newDecodeStateFromData(unsignedResult); state := newDecodeStateFromData(unsignedResult);
execDec("uint64", instr, state, t, unsafe.Pointer(&data)); execDec("uint64", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 { if data.a != 17 {
t.Errorf("uint64 a = %v not 17", data.a) t.Errorf("uint64 a = %v not 17", data.a);
} }
} }
// float // float
{ {
var data struct { a float }; var data struct {
instr := &decInstr{ decOpMap[valueKind(data.a)], 6, 0, 0, ovfl }; a float;
}
instr := &decInstr{decOpMap[valueKind(data.a)], 6, 0, 0, ovfl};
state := newDecodeStateFromData(floatResult); state := newDecodeStateFromData(floatResult);
execDec("float", instr, state, t, unsafe.Pointer(&data)); execDec("float", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 { if data.a != 17 {
t.Errorf("float a = %v not 17", data.a) t.Errorf("float a = %v not 17", data.a);
} }
} }
// float32 // float32
{ {
var data struct { a float32 }; var data struct {
instr := &decInstr{ decFloat32, 6, 0, 0, ovfl }; a float32;
}
instr := &decInstr{decFloat32, 6, 0, 0, ovfl};
state := newDecodeStateFromData(floatResult); state := newDecodeStateFromData(floatResult);
execDec("float32", instr, state, t, unsafe.Pointer(&data)); execDec("float32", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 { if data.a != 17 {
t.Errorf("float32 a = %v not 17", data.a) t.Errorf("float32 a = %v not 17", data.a);
} }
} }
// float64 // float64
{ {
var data struct { a float64 }; var data struct {
instr := &decInstr{ decFloat64, 6, 0, 0, ovfl }; a float64;
}
instr := &decInstr{decFloat64, 6, 0, 0, ovfl};
state := newDecodeStateFromData(floatResult); state := newDecodeStateFromData(floatResult);
execDec("float64", instr, state, t, unsafe.Pointer(&data)); execDec("float64", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 { if data.a != 17 {
t.Errorf("float64 a = %v not 17", data.a) t.Errorf("float64 a = %v not 17", data.a);
} }
} }
// bytes == []uint8 // bytes == []uint8
{ {
var data struct { a []byte }; var data struct {
instr := &decInstr{ decUint8Array, 6, 0, 0, ovfl }; a []byte;
}
instr := &decInstr{decUint8Array, 6, 0, 0, ovfl};
state := newDecodeStateFromData(bytesResult); state := newDecodeStateFromData(bytesResult);
execDec("bytes", instr, state, t, unsafe.Pointer(&data)); execDec("bytes", instr, state, t, unsafe.Pointer(&data));
if string(data.a) != "hello" { if string(data.a) != "hello" {
t.Errorf(`bytes a = %q not "hello"`, string(data.a)) t.Errorf(`bytes a = %q not "hello"`, string(data.a));
} }
} }
// string // string
{ {
var data struct { a string }; var data struct {
instr := &decInstr{ decString, 6, 0, 0, ovfl }; a string;
}
instr := &decInstr{decString, 6, 0, 0, ovfl};
state := newDecodeStateFromData(bytesResult); state := newDecodeStateFromData(bytesResult);
execDec("bytes", instr, state, t, unsafe.Pointer(&data)); execDec("bytes", instr, state, t, unsafe.Pointer(&data));
if data.a != "hello" { if data.a != "hello" {
t.Errorf(`bytes a = %q not "hello"`, data.a) t.Errorf(`bytes a = %q not "hello"`, data.a);
} }
} }
} }
func TestEndToEnd(t *testing.T) { func TestEndToEnd(t *testing.T) {
type T2 struct { type T2 struct {
t string t string;
} }
s1 := "string1"; s1 := "string1";
s2 := "string2"; s2 := "string2";
type T1 struct { type T1 struct {
a, b,c int; a, b, c int;
n *[3]float; n *[3]float;
strs *[2]string; strs *[2]string;
int64s *[]int64; int64s *[]int64;
@ -580,7 +647,7 @@ func TestOverflow(t *testing.T) {
// int8 // int8
b.Reset(); b.Reset();
it = inputT { it = inputT{
maxi: math.MaxInt8 + 1, maxi: math.MaxInt8 + 1,
}; };
type outi8 struct { type outi8 struct {
@ -591,21 +658,21 @@ func TestOverflow(t *testing.T) {
encode(b, it); encode(b, it);
err = decode(b, id, &o1); err = decode(b, id, &o1);
if err == nil || err.String() != `value for "maxi" out of range` { if err == nil || err.String() != `value for "maxi" out of range` {
t.Error("wrong overflow error for int8:", err) t.Error("wrong overflow error for int8:", err);
} }
it = inputT { it = inputT{
mini: math.MinInt8 - 1, mini: math.MinInt8 - 1,
}; };
b.Reset(); b.Reset();
encode(b, it); encode(b, it);
err = decode(b, id, &o1); err = decode(b, id, &o1);
if err == nil || err.String() != `value for "mini" out of range` { if err == nil || err.String() != `value for "mini" out of range` {
t.Error("wrong underflow error for int8:", err) t.Error("wrong underflow error for int8:", err);
} }
// int16 // int16
b.Reset(); b.Reset();
it = inputT { it = inputT{
maxi: math.MaxInt16 + 1, maxi: math.MaxInt16 + 1,
}; };
type outi16 struct { type outi16 struct {
@ -616,21 +683,21 @@ func TestOverflow(t *testing.T) {
encode(b, it); encode(b, it);
err = decode(b, id, &o2); err = decode(b, id, &o2);
if err == nil || err.String() != `value for "maxi" out of range` { if err == nil || err.String() != `value for "maxi" out of range` {
t.Error("wrong overflow error for int16:", err) t.Error("wrong overflow error for int16:", err);
} }
it = inputT { it = inputT{
mini: math.MinInt16 - 1, mini: math.MinInt16 - 1,
}; };
b.Reset(); b.Reset();
encode(b, it); encode(b, it);
err = decode(b, id, &o2); err = decode(b, id, &o2);
if err == nil || err.String() != `value for "mini" out of range` { if err == nil || err.String() != `value for "mini" out of range` {
t.Error("wrong underflow error for int16:", err) t.Error("wrong underflow error for int16:", err);
} }
// int32 // int32
b.Reset(); b.Reset();
it = inputT { it = inputT{
maxi: math.MaxInt32 + 1, maxi: math.MaxInt32 + 1,
}; };
type outi32 struct { type outi32 struct {
@ -641,21 +708,21 @@ func TestOverflow(t *testing.T) {
encode(b, it); encode(b, it);
err = decode(b, id, &o3); err = decode(b, id, &o3);
if err == nil || err.String() != `value for "maxi" out of range` { if err == nil || err.String() != `value for "maxi" out of range` {
t.Error("wrong overflow error for int32:", err) t.Error("wrong overflow error for int32:", err);
} }
it = inputT { it = inputT{
mini: math.MinInt32 - 1, mini: math.MinInt32 - 1,
}; };
b.Reset(); b.Reset();
encode(b, it); encode(b, it);
err = decode(b, id, &o3); err = decode(b, id, &o3);
if err == nil || err.String() != `value for "mini" out of range` { if err == nil || err.String() != `value for "mini" out of range` {
t.Error("wrong underflow error for int32:", err) t.Error("wrong underflow error for int32:", err);
} }
// uint8 // uint8
b.Reset(); b.Reset();
it = inputT { it = inputT{
maxu: math.MaxUint8 + 1, maxu: math.MaxUint8 + 1,
}; };
type outu8 struct { type outu8 struct {
@ -665,12 +732,12 @@ func TestOverflow(t *testing.T) {
encode(b, it); encode(b, it);
err = decode(b, id, &o4); err = decode(b, id, &o4);
if err == nil || err.String() != `value for "maxu" out of range` { if err == nil || err.String() != `value for "maxu" out of range` {
t.Error("wrong overflow error for uint8:", err) t.Error("wrong overflow error for uint8:", err);
} }
// uint16 // uint16
b.Reset(); b.Reset();
it = inputT { it = inputT{
maxu: math.MaxUint16 + 1, maxu: math.MaxUint16 + 1,
}; };
type outu16 struct { type outu16 struct {
@ -680,12 +747,12 @@ func TestOverflow(t *testing.T) {
encode(b, it); encode(b, it);
err = decode(b, id, &o5); err = decode(b, id, &o5);
if err == nil || err.String() != `value for "maxu" out of range` { if err == nil || err.String() != `value for "maxu" out of range` {
t.Error("wrong overflow error for uint16:", err) t.Error("wrong overflow error for uint16:", err);
} }
// uint32 // uint32
b.Reset(); b.Reset();
it = inputT { it = inputT{
maxu: math.MaxUint32 + 1, maxu: math.MaxUint32 + 1,
}; };
type outu32 struct { type outu32 struct {
@ -695,12 +762,12 @@ func TestOverflow(t *testing.T) {
encode(b, it); encode(b, it);
err = decode(b, id, &o6); err = decode(b, id, &o6);
if err == nil || err.String() != `value for "maxu" out of range` { if err == nil || err.String() != `value for "maxu" out of range` {
t.Error("wrong overflow error for uint32:", err) t.Error("wrong overflow error for uint32:", err);
} }
// float32 // float32
b.Reset(); b.Reset();
it = inputT { it = inputT{
maxf: math.MaxFloat32 * 2, maxf: math.MaxFloat32 * 2,
}; };
type outf32 struct { type outf32 struct {
@ -711,7 +778,7 @@ func TestOverflow(t *testing.T) {
encode(b, it); encode(b, it);
err = decode(b, id, &o7); err = decode(b, id, &o7);
if err == nil || err.String() != `value for "maxf" out of range` { if err == nil || err.String() != `value for "maxf" out of range` {
t.Error("wrong overflow error for float32:", err) t.Error("wrong overflow error for float32:", err);
} }
} }
@ -719,7 +786,7 @@ func TestOverflow(t *testing.T) {
func TestNesting(t *testing.T) { func TestNesting(t *testing.T) {
type RT struct { type RT struct {
a string; a string;
next *RT next *RT;
} }
rt := new(RT); rt := new(RT);
rt.a = "level1"; rt.a = "level1";
@ -764,9 +831,15 @@ func TestAutoIndirection(t *testing.T) {
// First transfer t1 into t0 // First transfer t1 into t0
var t1 T1; var t1 T1;
t1.a = 17; t1.a = 17;
t1.b = new(int); *t1.b = 177; t1.b = new(int);
t1.c = new(*int); *t1.c = new(int); **t1.c = 1777; *t1.b = 177;
t1.d = new(**int); *t1.d = new(*int); **t1.d = new(int); ***t1.d = 17777; 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); b := new(bytes.Buffer);
encode(b, t1); encode(b, t1);
var t0 T0; var t0 T0;
@ -779,9 +852,15 @@ func TestAutoIndirection(t *testing.T) {
// Now transfer t2 into t0 // Now transfer t2 into t0
var t2 T2; var t2 T2;
t2.d = 17777; t2.d = 17777;
t2.c = new(int); *t2.c = 1777; t2.c = new(int);
t2.b = new(*int); *t2.b = new(int); **t2.b = 177; *t2.c = 1777;
t2.a = new(**int); *t2.a = new(*int); **t2.a = new(int); ***t2.a = 17; 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(); b.Reset();
encode(b, t2); encode(b, t2);
t0 = T0{}; t0 = T0{};
@ -870,14 +949,14 @@ func TestIgnoredFields(t *testing.T) {
it0.a = 17; it0.a = 17;
it0.b = "hello"; it0.b = "hello";
it0.c = 3.14159; it0.c = 3.14159;
it0.ignore_d = []int{ 1, 2, 3 }; it0.ignore_d = []int{1, 2, 3};
it0.ignore_e[0] = 1.0; it0.ignore_e[0] = 1.0;
it0.ignore_e[1] = 2.0; it0.ignore_e[1] = 2.0;
it0.ignore_e[2] = 3.0; it0.ignore_e[2] = 3.0;
it0.ignore_f = true; it0.ignore_f = true;
it0.ignore_g = "pay no attention"; it0.ignore_g = "pay no attention";
it0.ignore_h = strings.Bytes("to the curtain"); it0.ignore_h = strings.Bytes("to the curtain");
it0.ignore_i = &RT1{ 3.1, "hi", 7, "hello" }; it0.ignore_i = &RT1{3.1, "hi", 7, "hello"};
b := new(bytes.Buffer); b := new(bytes.Buffer);
encode(b, it0); encode(b, it0);
@ -904,8 +983,8 @@ func TestInvalidField(t *testing.T) {
b := new(bytes.Buffer); b := new(bytes.Buffer);
err := encode(b, &bad0); err := encode(b, &bad0);
if err == nil { if err == nil {
t.Error("expected error; got none") t.Error("expected error; got none");
} else if strings.Index(err.String(), "interface") < 0 { } else if strings.Index(err.String(), "interface") < 0 {
t.Error("expected type error; got", err) t.Error("expected type error; got", err);
} }
} }

View file

@ -358,7 +358,7 @@ func decodeStruct(engine *decEngine, rtyp *reflect.StructType, b *bytes.Buffer,
if indir > 0 { if indir > 0 {
up := unsafe.Pointer(p); up := unsafe.Pointer(p);
if indir > 1 { if indir > 1 {
up = decIndirect(up, indir) up = decIndirect(up, indir);
} }
if *(*unsafe.Pointer)(up) == nil { if *(*unsafe.Pointer)(up) == nil {
// Allocate object by making a slice of bytes and recording the // Allocate object by making a slice of bytes and recording the
@ -579,9 +579,7 @@ func decIgnoreOpFor(wireId typeId) (decOp, os.Error) {
if err != nil { if err != nil {
return nil, err; return nil, err;
} }
op = func(i *decInstr, state *decodeState, p unsafe.Pointer) { op = func(i *decInstr, state *decodeState, p unsafe.Pointer) { state.err = ignoreSlice(state, elemOp) };
state.err = ignoreSlice(state, elemOp);
};
case *arrayType: case *arrayType:
elemId := wireId.gobType().(*arrayType).Elem; elemId := wireId.gobType().(*arrayType).Elem;

View file

@ -44,7 +44,7 @@ func TestBasicEncoder(t *testing.T) {
et1.et2 = new(ET2); et1.et2 = new(ET2);
enc.Encode(et1); enc.Encode(et1);
if enc.state.err != nil { if enc.state.err != nil {
t.Error("encoder fail:", enc.state.err) t.Error("encoder fail:", enc.state.err);
} }
// Decode the result by hand to verify; // Decode the result by hand to verify;
@ -114,14 +114,14 @@ func TestBasicEncoder(t *testing.T) {
} }
// 9) EOF // 9) EOF
if b.Len() != 0 { if b.Len() != 0 {
t.Error("not at eof;", b.Len(), "bytes left") t.Error("not at eof;", b.Len(), "bytes left");
} }
// Now do it again. This time we should see only the type id and value. // Now do it again. This time we should see only the type id and value.
b.Reset(); b.Reset();
enc.Encode(et1); enc.Encode(et1);
if enc.state.err != nil { if enc.state.err != nil {
t.Error("2nd round: encoder fail:", enc.state.err) t.Error("2nd round: encoder fail:", enc.state.err);
} }
// The length. // The length.
length = decodeUint(state); length = decodeUint(state);
@ -144,7 +144,7 @@ func TestBasicEncoder(t *testing.T) {
} }
// 7a) EOF // 7a) EOF
if b.Len() != 0 { if b.Len() != 0 {
t.Error("2nd round: not at eof;", b.Len(), "bytes left") t.Error("2nd round: not at eof;", b.Len(), "bytes left");
} }
} }
@ -156,7 +156,7 @@ func TestEncoderDecoder(t *testing.T) {
et1.et2 = new(ET2); et1.et2 = new(ET2);
enc.Encode(et1); enc.Encode(et1);
if enc.state.err != nil { if enc.state.err != nil {
t.Error("encoder fail:", enc.state.err) t.Error("encoder fail:", enc.state.err);
} }
dec := NewDecoder(b); dec := NewDecoder(b);
newEt1 := new(ET1); newEt1 := new(ET1);
@ -169,7 +169,7 @@ func TestEncoderDecoder(t *testing.T) {
t.Fatalf("invalid data for et1: expected %+v; got %+v\n", *et1, *newEt1); t.Fatalf("invalid data for et1: expected %+v; got %+v\n", *et1, *newEt1);
} }
if b.Len() != 0 { if b.Len() != 0 {
t.Error("not at eof;", b.Len(), "bytes left") t.Error("not at eof;", b.Len(), "bytes left");
} }
enc.Encode(et1); enc.Encode(et1);
@ -182,13 +182,13 @@ func TestEncoderDecoder(t *testing.T) {
t.Fatalf("round 2: invalid data for et1: expected %+v; got %+v\n", *et1, *newEt1); t.Fatalf("round 2: invalid data for et1: expected %+v; got %+v\n", *et1, *newEt1);
} }
if b.Len() != 0 { if b.Len() != 0 {
t.Error("round 2: not at eof;", b.Len(), "bytes left") t.Error("round 2: not at eof;", b.Len(), "bytes left");
} }
// Now test with a running encoder/decoder pair that we recognize a type mismatch. // Now test with a running encoder/decoder pair that we recognize a type mismatch.
enc.Encode(et1); enc.Encode(et1);
if enc.state.err != nil { if enc.state.err != nil {
t.Error("round 3: encoder fail:", enc.state.err) t.Error("round 3: encoder fail:", enc.state.err);
} }
newEt2 := new(ET2); newEt2 := new(ET2);
dec.Decode(newEt2); dec.Decode(newEt2);
@ -207,7 +207,7 @@ func badTypeCheck(e interface{}, shouldFail bool, msg string, t *testing.T) {
et1.et2 = new(ET2); et1.et2 = new(ET2);
enc.Encode(et1); enc.Encode(et1);
if enc.state.err != nil { if enc.state.err != nil {
t.Error("encoder fail:", enc.state.err) t.Error("encoder fail:", enc.state.err);
} }
dec := NewDecoder(b); dec := NewDecoder(b);
dec.Decode(e); dec.Decode(e);
@ -244,15 +244,15 @@ func TestBadData(t *testing.T) {
// Types not supported by the Encoder (only structs work at the top level). // Types not supported by the Encoder (only structs work at the top level).
// Basic types work implicitly. // Basic types work implicitly.
var unsupportedValues = []interface{} { var unsupportedValues = []interface{}{
3, 3,
"hi", "hi",
7.2, 7.2,
[]int{ 1, 2, 3 }, []int{1, 2, 3},
[3]int{ 1, 2, 3 }, [3]int{1, 2, 3},
make(chan int), make(chan int),
func(a int) bool { return true }, func(a int) bool { return true },
make(map[string] int), make(map[string]int),
new(interface{}), new(interface{}),
} }
@ -262,7 +262,7 @@ func TestUnsupported(t *testing.T) {
for _, v := range unsupportedValues { for _, v := range unsupportedValues {
err := enc.Encode(v); err := enc.Encode(v);
if err == nil { if err == nil {
t.Errorf("expected error for %T; got none", v) t.Errorf("expected error for %T; got none", v);
} }
} }
} }
@ -272,39 +272,45 @@ func encAndDec(in, out interface{}) os.Error {
enc := NewEncoder(b); enc := NewEncoder(b);
enc.Encode(in); enc.Encode(in);
if enc.state.err != nil { if enc.state.err != nil {
return enc.state.err return enc.state.err;
} }
dec := NewDecoder(b); dec := NewDecoder(b);
dec.Decode(out); dec.Decode(out);
if dec.state.err != nil { if dec.state.err != nil {
return dec.state.err return dec.state.err;
} }
return nil; return nil;
} }
func TestTypeToPtrType(t *testing.T) { func TestTypeToPtrType(t *testing.T) {
// Encode a T, decode a *T // Encode a T, decode a *T
type Type0 struct { a int } type Type0 struct {
a int;
}
t0 := Type0{7}; t0 := Type0{7};
t0p := (*Type0)(nil); t0p := (*Type0)(nil);
if err := encAndDec(t0, t0p); err != nil { if err := encAndDec(t0, t0p); err != nil {
t.Error(err) t.Error(err);
} }
} }
func TestPtrTypeToType(t *testing.T) { func TestPtrTypeToType(t *testing.T) {
// Encode a *T, decode a T // Encode a *T, decode a T
type Type1 struct { a uint } type Type1 struct {
a uint;
}
t1p := &Type1{17}; t1p := &Type1{17};
var t1 Type1; var t1 Type1;
if err := encAndDec(t1, t1p); err != nil { if err := encAndDec(t1, t1p); err != nil {
t.Error(err) t.Error(err);
} }
} }
func TestTypeToPtrPtrPtrPtrType(t *testing.T) { func TestTypeToPtrPtrPtrPtrType(t *testing.T) {
// Encode a *T, decode a T // Encode a *T, decode a T
type Type2 struct { a ****float } type Type2 struct {
a ****float;
}
t2 := Type2{}; t2 := Type2{};
t2.a = new(***float); t2.a = new(***float);
*t2.a = new(**float); *t2.a = new(**float);
@ -313,7 +319,7 @@ func TestTypeToPtrPtrPtrPtrType(t *testing.T) {
****t2.a = 27.4; ****t2.a = 27.4;
t2pppp := new(***Type2); t2pppp := new(***Type2);
if err := encAndDec(t2, t2pppp); err != nil { if err := encAndDec(t2, t2pppp); err != nil {
t.Error(err) t.Error(err);
} }
if ****(****t2pppp).a != ****t2.a { if ****(****t2pppp).a != ****t2.a {
t.Errorf("wrong value after decode: %g not %g", ****(****t2pppp).a, ****t2.a); t.Errorf("wrong value after decode: %g not %g", ****(****t2pppp).a, ****t2.a);