github.com/aeternity/aepp-sdk-go@v1.0.3-0.20190606142815-1c0ffdc21fd9/utils/utils_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"math"
     5  	"math/big"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  func TestBigIntNewStr(t *testing.T) {
    11  	// Use the convenience function NewBigIntFromString to set the number
    12  	a, err := NewIntFromString("20000000000000000000") // 2e19
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  
    17  	// Make a BigInt the old fashioned, raw way.
    18  	ex := new(big.Int)
    19  	ex.SetString("20000000000000000000", 10)
    20  
    21  	// They should equal each other.
    22  	if ex.Cmp(a) != 0 {
    23  		t.Fatalf("Expected 20000000000000000000 but got %v", a.String())
    24  	}
    25  
    26  }
    27  
    28  func TestBigIntLargerOrEqualToZero(t *testing.T) {
    29  	var amount = new(big.Int)
    30  	var tests = []struct {
    31  		input    int64
    32  		expected bool
    33  	}{
    34  		{math.MinInt64, false},
    35  		{-1, false},
    36  		{0, true},
    37  		{1, true},
    38  		{math.MaxInt64, true},
    39  	}
    40  
    41  	for _, test := range tests {
    42  		amount.SetInt64(test.input)
    43  		amountc := BigInt(*amount)
    44  		err := amountc.LargerOrEqualToZero()
    45  		if reflect.TypeOf(err) != reflect.TypeOf(test.expected) {
    46  			t.Errorf("Test Failed: %v inputted, %v expected, %#v received", test.input, test.expected, err)
    47  		}
    48  	}
    49  }
    50  
    51  func TestBigIntLargerThanZero(t *testing.T) {
    52  	var amount = new(big.Int)
    53  	var tests = []struct {
    54  		input    int64
    55  		expected bool
    56  	}{
    57  		{math.MinInt64, false},
    58  		{-1, false},
    59  		{0, false},
    60  		{1, true},
    61  		{math.MaxInt64, true},
    62  	}
    63  
    64  	for _, test := range tests {
    65  		amount.SetInt64(test.input)
    66  		amountc := BigInt(*amount)
    67  		err := amountc.LargerThanZero()
    68  		if reflect.TypeOf(err) != reflect.TypeOf(test.expected) {
    69  			t.Errorf("Test Failed: %v inputted, %v expected, %#v received", test.input, test.expected, err)
    70  		}
    71  	}
    72  }
    73  
    74  func TestBigInt_UnmarshalJSON(t *testing.T) {
    75  	type args struct {
    76  		text []byte
    77  	}
    78  	tests := []struct {
    79  		name    string
    80  		b       BigInt
    81  		args    args
    82  		wantErr bool
    83  	}{{
    84  		name:    "Deserialize this",
    85  		args:    args{[]byte("1600000000000000000129127208515966861305")},
    86  		b:       BigInt(*RequireIntFromString("1600000000000000000129127208515966861305")),
    87  		wantErr: false,
    88  	}}
    89  	for _, tt := range tests {
    90  		t.Run(tt.name, func(t *testing.T) {
    91  			b := &BigInt{}
    92  			if err := b.UnmarshalJSON(tt.args.text); (err != nil) != tt.wantErr {
    93  				t.Errorf("BigInt.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
    94  			}
    95  			if b.Cmp(&tt.b) != 0 {
    96  				t.Errorf("The values are not the same: b: %s, want: %s", b.String(), tt.b.String())
    97  			}
    98  		})
    99  	}
   100  }