@@ -21,6 +21,8 @@ import (
21
21
"encoding/hex"
22
22
"math/big"
23
23
"testing"
24
+
25
+ "github.com/ethereum/go-ethereum/common"
24
26
)
25
27
26
28
func TestHexOrDecimal256 (t * testing.T ) {
@@ -133,8 +135,44 @@ func TestPaddedBigBytes(t *testing.T) {
133
135
}
134
136
}
135
137
136
- func BenchmarkPaddedBigBytes (b * testing.B ) {
138
+ func BenchmarkPaddedBigBytesLargePadding (b * testing.B ) {
137
139
bigint := MustParseBig256 ("123456789123456789123456789123456789" )
140
+ for i := 0 ; i < b .N ; i ++ {
141
+ PaddedBigBytes (bigint , 200 )
142
+ }
143
+ }
144
+
145
+ func BenchmarkPaddedBigBytesSmallPadding (b * testing.B ) {
146
+ bigint := MustParseBig256 ("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC" )
147
+ for i := 0 ; i < b .N ; i ++ {
148
+ PaddedBigBytes (bigint , 5 )
149
+ }
150
+ }
151
+
152
+ func BenchmarkPaddedBigBytesSmallOnePadding (b * testing.B ) {
153
+ bigint := MustParseBig256 ("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC" )
154
+ for i := 0 ; i < b .N ; i ++ {
155
+ PaddedBigBytes (bigint , 32 )
156
+ }
157
+ }
158
+
159
+ func BenchmarkByteAtBrandNew (b * testing.B ) {
160
+ bigint := MustParseBig256 ("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC" )
161
+ for i := 0 ; i < b .N ; i ++ {
162
+ bigEndianByteAt (bigint , 15 )
163
+ }
164
+ }
165
+
166
+ func BenchmarkByteAt (b * testing.B ) {
167
+ bigint := MustParseBig256 ("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC" )
168
+ for i := 0 ; i < b .N ; i ++ {
169
+ bigEndianByteAt (bigint , 15 )
170
+ }
171
+ }
172
+
173
+ func BenchmarkByteAtOld (b * testing.B ) {
174
+
175
+ bigint := MustParseBig256 ("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC" )
138
176
for i := 0 ; i < b .N ; i ++ {
139
177
PaddedBigBytes (bigint , 32 )
140
178
}
@@ -174,6 +212,65 @@ func TestU256(t *testing.T) {
174
212
}
175
213
}
176
214
215
+ func TestBigEndianByteAt (t * testing.T ) {
216
+ tests := []struct {
217
+ x string
218
+ y int
219
+ exp byte
220
+ }{
221
+ {"00" , 0 , 0x00 },
222
+ {"01" , 1 , 0x00 },
223
+ {"00" , 1 , 0x00 },
224
+ {"01" , 0 , 0x01 },
225
+ {"0000000000000000000000000000000000000000000000000000000000102030" , 0 , 0x30 },
226
+ {"0000000000000000000000000000000000000000000000000000000000102030" , 1 , 0x20 },
227
+ {"ABCDEF0908070605040302010000000000000000000000000000000000000000" , 31 , 0xAB },
228
+ {"ABCDEF0908070605040302010000000000000000000000000000000000000000" , 32 , 0x00 },
229
+ {"ABCDEF0908070605040302010000000000000000000000000000000000000000" , 500 , 0x00 },
230
+ }
231
+ for _ , test := range tests {
232
+ v := new (big.Int ).SetBytes (common .Hex2Bytes (test .x ))
233
+ actual := bigEndianByteAt (v , test .y )
234
+ if actual != test .exp {
235
+ t .Fatalf ("Expected [%v] %v:th byte to be %v, was %v." , test .x , test .y , test .exp , actual )
236
+ }
237
+
238
+ }
239
+ }
240
+ func TestLittleEndianByteAt (t * testing.T ) {
241
+ tests := []struct {
242
+ x string
243
+ y int
244
+ exp byte
245
+ }{
246
+ {"00" , 0 , 0x00 },
247
+ {"01" , 1 , 0x00 },
248
+ {"00" , 1 , 0x00 },
249
+ {"01" , 0 , 0x00 },
250
+ {"0000000000000000000000000000000000000000000000000000000000102030" , 0 , 0x00 },
251
+ {"0000000000000000000000000000000000000000000000000000000000102030" , 1 , 0x00 },
252
+ {"ABCDEF0908070605040302010000000000000000000000000000000000000000" , 31 , 0x00 },
253
+ {"ABCDEF0908070605040302010000000000000000000000000000000000000000" , 32 , 0x00 },
254
+ {"ABCDEF0908070605040302010000000000000000000000000000000000000000" , 0 , 0xAB },
255
+ {"ABCDEF0908070605040302010000000000000000000000000000000000000000" , 1 , 0xCD },
256
+ {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" , 0 , 0x00 },
257
+ {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" , 1 , 0xCD },
258
+ {"0000000000000000000000000000000000000000000000000000000000102030" , 31 , 0x30 },
259
+ {"0000000000000000000000000000000000000000000000000000000000102030" , 30 , 0x20 },
260
+ {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , 32 , 0x0 },
261
+ {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , 31 , 0xFF },
262
+ {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , 0xFFFF , 0x0 },
263
+ }
264
+ for _ , test := range tests {
265
+ v := new (big.Int ).SetBytes (common .Hex2Bytes (test .x ))
266
+ actual := Byte (v , 32 , test .y )
267
+ if actual != test .exp {
268
+ t .Fatalf ("Expected [%v] %v:th byte to be %v, was %v." , test .x , test .y , test .exp , actual )
269
+ }
270
+
271
+ }
272
+ }
273
+
177
274
func TestS256 (t * testing.T ) {
178
275
tests := []struct { x , y * big.Int }{
179
276
{x : big .NewInt (0 ), y : big .NewInt (0 )},
0 commit comments