forked from woodsbury/decimal128
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_test.go
More file actions
66 lines (47 loc) · 1.33 KB
/
binary_test.go
File metadata and controls
66 lines (47 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package decimal128
import (
"fmt"
"testing"
)
func TestDecimalMarshalBinary(t *testing.T) {
t.Parallel()
initDecimalValues()
for _, val := range decimalValues {
decval := val.Decimal()
res, err := decval.MarshalBinary()
fmtval := fmt.Sprintf("%.16x%.16x", decval.hi, decval.lo)
fmtres := fmt.Sprintf("%x", res)
if fmtres != fmtval || err != nil {
t.Errorf("%v.MarshalBinary() = (%s, %v), want (%s, <nil>)", val, fmtres, err, fmtval)
}
var resval Decimal
err = resval.UnmarshalBinary(res)
if resval != decval || err != nil {
t.Errorf("Decimal.UnmarshalBinary(%x) = (%v, %v), want (%v, <nil>)", res, resval, err, decval)
}
res, err = decval.AppendBinary(res[:0])
fmtres = fmt.Sprintf("%x", res)
if fmtres != fmtval || err != nil {
t.Errorf("%v.AppendBinary() = (%s, %v), want (%s, <nil>)", val, fmtres, err, fmtval)
}
err = resval.UnmarshalBinary(res)
if resval != decval || err != nil {
t.Errorf("Decimal.UnmarshalBinary(%x) = (%v, %v), want (%v, <nil>)", res, resval, err, decval)
}
}
}
func BenchmarkDecimalAppendBinary(b *testing.B) {
b.ReportAllocs()
d := New(123456789, 10)
var buf []byte
for b.Loop() {
buf, _ = d.AppendBinary(buf[:0])
}
}
func BenchmarkDecimalMarshalBinary(b *testing.B) {
b.ReportAllocs()
d := New(123456789, 10)
for b.Loop() {
d.MarshalBinary()
}
}