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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [encoding/] [base32/] [base32_test.go] - Blame information for rev 774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 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 base32
6
 
7
import (
8
        "bytes"
9
        "io"
10
        "io/ioutil"
11
        "testing"
12
)
13
 
14
type testpair struct {
15
        decoded, encoded string
16
}
17
 
18
var pairs = []testpair{
19
        // RFC 4648 examples
20
        {"", ""},
21
        {"f", "MY======"},
22
        {"fo", "MZXQ===="},
23
        {"foo", "MZXW6==="},
24
        {"foob", "MZXW6YQ="},
25
        {"fooba", "MZXW6YTB"},
26
        {"foobar", "MZXW6YTBOI======"},
27
 
28
        // Wikipedia examples, converted to base32
29
        {"sure.", "ON2XEZJO"},
30
        {"sure", "ON2XEZI="},
31
        {"sur", "ON2XE==="},
32
        {"su", "ON2Q===="},
33
        {"leasure.", "NRSWC43VOJSS4==="},
34
        {"easure.", "MVQXG5LSMUXA===="},
35
        {"asure.", "MFZXK4TFFY======"},
36
        {"sure.", "ON2XEZJO"},
37
}
38
 
39
var bigtest = testpair{
40
        "Twas brillig, and the slithy toves",
41
        "KR3WC4ZAMJZGS3DMNFTSYIDBNZSCA5DIMUQHG3DJORUHSIDUN53GK4Y=",
42
}
43
 
44
func testEqual(t *testing.T, msg string, args ...interface{}) bool {
45
        if args[len(args)-2] != args[len(args)-1] {
46
                t.Errorf(msg, args...)
47
                return false
48
        }
49
        return true
50
}
51
 
52
func TestEncode(t *testing.T) {
53
        for _, p := range pairs {
54
                got := StdEncoding.EncodeToString([]byte(p.decoded))
55
                testEqual(t, "Encode(%q) = %q, want %q", p.decoded, got, p.encoded)
56
        }
57
}
58
 
59
func TestEncoder(t *testing.T) {
60
        for _, p := range pairs {
61
                bb := &bytes.Buffer{}
62
                encoder := NewEncoder(StdEncoding, bb)
63
                encoder.Write([]byte(p.decoded))
64
                encoder.Close()
65
                testEqual(t, "Encode(%q) = %q, want %q", p.decoded, bb.String(), p.encoded)
66
        }
67
}
68
 
69
func TestEncoderBuffering(t *testing.T) {
70
        input := []byte(bigtest.decoded)
71
        for bs := 1; bs <= 12; bs++ {
72
                bb := &bytes.Buffer{}
73
                encoder := NewEncoder(StdEncoding, bb)
74
                for pos := 0; pos < len(input); pos += bs {
75
                        end := pos + bs
76
                        if end > len(input) {
77
                                end = len(input)
78
                        }
79
                        n, err := encoder.Write(input[pos:end])
80
                        testEqual(t, "Write(%q) gave error %v, want %v", input[pos:end], err, error(nil))
81
                        testEqual(t, "Write(%q) gave length %v, want %v", input[pos:end], n, end-pos)
82
                }
83
                err := encoder.Close()
84
                testEqual(t, "Close gave error %v, want %v", err, error(nil))
85
                testEqual(t, "Encoding/%d of %q = %q, want %q", bs, bigtest.decoded, bb.String(), bigtest.encoded)
86
        }
87
}
88
 
89
func TestDecode(t *testing.T) {
90
        for _, p := range pairs {
91
                dbuf := make([]byte, StdEncoding.DecodedLen(len(p.encoded)))
92
                count, end, err := StdEncoding.decode(dbuf, []byte(p.encoded))
93
                testEqual(t, "Decode(%q) = error %v, want %v", p.encoded, err, error(nil))
94
                testEqual(t, "Decode(%q) = length %v, want %v", p.encoded, count, len(p.decoded))
95
                if len(p.encoded) > 0 {
96
                        testEqual(t, "Decode(%q) = end %v, want %v", p.encoded, end, (p.encoded[len(p.encoded)-1] == '='))
97
                }
98
                testEqual(t, "Decode(%q) = %q, want %q", p.encoded,
99
                        string(dbuf[0:count]),
100
                        p.decoded)
101
 
102
                dbuf, err = StdEncoding.DecodeString(p.encoded)
103
                testEqual(t, "DecodeString(%q) = error %v, want %v", p.encoded, err, error(nil))
104
                testEqual(t, "DecodeString(%q) = %q, want %q", p.encoded, string(dbuf), p.decoded)
105
        }
106
}
107
 
