github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/int_test.go (about)

     1  // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
     2  //
     3  // Copyright 2020 Stafi Protocol
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package types_test
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  
    23  	. "github.com/stafiprotocol/go-substrate-rpc-client/types"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestI8_EncodeDecode(t *testing.T) {
    28  	assertRoundtrip(t, NewI8(0))
    29  	assertRoundtrip(t, NewI8(12))
    30  	assertRoundtrip(t, NewI8(-12))
    31  }
    32  
    33  func TestI8_EncodedLength(t *testing.T) {
    34  	assertEncodedLength(t, []encodedLengthAssert{{NewI8(-13), 1}})
    35  }
    36  
    37  func TestI8_Encode(t *testing.T) {
    38  	assertEncode(t, []encodingAssert{
    39  		{NewI8(-29), MustHexDecodeString("0xe3")},
    40  	})
    41  }
    42  
    43  func TestI8_Hash(t *testing.T) {
    44  	assertHash(t, []hashAssert{
    45  		{NewI8(-29), MustHexDecodeString("0xb683f1b6c99388ff3443b35a0051eeaafdc5e364e771bdfc72c7fd5d2be800bc")},
    46  	})
    47  }
    48  
    49  func TestI8_Hex(t *testing.T) {
    50  	assertEncodeToHex(t, []encodeToHexAssert{
    51  		{NewI8(-29), "0xe3"},
    52  	})
    53  }
    54  
    55  func TestI8_String(t *testing.T) {
    56  	assertString(t, []stringAssert{
    57  		{NewI8(-29), "-29"},
    58  	})
    59  }
    60  
    61  func TestI8_Eq(t *testing.T) {
    62  	assertEq(t, []eqAssert{
    63  		{NewI8(23), NewI8(23), true},
    64  		{NewI8(-23), NewI8(23), false},
    65  		{NewI8(23), NewU8(23), false},
    66  		{NewI8(23), NewBool(false), false},
    67  	})
    68  }
    69  
    70  func TestI16_EncodeDecode(t *testing.T) {
    71  	assertRoundtrip(t, NewI16(0))
    72  	assertRoundtrip(t, NewI16(12))
    73  	assertRoundtrip(t, NewI16(-12))
    74  }
    75  
    76  func TestI16_EncodedLength(t *testing.T) {
    77  	assertEncodedLength(t, []encodedLengthAssert{{NewI16(-13), 2}})
    78  }
    79  
    80  func TestI16_Encode(t *testing.T) {
    81  	assertEncode(t, []encodingAssert{
    82  		{NewI16(-29), MustHexDecodeString("0xe3ff")},
    83  	})
    84  }
    85  
    86  func TestI16_Hash(t *testing.T) {
    87  	assertHash(t, []hashAssert{
    88  		{NewI16(-29), MustHexDecodeString("0x39fbf34f574b72d1815c602a2fe95b7af4b5dfd7bc92a2fc0824aa55f8b9d7b2")},
    89  	})
    90  }
    91  
    92  func TestI16_Hex(t *testing.T) {
    93  	assertEncodeToHex(t, []encodeToHexAssert{
    94  		{NewI16(-29), "0xe3ff"},
    95  	})
    96  }
    97  
    98  func TestI16_String(t *testing.T) {
    99  	assertString(t, []stringAssert{
   100  		{NewI16(-29), "-29"},
   101  	})
   102  }
   103  
   104  func TestI16_Eq(t *testing.T) {
   105  	assertEq(t, []eqAssert{
   106  		{NewI16(23), NewI16(23), true},
   107  		{NewI16(-23), NewI16(23), false},
   108  		{NewI16(23), NewU16(23), false},
   109  		{NewI16(23), NewBool(false), false},
   110  	})
   111  }
   112  
   113  func TestI32_EncodeDecode(t *testing.T) {
   114  	assertRoundtrip(t, NewI32(0))
   115  	assertRoundtrip(t, NewI32(12))
   116  	assertRoundtrip(t, NewI32(-12))
   117  }
   118  
   119  func TestI32_EncodedLength(t *testing.T) {
   120  	assertEncodedLength(t, []encodedLengthAssert{{NewI32(-13), 4}})
   121  }
   122  
   123  func TestI32_Encode(t *testing.T) {
   124  	assertEncode(t, []encodingAssert{
   125  		{NewI32(-29), MustHexDecodeString("0xe3ffffff")},
   126  	})
   127  }
   128  
   129  func TestI32_Hash(t *testing.T) {
   130  	assertHash(t, []hashAssert{
   131  		{NewI32(-29), MustHexDecodeString("0x6ef9d4772b9d657bfa727862d9690d5bf8b9045943279e95d3ae0743684f1b95")},
   132  	})
   133  }
   134  
   135  func TestI32_Hex(t *testing.T) {
   136  	assertEncodeToHex(t, []encodeToHexAssert{
   137  		{NewI32(-29), "0xe3ffffff"},
   138  	})
   139  }
   140  
   141  func TestI32_String(t *testing.T) {
   142  	assertString(t, []stringAssert{
   143  		{NewI32(-29), "-29"},
   144  	})
   145  }
   146  
   147  func TestI32_Eq(t *testing.T) {
   148  	assertEq(t, []eqAssert{
   149  		{NewI32(23), NewI32(23), true},
   150  		{NewI32(-23), NewI32(23), false},
   151  		{NewI32(23), NewU32(23), false},
   152  		{NewI32(23), NewBool(false), false},
   153  	})
   154  }
   155  
   156  func TestI64_EncodeDecode(t *testing.T) {
   157  	assertRoundtrip(t, NewI64(0))
   158  	assertRoundtrip(t, NewI64(12))
   159  	assertRoundtrip(t, NewI64(-12))
   160  }
   161  
   162  func TestI64_EncodedLength(t *testing.T) {
   163  	assertEncodedLength(t, []encodedLengthAssert{{NewI64(-13), 8}})
   164  }
   165  
   166  func TestI64_Encode(t *testing.T) {
   167  	assertEncode(t, []encodingAssert{
   168  		{NewI64(-29), MustHexDecodeString("0xe3ffffffffffffff")},
   169  	})
   170  }
   171  
   172  func TestI64_Hash(t *testing.T) {
   173  	assertHash(t, []hashAssert{
   174  		{NewI64(-29), MustHexDecodeString("0x4d42db2aa4a23bde81a3ad3705220affaa457c56a0135080c71db7783fec8f44")},
   175  	})
   176  }
   177  
   178  func TestI64_Hex(t *testing.T) {
   179  	assertEncodeToHex(t, []encodeToHexAssert{
   180  		{NewI64(-29), "0xe3ffffffffffffff"},
   181  	})
   182  }
   183  
   184  func TestI64_String(t *testing.T) {
   185  	assertString(t, []stringAssert{
   186  		{NewI64(-29), "-29"},
   187  	})
   188  }
   189  
   190  func TestI64_Eq(t *testing.T) {
   191  	assertEq(t, []eqAssert{
   192  		{NewI64(23), NewI64(23), true},
   193  		{NewI64(-23), NewI64(23), false},
   194  		{NewI64(23), NewU64(23), false},
   195  		{NewI64(23), NewBool(false), false},
   196  	})
   197  }
   198  
   199  func TestI128_EncodeDecode(t *testing.T) {
   200  	assertRoundtrip(t, NewI128(*big.NewInt(0)))
   201  	assertRoundtrip(t, NewI128(*big.NewInt(12)))
   202  	assertRoundtrip(t, NewI128(*big.NewInt(-12)))
   203  
   204  	bigPos := big.NewInt(0)
   205  	bigPos.SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
   206  	assertRoundtrip(t, NewI128(*bigPos))
   207  
   208  	bigNeg := big.NewInt(0)
   209  	bigNeg.SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
   210  	bigNeg.Neg(bigNeg)
   211  	assertRoundtrip(t, NewI128(*bigNeg))
   212  }
   213  
   214  func TestI128_EncodedLength(t *testing.T) {
   215  	assertEncodedLength(t, []encodedLengthAssert{{NewI128(*big.NewInt(-13)), 16}})
   216  }
   217  
   218  func TestI128_Encode(t *testing.T) {
   219  	a := big.NewInt(0).SetBytes([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 52})
   220  
   221  	b := big.NewInt(0).SetBytes([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 52})
   222  	b.Neg(b)
   223  
   224  	c := big.NewInt(0).SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
   225  
   226  	d := big.NewInt(0).SetBytes([]byte{127, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15})
   227  	d.Neg(d)
   228  
   229  	assertEncode(t, []encodingAssert{
   230  		{NewI128(*big.NewInt(0)), MustHexDecodeString("0x00000000000000000000000000000000")},
   231  		{NewI128(*big.NewInt(29)), MustHexDecodeString("0x1d000000000000000000000000000000")},
   232  		{NewI128(*big.NewInt(-29)), MustHexDecodeString("0xe3ffffffffffffffffffffffffffffff")},
   233  		{NewI128(*a), MustHexDecodeString("0x34120000000000000000000000000000")},
   234  		{NewI128(*b), MustHexDecodeString("0xccedffffffffffffffffffffffffffff")},
   235  		{NewI128(*c), MustHexDecodeString("0x100f0e0d0c0b0a090807060504030201")},
   236  		{NewI128(*d), MustHexDecodeString("0xf1f0f1f2f3f4f5f6f7f8f9fafbfcfd80")},
   237  	})
   238  }
   239  
   240  func TestI128_Decode(t *testing.T) {
   241  	a := big.NewInt(0).SetBytes([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 52})
   242  
   243  	b := big.NewInt(0).SetBytes([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 52})
   244  	b.Neg(b)
   245  
   246  	c := big.NewInt(0).SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
   247  
   248  	d := big.NewInt(0).SetBytes([]byte{127, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15})
   249  	d.Neg(d)
   250  
   251  	assertDecode(t, []decodingAssert{
   252  		{MustHexDecodeString("0x00000000000000000000000000000000"), NewI128(*big.NewInt(0))},
   253  		{MustHexDecodeString("0x1d000000000000000000000000000000"), NewI128(*big.NewInt(29))},
   254  		{MustHexDecodeString("0xe3ffffffffffffffffffffffffffffff"), NewI128(*big.NewInt(-29))},
   255  		{MustHexDecodeString("0x34120000000000000000000000000000"), NewI128(*a)},
   256  		{MustHexDecodeString("0xccedffffffffffffffffffffffffffff"), NewI128(*b)},
   257  		{MustHexDecodeString("0x100f0e0d0c0b0a090807060504030201"), NewI128(*c)},
   258  		{MustHexDecodeString("0xf1f0f1f2f3f4f5f6f7f8f9fafbfcfd80"), NewI128(*d)},
   259  	})
   260  }
   261  
   262  func TestI128_Hash(t *testing.T) {
   263  	assertHash(t, []hashAssert{
   264  		{NewI128(*big.NewInt(-29)), MustHexDecodeString(
   265  			"0x7f8f93dd36321a50796a2e88df3bc7238abad58361c2051009bc457a000c4de9")},
   266  	})
   267  }
   268  
   269  func TestI128_Hex(t *testing.T) {
   270  	assertEncodeToHex(t, []encodeToHexAssert{
   271  		{NewI128(*big.NewInt(-29)), "0xe3ffffffffffffffffffffffffffffff"},
   272  	})
   273  }
   274  
   275  func TestI128_String(t *testing.T) {
   276  	assertString(t, []stringAssert{
   277  		{NewI128(*big.NewInt(-29)), "-29"},
   278  	})
   279  }
   280  
   281  func TestI128_Eq(t *testing.T) {
   282  	assertEq(t, []eqAssert{
   283  		{NewI128(*big.NewInt(23)), NewI128(*big.NewInt(23)), true},
   284  		{NewI128(*big.NewInt(-23)), NewI128(*big.NewInt(23)), false},
   285  		{NewI128(*big.NewInt(23)), NewU64(23), false},
   286  		{NewI128(*big.NewInt(23)), NewBool(false), false},
   287  	})
   288  }
   289  
   290  func TestI256_EncodeDecode(t *testing.T) {
   291  	assertRoundtrip(t, NewI256(*big.NewInt(0)))
   292  	assertRoundtrip(t, NewI256(*big.NewInt(12)))
   293  	assertRoundtrip(t, NewI256(*big.NewInt(-12)))
   294  
   295  	bigPos := big.NewInt(0)
   296  	bigPos.SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
   297  		17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32})
   298  	assertRoundtrip(t, NewI256(*bigPos))
   299  
   300  	bigNeg := big.NewInt(0)
   301  	bigNeg.SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
   302  		17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32})
   303  	bigNeg.Neg(bigNeg)
   304  	assertRoundtrip(t, NewI256(*bigNeg))
   305  }
   306  
   307  func TestI256_EncodedLength(t *testing.T) {
   308  	assertEncodedLength(t, []encodedLengthAssert{{NewI256(*big.NewInt(-13)), 32}})
   309  }
   310  
   311  func TestI256_Encode(t *testing.T) {
   312  	a := big.NewInt(0).SetBytes([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   313  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 52})
   314  
   315  	b := big.NewInt(0).SetBytes([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   316  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 52})
   317  	b.Neg(b)
   318  
   319  	c := big.NewInt(0).SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
   320  		17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32})
   321  
   322  	d := big.NewInt(0).SetBytes([]byte{127, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15,
   323  		17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32})
   324  	d.Neg(d)
   325  
   326  	assertEncode(t, []encodingAssert{
   327  		{NewI256(*big.NewInt(0)), MustHexDecodeString(
   328  			"0x0000000000000000000000000000000000000000000000000000000000000000")},
   329  		{NewI256(*big.NewInt(29)), MustHexDecodeString(
   330  			"0x1d00000000000000000000000000000000000000000000000000000000000000")},
   331  		{NewI256(*big.NewInt(-29)), MustHexDecodeString(
   332  			"0xe3ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")},
   333  		{NewI256(*a), MustHexDecodeString("0x3412000000000000000000000000000000000000000000000000000000000000")},
   334  		{NewI256(*b), MustHexDecodeString("0xccedffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")},
   335  		{NewI256(*c), MustHexDecodeString("0x201f1e1d1c1b1a1817161514131211100f0e0d0c0b0a09080706050403020100")},
   336  		{NewI256(*d), MustHexDecodeString("0xe0e0e1e2e3e4e5e7e8e9eaebecedeef0f0f1f2f3f4f5f6f7f8f9fafbfcfd80ff")},
   337  	})
   338  }
   339  
   340  func TestI256_Decode(t *testing.T) {
   341  	a := big.NewInt(0).SetBytes([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   342  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 52})
   343  
   344  	b := big.NewInt(0).SetBytes([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   345  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 52})
   346  	b.Neg(b)
   347  
   348  	c := big.NewInt(0).SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
   349  		17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32})
   350  
   351  	d := big.NewInt(0).SetBytes([]byte{127, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15,
   352  		17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32})
   353  	d.Neg(d)
   354  
   355  	assertDecode(t, []decodingAssert{
   356  		{MustHexDecodeString("0x0000000000000000000000000000000000000000000000000000000000000000"),
   357  			NewI256(*big.NewInt(0))},
   358  		{MustHexDecodeString("0x1d00000000000000000000000000000000000000000000000000000000000000"),
   359  			NewI256(*big.NewInt(29))},
   360  		{MustHexDecodeString("0xe3ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
   361  			NewI256(*big.NewInt(-29))},
   362  		{MustHexDecodeString("0x3412000000000000000000000000000000000000000000000000000000000000"), NewI256(*a)},
   363  		{MustHexDecodeString("0xccedffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), NewI256(*b)},
   364  		{MustHexDecodeString("0x201f1e1d1c1b1a1817161514131211100f0e0d0c0b0a09080706050403020100"), NewI256(*c)},
   365  		{MustHexDecodeString("0xe0e0e1e2e3e4e5e7e8e9eaebecedeef0f0f1f2f3f4f5f6f7f8f9fafbfcfd80ff"), NewI256(*d)},
   366  	})
   367  }
   368  
   369  func TestI256_Hash(t *testing.T) {
   370  	assertHash(t, []hashAssert{
   371  		{NewI256(*big.NewInt(-29)), MustHexDecodeString(
   372  			"0xca6ae1636199279abc3e15e366ea463cf06829e7816e1ad08c0c15c158dfeba6")},
   373  	})
   374  }
   375  
   376  func TestI256_Hex(t *testing.T) {
   377  	assertEncodeToHex(t, []encodeToHexAssert{
   378  		{NewI256(*big.NewInt(-29)), "0xe3ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},
   379  	})
   380  }
   381  
   382  func TestI256_String(t *testing.T) {
   383  	assertString(t, []stringAssert{
   384  		{NewI256(*big.NewInt(-29)), "-29"},
   385  	})
   386  }
   387  
   388  func TestI256_Eq(t *testing.T) {
   389  	assertEq(t, []eqAssert{
   390  		{NewI256(*big.NewInt(23)), NewI256(*big.NewInt(23)), true},
   391  		{NewI256(*big.NewInt(-23)), NewI256(*big.NewInt(23)), false},
   392  		{NewI256(*big.NewInt(23)), NewI128(*big.NewInt(23)), false},
   393  		{NewI256(*big.NewInt(23)), NewU64(23), false},
   394  		{NewI256(*big.NewInt(23)), NewBool(false), false},
   395  	})
   396  }
   397  
   398  func TestBigIntToIntBytes(t *testing.T) {
   399  	res, err := BigIntToIntBytes(big.NewInt(4), 2)
   400  	assert.NoError(t, err)
   401  	assert.Equal(t, MustHexDecodeString("0x0004"), res)
   402  
   403  	res, err = BigIntToIntBytes(big.NewInt(0).Neg(big.NewInt(4)), 2)
   404  	assert.NoError(t, err)
   405  	assert.Equal(t, MustHexDecodeString("0xfffc"), res)
   406  
   407  	_, err = BigIntToIntBytes(big.NewInt(128), 1)
   408  	assert.EqualError(t, err, "cannot encode big.Int to []byte: given big.Int exceeds highest positive number "+
   409  		"127 for an int with 8 bits")
   410  
   411  	c := big.NewInt(129)
   412  	_, err = BigIntToIntBytes(c.Neg(c), 1)
   413  	assert.EqualError(t, err, "cannot encode big.Int to []byte: given big.Int exceeds highest negative number "+
   414  		"-128 for an int with 8 bits")
   415  }
   416  
   417  func TestIntBytesToBigInt(t *testing.T) {
   418  	res, err := IntBytesToBigInt(MustHexDecodeString("0x0004"))
   419  	assert.NoError(t, err)
   420  	assert.Equal(t, big.NewInt(4), res)
   421  
   422  	res, err = IntBytesToBigInt(MustHexDecodeString("0xfffc"))
   423  	assert.NoError(t, err)
   424  	assert.Equal(t, big.NewInt(0).Neg(big.NewInt(4)), res)
   425  
   426  	_, err = IntBytesToBigInt([]byte{})
   427  	assert.EqualError(t, err, "cannot decode an empty byte slice")
   428  }
   429  
   430  func TestBigIntToIntBytes_128(t *testing.T) {
   431  	b := big.NewInt(0)
   432  	b.SetBytes([]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x12, 0x34})
   433  
   434  	res, err := BigIntToIntBytes(b, 16)
   435  	assert.NoError(t, err)
   436  	assert.Equal(t, MustHexDecodeString("0x00000000000000000000000000001234"), res)
   437  
   438  	b = b.Neg(b)
   439  	res, err = BigIntToIntBytes(b, 16)
   440  	assert.NoError(t, err)
   441  	assert.Equal(t, MustHexDecodeString("0xffffffffffffffffffffffffffffedcc"), res)
   442  }