mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
make the low-level encoder and decoder private and have them access byte.Buffers rather
than io.Readers and io.Writers. change the Encoder/Decoder protocol so that each message is preceded by its length in bytes. R=rsc DELTA=468 (119 added, 23 deleted, 326 changed) OCL=31700 CL=31702
This commit is contained in:
parent
dc8c447689
commit
e76a335ada
8 changed files with 365 additions and 269 deletions
|
|
@ -57,17 +57,22 @@ func TestBasicEncoder(t *testing.T) {
|
|||
}
|
||||
|
||||
// Decode the result by hand to verify;
|
||||
state := new(DecState);
|
||||
state.r = b;
|
||||
state := new(decodeState);
|
||||
state.b = b;
|
||||
// The output should be:
|
||||
// 0) The length, 38.
|
||||
length := decodeUint(state);
|
||||
if length != 38 {
|
||||
t.Fatal("0. expected length 38; got", length);
|
||||
}
|
||||
// 1) -7: the type id of ET1
|
||||
id1 := DecodeInt(state);
|
||||
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);
|
||||
err := decode(b, wire1);
|
||||
if err != nil {
|
||||
t.Fatal("error decoding ET1 type:", err);
|
||||
}
|
||||
|
|
@ -76,14 +81,19 @@ func TestBasicEncoder(t *testing.T) {
|
|||
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);
|
||||
// 3) The length, 21.
|
||||
length = decodeUint(state);
|
||||
if length != 21 {
|
||||
t.Fatal("3. expected length 21; got", length);
|
||||
}
|
||||
// 4) -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
|
||||
// 5) The wireType for ET2
|
||||
wire2 := new(wireType);
|
||||
err = Decode(b, wire2);
|
||||
err = decode(b, wire2);
|
||||
if err != nil {
|
||||
t.Fatal("error decoding ET2 type:", err);
|
||||
}
|
||||
|
|
@ -92,21 +102,26 @@ func TestBasicEncoder(t *testing.T) {
|
|||
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);
|
||||
// 6) The length, 6.
|
||||
length = decodeUint(state);
|
||||
if length != 6 {
|
||||
t.Fatal("6. expected length 6; got", length);
|
||||
}
|
||||
// 7) 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
|
||||
// 8) The value of et1
|
||||
newEt1 := new(ET1);
|
||||
err = Decode(b, newEt1);
|
||||
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
|
||||
// 9) EOF
|
||||
if b.Len() != 0 {
|
||||
t.Error("not at eof;", b.Len(), "bytes left")
|
||||
}
|
||||
|
|
@ -117,14 +132,19 @@ func TestBasicEncoder(t *testing.T) {
|
|||
if enc.state.err != nil {
|
||||
t.Error("2nd round: encoder fail:", enc.state.err)
|
||||
}
|
||||
// The length.
|
||||
length = decodeUint(state);
|
||||
if length != 6 {
|
||||
t.Fatal("6. expected length 6; got", length);
|
||||
}
|
||||
// 5a) The type id for the et1 value
|
||||
newId1 = DecodeInt(state);
|
||||
newId1 = decodeInt(state);
|
||||
if newId1 != -id1 {
|
||||
t.Fatal("2nd round: expected Et1 id", -id1, "got", newId1);
|
||||
}
|
||||
// 6a) The value of et1
|
||||
newEt1 = new(ET1);
|
||||
err = Decode(b, newEt1);
|
||||
err = decode(b, newEt1);
|
||||
if err != nil {
|
||||
t.Fatal("2nd round: error decoding ET1 value:", err);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue