~ubuntu-branches/ubuntu/utopic/golang/utopic

« back to all changes in this revision

Viewing changes to src/pkg/net/http/httputil/chunked_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:
11
11
 
12
12
import (
13
13
        "bytes"
 
14
        "fmt"
 
15
        "io"
14
16
        "io/ioutil"
 
17
        "runtime"
15
18
        "testing"
16
19
)
17
20
 
39
42
                t.Errorf("chunk reader read %q; want %q", g, e)
40
43
        }
41
44
}
 
45
 
 
46
func TestChunkReaderAllocs(t *testing.T) {
 
47
        // temporarily set GOMAXPROCS to 1 as we are testing memory allocations
 
48
        defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
 
49
        var buf bytes.Buffer
 
50
        w := NewChunkedWriter(&buf)
 
51
        a, b, c := []byte("aaaaaa"), []byte("bbbbbbbbbbbb"), []byte("cccccccccccccccccccccccc")
 
52
        w.Write(a)
 
53
        w.Write(b)
 
54
        w.Write(c)
 
55
        w.Close()
 
56
 
 
57
        r := NewChunkedReader(&buf)
 
58
        readBuf := make([]byte, len(a)+len(b)+len(c)+1)
 
59
 
 
60
        var ms runtime.MemStats
 
61
        runtime.ReadMemStats(&ms)
 
62
        m0 := ms.Mallocs
 
63
 
 
64
        n, err := io.ReadFull(r, readBuf)
 
65
 
 
66
        runtime.ReadMemStats(&ms)
 
67
        mallocs := ms.Mallocs - m0
 
68
        if mallocs > 1 {
 
69
                t.Errorf("%d mallocs; want <= 1", mallocs)
 
70
        }
 
71
 
 
72
        if n != len(readBuf)-1 {
 
73
                t.Errorf("read %d bytes; want %d", n, len(readBuf)-1)
 
74
        }
 
75
        if err != io.ErrUnexpectedEOF {
 
76
                t.Errorf("read error = %v; want ErrUnexpectedEOF", err)
 
77
        }
 
78
}
 
79
 
 
80
func TestParseHexUint(t *testing.T) {
 
81
        for i := uint64(0); i <= 1234; i++ {
 
82
                line := []byte(fmt.Sprintf("%x", i))
 
83
                got, err := parseHexUint(line)
 
84
                if err != nil {
 
85
                        t.Fatalf("on %d: %v", i, err)
 
86
                }
 
87
                if got != i {
 
88
                        t.Errorf("for input %q = %d; want %d", line, got, i)
 
89
                }
 
90
        }
 
91
        _, err := parseHexUint([]byte("bogus"))
 
92
        if err == nil {
 
93
                t.Error("expected error on bogus input")
 
94
        }
 
95
}