github.com/MetalBlockchain/metalgo@v1.11.9/ids/id_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package ids
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/MetalBlockchain/metalgo/utils"
    14  	"github.com/MetalBlockchain/metalgo/utils/cb58"
    15  )
    16  
    17  func TestID(t *testing.T) {
    18  	require := require.New(t)
    19  
    20  	id := ID{24}
    21  	idCopy := ID{24}
    22  	prefixed := id.Prefix(0)
    23  
    24  	require.Equal(idCopy, id)
    25  	require.Equal(prefixed, id.Prefix(0))
    26  }
    27  
    28  func TestIDXOR(t *testing.T) {
    29  	require := require.New(t)
    30  
    31  	id1 := ID{1}
    32  	id3 := ID{3}
    33  
    34  	require.Equal(ID{2}, id1.XOR(id3))
    35  	require.Equal(ID{1}, id1)
    36  }
    37  
    38  func TestIDBit(t *testing.T) {
    39  	require := require.New(t)
    40  
    41  	id0 := ID{1 << 0}
    42  	id1 := ID{1 << 1}
    43  	id2 := ID{1 << 2}
    44  	id3 := ID{1 << 3}
    45  	id4 := ID{1 << 4}
    46  	id5 := ID{1 << 5}
    47  	id6 := ID{1 << 6}
    48  	id7 := ID{1 << 7}
    49  	id8 := ID{0, 1 << 0}
    50  
    51  	require.Equal(1, id0.Bit(0))
    52  	require.Equal(1, id1.Bit(1))
    53  	require.Equal(1, id2.Bit(2))
    54  	require.Equal(1, id3.Bit(3))
    55  	require.Equal(1, id4.Bit(4))
    56  	require.Equal(1, id5.Bit(5))
    57  	require.Equal(1, id6.Bit(6))
    58  	require.Equal(1, id7.Bit(7))
    59  	require.Equal(1, id8.Bit(8))
    60  }
    61  
    62  func TestFromString(t *testing.T) {
    63  	require := require.New(t)
    64  
    65  	id := ID{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'}
    66  	idStr := id.String()
    67  	id2, err := FromString(idStr)
    68  	require.NoError(err)
    69  	require.Equal(id, id2)
    70  }
    71  
    72  func TestIDFromStringError(t *testing.T) {
    73  	tests := []struct {
    74  		in          string
    75  		expectedErr error
    76  	}{
    77  		{
    78  			in:          "",
    79  			expectedErr: cb58.ErrBase58Decoding,
    80  		},
    81  		{
    82  			in:          "foo",
    83  			expectedErr: cb58.ErrMissingChecksum,
    84  		},
    85  		{
    86  			in:          "foobar",
    87  			expectedErr: cb58.ErrBadChecksum,
    88  		},
    89  	}
    90  	for _, tt := range tests {
    91  		t.Run(tt.in, func(t *testing.T) {
    92  			_, err := FromString(tt.in)
    93  			require.ErrorIs(t, err, tt.expectedErr)
    94  		})
    95  	}
    96  }
    97  
    98  func TestIDMarshalJSON(t *testing.T) {
    99  	tests := []struct {
   100  		label string
   101  		in    ID
   102  		out   []byte
   103  		err   error
   104  	}{
   105  		{
   106  			"ID{}",
   107  			ID{},
   108  			[]byte(`"11111111111111111111111111111111LpoYY"`),
   109  			nil,
   110  		},
   111  		{
   112  			`ID("ava labs")`,
   113  			ID{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
   114  			[]byte(`"jvYi6Tn9idMi7BaymUVi9zWjg5tpmW7trfKG1AYJLKZJ2fsU7"`),
   115  			nil,
   116  		},
   117  	}
   118  	for _, tt := range tests {
   119  		t.Run(tt.label, func(t *testing.T) {
   120  			require := require.New(t)
   121  
   122  			out, err := tt.in.MarshalJSON()
   123  			require.ErrorIs(err, tt.err)
   124  			require.Equal(tt.out, out)
   125  		})
   126  	}
   127  }
   128  
   129  func TestIDUnmarshalJSON(t *testing.T) {
   130  	tests := []struct {
   131  		label string
   132  		in    []byte
   133  		out   ID
   134  		err   error
   135  	}{
   136  		{
   137  			"ID{}",
   138  			[]byte("null"),
   139  			ID{},
   140  			nil,
   141  		},
   142  		{
   143  			`ID("ava labs")`,
   144  			[]byte(`"jvYi6Tn9idMi7BaymUVi9zWjg5tpmW7trfKG1AYJLKZJ2fsU7"`),
   145  			ID{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
   146  			nil,
   147  		},
   148  	}
   149  	for _, tt := range tests {
   150  		t.Run(tt.label, func(t *testing.T) {
   151  			require := require.New(t)
   152  
   153  			foo := ID{}
   154  			err := foo.UnmarshalJSON(tt.in)
   155  			require.ErrorIs(err, tt.err)
   156  			require.Equal(tt.out, foo)
   157  		})
   158  	}
   159  }
   160  
   161  func TestIDHex(t *testing.T) {
   162  	id := ID{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'}
   163  	expected := "617661206c616273000000000000000000000000000000000000000000000000"
   164  	require.Equal(t, expected, id.Hex())
   165  }
   166  
   167  func TestIDString(t *testing.T) {
   168  	tests := []struct {
   169  		label    string
   170  		id       ID
   171  		expected string
   172  	}{
   173  		{"ID{}", ID{}, "11111111111111111111111111111111LpoYY"},
   174  		{"ID{24}", ID{24}, "Ba3mm8Ra8JYYebeZ9p7zw1ayorDbeD1euwxhgzSLsncKqGoNt"},
   175  	}
   176  	for _, tt := range tests {
   177  		t.Run(tt.label, func(t *testing.T) {
   178  			require.Equal(t, tt.expected, tt.id.String())
   179  		})
   180  	}
   181  }
   182  
   183  func TestSortIDs(t *testing.T) {
   184  	ids := []ID{
   185  		{'e', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
   186  		{'W', 'a', 'l', 'l', 'e', ' ', 'l', 'a', 'b', 's'},
   187  		{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
   188  	}
   189  	utils.Sort(ids)
   190  	expected := []ID{
   191  		{'W', 'a', 'l', 'l', 'e', ' ', 'l', 'a', 'b', 's'},
   192  		{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
   193  		{'e', 'v', 'a', ' ', 'l', 'a', 'b', 's'},
   194  	}
   195  	require.Equal(t, expected, ids)
   196  }
   197  
   198  func TestIDMapMarshalling(t *testing.T) {
   199  	require := require.New(t)
   200  
   201  	originalMap := map[ID]int{
   202  		{'e', 'v', 'a', ' ', 'l', 'a', 'b', 's'}: 1,
   203  		{'a', 'v', 'a', ' ', 'l', 'a', 'b', 's'}: 2,
   204  	}
   205  	mapJSON, err := json.Marshal(originalMap)
   206  	require.NoError(err)
   207  
   208  	var unmarshalledMap map[ID]int
   209  	require.NoError(json.Unmarshal(mapJSON, &unmarshalledMap))
   210  
   211  	require.Equal(originalMap, unmarshalledMap)
   212  }
   213  
   214  func TestIDCompare(t *testing.T) {
   215  	tests := []struct {
   216  		a        ID
   217  		b        ID
   218  		expected int
   219  	}{
   220  		{
   221  			a:        ID{1},
   222  			b:        ID{0},
   223  			expected: 1,
   224  		},
   225  		{
   226  			a:        ID{1},
   227  			b:        ID{1},
   228  			expected: 0,
   229  		},
   230  		{
   231  			a:        ID{1, 0},
   232  			b:        ID{1, 2},
   233  			expected: -1,
   234  		},
   235  	}
   236  	for _, test := range tests {
   237  		t.Run(fmt.Sprintf("%s_%s_%d", test.a, test.b, test.expected), func(t *testing.T) {
   238  			require := require.New(t)
   239  
   240  			require.Equal(test.expected, test.a.Compare(test.b))
   241  			require.Equal(-test.expected, test.b.Compare(test.a))
   242  		})
   243  	}
   244  }