| 1 |
package encoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"math" |
| 5 |
"math/rand" |
| 6 |
"strconv" |
| 7 |
"testing" |
| 8 |
"unsafe" |
| 9 |
) |
| 10 |
|
| 11 |
// The integers of every size are written as strconv writes them, around every power of 10 and at the limits of |
| 12 |
// their sizes, after the bytes of the buffer and into a buffer of any capacity. |
| 13 |
func TestAppendIntegers(t *testing.T) { |
| 14 |
var values []uint64 |
| 15 |
for _, p := range pow10 { |
| 16 |
values = append(values, p-1, p, p+1) |
| 17 |
} |
| 18 |
values = append(values, 0, math.MaxInt8, math.MaxUint8, math.MaxInt16, math.MaxUint16, math.MaxInt32, |
| 19 |
math.MaxUint32, math.MaxInt64, math.MaxUint64, 1<<63) |
| 20 |
r := rand.New(rand.NewSource(1)) |
| 21 |
for i := 0; i < 10000; i++ { |
| 22 |
values = append(values, r.Uint64()>>uint(r.Intn(64))) |
| 23 |
} |
| 24 |
for _, v := range values { |
| 25 |
for _, size := range []uint8{8, 16, 32, 64} { |
| 26 |
code := &Opcode{NumBitSize: size} |
| 27 |
mask := numMask(size) |
| 28 |
// the value of the size as a signed and as an unsigned integer |
| 29 |
signed := int64(v&mask) << (64 - size) >> (64 - size) |
| 30 |
for _, prefix := range [][]byte{nil, []byte("x"), make([]byte, 3, 4)} { |
| 31 |
buf := append([]byte(nil), prefix...) |
| 32 |
got := AppendInt(nil, buf, valueOfSize(v, size), code) |
| 33 |
if want := string(prefix) + strconv.FormatInt(signed, 10); string(got) != want { |
| 34 |
t.Fatalf("AppendInt of %d as int%d: got %q, want %q", v, size, got, want) |
| 35 |
} |
| 36 |
buf = append([]byte(nil), prefix...) |
| 37 |
got = AppendUint(nil, buf, valueOfSize(v, size), code) |
| 38 |
if want := string(prefix) + strconv.FormatUint(v&mask, 10); string(got) != want { |
| 39 |
t.Fatalf("AppendUint of %d as uint%d: got %q, want %q", v, size, got, want) |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// valueOfSize returns a pointer to the lowest size bits of v as a variable of that size, as the encoder is given |
| 47 |
// an integer of that size: they are the first bytes of v only on a little-endian machine. |
| 48 |
func valueOfSize(v uint64, size uint8) unsafe.Pointer { |
| 49 |
switch size { |
| 50 |
case 8: |
| 51 |
u := uint8(v) |
| 52 |
return unsafe.Pointer(&u) |
| 53 |
case 16: |
| 54 |
u := uint16(v) |
| 55 |
return unsafe.Pointer(&u) |
| 56 |
case 32: |
| 57 |
u := uint32(v) |
| 58 |
return unsafe.Pointer(&u) |
| 59 |
} |
| 60 |
return unsafe.Pointer(&v) |
| 61 |
} |
| 62 |
|
| 63 |
func TestDecimalDigits(t *testing.T) { |
| 64 |
for i, p := range pow10 { |
| 65 |
if got := decimalDigits(p); got != i+1 { |
| 66 |
t.Fatalf("digits of %d: got %d, want %d", p, got, i+1) |
| 67 |
} |
| 68 |
if p > 1 { |
| 69 |
if got := decimalDigits(p - 1); got != i { |
| 70 |
t.Fatalf("digits of %d: got %d, want %d", p-1, got, i) |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
if got := decimalDigits(0); got != 1 { |
| 75 |
t.Fatalf("digits of 0: got %d", got) |
| 76 |
} |
| 77 |
if got := decimalDigits(math.MaxUint64); got != 20 { |
| 78 |
t.Fatalf("digits of the largest uint64: got %d", got) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func BenchmarkAppendInt(b *testing.B) { |
| 83 |
values := []int64{0, 7, 42, -6, 486, 123, -456, 98765, 123456, -12345678, 1234567890, -9876543210123, math.MinInt64} |
| 84 |
code := &Opcode{NumBitSize: 64} |
| 85 |
buf := make([]byte, 0, 64) |
| 86 |
for _, v := range values { |
| 87 |
v := v |
| 88 |
b.Run(strconv.FormatInt(v, 10), func(b *testing.B) { |
| 89 |
for i := 0; i < b.N; i++ { |
| 90 |
buf = AppendInt(nil, buf[:0], unsafe.Pointer(&v), code) |
| 91 |
} |
| 92 |
}) |
| 93 |
} |
| 94 |
} |