mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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:
parent
e98412290e
commit
330ab5fddb
3 changed files with 77 additions and 17 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue