github.com/status-im/status-go@v1.1.0/sqlite/fields_test.go (about)

     1  package sqlite
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  )
     7  
     8  func strToPtr(s string) *string {
     9  	res := new(string)
    10  	*res = s
    11  	return res
    12  }
    13  
    14  func TestBigIntToPadded128BitsStr(t *testing.T) {
    15  	testCases := []struct {
    16  		name     string
    17  		input    *big.Int
    18  		expected *string
    19  	}{
    20  		{
    21  			name:     "case small",
    22  			input:    big.NewInt(123456),
    23  			expected: strToPtr("0000000000000000000000000001e240"),
    24  		},
    25  		{
    26  			name:     "case zero",
    27  			input:    big.NewInt(0),
    28  			expected: strToPtr("00000000000000000000000000000000"),
    29  		},
    30  		{
    31  			name:     "case very large",
    32  			input:    new(big.Int).Exp(big.NewInt(10), big.NewInt(26), nil),
    33  			expected: strToPtr("000000000052b7d2dcc80cd2e4000000"),
    34  		},
    35  		{
    36  			name:     "case max",
    37  			input:    new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 128), big.NewInt(1)),
    38  			expected: strToPtr("ffffffffffffffffffffffffffffffff"),
    39  		},
    40  		{
    41  			name:     "case 3",
    42  			input:    nil,
    43  			expected: nil,
    44  		},
    45  	}
    46  
    47  	for _, tc := range testCases {
    48  		t.Run(tc.name, func(t *testing.T) {
    49  			result := BigIntToPadded128BitsStr(tc.input)
    50  			if result != nil && tc.expected != nil {
    51  				if *result != *tc.expected {
    52  					t.Errorf("expected %s, got %s", *tc.expected, *result)
    53  				}
    54  			} else if result != nil || tc.expected != nil {
    55  				t.Errorf("expected %v, got %v", tc.expected, result)
    56  			}
    57  		})
    58  	}
    59  }
    60  
    61  func TestInt64ToPadded128BitsStr(t *testing.T) {
    62  	testCases := []struct {
    63  		name     string
    64  		input    int64
    65  		expected *string
    66  	}{
    67  		{
    68  			name:     "case nonzero",
    69  			input:    123456,
    70  			expected: strToPtr("0000000000000000000000000001e240"),
    71  		},
    72  		{
    73  			name:     "case zero",
    74  			input:    0,
    75  			expected: strToPtr("00000000000000000000000000000000"),
    76  		},
    77  	}
    78  
    79  	for _, tc := range testCases {
    80  		t.Run(tc.name, func(t *testing.T) {
    81  			result := Int64ToPadded128BitsStr(tc.input)
    82  			if result != nil && tc.expected != nil {
    83  				if *result != *tc.expected {
    84  					t.Errorf("expected %s, got %s", *tc.expected, *result)
    85  				}
    86  			} else if result != nil || tc.expected != nil {
    87  				t.Errorf("expected %v, got %v", tc.expected, result)
    88  			}
    89  		})
    90  	}
    91  }