OSDN Git Service

fix bug action & remove ctx
[bytom/bytom.git] / common / bytes_test.go
1 // Copyright 2014 The go-ethereum Authors
2 // This file is part of the go-ethereum library.
3 //
4 // The go-ethereum library is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // The go-ethereum library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16
17 package common
18
19 import (
20         "bytes"
21         "testing"
22
23         checker "gopkg.in/check.v1"
24 )
25
26 type BytesSuite struct{}
27
28 var _ = checker.Suite(&BytesSuite{})
29
30 func (s *BytesSuite) TestNumberToBytes(c *checker.C) {
31         // data1 := int(1)
32         // res1 := NumberToBytes(data1, 16)
33         // c.Check(res1, checker.Panics)
34
35         var data2 float64 = 3.141592653
36         exp2 := []byte{0xe9, 0x38}
37         res2 := NumberToBytes(data2, 16)
38         c.Assert(res2, checker.DeepEquals, exp2)
39 }
40
41 func (s *BytesSuite) TestBytesToNumber(c *checker.C) {
42         datasmall := []byte{0xe9, 0x38, 0xe9, 0x38}
43         datalarge := []byte{0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38}
44
45         var expsmall uint64 = 0xe938e938
46         var explarge uint64 = 0x0
47
48         ressmall := BytesToNumber(datasmall)
49         reslarge := BytesToNumber(datalarge)
50
51         c.Assert(ressmall, checker.Equals, expsmall)
52         c.Assert(reslarge, checker.Equals, explarge)
53
54 }
55
56 func (s *BytesSuite) TestReadVarInt(c *checker.C) {
57         data8 := []byte{1, 2, 3, 4, 5, 6, 7, 8}
58         data4 := []byte{1, 2, 3, 4}
59         data2 := []byte{1, 2}
60         data1 := []byte{1}
61
62         exp8 := uint64(72623859790382856)
63         exp4 := uint64(16909060)
64         exp2 := uint64(258)
65         exp1 := uint64(1)
66
67         res8 := ReadVarInt(data8)
68         res4 := ReadVarInt(data4)
69         res2 := ReadVarInt(data2)
70         res1 := ReadVarInt(data1)
71
72         c.Assert(res8, checker.Equals, exp8)
73         c.Assert(res4, checker.Equals, exp4)
74         c.Assert(res2, checker.Equals, exp2)
75         c.Assert(res1, checker.Equals, exp1)
76 }
77
78 func (s *BytesSuite) TestCopyBytes(c *checker.C) {
79         data1 := []byte{1, 2, 3, 4}
80         exp1 := []byte{1, 2, 3, 4}
81         res1 := CopyBytes(data1)
82         c.Assert(res1, checker.DeepEquals, exp1)
83 }
84
85 func (s *BytesSuite) TestIsHex(c *checker.C) {
86         data1 := "a9e67e"
87         exp1 := false
88         res1 := IsHex(data1)
89         c.Assert(res1, checker.DeepEquals, exp1)
90
91         data2 := "0xa9e67e00"
92         exp2 := true
93         res2 := IsHex(data2)
94         c.Assert(res2, checker.DeepEquals, exp2)
95
96 }
97
98 func (s *BytesSuite) TestParseDataString(c *checker.C) {
99         res1 := ParseData("hello", "world", "0x0106")
100         data := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000"
101         exp1 := Hex2Bytes(data)
102         c.Assert(res1, checker.DeepEquals, exp1)
103 }
104
105 func (s *BytesSuite) TestParseDataBytes(c *checker.C) {
106         data1 := []byte{232, 212, 165, 16, 0}
107         exp1 := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 232, 212, 165, 16, 0}
108
109         res1 := ParseData(data1)
110         c.Assert(res1, checker.DeepEquals, exp1)
111
112 }
113
114 func (s *BytesSuite) TestLeftPadBytes(c *checker.C) {
115         val1 := []byte{1, 2, 3, 4}
116         exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4}
117
118         res1 := LeftPadBytes(val1, 8)
119         res2 := LeftPadBytes(val1, 2)
120
121         c.Assert(res1, checker.DeepEquals, exp1)
122         c.Assert(res2, checker.DeepEquals, val1)
123 }
124
125 func (s *BytesSuite) TestFormatData(c *checker.C) {
126         data1 := ""
127         data2 := "0xa9e67e00"
128         data3 := "a9e67e"
129         data4 := "\"a9e67e00\""
130
131         // exp1 := []byte{}
132         exp2 := []byte{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0xa9, 0xe6, 0x7e, 00}
133         exp3 := []byte{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00}
134         exp4 := []byte{0x61, 0x39, 0x65, 0x36, 0x37, 0x65, 0x30, 0x30, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00}
135
136         res1 := FormatData(data1)
137         res2 := FormatData(data2)
138         res3 := FormatData(data3)
139         res4 := FormatData(data4)
140
141         c.Assert(res1, checker.IsNil)
142         c.Assert(res2, checker.DeepEquals, exp2)
143         c.Assert(res3, checker.DeepEquals, exp3)
144         c.Assert(res4, checker.DeepEquals, exp4)
145 }
146
147 func (s *BytesSuite) TestRightPadBytes(c *checker.C) {
148         val := []byte{1, 2, 3, 4}
149         exp := []byte{1, 2, 3, 4, 0, 0, 0, 0}
150
151         resstd := RightPadBytes(val, 8)
152         resshrt := RightPadBytes(val, 2)
153
154         c.Assert(resstd, checker.DeepEquals, exp)
155         c.Assert(resshrt, checker.DeepEquals, val)
156 }
157
158 func (s *BytesSuite) TestLeftPadString(c *checker.C) {
159         val := "test"
160         exp := "\x30\x30\x30\x30" + val
161
162         resstd := LeftPadString(val, 8)
163         resshrt := LeftPadString(val, 2)
164
165         c.Assert(resstd, checker.Equals, exp)
166         c.Assert(resshrt, checker.Equals, val)
167 }
168
169 func (s *BytesSuite) TestRightPadString(c *checker.C) {
170         val := "test"
171         exp := val + "\x30\x30\x30\x30"
172
173         resstd := RightPadString(val, 8)
174         resshrt := RightPadString(val, 2)
175
176         c.Assert(resstd, checker.Equals, exp)
177         c.Assert(resshrt, checker.Equals, val)
178 }
179
180 func TestFromHex(t *testing.T) {
181         input := "0x01"
182         expected := []byte{1}
183         result := FromHex(input)
184         if bytes.Compare(expected, result) != 0 {
185                 t.Errorf("Expected % x got % x", expected, result)
186         }
187 }
188
189 func TestFromHexOddLength(t *testing.T) {
190         input := "0x1"
191         expected := []byte{1}
192         result := FromHex(input)
193         if bytes.Compare(expected, result) != 0 {
194                 t.Errorf("Expected % x got % x", expected, result)
195         }
196 }