~ubuntu-branches/ubuntu/trusty/golang/trusty

« back to all changes in this revision

Viewing changes to src/pkg/encoding/gob/gobencdec_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 20011 The Go Authors. All rights reserved.
 
1
// Copyright 2011 The Go Authors. All rights reserved.
2
2
// Use of this source code is governed by a BSD-style
3
3
// license that can be found in the LICENSE file.
4
4
 
142
142
        V *ValueGobber
143
143
}
144
144
 
 
145
type GobTest6 struct {
 
146
        X int // guarantee we have  something in common with GobTest*
 
147
        V ValueGobber
 
148
        W *ValueGobber
 
149
}
 
150
 
 
151
type GobTest7 struct {
 
152
        X int // guarantee we have  something in common with GobTest*
 
153
        V *ValueGobber
 
154
        W ValueGobber
 
155
}
 
156
 
145
157
type GobTestIgnoreEncoder struct {
146
158
        X int // guarantee we have  something in common with GobTest*
147
159
}
336
348
                t.Fatal("decode error:", err)
337
349
        }
338
350
        if y.G.s != "XYZ" {
339
 
                t.Fatalf("expected `XYZ` got %c", y.G.s)
 
351
                t.Fatalf("expected `XYZ` got %q", y.G.s)
340
352
        }
341
353
}
342
354
 
360
372
        }
361
373
}
362
374
 
 
375
// Test that we can use a value then a pointer type of a GobEncoder
 
376
// in the same encoded value.  Bug 4647.
 
377
func TestGobEncoderValueThenPointer(t *testing.T) {
 
378
        v := ValueGobber("forty-two")
 
379
        w := ValueGobber("six-by-nine")
 
380
 
 
381
        // this was a bug: encoding a GobEncoder by value before a GobEncoder
 
382
        // pointer would cause duplicate type definitions to be sent.
 
383
 
 
384
        b := new(bytes.Buffer)
 
385
        enc := NewEncoder(b)
 
386
        if err := enc.Encode(GobTest6{42, v, &w}); err != nil {
 
387
                t.Fatal("encode error:", err)
 
388
        }
 
389
        dec := NewDecoder(b)
 
390
        x := new(GobTest6)
 
391
        if err := dec.Decode(x); err != nil {
 
392
                t.Fatal("decode error:", err)
 
393
        }
 
394
        if got, want := x.V, v; got != want {
 
395
                t.Errorf("v = %q, want %q", got, want)
 
396
        }
 
397
        if got, want := x.W, w; got == nil {
 
398
                t.Errorf("w = nil, want %q", want)
 
399
        } else if *got != want {
 
400
                t.Errorf("w = %q, want %q", *got, want)
 
401
        }
 
402
}
 
403
 
 
404
// Test that we can use a pointer then a value type of a GobEncoder
 
405
// in the same encoded value.
 
406
func TestGobEncoderPointerThenValue(t *testing.T) {
 
407
        v := ValueGobber("forty-two")
 
408
        w := ValueGobber("six-by-nine")
 
409
 
 
410
        b := new(bytes.Buffer)
 
411
        enc := NewEncoder(b)
 
412
        if err := enc.Encode(GobTest7{42, &v, w}); err != nil {
 
413
                t.Fatal("encode error:", err)
 
414
        }
 
415
        dec := NewDecoder(b)
 
416
        x := new(GobTest7)
 
417
        if err := dec.Decode(x); err != nil {
 
418
                t.Fatal("decode error:", err)
 
419
        }
 
420
        if got, want := x.V, v; got == nil {
 
421
                t.Errorf("v = nil, want %q", want)
 
422
        } else if *got != want {
 
423
                t.Errorf("v = %q, want %q", got, want)
 
424
        }
 
425
        if got, want := x.W, w; got != want {
 
426
                t.Errorf("w = %q, want %q", got, want)
 
427
        }
 
428
}
 
429
 
363
430
func TestGobEncoderFieldTypeError(t *testing.T) {
364
431
        // GobEncoder to non-decoder: error
365
432
        b := new(bytes.Buffer)