goprotobuf: Make edge-case int32 encoding more compact, and the same as C++.
Fixes #39.
R=r
CC=golang-dev
https://codereview.appspot.com/10461043
diff --git a/proto/all_test.go b/proto/all_test.go
index ca765e7..07163f6 100644
--- a/proto/all_test.go
+++ b/proto/all_test.go
@@ -1620,6 +1620,27 @@
}
}
+func TestEncodingSizes(t *testing.T) {
+ tests := []struct {
+ m Message
+ n int
+ }{
+ {&Defaults{F_Int32: Int32(math.MaxInt32)}, 6},
+ {&Defaults{F_Int32: Int32(math.MinInt32)}, 6},
+ {&Defaults{F_Uint32: Uint32(math.MaxUint32)}, 6},
+ }
+ for _, test := range tests {
+ b, err := Marshal(test.m)
+ if err != nil {
+ t.Errorf("Marshal(%v): %v", test.m, err)
+ continue
+ }
+ if len(b) != test.n {
+ t.Errorf("Marshal(%v) yielded %d bytes, want %d bytes", test.m, len(b), test.n)
+ }
+ }
+}
+
func fuzzUnmarshal(t *testing.T, data []byte) {
defer func() {
if e := recover(); e != nil {
diff --git a/proto/encode.go b/proto/encode.go
index 8ba3935..9d592cd 100644
--- a/proto/encode.go
+++ b/proto/encode.go
@@ -235,7 +235,7 @@
}
x := word32_Get(v)
o.buf = append(o.buf, p.tagcode...)
- p.valEnc(o, uint64(int32(x)))
+ p.valEnc(o, uint64(x))
return nil
}