mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
a better encoder test, with a couple of fixes for bugs it uncovered.
R=rsc DELTA=84 (65 added, 9 deleted, 10 changed) OCL=31458 CL=31458
This commit is contained in:
parent
1880b90aff
commit
f0a9840d04
3 changed files with 75 additions and 19 deletions
|
|
@ -6,6 +6,7 @@ package gob
|
|||
|
||||
import (
|
||||
"bytes";
|
||||
"fmt"; // DELETE
|
||||
"gob";
|
||||
"os";
|
||||
"reflect";
|
||||
|
|
@ -34,4 +35,59 @@ func TestBasicEncoder(t *testing.T) {
|
|||
if enc.state.err != nil {
|
||||
t.Error("encoder fail:", enc.state.err)
|
||||
}
|
||||
|
||||
// Decode the result by hand to verify;
|
||||
state := new(DecState);
|
||||
state.r = b;
|
||||
// The output should be:
|
||||
// 1) -7: the type id of ET1
|
||||
id1 := DecodeInt(state);
|
||||
if id1 >= 0 {
|
||||
t.Fatal("expected ET1 negative id; got", id1);
|
||||
}
|
||||
// 2) The wireType for ET1
|
||||
wire1 := new(wireType);
|
||||
err := Decode(b, wire1);
|
||||
if err != nil {
|
||||
t.Fatal("error decoding ET1 type:", err);
|
||||
}
|
||||
info := getTypeInfo(reflect.Typeof(ET1{}));
|
||||
trueWire1 := &wireType{name:"ET1", s: info.typeId.gobType().(*structType)};
|
||||
if !reflect.DeepEqual(wire1, trueWire1) {
|
||||
t.Fatalf("invalid wireType for ET1: expected %+v; got %+v\n", *trueWire1, *wire1);
|
||||
}
|
||||
// 3) -8: the type id of ET2
|
||||
id2 := DecodeInt(state);
|
||||
if id2 >= 0 {
|
||||
t.Fatal("expected ET2 negative id; got", id2);
|
||||
}
|
||||
// 4) The wireType for ET2
|
||||
wire2 := new(wireType);
|
||||
err = Decode(b, wire2);
|
||||
if err != nil {
|
||||
t.Fatal("error decoding ET2 type:", err);
|
||||
}
|
||||
info = getTypeInfo(reflect.Typeof(ET2{}));
|
||||
trueWire2 := &wireType{name:"ET2", s: info.typeId.gobType().(*structType)};
|
||||
if !reflect.DeepEqual(wire2, trueWire2) {
|
||||
t.Fatalf("invalid wireType for ET2: expected %+v; got %+v\n", *trueWire2, *wire2);
|
||||
}
|
||||
// 5) The type id for the et1 value
|
||||
newId1 := DecodeInt(state);
|
||||
if newId1 != -id1 {
|
||||
t.Fatal("expected Et1 id", -id1, "got", newId1);
|
||||
}
|
||||
// 6) The value of et1
|
||||
newEt1 := new(ET1);
|
||||
err = Decode(b, newEt1);
|
||||
if err != nil {
|
||||
t.Fatal("error decoding ET1 value:", err);
|
||||
}
|
||||
if !reflect.DeepEqual(et1, newEt1) {
|
||||
t.Fatalf("invalid data for et1: expected %+v; got %+v\n", *et1, *newEt1);
|
||||
}
|
||||
// 7) EOF
|
||||
if b.Len() != 0 {
|
||||
t.Error("not at eof;", b.Len(), "bytes left")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue