| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| 3 | // Copyright 2010 Google Inc. All rights reserved. |
| 4 | // http://code.google.com/p/goprotobuf/ |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 32 | |
| 33 | package proto_test |
| 34 | |
| 35 | import ( |
| 36 | "bytes" |
| 37 | "fmt" |
| David Symonds | b79d99b | 2011-08-29 16:38:49 +1000 | [diff] [blame] | 38 | "math" |
| David Symonds | 03c9d41 | 2010-08-26 14:23:18 +1000 | [diff] [blame] | 39 | "os" |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 40 | "reflect" |
| 41 | "strings" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 42 | "testing" |
| 43 | |
| 44 | . "goprotobuf.googlecode.com/hg/proto" |
| 45 | . "./testdata/_obj/test_proto" |
| 46 | ) |
| 47 | |
| 48 | var globalO *Buffer |
| 49 | |
| 50 | func old() *Buffer { |
| 51 | if globalO == nil { |
| 52 | globalO = NewBuffer(nil) |
| 53 | } |
| 54 | globalO.Reset() |
| 55 | return globalO |
| 56 | } |
| 57 | |
| 58 | func equalbytes(b1, b2 []byte, t *testing.T) { |
| 59 | if len(b1) != len(b2) { |
| 60 | t.Errorf("wrong lengths: 2*%d != %d", len(b1), len(b2)) |
| 61 | return |
| 62 | } |
| 63 | for i := 0; i < len(b1); i++ { |
| 64 | if b1[i] != b2[i] { |
| 65 | t.Errorf("bad byte[%d]:%x %x: %s %s", i, b1[i], b2[i], b1, b2) |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func initGoTestField() *GoTestField { |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 71 | f := new(GoTestField) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 72 | f.Label = String("label") |
| 73 | f.Type = String("type") |
| 74 | return f |
| 75 | } |
| 76 | |
| 77 | // These are all structurally equivalent but the tag numbers differ. |
| 78 | // (It's remarkable that required, optional, and repeated all have |
| 79 | // 8 letters.) |
| 80 | func initGoTest_RequiredGroup() *GoTest_RequiredGroup { |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 81 | return &GoTest_RequiredGroup{ |
| 82 | RequiredField: String("required"), |
| 83 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | func initGoTest_OptionalGroup() *GoTest_OptionalGroup { |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 87 | return &GoTest_OptionalGroup{ |
| 88 | RequiredField: String("optional"), |
| 89 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | func initGoTest_RepeatedGroup() *GoTest_RepeatedGroup { |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 93 | return &GoTest_RepeatedGroup{ |
| 94 | RequiredField: String("repeated"), |
| 95 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | func initGoTest(setdefaults bool) *GoTest { |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 99 | pb := new(GoTest) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 100 | if setdefaults { |
| 101 | pb.F_BoolDefaulted = Bool(Default_GoTest_F_BoolDefaulted) |
| 102 | pb.F_Int32Defaulted = Int32(Default_GoTest_F_Int32Defaulted) |
| 103 | pb.F_Int64Defaulted = Int64(Default_GoTest_F_Int64Defaulted) |
| 104 | pb.F_Fixed32Defaulted = Uint32(Default_GoTest_F_Fixed32Defaulted) |
| 105 | pb.F_Fixed64Defaulted = Uint64(Default_GoTest_F_Fixed64Defaulted) |
| 106 | pb.F_Uint32Defaulted = Uint32(Default_GoTest_F_Uint32Defaulted) |
| 107 | pb.F_Uint64Defaulted = Uint64(Default_GoTest_F_Uint64Defaulted) |
| 108 | pb.F_FloatDefaulted = Float32(Default_GoTest_F_FloatDefaulted) |
| 109 | pb.F_DoubleDefaulted = Float64(Default_GoTest_F_DoubleDefaulted) |
| 110 | pb.F_StringDefaulted = String(Default_GoTest_F_StringDefaulted) |
| 111 | pb.F_BytesDefaulted = Default_GoTest_F_BytesDefaulted |
| 112 | pb.F_Sint32Defaulted = Int32(Default_GoTest_F_Sint32Defaulted) |
| 113 | pb.F_Sint64Defaulted = Int64(Default_GoTest_F_Sint64Defaulted) |
| 114 | } |
| 115 | |
| David Symonds | dd9ca04 | 2011-08-29 16:07:16 +1000 | [diff] [blame] | 116 | pb.Kind = NewGoTest_KIND(GoTest_TIME) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 117 | pb.RequiredField = initGoTestField() |
| 118 | pb.F_BoolRequired = Bool(true) |
| 119 | pb.F_Int32Required = Int32(3) |
| 120 | pb.F_Int64Required = Int64(6) |
| 121 | pb.F_Fixed32Required = Uint32(32) |
| 122 | pb.F_Fixed64Required = Uint64(64) |
| 123 | pb.F_Uint32Required = Uint32(3232) |
| 124 | pb.F_Uint64Required = Uint64(6464) |
| 125 | pb.F_FloatRequired = Float32(3232) |
| 126 | pb.F_DoubleRequired = Float64(6464) |
| 127 | pb.F_StringRequired = String("string") |
| 128 | pb.F_BytesRequired = []byte("bytes") |
| 129 | pb.F_Sint32Required = Int32(-32) |
| 130 | pb.F_Sint64Required = Int64(-64) |
| 131 | pb.Requiredgroup = initGoTest_RequiredGroup() |
| 132 | |
| 133 | return pb |
| 134 | } |
| 135 | |
| 136 | func fail(msg string, b *bytes.Buffer, s string, t *testing.T) { |
| 137 | data := b.Bytes() |
| 138 | ld := len(data) |
| 139 | ls := len(s) / 2 |
| 140 | |
| 141 | fmt.Printf("fail %s ld=%d ls=%d\n", msg, ld, ls) |
| 142 | |
| 143 | // find the interesting spot - n |
| 144 | n := ls |
| 145 | if ld < ls { |
| 146 | n = ld |
| 147 | } |
| 148 | j := 0 |
| 149 | for i := 0; i < n; i++ { |
| 150 | bs := hex(s[j])*16 + hex(s[j+1]) |
| 151 | j += 2 |
| 152 | if data[i] == bs { |
| 153 | continue |
| 154 | } |
| 155 | n = i |
| 156 | break |
| 157 | } |
| 158 | l := n - 10 |
| 159 | if l < 0 { |
| 160 | l = 0 |
| 161 | } |
| 162 | h := n + 10 |
| 163 | |
| 164 | // find the interesting spot - n |
| 165 | fmt.Printf("is[%d]:", l) |
| 166 | for i := l; i < h; i++ { |
| 167 | if i >= ld { |
| 168 | fmt.Printf(" --") |
| 169 | continue |
| 170 | } |
| 171 | fmt.Printf(" %.2x", data[i]) |
| 172 | } |
| 173 | fmt.Printf("\n") |
| 174 | |
| 175 | fmt.Printf("sb[%d]:", l) |
| 176 | for i := l; i < h; i++ { |
| 177 | if i >= ls { |
| 178 | fmt.Printf(" --") |
| 179 | continue |
| 180 | } |
| 181 | bs := hex(s[j])*16 + hex(s[j+1]) |
| 182 | j += 2 |
| 183 | fmt.Printf(" %.2x", bs) |
| 184 | } |
| 185 | fmt.Printf("\n") |
| 186 | |
| 187 | t.Fail() |
| 188 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 189 | // t.Errorf("%s: \ngood: %s\nbad: %x", msg, s, b.Bytes()) |
| 190 | // Print the output in a partially-decoded format; can |
| 191 | // be helpful when updating the test. It produces the output |
| 192 | // that is pasted, with minor edits, into the argument to verify(). |
| 193 | // data := b.Bytes() |
| 194 | // nesting := 0 |
| 195 | // for b.Len() > 0 { |
| 196 | // start := len(data) - b.Len() |
| 197 | // var u uint64 |
| 198 | // u, err := DecodeVarint(b) |
| 199 | // if err != nil { |
| 200 | // fmt.Printf("decode error on varint:", err) |
| 201 | // return |
| 202 | // } |
| 203 | // wire := u & 0x7 |
| 204 | // tag := u >> 3 |
| 205 | // switch wire { |
| 206 | // case WireVarint: |
| 207 | // v, err := DecodeVarint(b) |
| 208 | // if err != nil { |
| 209 | // fmt.Printf("decode error on varint:", err) |
| 210 | // return |
| 211 | // } |
| 212 | // fmt.Printf("\t\t\"%x\" // field %d, encoding %d, value %d\n", |
| 213 | // data[start:len(data)-b.Len()], tag, wire, v) |
| 214 | // case WireFixed32: |
| 215 | // v, err := DecodeFixed32(b) |
| 216 | // if err != nil { |
| 217 | // fmt.Printf("decode error on fixed32:", err) |
| 218 | // return |
| 219 | // } |
| 220 | // fmt.Printf("\t\t\"%x\" // field %d, encoding %d, value %d\n", |
| 221 | // data[start:len(data)-b.Len()], tag, wire, v) |
| 222 | // case WireFixed64: |
| 223 | // v, err := DecodeFixed64(b) |
| 224 | // if err != nil { |
| 225 | // fmt.Printf("decode error on fixed64:", err) |
| 226 | // return |
| 227 | // } |
| 228 | // fmt.Printf("\t\t\"%x\" // field %d, encoding %d, value %d\n", |
| 229 | // data[start:len(data)-b.Len()], tag, wire, v) |
| 230 | // case WireBytes: |
| 231 | // nb, err := DecodeVarint(b) |
| 232 | // if err != nil { |
| 233 | // fmt.Printf("decode error on bytes:", err) |
| 234 | // return |
| 235 | // } |
| 236 | // after_tag := len(data) - b.Len() |
| 237 | // str := make([]byte, nb) |
| 238 | // _, err = b.Read(str) |
| 239 | // if err != nil { |
| 240 | // fmt.Printf("decode error on bytes:", err) |
| 241 | // return |
| 242 | // } |
| 243 | // fmt.Printf("\t\t\"%x\" \"%x\" // field %d, encoding %d (FIELD)\n", |
| 244 | // data[start:after_tag], str, tag, wire) |
| 245 | // case WireStartGroup: |
| 246 | // nesting++ |
| 247 | // fmt.Printf("\t\t\"%x\"\t\t// start group field %d level %d\n", |
| 248 | // data[start:len(data)-b.Len()], tag, nesting) |
| 249 | // case WireEndGroup: |
| 250 | // fmt.Printf("\t\t\"%x\"\t\t// end group field %d level %d\n", |
| 251 | // data[start:len(data)-b.Len()], tag, nesting) |
| 252 | // nesting-- |
| 253 | // default: |
| 254 | // fmt.Printf("unrecognized wire type %d\n", wire) |
| 255 | // return |
| 256 | // } |
| 257 | // } |
| 258 | } |
| 259 | |
| 260 | func hex(c uint8) uint8 { |
| 261 | if '0' <= c && c <= '9' { |
| 262 | return c - '0' |
| 263 | } |
| 264 | if 'a' <= c && c <= 'f' { |
| 265 | return 10 + c - 'a' |
| 266 | } |
| 267 | if 'A' <= c && c <= 'F' { |
| 268 | return 10 + c - 'A' |
| 269 | } |
| 270 | return 0 |
| 271 | } |
| 272 | |
| 273 | func equal(b []byte, s string, t *testing.T) bool { |
| 274 | if 2*len(b) != len(s) { |
| 275 | // fail(fmt.Sprintf("wrong lengths: 2*%d != %d", len(b), len(s)), b, s, t) |
| 276 | fmt.Printf("wrong lengths: 2*%d != %d\n", len(b), len(s)) |
| 277 | return false |
| 278 | } |
| 279 | for i, j := 0, 0; i < len(b); i, j = i+1, j+2 { |
| 280 | x := hex(s[j])*16 + hex(s[j+1]) |
| 281 | if b[i] != x { |
| 282 | // fail(fmt.Sprintf("bad byte[%d]:%x %x", i, b[i], x), b, s, t) |
| 283 | fmt.Printf("bad byte[%d]:%x %x", i, b[i], x) |
| 284 | return false |
| 285 | } |
| 286 | } |
| 287 | return true |
| 288 | } |
| 289 | |
| 290 | func overify(t *testing.T, pb *GoTest, expected string) { |
| 291 | o := old() |
| 292 | err := o.Marshal(pb) |
| 293 | if err != nil { |
| 294 | fmt.Printf("overify marshal-1 err = %v", err) |
| 295 | o.DebugPrint("", o.Bytes()) |
| 296 | t.Fatalf("expected = %s", expected) |
| 297 | } |
| 298 | if !equal(o.Bytes(), expected, t) { |
| 299 | o.DebugPrint("overify neq 1", o.Bytes()) |
| 300 | t.Fatalf("expected = %s", expected) |
| 301 | } |
| 302 | |
| 303 | // Now test Unmarshal by recreating the original buffer. |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 304 | pbd := new(GoTest) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 305 | err = o.Unmarshal(pbd) |
| 306 | if err != nil { |
| 307 | t.Fatalf("overify unmarshal err = %v", err) |
| 308 | o.DebugPrint("", o.Bytes()) |
| 309 | t.Fatalf("string = %s", expected) |
| 310 | } |
| 311 | o.Reset() |
| 312 | err = o.Marshal(pbd) |
| 313 | if err != nil { |
| 314 | t.Errorf("overify marshal-2 err = %v", err) |
| 315 | o.DebugPrint("", o.Bytes()) |
| 316 | t.Fatalf("string = %s", expected) |
| 317 | } |
| 318 | if !equal(o.Bytes(), expected, t) { |
| 319 | o.DebugPrint("overify neq 2", o.Bytes()) |
| 320 | t.Fatalf("string = %s", expected) |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // Simple tests for numeric encode/decode primitives (varint, etc.) |
| 325 | func TestNumericPrimitives(t *testing.T) { |
| 326 | for i := uint64(0); i < 1e6; i += 111 { |
| 327 | o := old() |
| 328 | if o.EncodeVarint(i) != nil { |
| 329 | t.Error("EncodeVarint") |
| 330 | break |
| 331 | } |
| 332 | x, e := o.DecodeVarint() |
| 333 | if e != nil { |
| 334 | t.Fatal("DecodeVarint") |
| 335 | } |
| 336 | if x != i { |
| 337 | t.Fatal("varint decode fail:", i, x) |
| 338 | } |
| 339 | |
| 340 | o = old() |
| 341 | if o.EncodeFixed32(i) != nil { |
| 342 | t.Fatal("encFixed32") |
| 343 | } |
| 344 | x, e = o.DecodeFixed32() |
| 345 | if e != nil { |
| 346 | t.Fatal("decFixed32") |
| 347 | } |
| 348 | if x != i { |
| 349 | t.Fatal("fixed32 decode fail:", i, x) |
| 350 | } |
| 351 | |
| 352 | o = old() |
| 353 | if o.EncodeFixed64(i*1234567) != nil { |
| 354 | t.Error("encFixed64") |
| 355 | break |
| 356 | } |
| 357 | x, e = o.DecodeFixed64() |
| 358 | if e != nil { |
| 359 | t.Error("decFixed64") |
| 360 | break |
| 361 | } |
| 362 | if x != i*1234567 { |
| 363 | t.Error("fixed64 decode fail:", i*1234567, x) |
| 364 | break |
| 365 | } |
| 366 | |
| 367 | o = old() |
| 368 | i32 := int32(i - 12345) |
| 369 | if o.EncodeZigzag32(uint64(i32)) != nil { |
| 370 | t.Fatal("EncodeZigzag32") |
| 371 | } |
| 372 | x, e = o.DecodeZigzag32() |
| 373 | if e != nil { |
| 374 | t.Fatal("DecodeZigzag32") |
| 375 | } |
| 376 | if x != uint64(uint32(i32)) { |
| 377 | t.Fatal("zigzag32 decode fail:", i32, x) |
| 378 | } |
| 379 | |
| 380 | o = old() |
| 381 | i64 := int64(i - 12345) |
| 382 | if o.EncodeZigzag64(uint64(i64)) != nil { |
| 383 | t.Fatal("EncodeZigzag64") |
| 384 | } |
| 385 | x, e = o.DecodeZigzag64() |
| 386 | if e != nil { |
| 387 | t.Fatal("DecodeZigzag64") |
| 388 | } |
| 389 | if x != uint64(i64) { |
| 390 | t.Fatal("zigzag64 decode fail:", i64, x) |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | // Simple tests for bytes |
| 396 | func TestBytesPrimitives(t *testing.T) { |
| 397 | o := old() |
| 398 | bytes := []byte{'n', 'o', 'w', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 't', 'i', 'm', 'e'} |
| 399 | if o.EncodeRawBytes(bytes) != nil { |
| 400 | t.Error("EncodeRawBytes") |
| 401 | } |
| 402 | decb, e := o.DecodeRawBytes(false) |
| 403 | if e != nil { |
| 404 | t.Error("DecodeRawBytes") |
| 405 | } |
| 406 | equalbytes(bytes, decb, t) |
| 407 | } |
| 408 | |
| 409 | // Simple tests for strings |
| 410 | func TestStringPrimitives(t *testing.T) { |
| 411 | o := old() |
| 412 | s := "now is the time" |
| 413 | if o.EncodeStringBytes(s) != nil { |
| 414 | t.Error("enc_string") |
| 415 | } |
| 416 | decs, e := o.DecodeStringBytes() |
| 417 | if e != nil { |
| 418 | t.Error("dec_string") |
| 419 | } |
| 420 | if s != decs { |
| 421 | t.Error("string encode/decode fail:", s, decs) |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | // Do we catch the "required bit not set" case? |
| 426 | func TestRequiredBit(t *testing.T) { |
| 427 | o := old() |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 428 | pb := new(GoTest) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 429 | err := o.Marshal(pb) |
| 430 | if err == nil { |
| 431 | t.Error("did not catch missing required fields") |
| 432 | } else if strings.Index(err.String(), "GoTest") < 0 { |
| 433 | t.Error("wrong error type:", err) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | // Check that all fields are nil. |
| 438 | // Clearly silly, and a residue from a more interesting test with an earlier, |
| 439 | // different initialization property, but it once caught a compiler bug so |
| 440 | // it lives. |
| 441 | func checkInitialized(pb *GoTest, t *testing.T) { |
| 442 | if pb.F_BoolDefaulted != nil { |
| 443 | t.Error("New or Reset did not set boolean:", *pb.F_BoolDefaulted) |
| 444 | } |
| 445 | if pb.F_Int32Defaulted != nil { |
| 446 | t.Error("New or Reset did not set int32:", *pb.F_Int32Defaulted) |
| 447 | } |
| 448 | if pb.F_Int64Defaulted != nil { |
| 449 | t.Error("New or Reset did not set int64:", *pb.F_Int64Defaulted) |
| 450 | } |
| 451 | if pb.F_Fixed32Defaulted != nil { |
| 452 | t.Error("New or Reset did not set fixed32:", *pb.F_Fixed32Defaulted) |
| 453 | } |
| 454 | if pb.F_Fixed64Defaulted != nil { |
| 455 | t.Error("New or Reset did not set fixed64:", *pb.F_Fixed64Defaulted) |
| 456 | } |
| 457 | if pb.F_Uint32Defaulted != nil { |
| 458 | t.Error("New or Reset did not set uint32:", *pb.F_Uint32Defaulted) |
| 459 | } |
| 460 | if pb.F_Uint64Defaulted != nil { |
| 461 | t.Error("New or Reset did not set uint64:", *pb.F_Uint64Defaulted) |
| 462 | } |
| 463 | if pb.F_FloatDefaulted != nil { |
| 464 | t.Error("New or Reset did not set float:", *pb.F_FloatDefaulted) |
| 465 | } |
| 466 | if pb.F_DoubleDefaulted != nil { |
| 467 | t.Error("New or Reset did not set double:", *pb.F_DoubleDefaulted) |
| 468 | } |
| 469 | if pb.F_StringDefaulted != nil { |
| 470 | t.Error("New or Reset did not set string:", *pb.F_StringDefaulted) |
| 471 | } |
| 472 | if pb.F_BytesDefaulted != nil { |
| 473 | t.Error("New or Reset did not set bytes:", string(pb.F_BytesDefaulted)) |
| 474 | } |
| 475 | if pb.F_Sint32Defaulted != nil { |
| 476 | t.Error("New or Reset did not set int32:", *pb.F_Sint32Defaulted) |
| 477 | } |
| 478 | if pb.F_Sint64Defaulted != nil { |
| 479 | t.Error("New or Reset did not set int64:", *pb.F_Sint64Defaulted) |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | // Does Reset() reset? |
| 484 | func TestReset(t *testing.T) { |
| 485 | pb := initGoTest(true) |
| 486 | // muck with some values |
| 487 | pb.F_BoolDefaulted = Bool(false) |
| 488 | pb.F_Int32Defaulted = Int32(237) |
| 489 | pb.F_Int64Defaulted = Int64(12346) |
| 490 | pb.F_Fixed32Defaulted = Uint32(32000) |
| 491 | pb.F_Fixed64Defaulted = Uint64(666) |
| 492 | pb.F_Uint32Defaulted = Uint32(323232) |
| 493 | pb.F_Uint64Defaulted = nil |
| 494 | pb.F_FloatDefaulted = nil |
| 495 | pb.F_DoubleDefaulted = Float64(0) |
| 496 | pb.F_StringDefaulted = String("gotcha") |
| 497 | pb.F_BytesDefaulted = []byte("asdfasdf") |
| 498 | pb.F_Sint32Defaulted = Int32(123) |
| 499 | pb.F_Sint64Defaulted = Int64(789) |
| 500 | pb.Reset() |
| 501 | checkInitialized(pb, t) |
| 502 | } |
| 503 | |
| 504 | // All required fields set, no defaults provided. |
| 505 | func TestEncodeDecode1(t *testing.T) { |
| 506 | pb := initGoTest(false) |
| 507 | overify(t, pb, |
| 508 | "0807"+ // field 1, encoding 0, value 7 |
| 509 | "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField) |
| 510 | "5001"+ // field 10, encoding 0, value 1 |
| 511 | "5803"+ // field 11, encoding 0, value 3 |
| 512 | "6006"+ // field 12, encoding 0, value 6 |
| 513 | "6d20000000"+ // field 13, encoding 5, value 0x20 |
| 514 | "714000000000000000"+ // field 14, encoding 1, value 0x40 |
| 515 | "78a019"+ // field 15, encoding 0, value 0xca0 = 3232 |
| 516 | "8001c032"+ // field 16, encoding 0, value 0x1940 = 6464 |
| 517 | "8d0100004a45"+ // field 17, encoding 5, value 3232.0 |
| 518 | "9101000000000040b940"+ // field 18, encoding 1, value 6464.0 |
| 519 | "9a0106"+"737472696e67"+ // field 19, encoding 2, string "string" |
| 520 | "aa0605"+"6279746573"+ // field 101, encoding 2, string "bytes" |
| 521 | "b0063f"+ // field 102, encoding 0, 0x3f zigzag32 |
| 522 | "b8067f"+ // field 103, encoding 0, 0x7f zigzag64 |
| 523 | "b304"+ // field 70, encoding 3, start group |
| 524 | "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required" |
| 525 | "b404") // field 70, encoding 4, end group |
| 526 | } |
| 527 | |
| 528 | // All required fields set, defaults provided. |
| 529 | func TestEncodeDecode2(t *testing.T) { |
| 530 | pb := initGoTest(true) |
| 531 | overify(t, pb, |
| 532 | "0807"+ // field 1, encoding 0, value 7 |
| 533 | "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField) |
| 534 | "5001"+ // field 10, encoding 0, value 1 |
| 535 | "5803"+ // field 11, encoding 0, value 3 |
| 536 | "6006"+ // field 12, encoding 0, value 6 |
| 537 | "6d20000000"+ // field 13, encoding 5, value 32 |
| 538 | "714000000000000000"+ // field 14, encoding 1, value 64 |
| 539 | "78a019"+ // field 15, encoding 0, value 3232 |
| 540 | "8001c032"+ // field 16, encoding 0, value 6464 |
| 541 | "8d0100004a45"+ // field 17, encoding 5, value 3232.0 |
| 542 | "9101000000000040b940"+ // field 18, encoding 1, value 6464.0 |
| 543 | "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string" |
| 544 | "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes" |
| 545 | "b0063f"+ // field 102, encoding 0, 0x3f zigzag32 |
| 546 | "b8067f"+ // field 103, encoding 0, 0x7f zigzag64 |
| 547 | "c00201"+ // field 40, encoding 0, value 1 |
| 548 | "c80220"+ // field 41, encoding 0, value 32 |
| 549 | "d00240"+ // field 42, encoding 0, value 64 |
| 550 | "dd0240010000"+ // field 43, encoding 5, value 320 |
| 551 | "e1028002000000000000"+ // field 44, encoding 1, value 640 |
| 552 | "e8028019"+ // field 45, encoding 0, value 3200 |
| 553 | "f0028032"+ // field 46, encoding 0, value 6400 |
| 554 | "fd02e0659948"+ // field 47, encoding 5, value 314159.0 |
| 555 | "81030000000050971041"+ // field 48, encoding 1, value 271828.0 |
| 556 | "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n" |
| David Symonds | 5f7b9e0 | 2010-11-30 10:42:39 +1100 | [diff] [blame] | 557 | "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 558 | "90193f"+ // field 402, encoding 0, value 63 |
| 559 | "98197f"+ // field 403, encoding 0, value 127 |
| 560 | "b304"+ // start group field 70 level 1 |
| 561 | "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required" |
| 562 | "b404") // end group field 70 level 1 |
| 563 | |
| 564 | } |
| 565 | |
| 566 | // All default fields set to their default value by hand |
| 567 | func TestEncodeDecode3(t *testing.T) { |
| 568 | pb := initGoTest(false) |
| 569 | pb.F_BoolDefaulted = Bool(true) |
| 570 | pb.F_Int32Defaulted = Int32(32) |
| 571 | pb.F_Int64Defaulted = Int64(64) |
| 572 | pb.F_Fixed32Defaulted = Uint32(320) |
| 573 | pb.F_Fixed64Defaulted = Uint64(640) |
| 574 | pb.F_Uint32Defaulted = Uint32(3200) |
| 575 | pb.F_Uint64Defaulted = Uint64(6400) |
| 576 | pb.F_FloatDefaulted = Float32(314159) |
| 577 | pb.F_DoubleDefaulted = Float64(271828) |
| 578 | pb.F_StringDefaulted = String("hello, \"world!\"\n") |
| 579 | pb.F_BytesDefaulted = []byte("Bignose") |
| 580 | pb.F_Sint32Defaulted = Int32(-32) |
| 581 | pb.F_Sint64Defaulted = Int64(-64) |
| 582 | |
| 583 | overify(t, pb, |
| 584 | "0807"+ // field 1, encoding 0, value 7 |
| 585 | "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField) |
| 586 | "5001"+ // field 10, encoding 0, value 1 |
| 587 | "5803"+ // field 11, encoding 0, value 3 |
| 588 | "6006"+ // field 12, encoding 0, value 6 |
| 589 | "6d20000000"+ // field 13, encoding 5, value 32 |
| 590 | "714000000000000000"+ // field 14, encoding 1, value 64 |
| 591 | "78a019"+ // field 15, encoding 0, value 3232 |
| 592 | "8001c032"+ // field 16, encoding 0, value 6464 |
| 593 | "8d0100004a45"+ // field 17, encoding 5, value 3232.0 |
| 594 | "9101000000000040b940"+ // field 18, encoding 1, value 6464.0 |
| 595 | "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string" |
| 596 | "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes" |
| 597 | "b0063f"+ // field 102, encoding 0, 0x3f zigzag32 |
| 598 | "b8067f"+ // field 103, encoding 0, 0x7f zigzag64 |
| 599 | "c00201"+ // field 40, encoding 0, value 1 |
| 600 | "c80220"+ // field 41, encoding 0, value 32 |
| 601 | "d00240"+ // field 42, encoding 0, value 64 |
| 602 | "dd0240010000"+ // field 43, encoding 5, value 320 |
| 603 | "e1028002000000000000"+ // field 44, encoding 1, value 640 |
| 604 | "e8028019"+ // field 45, encoding 0, value 3200 |
| 605 | "f0028032"+ // field 46, encoding 0, value 6400 |
| 606 | "fd02e0659948"+ // field 47, encoding 5, value 314159.0 |
| 607 | "81030000000050971041"+ // field 48, encoding 1, value 271828.0 |
| 608 | "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n" |
| David Symonds | 5f7b9e0 | 2010-11-30 10:42:39 +1100 | [diff] [blame] | 609 | "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 610 | "90193f"+ // field 402, encoding 0, value 63 |
| 611 | "98197f"+ // field 403, encoding 0, value 127 |
| 612 | "b304"+ // start group field 70 level 1 |
| 613 | "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required" |
| 614 | "b404") // end group field 70 level 1 |
| 615 | |
| 616 | } |
| 617 | |
| 618 | // All required fields set, defaults provided, all non-defaulted optional fields have values. |
| 619 | func TestEncodeDecode4(t *testing.T) { |
| 620 | pb := initGoTest(true) |
| 621 | pb.Table = String("hello") |
| 622 | pb.Param = Int32(7) |
| 623 | pb.OptionalField = initGoTestField() |
| 624 | pb.F_BoolOptional = Bool(true) |
| 625 | pb.F_Int32Optional = Int32(32) |
| 626 | pb.F_Int64Optional = Int64(64) |
| 627 | pb.F_Fixed32Optional = Uint32(3232) |
| 628 | pb.F_Fixed64Optional = Uint64(6464) |
| 629 | pb.F_Uint32Optional = Uint32(323232) |
| 630 | pb.F_Uint64Optional = Uint64(646464) |
| 631 | pb.F_FloatOptional = Float32(32.) |
| 632 | pb.F_DoubleOptional = Float64(64.) |
| 633 | pb.F_StringOptional = String("hello") |
| 634 | pb.F_BytesOptional = []byte("Bignose") |
| 635 | pb.F_Sint32Optional = Int32(-32) |
| 636 | pb.F_Sint64Optional = Int64(-64) |
| 637 | pb.Optionalgroup = initGoTest_OptionalGroup() |
| 638 | |
| 639 | overify(t, pb, |
| 640 | "0807"+ // field 1, encoding 0, value 7 |
| 641 | "1205"+"68656c6c6f"+ // field 2, encoding 2, string "hello" |
| 642 | "1807"+ // field 3, encoding 0, value 7 |
| 643 | "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField) |
| 644 | "320d"+"0a056c6162656c120474797065"+ // field 6, encoding 2 (GoTestField) |
| 645 | "5001"+ // field 10, encoding 0, value 1 |
| 646 | "5803"+ // field 11, encoding 0, value 3 |
| 647 | "6006"+ // field 12, encoding 0, value 6 |
| 648 | "6d20000000"+ // field 13, encoding 5, value 32 |
| 649 | "714000000000000000"+ // field 14, encoding 1, value 64 |
| 650 | "78a019"+ // field 15, encoding 0, value 3232 |
| 651 | "8001c032"+ // field 16, encoding 0, value 6464 |
| 652 | "8d0100004a45"+ // field 17, encoding 5, value 3232.0 |
| 653 | "9101000000000040b940"+ // field 18, encoding 1, value 6464.0 |
| 654 | "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string" |
| 655 | "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes" |
| 656 | "b0063f"+ // field 102, encoding 0, 0x3f zigzag32 |
| 657 | "b8067f"+ // field 103, encoding 0, 0x7f zigzag64 |
| 658 | "f00101"+ // field 30, encoding 0, value 1 |
| 659 | "f80120"+ // field 31, encoding 0, value 32 |
| 660 | "800240"+ // field 32, encoding 0, value 64 |
| 661 | "8d02a00c0000"+ // field 33, encoding 5, value 3232 |
| 662 | "91024019000000000000"+ // field 34, encoding 1, value 6464 |
| 663 | "9802a0dd13"+ // field 35, encoding 0, value 323232 |
| 664 | "a002c0ba27"+ // field 36, encoding 0, value 646464 |
| 665 | "ad0200000042"+ // field 37, encoding 5, value 32.0 |
| 666 | "b1020000000000005040"+ // field 38, encoding 1, value 64.0 |
| 667 | "ba0205"+"68656c6c6f"+ // field 39, encoding 2, string "hello" |
| 668 | "ea1207"+"4269676e6f7365"+ // field 301, encoding 2, string "Bignose" |
| 669 | "f0123f"+ // field 302, encoding 0, value 63 |
| 670 | "f8127f"+ // field 303, encoding 0, value 127 |
| 671 | "c00201"+ // field 40, encoding 0, value 1 |
| 672 | "c80220"+ // field 41, encoding 0, value 32 |
| 673 | "d00240"+ // field 42, encoding 0, value 64 |
| 674 | "dd0240010000"+ // field 43, encoding 5, value 320 |
| 675 | "e1028002000000000000"+ // field 44, encoding 1, value 640 |
| 676 | "e8028019"+ // field 45, encoding 0, value 3200 |
| 677 | "f0028032"+ // field 46, encoding 0, value 6400 |
| 678 | "fd02e0659948"+ // field 47, encoding 5, value 314159.0 |
| 679 | "81030000000050971041"+ // field 48, encoding 1, value 271828.0 |
| 680 | "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n" |
| David Symonds | 5f7b9e0 | 2010-11-30 10:42:39 +1100 | [diff] [blame] | 681 | "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 682 | "90193f"+ // field 402, encoding 0, value 63 |
| 683 | "98197f"+ // field 403, encoding 0, value 127 |
| 684 | "b304"+ // start group field 70 level 1 |
| 685 | "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required" |
| 686 | "b404"+ // end group field 70 level 1 |
| 687 | "d305"+ // start group field 90 level 1 |
| 688 | "da0508"+"6f7074696f6e616c"+ // field 91, encoding 2, string "optional" |
| 689 | "d405") // end group field 90 level 1 |
| 690 | |
| 691 | } |
| 692 | |
| 693 | // All required fields set, defaults provided, all repeated fields given two values. |
| 694 | func TestEncodeDecode5(t *testing.T) { |
| 695 | pb := initGoTest(true) |
| 696 | pb.RepeatedField = []*GoTestField{initGoTestField(), initGoTestField()} |
| 697 | pb.F_BoolRepeated = []bool{false, true} |
| 698 | pb.F_Int32Repeated = []int32{32, 33} |
| 699 | pb.F_Int64Repeated = []int64{64, 65} |
| 700 | pb.F_Fixed32Repeated = []uint32{3232, 3333} |
| 701 | pb.F_Fixed64Repeated = []uint64{6464, 6565} |
| 702 | pb.F_Uint32Repeated = []uint32{323232, 333333} |
| 703 | pb.F_Uint64Repeated = []uint64{646464, 656565} |
| 704 | pb.F_FloatRepeated = []float32{32., 33.} |
| 705 | pb.F_DoubleRepeated = []float64{64., 65.} |
| 706 | pb.F_StringRepeated = []string{"hello", "sailor"} |
| 707 | pb.F_BytesRepeated = [][]byte{[]byte("big"), []byte("nose")} |
| 708 | pb.F_Sint32Repeated = []int32{32, -32} |
| 709 | pb.F_Sint64Repeated = []int64{64, -64} |
| 710 | pb.Repeatedgroup = []*GoTest_RepeatedGroup{initGoTest_RepeatedGroup(), initGoTest_RepeatedGroup()} |
| 711 | |
| 712 | overify(t, pb, |
| 713 | "0807"+ // field 1, encoding 0, value 7 |
| 714 | "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField) |
| 715 | "2a0d"+"0a056c6162656c120474797065"+ // field 5, encoding 2 (GoTestField) |
| 716 | "2a0d"+"0a056c6162656c120474797065"+ // field 5, encoding 2 (GoTestField) |
| 717 | "5001"+ // field 10, encoding 0, value 1 |
| 718 | "5803"+ // field 11, encoding 0, value 3 |
| 719 | "6006"+ // field 12, encoding 0, value 6 |
| 720 | "6d20000000"+ // field 13, encoding 5, value 32 |
| 721 | "714000000000000000"+ // field 14, encoding 1, value 64 |
| 722 | "78a019"+ // field 15, encoding 0, value 3232 |
| 723 | "8001c032"+ // field 16, encoding 0, value 6464 |
| 724 | "8d0100004a45"+ // field 17, encoding 5, value 3232.0 |
| 725 | "9101000000000040b940"+ // field 18, encoding 1, value 6464.0 |
| 726 | "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string" |
| 727 | "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes" |
| 728 | "b0063f"+ // field 102, encoding 0, 0x3f zigzag32 |
| 729 | "b8067f"+ // field 103, encoding 0, 0x7f zigzag64 |
| 730 | "a00100"+ // field 20, encoding 0, value 0 |
| 731 | "a00101"+ // field 20, encoding 0, value 1 |
| 732 | "a80120"+ // field 21, encoding 0, value 32 |
| 733 | "a80121"+ // field 21, encoding 0, value 33 |
| 734 | "b00140"+ // field 22, encoding 0, value 64 |
| 735 | "b00141"+ // field 22, encoding 0, value 65 |
| 736 | "bd01a00c0000"+ // field 23, encoding 5, value 3232 |
| 737 | "bd01050d0000"+ // field 23, encoding 5, value 3333 |
| 738 | "c1014019000000000000"+ // field 24, encoding 1, value 6464 |
| 739 | "c101a519000000000000"+ // field 24, encoding 1, value 6565 |
| 740 | "c801a0dd13"+ // field 25, encoding 0, value 323232 |
| 741 | "c80195ac14"+ // field 25, encoding 0, value 333333 |
| 742 | "d001c0ba27"+ // field 26, encoding 0, value 646464 |
| 743 | "d001b58928"+ // field 26, encoding 0, value 656565 |
| 744 | "dd0100000042"+ // field 27, encoding 5, value 32.0 |
| 745 | "dd0100000442"+ // field 27, encoding 5, value 33.0 |
| 746 | "e1010000000000005040"+ // field 28, encoding 1, value 64.0 |
| 747 | "e1010000000000405040"+ // field 28, encoding 1, value 65.0 |
| 748 | "ea0105"+"68656c6c6f"+ // field 29, encoding 2, string "hello" |
| 749 | "ea0106"+"7361696c6f72"+ // field 29, encoding 2, string "sailor" |
| 750 | "ca0c03"+"626967"+ // field 201, encoding 2, string "big" |
| 751 | "ca0c04"+"6e6f7365"+ // field 201, encoding 2, string "nose" |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 752 | "d00c40"+ // field 202, encoding 0, value 32 |
| 753 | "d00c3f"+ // field 202, encoding 0, value -32 |
| 754 | "d80c8001"+ // field 203, encoding 0, value 64 |
| 755 | "d80c7f"+ // field 203, encoding 0, value -64 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 756 | "c00201"+ // field 40, encoding 0, value 1 |
| 757 | "c80220"+ // field 41, encoding 0, value 32 |
| 758 | "d00240"+ // field 42, encoding 0, value 64 |
| 759 | "dd0240010000"+ // field 43, encoding 5, value 320 |
| 760 | "e1028002000000000000"+ // field 44, encoding 1, value 640 |
| 761 | "e8028019"+ // field 45, encoding 0, value 3200 |
| 762 | "f0028032"+ // field 46, encoding 0, value 6400 |
| 763 | "fd02e0659948"+ // field 47, encoding 5, value 314159.0 |
| 764 | "81030000000050971041"+ // field 48, encoding 1, value 271828.0 |
| 765 | "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n" |
| David Symonds | 5f7b9e0 | 2010-11-30 10:42:39 +1100 | [diff] [blame] | 766 | "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 767 | "90193f"+ // field 402, encoding 0, value 63 |
| 768 | "98197f"+ // field 403, encoding 0, value 127 |
| 769 | "b304"+ // start group field 70 level 1 |
| 770 | "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required" |
| 771 | "b404"+ // end group field 70 level 1 |
| 772 | "8305"+ // start group field 80 level 1 |
| 773 | "8a0508"+"7265706561746564"+ // field 81, encoding 2, string "repeated" |
| 774 | "8405"+ // end group field 80 level 1 |
| 775 | "8305"+ // start group field 80 level 1 |
| 776 | "8a0508"+"7265706561746564"+ // field 81, encoding 2, string "repeated" |
| 777 | "8405") // end group field 80 level 1 |
| 778 | |
| 779 | } |
| 780 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 781 | // All required fields set, all packed repeated fields given two values. |
| 782 | func TestEncodeDecode6(t *testing.T) { |
| 783 | pb := initGoTest(false) |
| 784 | pb.F_BoolRepeatedPacked = []bool{false, true} |
| 785 | pb.F_Int32RepeatedPacked = []int32{32, 33} |
| 786 | pb.F_Int64RepeatedPacked = []int64{64, 65} |
| 787 | pb.F_Fixed32RepeatedPacked = []uint32{3232, 3333} |
| 788 | pb.F_Fixed64RepeatedPacked = []uint64{6464, 6565} |
| 789 | pb.F_Uint32RepeatedPacked = []uint32{323232, 333333} |
| 790 | pb.F_Uint64RepeatedPacked = []uint64{646464, 656565} |
| 791 | pb.F_FloatRepeatedPacked = []float32{32., 33.} |
| 792 | pb.F_DoubleRepeatedPacked = []float64{64., 65.} |
| 793 | pb.F_Sint32RepeatedPacked = []int32{32, -32} |
| 794 | pb.F_Sint64RepeatedPacked = []int64{64, -64} |
| 795 | |
| 796 | overify(t, pb, |
| 797 | "0807"+ // field 1, encoding 0, value 7 |
| 798 | "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField) |
| 799 | "5001"+ // field 10, encoding 0, value 1 |
| 800 | "5803"+ // field 11, encoding 0, value 3 |
| 801 | "6006"+ // field 12, encoding 0, value 6 |
| 802 | "6d20000000"+ // field 13, encoding 5, value 32 |
| 803 | "714000000000000000"+ // field 14, encoding 1, value 64 |
| 804 | "78a019"+ // field 15, encoding 0, value 3232 |
| 805 | "8001c032"+ // field 16, encoding 0, value 6464 |
| 806 | "8d0100004a45"+ // field 17, encoding 5, value 3232.0 |
| 807 | "9101000000000040b940"+ // field 18, encoding 1, value 6464.0 |
| 808 | "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string" |
| 809 | "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes" |
| 810 | "b0063f"+ // field 102, encoding 0, 0x3f zigzag32 |
| 811 | "b8067f"+ // field 103, encoding 0, 0x7f zigzag64 |
| 812 | "9203020001"+ // field 50, encoding 2, 2 bytes, value 0, value 1 |
| 813 | "9a03022021"+ // field 51, encoding 2, 2 bytes, value 32, value 33 |
| 814 | "a203024041"+ // field 52, encoding 2, 2 bytes, value 64, value 65 |
| 815 | "aa0308"+ // field 53, encoding 2, 8 bytes |
| 816 | "a00c0000050d0000"+ // value 3232, value 3333 |
| 817 | "b20310"+ // field 54, encoding 2, 16 bytes |
| 818 | "4019000000000000a519000000000000"+ // value 6464, value 6565 |
| 819 | "ba0306"+ // field 55, encoding 2, 6 bytes |
| 820 | "a0dd1395ac14"+ // value 323232, value 333333 |
| 821 | "c20306"+ // field 56, encoding 2, 6 bytes |
| 822 | "c0ba27b58928"+ // value 646464, value 656565 |
| 823 | "ca0308"+ // field 57, encoding 2, 8 bytes |
| 824 | "0000004200000442"+ // value 32.0, value 33.0 |
| 825 | "d20310"+ // field 58, encoding 2, 16 bytes |
| 826 | "00000000000050400000000000405040"+ // value 64.0, value 65.0 |
| 827 | "b21f02"+ // field 502, encoding 2, 2 bytes |
| 828 | "403f"+ // value 32, value -32 |
| 829 | "ba1f03"+ // field 503, encoding 2, 3 bytes |
| 830 | "80017f"+ // value 64, value -64 |
| 831 | "b304"+ // start group field 70 level 1 |
| 832 | "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required" |
| 833 | "b404") // end group field 70 level 1 |
| 834 | } |
| 835 | |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 836 | // Test that we can encode empty bytes fields. |
| 837 | func TestEncodeDecodeBytes1(t *testing.T) { |
| 838 | pb := initGoTest(false) |
| 839 | |
| 840 | // Create our bytes |
| 841 | pb.F_BytesRequired = []byte{} |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame^] | 842 | pb.F_BytesRepeated = [][]byte{{}} |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 843 | pb.F_BytesOptional = []byte{} |
| 844 | |
| 845 | d, err := Marshal(pb) |
| 846 | if err != nil { |
| 847 | t.Errorf(err.String()) |
| 848 | } |
| 849 | |
| 850 | pbd := new(GoTest) |
| 851 | if err := Unmarshal(d, pbd); err != nil { |
| 852 | t.Errorf(err.String()) |
| 853 | } |
| 854 | |
| 855 | if pbd.F_BytesRequired == nil || len(pbd.F_BytesRequired) != 0 { |
| 856 | t.Errorf("required empty bytes field is incorrect") |
| 857 | } |
| 858 | if pbd.F_BytesRepeated == nil || len(pbd.F_BytesRepeated) == 1 && pbd.F_BytesRepeated[0] == nil { |
| 859 | t.Errorf("repeated empty bytes field is incorrect") |
| 860 | } |
| 861 | if pbd.F_BytesOptional == nil || len(pbd.F_BytesOptional) != 0 { |
| 862 | t.Errorf("optional empty bytes field is incorrect") |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | // Test that we encode nil-valued fields of a repeated bytes field correctly. |
| 867 | // Since entries in a repeated field cannot be nil, nil must mean empty value. |
| 868 | func TestEncodeDecodeBytes2(t *testing.T) { |
| 869 | pb := initGoTest(false) |
| 870 | |
| 871 | // Create our bytes |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 872 | pb.F_BytesRepeated = [][]byte{nil} |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 873 | |
| 874 | d, err := Marshal(pb) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 875 | if err != nil { |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 876 | t.Errorf(err.String()) |
| 877 | } |
| 878 | |
| 879 | pbd := new(GoTest) |
| 880 | if err := Unmarshal(d, pbd); err != nil { |
| 881 | t.Errorf(err.String()) |
| 882 | } |
| 883 | |
| 884 | if len(pbd.F_BytesRepeated) != 1 || pbd.F_BytesRepeated[0] == nil { |
| 885 | t.Errorf("Unexpected value for repeated bytes field") |
| 886 | } |
| 887 | } |
| 888 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 889 | // All required fields set, defaults provided, all repeated fields given two values. |
| 890 | func TestSkippingUnrecognizedFields(t *testing.T) { |
| 891 | o := old() |
| 892 | pb := initGoTestField() |
| 893 | |
| 894 | // Marshal it normally. |
| 895 | o.Marshal(pb) |
| 896 | |
| 897 | // Now new a GoSkipTest record. |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 898 | skip := &GoSkipTest{ |
| 899 | SkipInt32: Int32(32), |
| 900 | SkipFixed32: Uint32(3232), |
| 901 | SkipFixed64: Uint64(6464), |
| 902 | SkipString: String("skipper"), |
| 903 | Skipgroup: &GoSkipTest_SkipGroup{ |
| 904 | GroupInt32: Int32(75), |
| 905 | GroupString: String("wxyz"), |
| 906 | }, |
| 907 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 908 | |
| 909 | // Marshal it into same buffer. |
| 910 | o.Marshal(skip) |
| 911 | |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 912 | pbd := new(GoTestField) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 913 | o.Unmarshal(pbd) |
| 914 | |
| 915 | // The __unrecognized field should be a marshaling of GoSkipTest |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 916 | skipd := new(GoSkipTest) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 917 | |
| 918 | o.SetBuf(pbd.XXX_unrecognized) |
| 919 | o.Unmarshal(skipd) |
| 920 | |
| 921 | if *skipd.SkipInt32 != *skip.SkipInt32 { |
| 922 | t.Error("skip int32", skipd.SkipInt32) |
| 923 | } |
| 924 | if *skipd.SkipFixed32 != *skip.SkipFixed32 { |
| 925 | t.Error("skip fixed32", skipd.SkipFixed32) |
| 926 | } |
| 927 | if *skipd.SkipFixed64 != *skip.SkipFixed64 { |
| 928 | t.Error("skip fixed64", skipd.SkipFixed64) |
| 929 | } |
| 930 | if *skipd.SkipString != *skip.SkipString { |
| 931 | t.Error("skip string", *skipd.SkipString) |
| 932 | } |
| 933 | if *skipd.Skipgroup.GroupInt32 != *skip.Skipgroup.GroupInt32 { |
| 934 | t.Error("skip group int32", skipd.Skipgroup.GroupInt32) |
| 935 | } |
| 936 | if *skipd.Skipgroup.GroupString != *skip.Skipgroup.GroupString { |
| 937 | t.Error("skip group string", *skipd.Skipgroup.GroupString) |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | // Check that we can grow an array (repeated field) to have many elements. |
| 942 | // This test doesn't depend only on our encoding; for variety, it makes sure |
| 943 | // we create, encode, and decode the correct contents explicitly. It's therefore |
| 944 | // a bit messier. |
| 945 | // This test also uses (and hence tests) the Marshal/Unmarshal functions |
| 946 | // instead of the methods. |
| 947 | func TestBigRepeated(t *testing.T) { |
| 948 | pb := initGoTest(true) |
| 949 | |
| 950 | // Create the arrays |
| 951 | const N = 50 // Internally the library starts much smaller. |
| 952 | pb.Repeatedgroup = make([]*GoTest_RepeatedGroup, N) |
| 953 | pb.F_Sint64Repeated = make([]int64, N) |
| 954 | pb.F_Sint32Repeated = make([]int32, N) |
| 955 | pb.F_BytesRepeated = make([][]byte, N) |
| 956 | pb.F_StringRepeated = make([]string, N) |
| 957 | pb.F_DoubleRepeated = make([]float64, N) |
| 958 | pb.F_FloatRepeated = make([]float32, N) |
| 959 | pb.F_Uint64Repeated = make([]uint64, N) |
| 960 | pb.F_Uint32Repeated = make([]uint32, N) |
| 961 | pb.F_Fixed64Repeated = make([]uint64, N) |
| 962 | pb.F_Fixed32Repeated = make([]uint32, N) |
| 963 | pb.F_Int64Repeated = make([]int64, N) |
| 964 | pb.F_Int32Repeated = make([]int32, N) |
| 965 | pb.F_BoolRepeated = make([]bool, N) |
| 966 | pb.RepeatedField = make([]*GoTestField, N) |
| 967 | |
| 968 | // Fill in the arrays with checkable values. |
| 969 | igtf := initGoTestField() |
| 970 | igtrg := initGoTest_RepeatedGroup() |
| 971 | for i := 0; i < N; i++ { |
| 972 | pb.Repeatedgroup[i] = igtrg |
| 973 | pb.F_Sint64Repeated[i] = int64(i) |
| 974 | pb.F_Sint32Repeated[i] = int32(i) |
| 975 | s := fmt.Sprint(i) |
| 976 | pb.F_BytesRepeated[i] = []byte(s) |
| 977 | pb.F_StringRepeated[i] = s |
| 978 | pb.F_DoubleRepeated[i] = float64(i) |
| 979 | pb.F_FloatRepeated[i] = float32(i) |
| 980 | pb.F_Uint64Repeated[i] = uint64(i) |
| 981 | pb.F_Uint32Repeated[i] = uint32(i) |
| 982 | pb.F_Fixed64Repeated[i] = uint64(i) |
| 983 | pb.F_Fixed32Repeated[i] = uint32(i) |
| 984 | pb.F_Int64Repeated[i] = int64(i) |
| 985 | pb.F_Int32Repeated[i] = int32(i) |
| 986 | pb.F_BoolRepeated[i] = i%2 == 0 |
| 987 | pb.RepeatedField[i] = igtf |
| 988 | } |
| 989 | |
| 990 | // Marshal. |
| 991 | buf, _ := Marshal(pb) |
| 992 | |
| 993 | // Now test Unmarshal by recreating the original buffer. |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 994 | pbd := new(GoTest) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 995 | Unmarshal(buf, pbd) |
| 996 | |
| 997 | // Check the checkable values |
| 998 | for i := uint64(0); i < N; i++ { |
| 999 | if pbd.Repeatedgroup[i] == nil { // TODO: more checking? |
| 1000 | t.Error("pbd.Repeatedgroup bad") |
| 1001 | } |
| 1002 | var x uint64 |
| 1003 | x = uint64(pbd.F_Sint64Repeated[i]) |
| 1004 | if x != i { |
| 1005 | t.Error("pbd.F_Sint64Repeated bad", x, i) |
| 1006 | } |
| 1007 | x = uint64(pbd.F_Sint32Repeated[i]) |
| 1008 | if x != i { |
| 1009 | t.Error("pbd.F_Sint32Repeated bad", x, i) |
| 1010 | } |
| 1011 | s := fmt.Sprint(i) |
| 1012 | equalbytes(pbd.F_BytesRepeated[i], []byte(s), t) |
| 1013 | if pbd.F_StringRepeated[i] != s { |
| 1014 | t.Error("pbd.F_Sint32Repeated bad", pbd.F_StringRepeated[i], i) |
| 1015 | } |
| 1016 | x = uint64(pbd.F_DoubleRepeated[i]) |
| 1017 | if x != i { |
| 1018 | t.Error("pbd.F_DoubleRepeated bad", x, i) |
| 1019 | } |
| 1020 | x = uint64(pbd.F_FloatRepeated[i]) |
| 1021 | if x != i { |
| 1022 | t.Error("pbd.F_FloatRepeated bad", x, i) |
| 1023 | } |
| 1024 | x = pbd.F_Uint64Repeated[i] |
| 1025 | if x != i { |
| 1026 | t.Error("pbd.F_Uint64Repeated bad", x, i) |
| 1027 | } |
| 1028 | x = uint64(pbd.F_Uint32Repeated[i]) |
| 1029 | if x != i { |
| 1030 | t.Error("pbd.F_Uint32Repeated bad", x, i) |
| 1031 | } |
| 1032 | x = pbd.F_Fixed64Repeated[i] |
| 1033 | if x != i { |
| 1034 | t.Error("pbd.F_Fixed64Repeated bad", x, i) |
| 1035 | } |
| 1036 | x = uint64(pbd.F_Fixed32Repeated[i]) |
| 1037 | if x != i { |
| 1038 | t.Error("pbd.F_Fixed32Repeated bad", x, i) |
| 1039 | } |
| 1040 | x = uint64(pbd.F_Int64Repeated[i]) |
| 1041 | if x != i { |
| 1042 | t.Error("pbd.F_Int64Repeated bad", x, i) |
| 1043 | } |
| 1044 | x = uint64(pbd.F_Int32Repeated[i]) |
| 1045 | if x != i { |
| 1046 | t.Error("pbd.F_Int32Repeated bad", x, i) |
| 1047 | } |
| 1048 | if pbd.F_BoolRepeated[i] != (i%2 == 0) { |
| 1049 | t.Error("pbd.F_BoolRepeated bad", x, i) |
| 1050 | } |
| 1051 | if pbd.RepeatedField[i] == nil { // TODO: more checking? |
| 1052 | t.Error("pbd.RepeatedField bad") |
| 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | // Verify we give a useful message when decoding to the wrong structure type. |
| 1058 | func TestTypeMismatch(t *testing.T) { |
| 1059 | pb1 := initGoTest(true) |
| 1060 | |
| 1061 | // Marshal |
| 1062 | o := old() |
| 1063 | o.Marshal(pb1) |
| 1064 | |
| 1065 | // Now Unmarshal it to the wrong type. |
| 1066 | pb2 := initGoTestField() |
| 1067 | err := o.Unmarshal(pb2) |
| 1068 | switch err { |
| 1069 | case ErrWrongType: |
| 1070 | // fine |
| 1071 | case nil: |
| 1072 | t.Error("expected wrong type error, got no error") |
| 1073 | default: |
| 1074 | t.Error("expected wrong type error, got", err) |
| 1075 | } |
| 1076 | } |
| 1077 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 1078 | func encodeDecode(t *testing.T, in, out interface{}, msg string) { |
| 1079 | buf, err := Marshal(in) |
| 1080 | if err != nil { |
| 1081 | t.Fatalf("failed marshaling %v: %v", msg, err) |
| 1082 | } |
| 1083 | if err := Unmarshal(buf, out); err != nil { |
| 1084 | t.Fatalf("failed unmarshaling %v: %v", msg, err) |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | func TestPackedNonPackedDecoderSwitching(t *testing.T) { |
| 1089 | np, p := new(NonPackedTest), new(PackedTest) |
| 1090 | |
| 1091 | // non-packed -> packed |
| 1092 | np.A = []int32{0, 1, 1, 2, 3, 5} |
| 1093 | encodeDecode(t, np, p, "non-packed -> packed") |
| 1094 | if !reflect.DeepEqual(np.A, p.B) { |
| 1095 | t.Errorf("failed non-packed -> packed; np.A=%+v, p.B=%+v", np.A, p.B) |
| 1096 | } |
| 1097 | |
| 1098 | // packed -> non-packed |
| 1099 | np.Reset() |
| 1100 | p.B = []int32{3, 1, 4, 1, 5, 9} |
| 1101 | encodeDecode(t, p, np, "packed -> non-packed") |
| 1102 | if !reflect.DeepEqual(p.B, np.A) { |
| 1103 | t.Errorf("failed packed -> non-packed; p.B=%+v, np.A=%+v", p.B, np.A) |
| 1104 | } |
| 1105 | } |
| 1106 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1107 | func TestProto1RepeatedGroup(t *testing.T) { |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1108 | pb := &MessageList{ |
| 1109 | Message: []*MessageList_Message{ |
| 1110 | &MessageList_Message{ |
| 1111 | Name: String("blah"), |
| 1112 | Count: Int32(7), |
| 1113 | }, |
| 1114 | // NOTE: pb.Message[1] is a nil |
| 1115 | nil, |
| 1116 | }, |
| 1117 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1118 | |
| 1119 | o := old() |
| 1120 | if err := o.Marshal(pb); err != ErrRepeatedHasNil { |
| 1121 | t.Fatalf("unexpected or no error when marshaling: %v", err) |
| 1122 | } |
| 1123 | } |
| 1124 | |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 1125 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1126 | // Test that enums work. Checks for a bug introduced by making enums |
| 1127 | // named types instead of int32: newInt32FromUint64 would crash with |
| 1128 | // a type mismatch in reflect.PointTo. |
| 1129 | func TestEnum(t *testing.T) { |
| 1130 | pb := new(GoEnum) |
| 1131 | pb.Foo = NewFOO(FOO_FOO1) |
| 1132 | o := old() |
| 1133 | if err := o.Marshal(pb); err != nil { |
| Rob Pike | 853f911 | 2010-12-17 13:56:46 -0800 | [diff] [blame] | 1134 | t.Fatal("error encoding enum:", err) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1135 | } |
| 1136 | pb1 := new(GoEnum) |
| 1137 | if err := o.Unmarshal(pb1); err != nil { |
| Rob Pike | 853f911 | 2010-12-17 13:56:46 -0800 | [diff] [blame] | 1138 | t.Fatal("error decoding enum:", err) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1139 | } |
| 1140 | if *pb1.Foo != FOO_FOO1 { |
| Rob Pike | 853f911 | 2010-12-17 13:56:46 -0800 | [diff] [blame] | 1141 | t.Error("expected 7 but got ", *pb1.Foo) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 1145 | // Enum types have String methods. Check that enum fields can be printed. |
| 1146 | // We don't care what the value actually is, just as long as it doesn't crash. |
| 1147 | func TestPrintingNilEnumFields(t *testing.T) { |
| 1148 | pb := new(GoEnum) |
| 1149 | fmt.Sprintf("%+v", pb) |
| 1150 | } |
| 1151 | |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1152 | // Verify that absent required fields cause Marshal/Unmarshal to return errors. |
| 1153 | func TestRequiredFieldEnforcement(t *testing.T) { |
| 1154 | pb := new(GoTestField) |
| 1155 | _, err := Marshal(pb) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 1156 | if err == nil { |
| 1157 | t.Error("marshal: expected error, got nil") |
| 1158 | } else if strings.Index(err.String(), "GoTestField") < 0 { |
| 1159 | t.Errorf("marshal: bad error type: %v", err) |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | // A slightly sneaky, yet valid, proto. It encodes the same required field twice, |
| 1163 | // so simply counting the required fields is insufficient. |
| 1164 | // field 1, encoding 2, value "hi" |
| 1165 | buf := []byte("\x0A\x02hi\x0A\x02hi") |
| 1166 | err = Unmarshal(buf, pb) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 1167 | if err == nil { |
| 1168 | t.Error("unmarshal: expected error, got nil") |
| 1169 | } else if strings.Index(err.String(), "GoTestField") < 0 { |
| 1170 | t.Errorf("unmarshal: bad error type: %v", err) |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | |
| David Symonds | 03c9d41 | 2010-08-26 14:23:18 +1000 | [diff] [blame] | 1174 | // A type that implements the Marshaler interface, but is not nillable. |
| 1175 | type nonNillableInt uint64 |
| 1176 | |
| 1177 | func (nni nonNillableInt) Marshal() ([]byte, os.Error) { |
| 1178 | return EncodeVarint(uint64(nni)), nil |
| 1179 | } |
| 1180 | |
| 1181 | type NNIMessage struct { |
| 1182 | nni nonNillableInt |
| 1183 | } |
| 1184 | |
| 1185 | // A type that implements the Marshaler interface and is nillable. |
| 1186 | type nillableMessage struct { |
| 1187 | x uint64 |
| 1188 | } |
| 1189 | |
| 1190 | func (nm *nillableMessage) Marshal() ([]byte, os.Error) { |
| 1191 | return EncodeVarint(nm.x), nil |
| 1192 | } |
| 1193 | |
| 1194 | type NMMessage struct { |
| 1195 | nm *nillableMessage |
| 1196 | } |
| 1197 | |
| 1198 | // Verify a type that uses the Marshaler interface, but has a nil pointer. |
| 1199 | func TestNilMarshaler(t *testing.T) { |
| 1200 | // Try a struct with a Marshaler field that is nil. |
| 1201 | // It should be directly marshable. |
| 1202 | nmm := new(NMMessage) |
| 1203 | if _, err := Marshal(nmm); err != nil { |
| 1204 | t.Error("unexpected error marshaling nmm: ", err) |
| 1205 | } |
| 1206 | |
| 1207 | // Try a struct with a Marshaler field that is not nillable. |
| 1208 | nnim := new(NNIMessage) |
| 1209 | nnim.nni = 7 |
| 1210 | var _ Marshaler = nnim.nni // verify it is truly a Marshaler |
| 1211 | if _, err := Marshal(nnim); err != nil { |
| 1212 | t.Error("unexpected error marshaling nnim: ", err) |
| 1213 | } |
| 1214 | } |
| 1215 | |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 1216 | // Check that passing a struct to Marshal returns a good error, |
| 1217 | // rather than panicking. |
| 1218 | func TestStructMarshaling(t *testing.T) { |
| 1219 | _, err := Marshal(OtherMessage{}) |
| 1220 | if err != ErrNotPtr { |
| 1221 | t.Errorf("got %v, expected %v", err, ErrNotPtr) |
| 1222 | } |
| 1223 | } |
| 1224 | |
| David Symonds | b79d99b | 2011-08-29 16:38:49 +1000 | [diff] [blame] | 1225 | func TestAllSetDefaults(t *testing.T) { |
| 1226 | // Exercise SetDefaults with all scalar field types. |
| 1227 | m := &Defaults{ |
| 1228 | // NaN != NaN, so override that here. |
| 1229 | F_Nan: Float32(1.7), |
| 1230 | } |
| 1231 | expected := &Defaults{ |
| 1232 | F_Bool: Bool(true), |
| 1233 | F_Int32: Int32(32), |
| 1234 | F_Int64: Int64(64), |
| 1235 | F_Fixed32: Uint32(320), |
| 1236 | F_Fixed64: Uint64(640), |
| 1237 | F_Uint32: Uint32(3200), |
| 1238 | F_Uint64: Uint64(6400), |
| 1239 | F_Float: Float32(314159), |
| 1240 | F_Double: Float64(271828), |
| 1241 | F_String: String(`hello, "world!"` + "\n"), |
| 1242 | F_Bytes: []byte("Bignose"), |
| 1243 | F_Sint32: Int32(-32), |
| 1244 | F_Sint64: Int64(-64), |
| 1245 | F_Enum: NewDefaults_Color(Defaults_GREEN), |
| 1246 | F_Pinf: Float32(float32(math.Inf(1))), |
| 1247 | F_Ninf: Float32(float32(math.Inf(-1))), |
| 1248 | F_Nan: Float32(1.7), |
| 1249 | } |
| 1250 | SetDefaults(m) |
| 1251 | if !Equal(m, expected) { |
| 1252 | t.Errorf(" got %v\nwant %v", m, expected) |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | func TestSetDefaultsWithSetField(t *testing.T) { |
| 1257 | // Check that a set value is not overridden. |
| 1258 | m := &Defaults{ |
| 1259 | F_Int32: Int32(12), |
| 1260 | } |
| 1261 | SetDefaults(m) |
| 1262 | if v := GetInt32(m.F_Int32); v != 12 { |
| 1263 | t.Errorf("m.FInt32 = %v, want 12", v) |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | func TestSetDefaultsWithSubMessage(t *testing.T) { |
| 1268 | m := &OtherMessage{ |
| 1269 | Key: Int64(123), |
| 1270 | Inner: &InnerMessage{ |
| 1271 | Host: String("gopher"), |
| 1272 | }, |
| 1273 | } |
| 1274 | expected := &OtherMessage{ |
| 1275 | Key: Int64(123), |
| 1276 | Inner: &InnerMessage{ |
| 1277 | Host: String("gopher"), |
| 1278 | Port: Int32(4000), |
| 1279 | }, |
| 1280 | } |
| 1281 | SetDefaults(m) |
| 1282 | if !Equal(m, expected) { |
| 1283 | t.Errorf(" got %v\nwant %v", m, expected) |
| 1284 | } |
| 1285 | } |
| 1286 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1287 | func BenchmarkMarshal(b *testing.B) { |
| 1288 | b.StopTimer() |
| 1289 | |
| 1290 | pb := initGoTest(true) |
| 1291 | |
| 1292 | // Create an array |
| 1293 | const N = 1000 // Internally the library starts much smaller. |
| 1294 | pb.F_Int32Repeated = make([]int32, N) |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame^] | 1295 | pb.F_DoubleRepeated = make([]float64, N) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1296 | |
| 1297 | // Fill in the array with some values. |
| 1298 | for i := 0; i < N; i++ { |
| 1299 | pb.F_Int32Repeated[i] = int32(i) |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame^] | 1300 | pb.F_DoubleRepeated[i] = float64(i) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1301 | } |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame^] | 1302 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1303 | p := NewBuffer(nil) |
| 1304 | |
| 1305 | b.StartTimer() |
| 1306 | for i := 0; i < b.N; i++ { |
| 1307 | p.Reset() |
| 1308 | p.Marshal(pb) |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | func BenchmarkUnmarshal(b *testing.B) { |
| 1313 | b.StopTimer() |
| 1314 | |
| 1315 | pb := initGoTest(true) |
| 1316 | |
| 1317 | // Create an array |
| 1318 | const N = 1000 // Internally the library starts much smaller. |
| 1319 | pb.F_Int32Repeated = make([]int32, N) |
| 1320 | |
| 1321 | // Fill in the array with some values. |
| 1322 | for i := 0; i < N; i++ { |
| 1323 | pb.F_Int32Repeated[i] = int32(i) |
| 1324 | } |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1325 | pbd := new(GoTest) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1326 | p := NewBuffer(nil) |
| 1327 | p.Marshal(pb) |
| 1328 | p2 := NewBuffer(nil) |
| 1329 | |
| 1330 | b.StartTimer() |
| 1331 | for i := 0; i < b.N; i++ { |
| 1332 | p2.SetBuf(p.Bytes()) |
| 1333 | p2.Unmarshal(pbd) |
| 1334 | } |
| 1335 | } |