github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/protocol/vm/types_test.go (about)

     1  package vm
     2  
     3  import (
     4  	"bytes"
     5  	"math/big"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/holiman/uint256"
    10  
    11  	"github.com/bytom/bytom/testutil"
    12  )
    13  
    14  func TestBoolBytes(t *testing.T) {
    15  	got := BoolBytes(true)
    16  	want := []byte{1}
    17  	if !bytes.Equal(got, want) {
    18  		t.Errorf("BoolBytes(t) = %x want %x", got, want)
    19  	}
    20  
    21  	got = BoolBytes(false)
    22  	want = []byte{}
    23  	if !bytes.Equal(got, want) {
    24  		t.Errorf("BoolBytes(f) = %x want %x", got, want)
    25  	}
    26  }
    27  
    28  func TestAsBool(t *testing.T) {
    29  	cases := []struct {
    30  		data []byte
    31  		want bool
    32  	}{
    33  		{[]byte{0, 0, 0, 0}, false},
    34  		{[]byte{0}, false},
    35  		{[]byte{}, false},
    36  		{[]byte{1}, true},
    37  		{[]byte{1, 1, 1, 1}, true},
    38  		{[]byte{0, 0, 0, 1}, true},
    39  		{[]byte{1, 0, 0, 0}, true},
    40  		{[]byte{2}, true},
    41  	}
    42  
    43  	for _, c := range cases {
    44  		got := AsBool(c.data)
    45  
    46  		if got != c.want {
    47  			t.Errorf("AsBool(%x) = %v want %v", c.data, got, c.want)
    48  		}
    49  	}
    50  }
    51  
    52  func TestBigIntBytes(t *testing.T) {
    53  	tests := []struct {
    54  		input []byte
    55  		num   *big.Int
    56  	}{
    57  		{num: new(big.Int), input: []byte{}},
    58  		{num: new(big.Int).SetInt64(0), input: []byte{}},
    59  		{num: new(big.Int).SetInt64(1), input: []byte{0x01}},
    60  		{num: new(big.Int).SetInt64(255), input: []byte{0xff}},
    61  		{num: new(big.Int).SetInt64(256), input: []byte{0x00, 0x01}},
    62  		{num: new(big.Int).SetInt64(46657), input: []byte{0x41, 0xb6}},
    63  		{num: new(big.Int).SetInt64(1 << 32), input: []byte{0x00, 0x00, 0x00, 0x00, 0x01}},
    64  		{
    65  			num:   new(big.Int).Exp(new(big.Int).SetInt64(10), new(big.Int).SetInt64(32), nil),
    66  			input: []byte{0x00, 0x00, 0x00, 0x00, 0x81, 0xef, 0xac, 0x85, 0x5b, 0x41, 0x6d, 0x2d, 0xee, 0x04},
    67  		},
    68  		{num: new(big.Int).SetInt64(-1), input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    69  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    70  			0xff, 0xff, 0xff, 0xff}},
    71  		{num: new(big.Int).SetInt64(-256), input: []byte{0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    72  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    73  			0xff, 0xff, 0xff, 0xff}},
    74  	}
    75  	for _, test := range tests {
    76  		fromBig, b := uint256.FromBig(test.num)
    77  		if b {
    78  			t.Errorf("FromBig overflow")
    79  		}
    80  
    81  		gotData := BigIntBytes(fromBig)
    82  		if !bytes.Equal(gotData, test.input) {
    83  			t.Errorf("BigIntBytes(%s) = %x want %x", test.num.String(), gotData, test.input)
    84  		}
    85  	}
    86  }
    87  
    88  func TestAsBigInt(t *testing.T) {
    89  	tests := []struct {
    90  		input     []byte
    91  		num       *big.Int
    92  		wantError bool
    93  	}{
    94  		{num: new(big.Int), input: []byte{}},
    95  		{num: new(big.Int), input: []byte{0x00}},
    96  		{num: new(big.Int).SetInt64(0), input: []byte{0x00}},
    97  		{num: new(big.Int).SetInt64(1), input: []byte{0x01}},
    98  		{num: new(big.Int).SetInt64(255), input: []byte{0xff}},
    99  		{num: new(big.Int).SetInt64(256), input: []byte{0x00, 0x01}},
   100  		{num: new(big.Int).SetInt64(46657), input: []byte{0x41, 0xb6}},
   101  		{num: new(big.Int).SetInt64(1 << 32), input: []byte{0x00, 0x00, 0x00, 0x00, 0x01}},
   102  		{
   103  			num:   new(big.Int).Exp(new(big.Int).SetInt64(10), new(big.Int).SetInt64(32), nil),
   104  			input: []byte{0x00, 0x00, 0x00, 0x00, 0x81, 0xef, 0xac, 0x85, 0x5b, 0x41, 0x6d, 0x2d, 0xee, 0x04},
   105  		},
   106  		{num: new(big.Int).SetInt64(-1), input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, wantError: true},
   107  		{num: new(big.Int).SetInt64(-256), input: []byte{0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00}, wantError: true},
   108  		{input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, wantError: true},
   109  	}
   110  	for _, test := range tests {
   111  		data, err := AsBigInt(test.input)
   112  		if err != nil {
   113  			if test.wantError {
   114  				continue
   115  			}
   116  			t.Errorf("AsBigInt(%q) --> err %s", test.input, err.Error())
   117  		}
   118  
   119  		fromBig, b := uint256.FromBig(test.num)
   120  		if b {
   121  			t.Errorf("FromBig overflow")
   122  		}
   123  
   124  		if data != nil && !data.Eq(fromBig) {
   125  			t.Errorf("AsBigInt(%s) = %x want %x", test.num.String(), data, test.input)
   126  		}
   127  	}
   128  }
   129  
   130  func TestInt64BigIntConvert(t *testing.T) {
   131  	cases := []uint64{0, 1, 2, 1024, 65536, 9223372036854775807}
   132  	for i, c := range cases {
   133  		x := Uint64Bytes(c)
   134  		y := BigIntBytes(uint256.NewInt(c))
   135  		if !testutil.DeepEqual(x, y) {
   136  			t.Errorf("case %d fail on compare %d bytes", i, c)
   137  		}
   138  	}
   139  }
   140  
   141  func Test_reverse(t *testing.T) {
   142  	type args struct {
   143  		b []byte
   144  	}
   145  	type wants struct {
   146  		origin []byte
   147  		want   []byte
   148  	}
   149  	tests := []struct {
   150  		name  string
   151  		args  args
   152  		wants wants
   153  	}{
   154  		{
   155  			name: "test reverse",
   156  			args: args{
   157  				b: []byte{0x00, 0x00, 0x00, 0x00, 0x01},
   158  			},
   159  			wants: wants{
   160  				origin: []byte{0x00, 0x00, 0x00, 0x00, 0x01},
   161  				want:   []byte{0x01, 0x00, 0x00, 0x00, 0x00},
   162  			},
   163  		},
   164  		{
   165  			name: "test reverse 1",
   166  			args: args{
   167  				b: []byte{0x01, 0x02, 0x20, 0x03, 0x01},
   168  			},
   169  			wants: wants{
   170  				origin: []byte{0x01, 0x02, 0x20, 0x03, 0x01},
   171  				want:   []byte{0x01, 0x03, 0x20, 0x02, 0x01},
   172  			},
   173  		},
   174  	}
   175  	for _, tt := range tests {
   176  		t.Run(tt.name, func(t *testing.T) {
   177  			if got := reverse(tt.args.b); !reflect.DeepEqual(got, tt.wants.want) {
   178  				t.Errorf("reverse() = %v, want %v", got, tt.wants.want)
   179  			}
   180  			if !reflect.DeepEqual(tt.args.b, tt.wants.origin) {
   181  				t.Errorf("after reverse args = %v, origin %v", tt.args.b, tt.wants.origin)
   182  			}
   183  		})
   184  	}
   185  }