github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/uint256/cmp_test.gno (about) 1 package uint256 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func TestCmp(t *testing.T) { 9 tests := []struct { 10 x, y string 11 want int 12 }{ 13 {"0", "0", 0}, 14 {"0", "1", -1}, 15 {"1", "0", 1}, 16 {"1", "1", 0}, 17 {"10", "10", 0}, 18 {"10", "11", -1}, 19 {"11", "10", 1}, 20 } 21 22 for _, tc := range tests { 23 x, err := FromDecimal(tc.x) 24 if err != nil { 25 t.Error(err) 26 continue 27 } 28 29 y, err := FromDecimal(tc.y) 30 if err != nil { 31 t.Error(err) 32 continue 33 } 34 35 got := x.Cmp(y) 36 if got != tc.want { 37 t.Errorf("Cmp(%s, %s) = %v, want %v", tc.x, tc.y, got, tc.want) 38 } 39 } 40 } 41 42 func TestIsZero(t *testing.T) { 43 tests := []struct { 44 x string 45 want bool 46 }{ 47 {"0", true}, 48 {"1", false}, 49 {"10", false}, 50 } 51 52 for _, tc := range tests { 53 x, err := FromDecimal(tc.x) 54 if err != nil { 55 t.Error(err) 56 continue 57 } 58 59 got := x.IsZero() 60 if got != tc.want { 61 t.Errorf("IsZero(%s) = %v, want %v", tc.x, got, tc.want) 62 } 63 } 64 } 65 66 func TestLtUint64(t *testing.T) { 67 tests := []struct { 68 x string 69 y uint64 70 want bool 71 }{ 72 {"0", 1, true}, 73 {"1", 0, false}, 74 {"10", 10, false}, 75 {"0xffffffffffffffff", 0, false}, 76 {"0x10000000000000000", 10000000000000000, false}, 77 } 78 79 for _, tc := range tests { 80 var x *Uint 81 var err error 82 83 if strings.HasPrefix(tc.x, "0x") { 84 x, err = FromHex(tc.x) 85 if err != nil { 86 t.Error(err) 87 continue 88 } 89 } else { 90 x, err = FromDecimal(tc.x) 91 if err != nil { 92 t.Error(err) 93 continue 94 } 95 } 96 97 got := x.LtUint64(tc.y) 98 99 if got != tc.want { 100 t.Errorf("LtUint64(%s, %d) = %v, want %v", tc.x, tc.y, got, tc.want) 101 } 102 } 103 } 104 105 func TestSGT(t *testing.T) { 106 x := MustFromHex("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe") 107 y := MustFromHex("0x0") 108 actual := x.Sgt(y) 109 if actual { 110 t.Fatalf("Expected %v false", actual) 111 } 112 113 x = MustFromHex("0x0") 114 y = MustFromHex("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe") 115 actual = x.Sgt(y) 116 if !actual { 117 t.Fatalf("Expected %v true", actual) 118 } 119 } 120 121 func TestEq(t *testing.T) { 122 tests := []struct { 123 x string 124 y string 125 want bool 126 }{ 127 {"0xffffffffffffffff", "18446744073709551615", true}, 128 {"0x10000000000000000", "18446744073709551616", true}, 129 {"0", "0", true}, 130 {"115792089237316195423570985008687907853269984665640564039457584007913129639935", "115792089237316195423570985008687907853269984665640564039457584007913129639935", true}, 131 } 132 133 for i, tc := range tests { 134 var x *Uint 135 var err error 136 137 if strings.HasPrefix(tc.x, "0x") { 138 x, err = FromHex(tc.x) 139 if err != nil { 140 t.Error(err) 141 continue 142 } 143 } else { 144 x, err = FromDecimal(tc.x) 145 if err != nil { 146 t.Error(err) 147 continue 148 } 149 } 150 151 y, err := FromDecimal(tc.y) 152 if err != nil { 153 t.Error(err) 154 continue 155 } 156 157 got := x.Eq(y) 158 159 if got != tc.want { 160 t.Errorf("Eq(%s, %s) = %v, want %v", tc.x, tc.y, got, tc.want) 161 } 162 } 163 }