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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [encoding/] [hex/] [hex_test.go] - Blame information for rev 814

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 hex
6
 
7
import (
8
        "bytes"
9
        "testing"
10
)
11
 
12
type encDecTest struct {
13
        enc string
14
        dec []byte
15
}
16
 
17
var encDecTests = []encDecTest{
18
        {"", []byte{}},
19
        {"0001020304050607", []byte{0, 1, 2, 3, 4, 5, 6, 7}},
20
        {"08090a0b0c0d0e0f", []byte{8, 9, 10, 11, 12, 13, 14, 15}},
21
        {"f0f1f2f3f4f5f6f7", []byte{0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7}},
22
        {"f8f9fafbfcfdfeff", []byte{0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}},
23
        {"67", []byte{'g'}},
24
        {"e3a1", []byte{0xe3, 0xa1}},
25
}
26
 
27
func TestEncode(t *testing.T) {
28
        for i, test := range encDecTests {
29
                dst := make([]byte, EncodedLen(len(test.dec)))
30
                n := Encode(dst, test.dec)
31
                if n != len(dst) {
32
                        t.Errorf("#%d: bad return value: got: %d want: %d", i, n, len(dst))
33
                }
34
                if string(dst) != test.enc {
35
                        t.Errorf("#%d: got: %#v want: %#v", i, dst, test.enc)
36
                }
37
        }
38
}
39
 
40
func TestDecode(t *testing.T) {
41
        for i, test := range encDecTests {
42
                dst := make([]byte, DecodedLen(len(test.enc)))
43
                n, err := Decode(dst, []byte(test.enc))
44
                if err != nil {
45
                        t.Errorf("#%d: bad return value: got:%d want:%d", i, n, len(dst))
46
                } else if !bytes.Equal(dst, test.dec) {
47
                        t.Errorf("#%d: got: %#v want: %#v", i, dst, test.dec)
48
                }
49
        }
50
}
51
 
52
func TestEncodeToString(t *testing.T) {
53
        for i, test := range encDecTests {
54
                s := EncodeToString(test.dec)
55
                if s != test.enc {
56
                        t.Errorf("#%d got:%s want:%s", i, s, test.enc)
57
                }
58
        }
59
}
60
 
61
func TestDecodeString(t *testing.T) {
62
        for i, test := range encDecTests {
63
                dst, err := DecodeString(test.enc)
64
                if err != nil {
65
                        t.Errorf("#%d: unexpected err value: %s", i, err)
66
                        continue
67
                }
68
                if bytes.Compare(dst, test.dec) != 0 {
69
                        t.Errorf("#%d: got: %#v want: #%v", i, dst, test.dec)
70
                }
71
        }
72
}
73
 
74
type errTest struct {
75
        in  string
76
        err string
77
}
78
 
79
var errTests = []errTest{
80
        {"0", "encoding/hex: odd length hex string"},
81
        {"0g", "encoding/hex: invalid byte: U+0067 'g'"},
82
        {"0\x01", "encoding/hex: invalid byte: U+0001"},
83
}
84
 
85
func TestInvalidErr(t *testing.T) {
86
        for i, test := range errTests {
87
                dst := make([]byte, DecodedLen(len(test.in)))
88
                _, err := Decode(dst, []byte(test.in))
89
                if err == nil {
90
                        t.Errorf("#%d: expected error; got none")
91
                } else if err.Error() != test.err {
92
                        t.Errorf("#%d: got: %v want: %v", i, err, test.err)
93
                }
94
        }
95
}
96
 
97
func TestInvalidStringErr(t *testing.T) {
98
        for i, test := range errTests {
99
                _, err := DecodeString(test.in)
100
                if err == nil {
101
                        t.Errorf("#%d: expected error; got none")
102
                } else if err.Error() != test.err {
103
                        t.Errorf("#%d: got: %v want: %v", i, err, test.err)
104
                }
105
        }
106
}
107
 
108
func TestDumper(t *testing.T) {
109
        var in [40]byte
110
        for i := range in {
111
                in[i] = byte(i + 30)
112
        }
113
 
114
        for stride := 1; stride < len(in); stride++ {
115
                var out bytes.Buffer
116
                dumper := Dumper(&out)
117
                done := 0
118
                for done < len(in) {
119
                        todo := done + stride
120
                        if todo > len(in) {
121
                                todo = len(in)
122
                        }
123
                        dumper.Write(in[done:todo])
124
                        done = todo
125
                }
126
 
127
                dumper.Close()
128
                if !bytes.Equal(out.Bytes(), expectedHexDump) {
129
                        t.Errorf("stride: %d failed. got:\n%s\nwant:\n%s", stride, out.Bytes(), expectedHexDump)
130
                }
131
        }
132
}
133
 
134
func TestDump(t *testing.T) {
135
        var in [40]byte
136
        for i := range in {
137
                in[i] = byte(i + 30)
138
        }
139
 
140
        out := []byte(Dump(in[:]))
141
        if !bytes.Equal(out, expectedHexDump) {
142
                t.Errorf("got:\n%s\nwant:\n%s", out, expectedHexDump)
143
        }
144
}
145
 
146
var expectedHexDump = []byte(`00000000  1e 1f 20 21 22 23 24 25  26 27 28 29 2a 2b 2c 2d  |.. !"#$%&'()*+,-|
147
00000010  2e 2f 30 31 32 33 34 35  36 37 38 39 3a 3b 3c 3d  |./0123456789:;<=|
148
00000020  3e 3f 40 41 42 43 44 45                           |>?@ABCDE|
149
`)

powered by: WebSVN 2.1.0

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