2021-12-06 02:09:53 +01:00
|
|
|
package decimal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMsgPack(t *testing.T) {
|
|
|
|
for _, x := range testTable {
|
|
|
|
s := x.short
|
2021-12-06 04:35:43 +01:00
|
|
|
// limit to 31 digits
|
2021-12-06 02:09:53 +01:00
|
|
|
if len(s) > 31 {
|
|
|
|
s = s[:31]
|
|
|
|
if s[30] == '.' {
|
|
|
|
s = s[:30]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare Test Decimal Data
|
|
|
|
amount, err := NewFromString(s)
|
2021-12-06 04:35:43 +01:00
|
|
|
if err != nil {
|
2021-12-06 02:09:53 +01:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2021-12-06 02:22:32 +01:00
|
|
|
s = amount.String()
|
2021-12-06 02:09:53 +01:00
|
|
|
|
|
|
|
// MarshalMsg
|
|
|
|
var b []byte
|
2021-12-06 04:35:43 +01:00
|
|
|
out, err := amount.MarshalMsg(b)
|
|
|
|
if err != nil {
|
2021-12-06 02:09:53 +01:00
|
|
|
t.Errorf("error marshalMsg %s: %v", s, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check msg type
|
|
|
|
typ := out[0] & 0xe0
|
|
|
|
if typ != 0xa0 {
|
|
|
|
t.Errorf("error marshalMsg, expected type = %b, got %b", 0xa0, typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check msg len
|
|
|
|
sz := int(out[0] & 0x1f)
|
|
|
|
if sz != len(s) {
|
|
|
|
t.Errorf("error marshalMsg, expected size = %d, got %d", len(s), sz)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalMsg
|
|
|
|
var unmarshalAmount Decimal
|
|
|
|
_, err = unmarshalAmount.UnmarshalMsg(out)
|
2021-12-06 04:35:43 +01:00
|
|
|
if err != nil {
|
2021-12-06 02:09:53 +01:00
|
|
|
t.Errorf("error unmarshalMsg %s: %v", s, err)
|
2021-12-06 04:35:43 +01:00
|
|
|
} else if !unmarshalAmount.Equal(amount) {
|
|
|
|
t.Errorf("expected %s, got %s (%s, %d)",
|
|
|
|
amount.String(), unmarshalAmount.String(),
|
|
|
|
unmarshalAmount.value.String(), unmarshalAmount.exp)
|
2021-12-06 02:09:53 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-06 04:35:43 +01:00
|
|
|
}
|