Browse Source
Merge pull request #284 from thinxer/binary
Merge pull request #284 from thinxer/binary
replace util/bytes.go with binary.BigEndian (again)pull/285/head
12 changed files with 123 additions and 117 deletions
-
7go/operation/sync_volume.go
-
8go/storage/compact_map_perf_test.go
-
6go/storage/crc.go
-
6go/storage/file_id.go
-
29go/storage/needle.go
-
15go/storage/needle_map.go
-
17go/storage/needle_map_boltdb.go
-
16go/storage/needle_map_leveldb.go
-
40go/storage/needle_read_write.go
-
45go/storage/needle_test.go
-
6go/storage/volume_super_block.go
-
45go/util/bytes.go
@ -0,0 +1,45 @@ |
|||||
|
package storage |
||||
|
|
||||
|
import "testing" |
||||
|
|
||||
|
func TestParseKeyHash(t *testing.T) { |
||||
|
testcases := []struct { |
||||
|
KeyHash string |
||||
|
ID uint64 |
||||
|
Cookie uint32 |
||||
|
Err bool |
||||
|
}{ |
||||
|
// normal
|
||||
|
{"4ed4c8116e41", 0x4ed4, 0xc8116e41, false}, |
||||
|
// cookie with leading zeros
|
||||
|
{"4ed401116e41", 0x4ed4, 0x01116e41, false}, |
||||
|
// odd length
|
||||
|
{"ed400116e41", 0xed4, 0x00116e41, false}, |
||||
|
// uint
|
||||
|
{"fed4c8114ed4c811f0116e41", 0xfed4c8114ed4c811, 0xf0116e41, false}, |
||||
|
// err: too short
|
||||
|
{"4ed4c811", 0, 0, true}, |
||||
|
// err: too long
|
||||
|
{"4ed4c8114ed4c8114ed4c8111", 0, 0, true}, |
||||
|
// err: invalid character
|
||||
|
{"helloworld", 0, 0, true}, |
||||
|
} |
||||
|
|
||||
|
for _, tc := range testcases { |
||||
|
if id, cookie, err := ParseKeyHash(tc.KeyHash); err != nil && !tc.Err { |
||||
|
t.Fatalf("Parse %s error: %v", tc.KeyHash, err) |
||||
|
} else if err == nil && tc.Err { |
||||
|
t.Fatalf("Parse %s expected error got nil", tc.KeyHash) |
||||
|
} else if id != tc.ID || cookie != tc.Cookie { |
||||
|
t.Fatalf("Parse %s wrong result. Expected: (%d, %d) got: (%d, %d)", tc.KeyHash, tc.ID, tc.Cookie, id, cookie) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func BenchmarkParseKeyHash(b *testing.B) { |
||||
|
b.ReportAllocs() |
||||
|
|
||||
|
for i := 0; i < b.N; i++ { |
||||
|
ParseKeyHash("4ed44ed44ed44ed4c8116e41") |
||||
|
} |
||||
|
} |
@ -1,45 +0,0 @@ |
|||||
package util |
|
||||
|
|
||||
// big endian
|
|
||||
|
|
||||
func BytesToUint64(b []byte) (v uint64) { |
|
||||
length := uint(len(b)) |
|
||||
for i := uint(0); i < length-1; i++ { |
|
||||
v += uint64(b[i]) |
|
||||
v <<= 8 |
|
||||
} |
|
||||
v += uint64(b[length-1]) |
|
||||
return |
|
||||
} |
|
||||
func BytesToUint32(b []byte) (v uint32) { |
|
||||
length := uint(len(b)) |
|
||||
for i := uint(0); i < length-1; i++ { |
|
||||
v += uint32(b[i]) |
|
||||
v <<= 8 |
|
||||
} |
|
||||
v += uint32(b[length-1]) |
|
||||
return |
|
||||
} |
|
||||
func BytesToUint16(b []byte) (v uint16) { |
|
||||
v += uint16(b[0]) |
|
||||
v <<= 8 |
|
||||
v += uint16(b[1]) |
|
||||
return |
|
||||
} |
|
||||
func Uint64toBytes(b []byte, v uint64) { |
|
||||
for i := uint(0); i < 8; i++ { |
|
||||
b[7-i] = byte(v >> (i * 8)) |
|
||||
} |
|
||||
} |
|
||||
func Uint32toBytes(b []byte, v uint32) { |
|
||||
for i := uint(0); i < 4; i++ { |
|
||||
b[3-i] = byte(v >> (i * 8)) |
|
||||
} |
|
||||
} |
|
||||
func Uint16toBytes(b []byte, v uint16) { |
|
||||
b[0] = byte(v >> 8) |
|
||||
b[1] = byte(v) |
|
||||
} |
|
||||
func Uint8toBytes(b []byte, v uint8) { |
|
||||
b[0] = byte(v) |
|
||||
} |
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue