OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [encoding/] [binary/] [varint_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
package binary
6
 
7
import (
8
        "bytes"
9
        "io"
10
        "testing"
11
)
12
 
13
func testConstant(t *testing.T, w uint, max int) {
14
        buf := make([]byte, MaxVarintLen64)
15
        n := PutUvarint(buf, 1<
16
        if n != max {
17
                t.Errorf("MaxVarintLen%d = %d; want %d", w, max, n)
18
        }
19
}
20
 
21
func TestConstants(t *testing.T) {
22
        testConstant(t, 16, MaxVarintLen16)
23
        testConstant(t, 32, MaxVarintLen32)
24
        testConstant(t, 64, MaxVarintLen64)
25
}
26
 
27
func testVarint(t *testing.T, x int64) {
28
        buf := make([]byte, MaxVarintLen64)
29
        n := PutVarint(buf, x)
30
        y, m := Varint(buf[0:n])
31
        if x != y {
32
                t.Errorf("Varint(%d): got %d", x, y)
33
        }
34
        if n != m {
35
                t.Errorf("Varint(%d): got n = %d; want %d", x, m, n)
36
        }
37
 
38
        y, err := ReadVarint(bytes.NewBuffer(buf))
39
        if err != nil {
40
                t.Errorf("ReadVarint(%d): %s", x, err)
41
        }
42
        if x != y {
43
                t.Errorf("ReadVarint(%d): got %d", x, y)
44
        }
45
}
46
 
47
func testUvarint(t *testing.T, x uint64) {
48
        buf := make([]byte, MaxVarintLen64)
49
        n := PutUvarint(buf, x)
50
        y, m := Uvarint(buf[0:n])
51
        if x != y {
52
                t.Errorf("Uvarint(%d): got %d", x, y)
53
        }
54
        if n != m {
55
                t.Errorf("Uvarint(%d): got n = %d; want %d", x, m, n)
56
        }
57
 
58
        y, err := ReadUvarint(bytes.NewBuffer(buf))
59
        if err != nil {
60
                t.Errorf("ReadUvarint(%d): %s", x, err)
61
        }
62
        if x != y {
63
                t.Errorf("ReadUvarint(%d): got %d", x, y)
64
        }
65
}
66
 
67
var tests = []int64{
68
        -1 << 63,
69
        -1<<63 + 1,
70
        -1,
71
        0,
72
        1,
73
        2,
74
        10,
75
        20,
76
        63,
77
        64,
78
        65,
79
        127,
80
        128,
81
        129,
82
        255,
83
        256,
84
        257,
85
        1<<63 - 1,
86
}
87
 
88
func TestVarint(t *testing.T) {
89
        for _, x := range tests {
90
                testVarint(t, x)
91
                testVarint(t, -x)
92
        }
93
        for x := int64(0x7); x != 0; x <<= 1 {
94
                testVarint(t, x)
95
                testVarint(t, -x)
96
        }
97
}
98
 
99
func TestUvarint(t *testing.T) {
100
        for _, x := range tests {
101
                testUvarint(t, uint64(x))
102
        }
103
        for x := uint64(0x7); x != 0; x <<= 1 {
104
                testUvarint(t, x)
105
        }
106
}
107
 
108
func TestBufferTooSmall(t *testing.T) {
109
        buf := []byte{0x80, 0x80, 0x80, 0x80}
110
        for i := 0; i <= len(buf); i++ {
111
                buf := buf[0:i]
112
                x, n := Uvarint(buf)
113
                if x != 0 || n != 0 {
114
                        t.Errorf("Uvarint(%v): got x = %d, n = %d", buf, x, n)
115
                }
116
 
117
                x, err := ReadUvarint(bytes.NewBuffer(buf))
118
                if x != 0 || err != io.EOF {
119
                        t.Errorf("ReadUvarint(%v): got x = %d, err = %s", buf, x, err)
120
                }
121
        }
122
}
123
 
124
func testOverflow(t *testing.T, buf []byte, n0 int, err0 error) {
125
        x, n := Uvarint(buf)
126
        if x != 0 || n != n0 {
127
                t.Errorf("Uvarint(%v): got x = %d, n = %d; want 0, %d", buf, x, n, n0)
128
        }
129
 
130
        x, err := ReadUvarint(bytes.NewBuffer(buf))
131
        if x != 0 || err != err0 {
132
                t.Errorf("ReadUvarint(%v): got x = %d, err = %s; want 0, %s", buf, x, err, err0)
133
        }
134
}
135
 
136
func TestOverflow(t *testing.T) {
137
        testOverflow(t, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x2}, -10, overflow)
138
        testOverflow(t, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x1, 0, 0}, -13, overflow)
139
}
140
 
141
func TestNonCanonicalZero(t *testing.T) {
142
        buf := []byte{0x80, 0x80, 0x80, 0}
143
        x, n := Uvarint(buf)
144
        if x != 0 || n != 4 {
145
                t.Errorf("Uvarint(%v): got x = %d, n = %d; want 0, 4", buf, x, n)
146
 
147
        }
148
}
149
 
150
func BenchmarkPutUvarint32(b *testing.B) {
151
        buf := make([]byte, MaxVarintLen32)
152
        b.SetBytes(4)
153
        for i := 0; i < b.N; i++ {
154
                for j := uint(0); j < MaxVarintLen32; j++ {
155
                        PutUvarint(buf, 1<<(j*7))
156
                }
157
        }
158
}
159
 
160
func BenchmarkPutUvarint64(b *testing.B) {
161
        buf := make([]byte, MaxVarintLen64)
162
        b.SetBytes(8)
163
        for i := 0; i < b.N; i++ {
164
                for j := uint(0); j < MaxVarintLen64; j++ {
165
                        PutUvarint(buf, 1<<(j*7))
166
                }
167
        }
168
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.