1 // 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.
14 type testpair
struct {
15 decoded
, encoded
string
18 var pairs
= []testpair
{
19 // Encode returns 0 when len(src) is 0
26 "Man is distinguished, not only by his reason, but by this singular passion from " +
27 "other animals, which is a lust of the mind, that by a perseverance of delight in " +
28 "the continued and indefatigable generation of knowledge, exceeds the short " +
29 "vehemence of any carnal pleasure.",
30 "9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>Cj@.4Gp$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,\n" +
31 "O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF\"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKY\n" +
32 "i(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIa\n" +
33 "l(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G\n" +
34 ">uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c\n",
36 // Special case when shortening !!!!! to z.
43 var bigtest
= pairs
[len(pairs
)-1]
45 func testEqual(t
*testing
.T
, msg
string, args
...interface{}) bool {
46 if args
[len(args
)-2] != args
[len(args
)-1] {
47 t
.Errorf(msg
, args
...)
53 func strip85(s
string) string {
54 t
:= make([]byte, len(s
))
56 for r
:= 0; r
< len(s
); r
++ {
66 func TestEncode(t
*testing
.T
) {
67 for _
, p
:= range pairs
{
68 buf
:= make([]byte, MaxEncodedLen(len(p
.decoded
)))
69 n
:= Encode(buf
, []byte(p
.decoded
))
71 testEqual(t
, "Encode(%q) = %q, want %q", p
.decoded
, strip85(string(buf
)), strip85(p
.encoded
))
75 func TestEncoder(t
*testing
.T
) {
76 for _
, p
:= range pairs
{
78 encoder
:= NewEncoder(bb
)
79 encoder
.Write([]byte(p
.decoded
))
81 testEqual(t
, "Encode(%q) = %q, want %q", p
.decoded
, strip85(bb
.String()), strip85(p
.encoded
))
85 func TestEncoderBuffering(t
*testing
.T
) {
86 input
:= []byte(bigtest
.decoded
)
87 for bs
:= 1; bs
<= 12; bs
++ {
89 encoder
:= NewEncoder(bb
)
90 for pos
:= 0; pos
< len(input
); pos
+= bs
{
95 n
, err
:= encoder
.Write(input
[pos
:end
])
96 testEqual(t
, "Write(%q) gave error %v, want %v", input
[pos
:end
], err
, error(nil))
97 testEqual(t
, "Write(%q) gave length %v, want %v", input
[pos
:end
], n
, end
-pos
)
99 err
:= encoder
.Close()
100 testEqual(t
, "Close gave error %v, want %v", err
, error(nil))
101 testEqual(t
, "Encoding/%d of %q = %q, want %q", bs
, bigtest
.decoded
, strip85(bb
.String()), strip85(bigtest
.encoded
))
105 func TestDecode(t
*testing
.T
) {
106 for _
, p
:= range pairs
{
107 dbuf
:= make([]byte, 4*len(p
.encoded
))
108 ndst
, nsrc
, err
:= Decode(dbuf
, []byte(p
.encoded
), true)
109 testEqual(t
, "Decode(%q) = error %v, want %v", p
.encoded
, err
, error(nil))
110 testEqual(t
, "Decode(%q) = nsrc %v, want %v", p
.encoded
, nsrc
, len(p
.encoded
))
111 testEqual(t
, "Decode(%q) = ndst %v, want %v", p
.encoded
, ndst
, len(p
.decoded
))
112 testEqual(t
, "Decode(%q) = %q, want %q", p
.encoded
, string(dbuf
[0:ndst
]), p
.decoded
)
116 func TestDecoder(t
*testing
.T
) {
117 for _
, p
:= range pairs
{
118 decoder
:= NewDecoder(bytes
.NewBufferString(p
.encoded
))
119 dbuf
, err
:= ioutil
.ReadAll(decoder
)
121 t
.Fatal("Read failed", err
)
123 testEqual(t
, "Read from %q = length %v, want %v", p
.encoded
, len(dbuf
), len(p
.decoded
))
124 testEqual(t
, "Decoding of %q = %q, want %q", p
.encoded
, string(dbuf
), p
.decoded
)
126 testEqual(t
, "Read from %q = %v, want %v", p
.encoded
, err
, io
.EOF
)
131 func TestDecoderBuffering(t
*testing
.T
) {
132 for bs
:= 1; bs
<= 12; bs
++ {
133 decoder
:= NewDecoder(bytes
.NewBufferString(bigtest
.encoded
))
134 buf
:= make([]byte, len(bigtest
.decoded
)+12)
136 for total
= 0; total
< len(bigtest
.decoded
); {
137 n
, err
:= decoder
.Read(buf
[total
: total
+bs
])
138 testEqual(t
, "Read from %q at pos %d = %d, %v, want _, %v", bigtest
.encoded
, total
, n
, err
, error(nil))
141 testEqual(t
, "Decoding/%d of %q = %q, want %q", bs
, bigtest
.encoded
, string(buf
[0:total
]), bigtest
.decoded
)
145 func TestDecodeCorrupt(t
*testing
.T
) {
146 type corrupt
struct {
150 examples
:= []corrupt
{
155 for _
, e
:= range examples
{
156 dbuf
:= make([]byte, 4*len(e
.e
))
157 _
, _
, err
:= Decode(dbuf
, []byte(e
.e
), true)
158 switch err
:= err
.(type) {
159 case CorruptInputError
:
160 testEqual(t
, "Corruption in %q at offset %v, want %v", e
.e
, int(err
), e
.p
)
162 t
.Error("Decoder failed to detect corruption in", e
)
167 func TestBig(t
*testing
.T
) {
169 raw
:= make([]byte, n
)
170 const alpha
= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
171 for i
:= 0; i
< n
; i
++ {
172 raw
[i
] = alpha
[i%len
(alpha
)]
174 encoded
:= new(bytes
.Buffer
)
175 w
:= NewEncoder(encoded
)
176 nn
, err
:= w
.Write(raw
)
177 if nn
!= n || err
!= nil {
178 t
.Fatalf("Encoder.Write(raw) = %d, %v want %d, nil", nn
, err
, n
)
182 t
.Fatalf("Encoder.Close() = %v want nil", err
)
184 decoded
, err
:= ioutil
.ReadAll(NewDecoder(encoded
))
186 t
.Fatalf("io.ReadAll(NewDecoder(...)): %v", err
)
189 if !bytes
.Equal(raw
, decoded
) {
191 for i
= 0; i
< len(decoded
) && i
< len(raw
); i
++ {
192 if decoded
[i
] != raw
[i
] {
196 t
.Errorf("Decode(Encode(%d-byte string)) failed at offset %d", n
, i
)