github.com/igggame/nebulas-go@v2.1.0+incompatible/util/byteutils/bytes_test.go (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package byteutils
    20  
    21  import (
    22  	"errors"
    23  	"reflect"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  type testStringEncoderAndDecoder struct {
    30  }
    31  
    32  func (o *testStringEncoderAndDecoder) EncodeToBytes(s interface{}) ([]byte, error) {
    33  	str := s.(string)
    34  
    35  	if len(str) == 0 {
    36  		return nil, errors.New("s must be string")
    37  	}
    38  
    39  	return []byte(str), nil
    40  }
    41  
    42  func (o *testStringEncoderAndDecoder) DecodeFromBytes(data []byte) (interface{}, error) {
    43  	return string(data), nil
    44  }
    45  
    46  func TestEncode(t *testing.T) {
    47  	o := &testStringEncoderAndDecoder{}
    48  
    49  	src := "Hello, world"
    50  	want := []byte{72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100}
    51  
    52  	ret, err := Encode(src, o)
    53  	assert.Nil(t, err, "err should be nil")
    54  	assert.Equal(t, want, ret, "Encode() = %v, want %v", ret, want)
    55  }
    56  
    57  func TestDecode(t *testing.T) {
    58  	o := &testStringEncoderAndDecoder{}
    59  
    60  	src := []byte{72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100}
    61  	want := "Hello, world"
    62  
    63  	ret, err := Decode(src, o)
    64  	assert.Nil(t, err, "err should be nil")
    65  	assert.Equal(t, want, ret, "Decode() = \"%v\", want \"%v\"", ret, want)
    66  }
    67  
    68  func TestHex(t *testing.T) {
    69  	type args struct {
    70  		data []byte
    71  	}
    72  	tests := []struct {
    73  		name string
    74  		args args
    75  		want string
    76  	}{
    77  		{
    78  			"a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a",
    79  			args{[]byte{167, 255, 198, 248, 191, 30, 215, 102, 81, 193, 71, 86, 160, 97, 214, 98, 245, 128, 255, 77, 228, 59, 73, 250, 130, 216, 10, 75, 128, 248, 67, 74}},
    80  			"a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a",
    81  		},
    82  		{
    83  			"3550aba97492de38af3066f0157fc532db6791b37d53262ce7688dcc5d461856",
    84  			args{[]byte{53, 80, 171, 169, 116, 146, 222, 56, 175, 48, 102, 240, 21, 127, 197, 50, 219, 103, 145, 179, 125, 83, 38, 44, 231, 104, 141, 204, 93, 70, 24, 86}},
    85  			"3550aba97492de38af3066f0157fc532db6791b37d53262ce7688dcc5d461856",
    86  		},
    87  		{
    88  			"blank string",
    89  			args{[]byte{}},
    90  			"",
    91  		},
    92  	}
    93  	for _, tt := range tests {
    94  		t.Run(tt.name, func(t *testing.T) {
    95  			if got := Hex(tt.args.data); got != tt.want {
    96  				t.Errorf("Hex() = %v, want %v", got, tt.want)
    97  			}
    98  		})
    99  	}
   100  }
   101  
   102  func TestFromHex(t *testing.T) {
   103  	type args struct {
   104  		data string
   105  	}
   106  	tests := []struct {
   107  		name    string
   108  		args    args
   109  		want    []byte
   110  		wantErr bool
   111  	}{
   112  		{
   113  			"a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a",
   114  			args{"a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"},
   115  			[]byte{167, 255, 198, 248, 191, 30, 215, 102, 81, 193, 71, 86, 160, 97, 214, 98, 245, 128, 255, 77, 228, 59, 73, 250, 130, 216, 10, 75, 128, 248, 67, 74},
   116  			false,
   117  		},
   118  		{
   119  			"3550aba97492de38af3066f0157fc532db6791b37d53262ce7688dcc5d461856",
   120  			args{"3550aba97492de38af3066f0157fc532db6791b37d53262ce7688dcc5d461856"},
   121  			[]byte{53, 80, 171, 169, 116, 146, 222, 56, 175, 48, 102, 240, 21, 127, 197, 50, 219, 103, 145, 179, 125, 83, 38, 44, 231, 104, 141, 204, 93, 70, 24, 86},
   122  			false,
   123  		},
   124  		{
   125  			"blank string",
   126  			args{""},
   127  			[]byte{},
   128  			false,
   129  		},
   130  	}
   131  	for _, tt := range tests {
   132  		t.Run(tt.name, func(t *testing.T) {
   133  			got, err := FromHex(tt.args.data)
   134  			if (err != nil) != tt.wantErr {
   135  				t.Errorf("FromHex() error = %v, wantErr %v", err, tt.wantErr)
   136  				return
   137  			}
   138  			if !reflect.DeepEqual(got, tt.want) {
   139  				t.Errorf("FromHex() = %v, want %v", got, tt.want)
   140  			}
   141  		})
   142  	}
   143  }
   144  
   145  func TestUint64(t *testing.T) {
   146  	type args struct {
   147  		data []byte
   148  	}
   149  	tests := []struct {
   150  		name string
   151  		args args
   152  		want uint64
   153  	}{
   154  		{
   155  			"0",
   156  			args{[]byte{0, 0, 0, 0, 0, 0, 0, 0}},
   157  			uint64(0),
   158  		},
   159  		{
   160  			"1024",
   161  			args{[]byte{0, 0, 0, 0, 0, 0, 4, 0}},
   162  			uint64(1024),
   163  		},
   164  		{
   165  			"Uint64.Max",
   166  			args{[]byte{255, 255, 255, 255, 255, 255, 255, 255}},
   167  			uint64(18446744073709551615),
   168  		},
   169  	}
   170  	for _, tt := range tests {
   171  		t.Run(tt.name, func(t *testing.T) {
   172  			if got := Uint64(tt.args.data); got != tt.want {
   173  				t.Errorf("Uint64() = %v, want %v", got, tt.want)
   174  			}
   175  		})
   176  	}
   177  }
   178  
   179  func TestFromUint64(t *testing.T) {
   180  	type args struct {
   181  		v uint64
   182  	}
   183  	tests := []struct {
   184  		name  string
   185  		args  args
   186  		wantB []byte
   187  	}{
   188  		{
   189  			"0",
   190  			args{uint64(0)},
   191  			[]byte{0, 0, 0, 0, 0, 0, 0, 0},
   192  		},
   193  		{
   194  			"1024",
   195  			args{uint64(1024)},
   196  			[]byte{0, 0, 0, 0, 0, 0, 4, 0},
   197  		},
   198  		{
   199  			"Uint64.Max",
   200  			args{uint64(18446744073709551615)},
   201  			[]byte{255, 255, 255, 255, 255, 255, 255, 255},
   202  		},
   203  	}
   204  	for _, tt := range tests {
   205  		t.Run(tt.name, func(t *testing.T) {
   206  			if gotB := FromUint64(tt.args.v); !reflect.DeepEqual(gotB, tt.wantB) {
   207  				t.Errorf("FromUint64() = %v, want %v", gotB, tt.wantB)
   208  			}
   209  		})
   210  	}
   211  }
   212  
   213  func TestUint32(t *testing.T) {
   214  	type args struct {
   215  		data []byte
   216  	}
   217  	tests := []struct {
   218  		name string
   219  		args args
   220  		want uint32
   221  	}{
   222  		{
   223  			"0",
   224  			args{[]byte{0, 0, 0, 0}},
   225  			uint32(0),
   226  		},
   227  		{
   228  			"1024",
   229  			args{[]byte{0, 0, 4, 0}},
   230  			uint32(1024),
   231  		},
   232  		{
   233  			"Uint32.Max",
   234  			args{[]byte{255, 255, 255, 255}},
   235  			uint32(4294967295),
   236  		},
   237  	}
   238  	for _, tt := range tests {
   239  		t.Run(tt.name, func(t *testing.T) {
   240  			if got := Uint32(tt.args.data); got != tt.want {
   241  				t.Errorf("Uint32() = %v, want %v", got, tt.want)
   242  			}
   243  		})
   244  	}
   245  }
   246  
   247  func TestFromUint32(t *testing.T) {
   248  	type args struct {
   249  		v uint32
   250  	}
   251  	tests := []struct {
   252  		name  string
   253  		args  args
   254  		wantB []byte
   255  	}{
   256  		{
   257  			"0",
   258  			args{uint32(0)},
   259  			[]byte{0, 0, 0, 0},
   260  		},
   261  		{
   262  			"1024",
   263  			args{uint32(1024)},
   264  			[]byte{0, 0, 4, 0},
   265  		},
   266  		{
   267  			"Uint32.Max",
   268  			args{uint32(4294967295)},
   269  			[]byte{255, 255, 255, 255},
   270  		},
   271  	}
   272  	for _, tt := range tests {
   273  		t.Run(tt.name, func(t *testing.T) {
   274  			if gotB := FromUint32(tt.args.v); !reflect.DeepEqual(gotB, tt.wantB) {
   275  				t.Errorf("FromUint32() = %v, want %v", gotB, tt.wantB)
   276  			}
   277  		})
   278  	}
   279  }
   280  
   281  func TestUint16(t *testing.T) {
   282  	type args struct {
   283  		data []byte
   284  	}
   285  	tests := []struct {
   286  		name string
   287  		args args
   288  		want uint16
   289  	}{
   290  		{
   291  			"0",
   292  			args{[]byte{0, 0}},
   293  			uint16(0),
   294  		},
   295  		{
   296  			"1024",
   297  			args{[]byte{4, 0}},
   298  			uint16(1024),
   299  		},
   300  		{
   301  			"Uint16.Max",
   302  			args{[]byte{255, 255}},
   303  			uint16(65535),
   304  		},
   305  	}
   306  	for _, tt := range tests {
   307  		t.Run(tt.name, func(t *testing.T) {
   308  			if got := Uint16(tt.args.data); got != tt.want {
   309  				t.Errorf("Uint16() = %v, want %v", got, tt.want)
   310  			}
   311  		})
   312  	}
   313  }
   314  
   315  func TestFromUint16(t *testing.T) {
   316  	type args struct {
   317  		v uint16
   318  	}
   319  	tests := []struct {
   320  		name  string
   321  		args  args
   322  		wantB []byte
   323  	}{
   324  		{
   325  			"0",
   326  			args{uint16(0)},
   327  			[]byte{0, 0},
   328  		},
   329  		{
   330  			"1024",
   331  			args{uint16(1024)},
   332  			[]byte{4, 0},
   333  		},
   334  		{
   335  			"Uint16.Max",
   336  			args{uint16(65535)},
   337  			[]byte{255, 255},
   338  		},
   339  	}
   340  	for _, tt := range tests {
   341  		t.Run(tt.name, func(t *testing.T) {
   342  			if gotB := FromUint16(tt.args.v); !reflect.DeepEqual(gotB, tt.wantB) {
   343  				t.Errorf("FromUint16() = %v, want %v", gotB, tt.wantB)
   344  			}
   345  		})
   346  	}
   347  }
   348  
   349  func TestInt64(t *testing.T) {
   350  	type args struct {
   351  		data []byte
   352  	}
   353  	tests := []struct {
   354  		name string
   355  		args args
   356  		want int64
   357  	}{
   358  		{
   359  			"0",
   360  			args{[]byte{0, 0, 0, 0, 0, 0, 0, 0}},
   361  			int64(0),
   362  		},
   363  		{
   364  			"1024",
   365  			args{[]byte{0, 0, 0, 0, 0, 0, 4, 0}},
   366  			int64(1024),
   367  		},
   368  		{
   369  			"-1024",
   370  			args{[]byte{255, 255, 255, 255, 255, 255, 252, 0}},
   371  			int64(-1024),
   372  		},
   373  		{
   374  			"Int64.Max",
   375  			args{[]byte{127, 255, 255, 255, 255, 255, 255, 255}},
   376  			int64(9223372036854775807),
   377  		},
   378  		{
   379  			"Int64.Min",
   380  			args{[]byte{128, 0, 0, 0, 0, 0, 0, 0}},
   381  			int64(-9223372036854775808),
   382  		},
   383  	}
   384  	for _, tt := range tests {
   385  		t.Run(tt.name, func(t *testing.T) {
   386  			if got := Int64(tt.args.data); got != tt.want {
   387  				t.Errorf("Int64() = %v, want %v", got, tt.want)
   388  			}
   389  		})
   390  	}
   391  }
   392  
   393  func TestFromInt64(t *testing.T) {
   394  	type args struct {
   395  		v int64
   396  	}
   397  	tests := []struct {
   398  		name  string
   399  		args  args
   400  		wantB []byte
   401  	}{
   402  		{
   403  			"0",
   404  			args{int64(0)},
   405  			[]byte{0, 0, 0, 0, 0, 0, 0, 0},
   406  		},
   407  		{
   408  			"1024",
   409  			args{int64(1024)},
   410  			[]byte{0, 0, 0, 0, 0, 0, 4, 0},
   411  		},
   412  		{
   413  			"-1024",
   414  			args{int64(-1024)},
   415  			[]byte{255, 255, 255, 255, 255, 255, 252, 0},
   416  		},
   417  		{
   418  			"Int64.Max",
   419  			args{int64(9223372036854775807)},
   420  			[]byte{127, 255, 255, 255, 255, 255, 255, 255},
   421  		},
   422  		{
   423  			"Int64.Min",
   424  			args{int64(-9223372036854775808)},
   425  			[]byte{128, 0, 0, 0, 0, 0, 0, 0},
   426  		},
   427  	}
   428  	for _, tt := range tests {
   429  		t.Run(tt.name, func(t *testing.T) {
   430  			if gotB := FromInt64(tt.args.v); !reflect.DeepEqual(gotB, tt.wantB) {
   431  				t.Errorf("FromInt64() = %v, want %v", gotB, tt.wantB)
   432  			}
   433  		})
   434  	}
   435  }
   436  
   437  func TestInt32(t *testing.T) {
   438  	type args struct {
   439  		data []byte
   440  	}
   441  	tests := []struct {
   442  		name string
   443  		args args
   444  		want int32
   445  	}{
   446  		{
   447  			"0",
   448  			args{[]byte{0, 0, 0, 0}},
   449  			int32(0),
   450  		},
   451  		{
   452  			"1024",
   453  			args{[]byte{0, 0, 4, 0}},
   454  			int32(1024),
   455  		},
   456  		{
   457  			"-1024",
   458  			args{[]byte{255, 255, 252, 0}},
   459  			int32(-1024),
   460  		},
   461  		{
   462  			"Int32.Max",
   463  			args{[]byte{127, 255, 255, 255}},
   464  			int32(2147483647),
   465  		},
   466  		{
   467  			"Int32.Min",
   468  			args{[]byte{128, 0, 0, 0}},
   469  			int32(-2147483648),
   470  		},
   471  	}
   472  	for _, tt := range tests {
   473  		t.Run(tt.name, func(t *testing.T) {
   474  			if got := Int32(tt.args.data); got != tt.want {
   475  				t.Errorf("Int32() = %v, want %v", got, tt.want)
   476  			}
   477  		})
   478  	}
   479  }
   480  
   481  func TestFromInt32(t *testing.T) {
   482  	type args struct {
   483  		v int32
   484  	}
   485  	tests := []struct {
   486  		name  string
   487  		args  args
   488  		wantB []byte
   489  	}{
   490  		{
   491  			"0",
   492  			args{int32(0)},
   493  			[]byte{0, 0, 0, 0},
   494  		},
   495  		{
   496  			"1024",
   497  			args{int32(1024)},
   498  			[]byte{0, 0, 4, 0},
   499  		},
   500  		{
   501  			"-1024",
   502  			args{int32(-1024)},
   503  			[]byte{255, 255, 252, 0},
   504  		},
   505  		{
   506  			"Int32.Max",
   507  			args{int32(2147483647)},
   508  			[]byte{127, 255, 255, 255},
   509  		},
   510  		{
   511  			"Int32.Min",
   512  			args{int32(-2147483648)},
   513  			[]byte{128, 0, 0, 0},
   514  		},
   515  	}
   516  	for _, tt := range tests {
   517  		t.Run(tt.name, func(t *testing.T) {
   518  			if gotB := FromInt32(tt.args.v); !reflect.DeepEqual(gotB, tt.wantB) {
   519  				t.Errorf("FromInt32() = %v, want %v", gotB, tt.wantB)
   520  			}
   521  		})
   522  	}
   523  }
   524  
   525  func TestInt16(t *testing.T) {
   526  	type args struct {
   527  		data []byte
   528  	}
   529  	tests := []struct {
   530  		name string
   531  		args args
   532  		want int16
   533  	}{
   534  		{
   535  			"0",
   536  			args{[]byte{0, 0}},
   537  			int16(0),
   538  		},
   539  		{
   540  			"1024",
   541  			args{[]byte{4, 0}},
   542  			int16(1024),
   543  		},
   544  		{
   545  			"-1024",
   546  			args{[]byte{252, 0}},
   547  			int16(-1024),
   548  		},
   549  		{
   550  			"Int16.Max",
   551  			args{[]byte{127, 255}},
   552  			int16(32767),
   553  		},
   554  		{
   555  			"Int16.Min",
   556  			args{[]byte{128, 0}},
   557  			int16(-32768),
   558  		},
   559  	}
   560  	for _, tt := range tests {
   561  		t.Run(tt.name, func(t *testing.T) {
   562  			if got := Int16(tt.args.data); got != tt.want {
   563  				t.Errorf("Int16() = %v, want %v", got, tt.want)
   564  			}
   565  		})
   566  	}
   567  }
   568  
   569  func TestFromInt16(t *testing.T) {
   570  	type args struct {
   571  		v int16
   572  	}
   573  	tests := []struct {
   574  		name  string
   575  		args  args
   576  		wantB []byte
   577  	}{
   578  		{
   579  			"0",
   580  			args{int16(0)},
   581  			[]byte{0, 0},
   582  		},
   583  		{
   584  			"1024",
   585  			args{int16(1024)},
   586  			[]byte{4, 0},
   587  		},
   588  		{
   589  			"-1024",
   590  			args{int16(-1024)},
   591  			[]byte{252, 0},
   592  		},
   593  		{
   594  			"Int16.Max",
   595  			args{int16(32767)},
   596  			[]byte{127, 255},
   597  		},
   598  		{
   599  			"Int16.Min",
   600  			args{int16(-32768)},
   601  			[]byte{128, 0},
   602  		},
   603  	}
   604  	for _, tt := range tests {
   605  		t.Run(tt.name, func(t *testing.T) {
   606  			if gotB := FromInt16(tt.args.v); !reflect.DeepEqual(gotB, tt.wantB) {
   607  				t.Errorf("FromInt16() = %v, want %v", gotB, tt.wantB)
   608  			}
   609  		})
   610  	}
   611  }