github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/bytes_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  func TestBytes_EncodeDecode(t *testing.T) {
    26  	assertRoundtrip(t, NewBytes(MustHexDecodeString("0x00")))
    27  	assertRoundtrip(t, NewBytes(MustHexDecodeString("0xab1234")))
    28  	assertRoundtrip(t, NewBytes(MustHexDecodeString("0x0001")))
    29  }
    30  
    31  func TestBytes_EncodedLength(t *testing.T) {
    32  	assertEncodedLength(t, []encodedLengthAssert{
    33  		{NewBytes(MustHexDecodeString("0x00")), 2},
    34  		{NewBytes(MustHexDecodeString("0xab1234")), 4},
    35  		{NewBytes(MustHexDecodeString("0x0001")), 3},
    36  	})
    37  }
    38  
    39  func TestBytes_Encode(t *testing.T) {
    40  	assertEncode(t, []encodingAssert{
    41  		{NewBytes([]byte{0, 0, 0}), MustHexDecodeString("0x0c000000")},
    42  		{NewBytes([]byte{171, 18, 52}), MustHexDecodeString("0x0cab1234")},
    43  		{NewBytes([]byte{0, 1}), MustHexDecodeString("0x080001")},
    44  		{NewBytes([]byte{18, 52, 86}), MustHexDecodeString("0x0c123456")},
    45  	})
    46  }
    47  
    48  func TestBytes_Hash(t *testing.T) {
    49  	assertHash(t, []hashAssert{
    50  		{NewBytes([]byte{0, 42, 254}), MustHexDecodeString(
    51  			"0xabf7fe6eb94e0816bf2db57abb296d012f7cb9ddfe59ebf52f9c2770f49a0a46")},
    52  		{NewBytes([]byte{0, 0}), MustHexDecodeString(
    53  			"0xd1200120e01c48b4bbf7e1cd7ebab20087b34ea11e1e9e4ebc2f207aea77139d")},
    54  	})
    55  }
    56  
    57  func TestBytes_Hex(t *testing.T) {
    58  	assertEncodeToHex(t, []encodeToHexAssert{
    59  		{NewBytes([]byte{0, 0, 0}), "0x0c000000"},
    60  		{NewBytes([]byte{171, 18, 52}), "0x0cab1234"},
    61  		{NewBytes([]byte{0, 1}), "0x080001"},
    62  		{NewBytes([]byte{18, 52, 86}), "0x0c123456"},
    63  	})
    64  }
    65  
    66  func TestBytes_String(t *testing.T) {
    67  	assertString(t, []stringAssert{
    68  		{NewBytes([]byte{0, 0, 0}), "[0 0 0]"},
    69  		{NewBytes([]byte{171, 18, 52}), "[171 18 52]"},
    70  		{NewBytes([]byte{0, 1}), "[0 1]"},
    71  		{NewBytes([]byte{18, 52, 86}), "[18 52 86]"},
    72  	})
    73  }
    74  
    75  func TestBytes_Eq(t *testing.T) {
    76  	assertEq(t, []eqAssert{
    77  		{NewBytes([]byte{1, 0, 0}), NewBytes([]byte{1, 0}), false},
    78  		{NewBytes([]byte{0, 0, 1}), NewBytes([]byte{0, 1}), false},
    79  		{NewBytes([]byte{0, 0, 0}), NewBytes([]byte{0, 0}), false},
    80  		{NewBytes([]byte{12, 48, 255}), NewBytes([]byte{12, 48, 255}), true},
    81  		{NewBytes([]byte{0}), NewBytes([]byte{0}), true},
    82  		{NewBytes([]byte{1}), NewBool(true), false},
    83  		{NewBytes([]byte{0}), NewBool(false), false},
    84  	})
    85  }
    86  
    87  func TestBytes8_EncodeDecode(t *testing.T) {
    88  	assertRoundtrip(t, NewBytes8([8]byte{}))
    89  	assertRoundtrip(t, NewBytes8([8]byte{0, 1, 2, 3, 4, 5, 6, 7}))
    90  }
    91  
    92  func TestBytes8_EncodedLength(t *testing.T) {
    93  	assertEncodedLength(t, []encodedLengthAssert{
    94  		{NewBytes8([8]byte{}), 8},
    95  		{NewBytes8([8]byte{7, 6, 5, 4, 3, 2, 1, 0}), 8},
    96  	})
    97  }
    98  
    99  func TestBytes8_Encode(t *testing.T) {
   100  	assertEncode(t, []encodingAssert{
   101  		{NewBytes8([8]byte{0, 0, 0}), MustHexDecodeString("0x0000000000000000")},
   102  		{NewBytes8([8]byte{171, 18, 52}), MustHexDecodeString("0xab12340000000000")},
   103  	})
   104  }
   105  
   106  func TestBytes8_Hash(t *testing.T) {
   107  	assertHash(t, []hashAssert{
   108  		{NewBytes8([8]byte{0, 42, 254}), MustHexDecodeString(
   109  			"0xb327a728de8af3187dd7eaeb74bfff9e9c37eda8c472c7459d51e1fc450faf74")},
   110  		{NewBytes8([8]byte{0, 0}), MustHexDecodeString(
   111  			"0x81e47a19e6b29b0a65b9591762ce5143ed30d0261e5d24a3201752506b20f15c")},
   112  	})
   113  }
   114  
   115  func TestBytes8_Hex(t *testing.T) {
   116  	assertEncodeToHex(t, []encodeToHexAssert{
   117  		{NewBytes8([8]byte{0, 0, 0}), "0x0000000000000000"},
   118  		{NewBytes8([8]byte{171, 18, 52}), "0xab12340000000000"},
   119  	})
   120  }
   121  
   122  func TestBytes8_String(t *testing.T) {
   123  	assertString(t, []stringAssert{
   124  		{NewBytes8([8]byte{0, 0, 0}), "[0 0 0 0 0 0 0 0]"},
   125  		{NewBytes8([8]byte{171, 18, 52}), "[171 18 52 0 0 0 0 0]"},
   126  	})
   127  }
   128  
   129  func TestBytes8_Eq(t *testing.T) {
   130  	assertEq(t, []eqAssert{
   131  		{NewBytes8([8]byte{1, 0, 0}), NewBytes8([8]byte{1, 0}), true},
   132  		{NewBytes8([8]byte{0, 0, 1}), NewBytes8([8]byte{0, 1}), false},
   133  		{NewBytes8([8]byte{0, 0, 0}), NewBytes8([8]byte{0, 0}), true},
   134  		{NewBytes8([8]byte{12, 48, 255}), NewBytes8([8]byte{12, 48, 255}), true},
   135  		{NewBytes8([8]byte{0}), NewBytes8([8]byte{0}), true},
   136  		{NewBytes8([8]byte{1}), NewBool(true), false},
   137  		{NewBytes8([8]byte{0}), NewBool(false), false},
   138  	})
   139  }
   140  
   141  func TestBytes16_EncodeDecode(t *testing.T) {
   142  	assertRoundtrip(t, NewBytes16([16]byte{0, 255, 0, 42}))
   143  }
   144  
   145  func TestBytes32_EncodeDecode(t *testing.T) {
   146  	assertRoundtrip(t, NewBytes32([32]byte{0, 255, 0, 42}))
   147  }
   148  
   149  func TestBytes64_EncodeDecode(t *testing.T) {
   150  	assertRoundtrip(t, NewBytes64([64]byte{0, 255, 0, 42}))
   151  }
   152  
   153  func TestBytes128_EncodeDecode(t *testing.T) {
   154  	assertRoundtrip(t, NewBytes128([128]byte{0, 255, 0, 42}))
   155  }
   156  
   157  func TestBytes256_EncodeDecode(t *testing.T) {
   158  	assertRoundtrip(t, NewBytes256([256]byte{0, 255, 0, 42}))
   159  }
   160  
   161  func TestBytes512_EncodeDecode(t *testing.T) {
   162  	assertRoundtrip(t, NewBytes512([512]byte{0, 255, 0, 42}))
   163  }
   164  
   165  func TestBytes1024_EncodeDecode(t *testing.T) {
   166  	assertRoundtrip(t, NewBytes1024([1024]byte{0, 255, 0, 42}))
   167  }
   168  
   169  func TestBytes2048_EncodeDecode(t *testing.T) {
   170  	assertRoundtrip(t, NewBytes2048([2048]byte{0, 255, 0, 42}))
   171  }