fix bugs in gob.

1) didn't handle attempts to encode non-structs properly.
2) if there were multiple indirections involving allocation, didn't allocate the
intermediate cells.
tests added.

R=rsc
DELTA=82  (65 added, 5 deleted, 12 changed)
OCL=35582
CL=35582
This commit is contained in:
Rob Pike 2009-10-11 17:37:22 -07:00
parent e98412290e
commit 330ab5fddb
3 changed files with 77 additions and 17 deletions

View file

@ -245,6 +245,9 @@ func TestBadData(t *testing.T) {
// Types not supported by the Encoder (only structs work at the top level).
// Basic types work implicitly.
var unsupportedValues = []interface{} {
3,
"hi",
7.2,
[]int{ 1, 2, 3 },
[3]int{ 1, 2, 3 },
make(chan int),
@ -263,3 +266,56 @@ func TestUnsupported(t *testing.T) {
}
}
}
func encAndDec(in, out interface{}) os.Error {
b := new(bytes.Buffer);
enc := NewEncoder(b);
enc.Encode(in);
if enc.state.err != nil {
return enc.state.err
}
dec := NewDecoder(b);
dec.Decode(out);
if dec.state.err != nil {
return dec.state.err
}
return nil;
}
func TestTypeToPtrType(t *testing.T) {
// Encode a T, decode a *T
type Type0 struct { a int }
t0 := Type0{7};
t0p := (*Type0)(nil);
if err := encAndDec(t0, t0p); err != nil {
t.Error(err)
}
}
func TestPtrTypeToType(t *testing.T) {
// Encode a *T, decode a T
type Type1 struct { a uint }
t1p := &Type1{17};
var t1 Type1;
if err := encAndDec(t1, t1p); err != nil {
t.Error(err)
}
}
func TestTypeToPtrPtrPtrPtrType(t *testing.T) {
// Encode a *T, decode a T
type Type2 struct { a ****float }
t2 := Type2{};
t2.a = new(***float);
*t2.a = new(**float);
**t2.a = new(*float);
***t2.a = new(float);
****t2.a = 27.4;
t2pppp := new(***Type2);
if err := encAndDec(t2, t2pppp); err != nil {
t.Error(err)
}
if ****(****t2pppp).a != ****t2.a {
t.Errorf("wrong value after decode: %g not %g", ****(****t2pppp).a, ****t2.a);
}
}