-
Notifications
You must be signed in to change notification settings - Fork 13
/
u128_test.go
64 lines (57 loc) · 1.62 KB
/
u128_test.go
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
package bin
import (
"encoding/json"
"testing"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/require"
)
func TestUint128(t *testing.T) {
// from bytes:
data := []byte{51, 47, 223, 255, 255, 255, 255, 255, 30, 12, 0, 0, 0, 0, 0, 0}
numberString := "57240246860720736513843"
parsed, err := decimal.NewFromString(numberString)
if err != nil {
panic(err)
}
{
if parsed.String() != numberString {
t.Errorf("parsed.String() != numberString")
}
}
{
u128 := NewUint128LittleEndian()
err := u128.UnmarshalWithDecoder(NewBorshDecoder(data))
require.NoError(t, err)
require.Equal(t, uint64(3102), u128.Hi)
require.Equal(t, uint64(18446744073707401011), u128.Lo)
require.Equal(t, parsed.BigInt(), u128.BigInt())
require.Equal(t, parsed.String(), u128.DecimalString())
}
{
u128 := NewUint128BigEndian()
ReverseBytes(data)
err := u128.UnmarshalWithDecoder(NewBorshDecoder(data))
require.NoError(t, err)
require.Equal(t, uint64(3102), u128.Hi)
require.Equal(t, uint64(18446744073707401011), u128.Lo)
require.Equal(t, parsed.BigInt(), u128.BigInt())
require.Equal(t, parsed.String(), u128.DecimalString())
}
{
j := []byte(`{"i":"57240246860720736513843"}`)
var object struct {
I Uint128 `json:"i"`
}
err := json.Unmarshal(j, &object)
require.NoError(t, err)
require.Equal(t, uint64(3102), object.I.Hi)
require.Equal(t, uint64(18446744073707401011), object.I.Lo)
require.Equal(t, parsed.BigInt(), object.I.BigInt())
require.Equal(t, parsed.String(), object.I.DecimalString())
{
out, err := json.Marshal(object)
require.NoError(t, err)
require.Equal(t, j, out)
}
}
}