108
func TestDecoder(t *testing.T) {
109
        for _, p := range pairs {
110
                decoder := NewDecoder(StdEncoding, bytes.NewBufferString(p.encoded))
111
                dbuf := make([]byte, StdEncoding.DecodedLen(len(p.encoded)))
112
                count, err := decoder.Read(dbuf)
113
                if err != nil && err != io.EOF {
114
                        t.Fatal("Read failed", err)
115
                }
116
                testEqual(t, "Read from %q = length %v, want %v", p.encoded, count, len(p.decoded))
117
                testEqual(t, "Decoding of %q = %q, want %q", p.encoded, string(dbuf[0:count]), p.decoded)
118
                if err != io.EOF {
119
                        count, err = decoder.Read(dbuf)
120
                }
121
                testEqual(t, "Read from %q = %v, want %v", p.encoded, err, io.EOF)
122
        }
123
}
124
 
125
func TestDecoderBuffering(t *testing.T) {
126
        for bs := 1; bs <= 12; bs++ {
127
                decoder := NewDecoder(StdEncoding, bytes.NewBufferString(bigtest.encoded))
128
                buf := make([]byte, len(bigtest.decoded)+12)
129
                var total int
130
                for total = 0; total < len(bigtest.decoded); {
131
                        n, err := decoder.Read(buf[total : total+bs])
132
                        testEqual(t, "Read from %q at pos %d = %d, %v, want _, %v", bigtest.encoded, total, n, err, error(nil))
133
                        total += n
134
                }
135
                testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded)
136
        }
137
}
138
 
139
func TestDecodeCorrupt(t *testing.T) {
140
        type corrupt struct {
141
                e string
142
                p int
143
        }
144
        examples := []corrupt{
145
                {"!!!!", 0},
146
                {"x===", 0},
147
                {"AA=A====", 2},
148
                {"AAA=AAAA", 3},
149
                {"MMMMMMMMM", 8},
150
                {"MMMMMM", 0},
151
        }
152
 
153
        for _, e := range examples {
154
                dbuf := make([]byte, StdEncoding.DecodedLen(len(e.e)))
155
                _, err := StdEncoding.Decode(dbuf, []byte(e.e))
156
                switch err := err.(type) {
157
                case CorruptInputError:
158
                        testEqual(t, "Corruption in %q at offset %v, want %v", e.e, int(err), e.p)
159
                default:
160
                        t.Error("Decoder failed to detect corruption in", e)
161
                }
162
        }
163
}
164
 
165
func TestBig(t *testing.T) {
166
        n := 3*1000 + 1
167
        raw := make([]byte, n)
168
        const alpha = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
169
        for i := 0; i < n; i++ {
170
                raw[i] = alpha[i%len(alpha)]
171
        }
172
        encoded := new(bytes.Buffer)
173
        w := NewEncoder(StdEncoding, encoded)
174
        nn, err := w.Write(raw)
175
        if nn != n || err != nil {
176
                t.Fatalf("Encoder.Write(raw) = %d, %v want %d, nil", nn, err, n)
177
        }
178
        err = w.Close()
179
        if err != nil {
180
                t.Fatalf("Encoder.Close() = %v want nil", err)
181
        }
182
        decoded, err := ioutil.ReadAll(NewDecoder(StdEncoding, encoded))
183
        if err != nil {
184
                t.Fatalf("ioutil.ReadAll(NewDecoder(...)): %v", err)
185
        }
186
 
187
        if !bytes.Equal(raw, decoded) {
188
                var i int
189
                for i = 0; i < len(decoded) && i < len(raw); i++ {
190
                        if decoded[i] != raw[i] {
191
                                break
192
                        }
193
                }
194
                t.Errorf("Decode(Encode(%d-byte string)) failed at offset %d", n, i)
195
        }
196
}
197
 
198
func TestNewLineCharacters(t *testing.T) {
199
        // Each of these should decode to the string "sure", without errors.
200
        const expected = "sure"
201
        examples := []string{
202
                "ON2XEZI=",
203
                "ON2XEZI=\r",
204
                "ON2XEZI=\n",
205
                "ON2XEZI=\r\n",
206
                "ON2XEZ\r\nI=",
207
                "ON2X\rEZ\nI=",
208
                "ON2X\nEZ\rI=",
209
                "ON2XEZ\nI=",
210
                "ON2XEZI\n=",
211
        }
212
        for _, e := range examples {
213
                buf, err := StdEncoding.DecodeString(e)
214
                if err != nil {
215
                        t.Errorf("Decode(%q) failed: %v", e, err)
216
                        continue
217
                }
218
                if s := string(buf); s != expected {
219
                        t.Errorf("Decode(%q) = %q, want %q", e, s, expected)
220
                }
221
        }
222
}

powered by: WebSVN 2.1.0

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