github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/hash_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  	"testing"
    21  
    22  	. "github.com/stafiprotocol/go-substrate-rpc-client/types"
    23  )
    24  
    25  var hash20 = []byte{
    26  	1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
    27  }
    28  
    29  func TestH160_EncodeDecode(t *testing.T) {
    30  	assertRoundtrip(t, NewH160(hash20))
    31  }
    32  
    33  func TestH160_EncodedLength(t *testing.T) {
    34  	assertEncodedLength(t, []encodedLengthAssert{{NewH160(hash20), 20}})
    35  }
    36  
    37  func TestH160_Encode(t *testing.T) {
    38  	assertEncode(t, []encodingAssert{
    39  		{NewH160(hash20), MustHexDecodeString("0x0102030405060708090001020304050607080900")},
    40  	})
    41  }
    42  
    43  func TestH160_Hash(t *testing.T) {
    44  	assertHash(t, []hashAssert{
    45  		{NewH160(hash20), MustHexDecodeString("0xdb34a42220dae1c95e29ee2e97d09995887790554cb1ac302680934ffc636b82")},
    46  	})
    47  }
    48  
    49  func TestH160_Hex(t *testing.T) {
    50  	assertEncodeToHex(t, []encodeToHexAssert{
    51  		{NewH160(hash20), "0x0102030405060708090001020304050607080900"},
    52  	})
    53  }
    54  
    55  func TestH160_String(t *testing.T) {
    56  	assertString(t, []stringAssert{
    57  		{NewH160(hash20), "[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0]"},
    58  	})
    59  }
    60  
    61  func TestH160_Eq(t *testing.T) {
    62  	assertEq(t, []eqAssert{
    63  		{NewH160(hash20), NewH160(hash20), true},
    64  		{NewH160(hash20), NewBytes(hash20), false},
    65  		{NewH160(hash20), NewBool(false), false},
    66  	})
    67  }
    68  
    69  var hash32 = []byte{
    70  	1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
    71  	1, 2,
    72  }
    73  
    74  func TestH256_EncodeDecode(t *testing.T) {
    75  	assertRoundtrip(t, NewH256(hash32))
    76  }
    77  
    78  func TestH256_EncodedLength(t *testing.T) {
    79  	assertEncodedLength(t, []encodedLengthAssert{{NewH256(hash32), 32}})
    80  }
    81  
    82  func TestH256_Encode(t *testing.T) {
    83  	assertEncode(t, []encodingAssert{
    84  		{NewH256(hash32), MustHexDecodeString("0x0102030405060708090001020304050607080900010203040506070809000102")},
    85  	})
    86  }
    87  
    88  func TestH256_Hash(t *testing.T) {
    89  	assertHash(t, []hashAssert{
    90  		{NewH256(hash32), MustHexDecodeString("0x95248da71ae3de701a70523b32a1e5a982de1e49dcf9891e188748e8f9189a2c")},
    91  	})
    92  }
    93  
    94  func TestH256_Hex(t *testing.T) {
    95  	assertEncodeToHex(t, []encodeToHexAssert{
    96  		{NewH256(hash32), "0x0102030405060708090001020304050607080900010203040506070809000102"},
    97  	})
    98  }
    99  
   100  func TestH256_String(t *testing.T) {
   101  	assertString(t, []stringAssert{
   102  		{NewH256(hash32), "[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2]"},
   103  	})
   104  }
   105  
   106  func TestH256_Eq(t *testing.T) {
   107  	assertEq(t, []eqAssert{
   108  		{NewH256(hash32), NewH256(hash32), true},
   109  		{NewH256(hash32), NewBytes(hash32), false},
   110  		{NewH256(hash32), NewBool(false), false},
   111  	})
   112  }
   113  
   114  func TestHash_EncodeDecode(t *testing.T) {
   115  	assertRoundtrip(t, NewHash(hash32))
   116  }
   117  
   118  func TestHash_EncodedLength(t *testing.T) {
   119  	assertEncodedLength(t, []encodedLengthAssert{{NewHash(hash32), 32}})
   120  }
   121  
   122  func TestHash_Encode(t *testing.T) {
   123  	assertEncode(t, []encodingAssert{
   124  		{NewHash(hash32), MustHexDecodeString("0x0102030405060708090001020304050607080900010203040506070809000102")},
   125  	})
   126  }
   127  
   128  func TestHash_Hash(t *testing.T) {
   129  	assertHash(t, []hashAssert{
   130  		{NewHash(hash32), MustHexDecodeString("0x95248da71ae3de701a70523b32a1e5a982de1e49dcf9891e188748e8f9189a2c")},
   131  	})
   132  }
   133  
   134  func TestHash_Hex(t *testing.T) {
   135  	assertEncodeToHex(t, []encodeToHexAssert{
   136  		{NewHash(hash32), "0x0102030405060708090001020304050607080900010203040506070809000102"},
   137  	})
   138  }
   139  
   140  func TestHash_String(t *testing.T) {
   141  	assertString(t, []stringAssert{
   142  		{NewHash(hash32), "[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2]"},
   143  	})
   144  }
   145  
   146  func TestHash_Eq(t *testing.T) {
   147  	assertEq(t, []eqAssert{
   148  		{NewHash(hash32), NewHash(hash32), true},
   149  		{NewHash(hash32), NewBytes(hash32), false},
   150  		{NewHash(hash32), NewBool(false), false},
   151  	})
   152  }
   153  
   154  var hash64 = []byte{
   155  	1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
   156  	1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
   157  	1, 2, 3, 4,
   158  }
   159  
   160  func TestH512_EncodeDecode(t *testing.T) {
   161  	assertRoundtrip(t, NewH512(hash64))
   162  }
   163  
   164  func TestH512_EncodedLength(t *testing.T) {
   165  	assertEncodedLength(t, []encodedLengthAssert{{NewH512(hash64), 64}})
   166  }
   167  
   168  func TestH512_Encode(t *testing.T) {
   169  	assertEncode(t, []encodingAssert{
   170  		{NewH512(hash64), MustHexDecodeString("0x01020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304")}, //nolint:lll
   171  	})
   172  }
   173  
   174  func TestH512_Hash(t *testing.T) {
   175  	assertHash(t, []hashAssert{
   176  		{NewH512(hash64), MustHexDecodeString("0x893a41fa8d4e6447fe2d74a3ae529b1f1a13f3ac5a194907bf19e78e084a0ef6")},
   177  	})
   178  }
   179  
   180  func TestH512_Hex(t *testing.T) {
   181  	assertEncodeToHex(t, []encodeToHexAssert{
   182  		{NewH512(hash64), "0x01020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304"}, //nolint:lll
   183  	})
   184  }
   185  
   186  func TestH512_String(t *testing.T) {
   187  	assertString(t, []stringAssert{
   188  		{NewH512(hash64), "[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4]"}, //nolint:lll
   189  	})
   190  }
   191  
   192  func TestH512_Eq(t *testing.T) {
   193  	assertEq(t, []eqAssert{
   194  		{NewH512(hash64), NewH512(hash64), true},
   195  		{NewH512(hash64), NewBytes(hash64), false},
   196  		{NewH512(hash64), NewBool(false), false},
   197  	})
   198  }