github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/int256/conversion_test.gno (about)

     1  package int256
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gno.land/p/demo/uint256"
     7  )
     8  
     9  func TestSetInt64(t *testing.T) {
    10  	tests := []struct {
    11  		x    int64
    12  		want string
    13  	}{
    14  		{0, "0"},
    15  		{1, "1"},
    16  		{-1, "-1"},
    17  		{9223372036854775807, "9223372036854775807"},
    18  		{-9223372036854775808, "-9223372036854775808"},
    19  	}
    20  
    21  	for _, tc := range tests {
    22  		var z Int
    23  		z.SetInt64(tc.x)
    24  
    25  		got := z.ToString()
    26  		if got != tc.want {
    27  			t.Errorf("SetInt64(%d) = %s, want %s", tc.x, got, tc.want)
    28  		}
    29  	}
    30  }
    31  
    32  func TestSetUint64(t *testing.T) {
    33  	tests := []struct {
    34  		x    uint64
    35  		want string
    36  	}{
    37  		{0, "0"},
    38  		{1, "1"},
    39  	}
    40  
    41  	for _, tc := range tests {
    42  		var z Int
    43  		z.SetUint64(tc.x)
    44  
    45  		got := z.ToString()
    46  		if got != tc.want {
    47  			t.Errorf("SetUint64(%d) = %s, want %s", tc.x, got, tc.want)
    48  		}
    49  	}
    50  }
    51  
    52  func TestUint64(t *testing.T) {
    53  	tests := []struct {
    54  		x    string
    55  		want uint64
    56  	}{
    57  		{"0", 0},
    58  		{"1", 1},
    59  		{"9223372036854775807", 9223372036854775807},
    60  		{"9223372036854775808", 9223372036854775808},
    61  		{"18446744073709551615", 18446744073709551615},
    62  		{"18446744073709551616", 0},
    63  		{"18446744073709551617", 1},
    64  		{"-1", 1},
    65  		{"-18446744073709551615", 18446744073709551615},
    66  		{"-18446744073709551616", 0},
    67  		{"-18446744073709551617", 1},
    68  	}
    69  
    70  	for _, tc := range tests {
    71  		z := MustFromDecimal(tc.x)
    72  
    73  		got := z.Uint64()
    74  		if got != tc.want {
    75  			t.Errorf("Uint64(%s) = %d, want %d", tc.x, got, tc.want)
    76  		}
    77  	}
    78  }
    79  
    80  func TestInt64(t *testing.T) {
    81  	tests := []struct {
    82  		x    string
    83  		want int64
    84  	}{
    85  		{"0", 0},
    86  		{"1", 1},
    87  		{"9223372036854775807", 9223372036854775807},
    88  		{"18446744073709551616", 0},
    89  		{"18446744073709551617", 1},
    90  		{"-1", -1},
    91  		{"-9223372036854775808", -9223372036854775808},
    92  	}
    93  
    94  	for _, tc := range tests {
    95  		z := MustFromDecimal(tc.x)
    96  
    97  		got := z.Int64()
    98  		if got != tc.want {
    99  			t.Errorf("Uint64(%s) = %d, want %d", tc.x, got, tc.want)
   100  		}
   101  	}
   102  }
   103  
   104  func TestNeg(t *testing.T) {
   105  	tests := []struct {
   106  		x    string
   107  		want string
   108  	}{
   109  		{"0", "0"},
   110  		{"1", "-1"},
   111  		{"-1", "1"},
   112  		{"9223372036854775807", "-9223372036854775807"},
   113  		{"-18446744073709551615", "18446744073709551615"},
   114  	}
   115  
   116  	for _, tc := range tests {
   117  		z := MustFromDecimal(tc.x)
   118  		z.Neg(z)
   119  
   120  		got := z.ToString()
   121  		if got != tc.want {
   122  			t.Errorf("Neg(%s) = %s, want %s", tc.x, got, tc.want)
   123  		}
   124  	}
   125  }
   126  
   127  func TestSet(t *testing.T) {
   128  	tests := []struct {
   129  		x    string
   130  		want string
   131  	}{
   132  		{"0", "0"},
   133  		{"1", "1"},
   134  		{"-1", "-1"},
   135  		{"9223372036854775807", "9223372036854775807"},
   136  		{"-18446744073709551615", "-18446744073709551615"},
   137  	}
   138  
   139  	for _, tc := range tests {
   140  		z := MustFromDecimal(tc.x)
   141  		z.Set(z)
   142  
   143  		got := z.ToString()
   144  		if got != tc.want {
   145  			t.Errorf("Set(%s) = %s, want %s", tc.x, got, tc.want)
   146  		}
   147  	}
   148  }
   149  
   150  func TestSetUint256(t *testing.T) {
   151  	tests := []struct {
   152  		x    string
   153  		want string
   154  	}{
   155  		{"0", "0"},
   156  		{"1", "1"},
   157  		{"9223372036854775807", "9223372036854775807"},
   158  		{"18446744073709551615", "18446744073709551615"},
   159  	}
   160  
   161  	for _, tc := range tests {
   162  		got := New()
   163  
   164  		z := uint256.MustFromDecimal(tc.x)
   165  		got.SetUint256(z)
   166  
   167  		if got.ToString() != tc.want {
   168  			t.Errorf("SetUint256(%s) = %s, want %s", tc.x, got.ToString(), tc.want)
   169  		}
   170  	}
   171  }