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

     1  package int256
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gno.land/p/demo/uint256"
     7  )
     8  
     9  func TestAbs(t *testing.T) {
    10  	tests := []struct {
    11  		x, want string
    12  	}{
    13  		{"0", "0"},
    14  		{"1", "1"},
    15  		{"-1", "1"},
    16  		{"-2", "2"},
    17  		{"-115792089237316195423570985008687907853269984665640564039457584007913129639935", "115792089237316195423570985008687907853269984665640564039457584007913129639935"},
    18  	}
    19  
    20  	for _, tc := range tests {
    21  		x, err := FromDecimal(tc.x)
    22  		if err != nil {
    23  			t.Error(err)
    24  			continue
    25  		}
    26  
    27  		got := x.Abs()
    28  
    29  		if got.ToString() != tc.want {
    30  			t.Errorf("Abs(%s) = %v, want %v", tc.x, got.ToString(), tc.want)
    31  		}
    32  	}
    33  }
    34  
    35  func TestAbsGt(t *testing.T) {
    36  	tests := []struct {
    37  		x, y, want string
    38  	}{
    39  		{"0", "0", "false"},
    40  		{"1", "0", "true"},
    41  		{"-1", "0", "true"},
    42  		{"-1", "1", "false"},
    43  		{"-2", "1", "true"},
    44  		{"-115792089237316195423570985008687907853269984665640564039457584007913129639935", "0", "true"},
    45  		{"-115792089237316195423570985008687907853269984665640564039457584007913129639935", "1", "true"},
    46  		{"-115792089237316195423570985008687907853269984665640564039457584007913129639935", "115792089237316195423570985008687907853269984665640564039457584007913129639935", "false"},
    47  	}
    48  
    49  	for _, tc := range tests {
    50  		x, err := FromDecimal(tc.x)
    51  		if err != nil {
    52  			t.Error(err)
    53  			continue
    54  		}
    55  
    56  		y, err := uint256.FromDecimal(tc.y)
    57  		if err != nil {
    58  			t.Error(err)
    59  			continue
    60  		}
    61  
    62  		got := x.AbsGt(y)
    63  
    64  		if got != (tc.want == "true") {
    65  			t.Errorf("AbsGt(%s, %s) = %v, want %v", tc.x, tc.y, got, tc.want)
    66  		}
    67  	}
    68  }
    69  
    70  func TestAbsLt(t *testing.T) {
    71  	tests := []struct {
    72  		x, y, want string
    73  	}{
    74  		{"0", "0", "false"},
    75  		{"1", "0", "false"},
    76  		{"-1", "0", "false"},
    77  		{"-1", "1", "false"},
    78  		{"-2", "1", "false"},
    79  		{"-5", "10", "true"},
    80  		{"31330", "31337", "true"},
    81  		{"-115792089237316195423570985008687907853269984665640564039457584007913129639935", "0", "false"},
    82  		{"-115792089237316195423570985008687907853269984665640564039457584007913129639935", "1", "false"},
    83  		{"-115792089237316195423570985008687907853269984665640564039457584007913129639935", "115792089237316195423570985008687907853269984665640564039457584007913129639935", "false"},
    84  	}
    85  
    86  	for _, tc := range tests {
    87  		x, err := FromDecimal(tc.x)
    88  		if err != nil {
    89  			t.Error(err)
    90  			continue
    91  		}
    92  
    93  		y, err := uint256.FromDecimal(tc.y)
    94  		if err != nil {
    95  			t.Error(err)
    96  			continue
    97  		}
    98  
    99  		got := x.AbsLt(y)
   100  
   101  		if got != (tc.want == "true") {
   102  			t.Errorf("AbsLt(%s, %s) = %v, want %v", tc.x, tc.y, got, tc.want)
   103  		}
   104  	}
   105  }