github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/int256/cmp_test.gno (about) 1 package int256 2 3 import ( 4 "testing" 5 ) 6 7 func TestEq(t *testing.T) { 8 tests := []struct { 9 x, y string 10 want bool 11 }{ 12 {"0", "0", true}, 13 {"0", "1", false}, 14 {"1", "0", false}, 15 {"-1", "0", false}, 16 {"0", "-1", false}, 17 {"1", "1", true}, 18 {"-1", "-1", true}, 19 {"115792089237316195423570985008687907853269984665640564039457584007913129639935", "-115792089237316195423570985008687907853269984665640564039457584007913129639935", false}, 20 {"-115792089237316195423570985008687907853269984665640564039457584007913129639935", "-115792089237316195423570985008687907853269984665640564039457584007913129639935", true}, 21 } 22 23 for _, tc := range tests { 24 x, err := FromDecimal(tc.x) 25 if err != nil { 26 t.Error(err) 27 continue 28 } 29 30 y, err := FromDecimal(tc.y) 31 if err != nil { 32 t.Error(err) 33 continue 34 } 35 36 got := x.Eq(y) 37 if got != tc.want { 38 t.Errorf("Eq(%s, %s) = %v, want %v", tc.x, tc.y, got, tc.want) 39 } 40 } 41 } 42 43 func TestNeq(t *testing.T) { 44 tests := []struct { 45 x, y string 46 want bool 47 }{ 48 {"0", "0", false}, 49 {"0", "1", true}, 50 {"1", "0", true}, 51 {"-1", "0", true}, 52 {"0", "-1", true}, 53 {"1", "1", false}, 54 {"-1", "-1", false}, 55 {"115792089237316195423570985008687907853269984665640564039457584007913129639935", "-115792089237316195423570985008687907853269984665640564039457584007913129639935", true}, 56 {"-115792089237316195423570985008687907853269984665640564039457584007913129639935", "-115792089237316195423570985008687907853269984665640564039457584007913129639935", false}, 57 } 58 59 for _, tc := range tests { 60 x, err := FromDecimal(tc.x) 61 if err != nil { 62 t.Error(err) 63 continue 64 } 65 66 y, err := FromDecimal(tc.y) 67 if err != nil { 68 t.Error(err) 69 continue 70 } 71 72 got := x.Neq(y) 73 if got != tc.want { 74 t.Errorf("Neq(%s, %s) = %v, want %v", tc.x, tc.y, got, tc.want) 75 } 76 } 77 } 78 79 func TestCmp(t *testing.T) { 80 tests := []struct { 81 x, y string 82 want int 83 }{ 84 {"0", "0", 0}, 85 {"0", "1", -1}, 86 {"1", "0", 1}, 87 {"-1", "0", -1}, 88 {"0", "-1", 1}, 89 {"1", "1", 0}, 90 {"115792089237316195423570985008687907853269984665640564039457584007913129639935", "-115792089237316195423570985008687907853269984665640564039457584007913129639935", 1}, 91 } 92 93 for _, tc := range tests { 94 x, err := FromDecimal(tc.x) 95 if err != nil { 96 t.Error(err) 97 continue 98 } 99 100 y, err := FromDecimal(tc.y) 101 if err != nil { 102 t.Error(err) 103 continue 104 } 105 106 got := x.Cmp(y) 107 if got != tc.want { 108 t.Errorf("Cmp(%s, %s) = %v, want %v", tc.x, tc.y, got, tc.want) 109 } 110 } 111 } 112 113 func TestIsZero(t *testing.T) { 114 tests := []struct { 115 x string 116 want bool 117 }{ 118 {"0", true}, 119 {"-0", true}, 120 {"1", false}, 121 {"-1", false}, 122 {"10", false}, 123 } 124 125 for _, tc := range tests { 126 x, err := FromDecimal(tc.x) 127 if err != nil { 128 t.Error(err) 129 continue 130 } 131 132 got := x.IsZero() 133 if got != tc.want { 134 t.Errorf("IsZero(%s) = %v, want %v", tc.x, got, tc.want) 135 } 136 } 137 } 138 139 func TestIsNeg(t *testing.T) { 140 tests := []struct { 141 x string 142 want bool 143 }{ 144 {"0", false}, 145 {"-0", true}, // TODO: should this be false? 146 {"1", false}, 147 {"-1", true}, 148 {"10", false}, 149 {"-10", true}, 150 } 151 152 for _, tc := range tests { 153 x, err := FromDecimal(tc.x) 154 if err != nil { 155 t.Error(err) 156 continue 157 } 158 159 got := x.IsNeg() 160 if got != tc.want { 161 t.Errorf("IsNeg(%s) = %v, want %v", tc.x, got, tc.want) 162 } 163 } 164 } 165 166 func TestLt(t *testing.T) { 167 tests := []struct { 168 x, y string 169 want bool 170 }{ 171 {"0", "0", false}, 172 {"0", "1", true}, 173 {"1", "0", false}, 174 {"-1", "0", true}, 175 {"0", "-1", false}, 176 {"1", "1", false}, 177 {"-1", "-1", false}, 178 {"115792089237316195423570985008687907853269984665640564039457584007913129639935", "-115792089237316195423570985008687907853269984665640564039457584007913129639935", false}, 179 } 180 181 for _, tc := range tests { 182 x, err := FromDecimal(tc.x) 183 if err != nil { 184 t.Error(err) 185 continue 186 } 187 188 y, err := FromDecimal(tc.y) 189 if err != nil { 190 t.Error(err) 191 continue 192 } 193 194 got := x.Lt(y) 195 if got != tc.want { 196 t.Errorf("Lt(%s, %s) = %v, want %v", tc.x, tc.y, got, tc.want) 197 } 198 } 199 } 200 201 func TestGt(t *testing.T) { 202 tests := []struct { 203 x, y string 204 want bool 205 }{ 206 {"0", "0", false}, 207 {"0", "1", false}, 208 {"1", "0", true}, 209 {"-1", "0", false}, 210 {"0", "-1", true}, 211 {"1", "1", false}, 212 {"-1", "-1", false}, 213 {"115792089237316195423570985008687907853269984665640564039457584007913129639935", "-115792089237316195423570985008687907853269984665640564039457584007913129639935", true}, 214 } 215 216 for _, tc := range tests { 217 x, err := FromDecimal(tc.x) 218 if err != nil { 219 t.Error(err) 220 continue 221 } 222 223 y, err := FromDecimal(tc.y) 224 if err != nil { 225 t.Error(err) 226 continue 227 } 228 229 got := x.Gt(y) 230 if got != tc.want { 231 t.Errorf("Gt(%s, %s) = %v, want %v", tc.x, tc.y, got, tc.want) 232 } 233 } 234 } 235 236 func TestClone(t *testing.T) { 237 tests := []struct { 238 x string 239 }{ 240 {"0"}, 241 {"-0"}, 242 {"1"}, 243 {"-1"}, 244 {"10"}, 245 {"-10"}, 246 {"115792089237316195423570985008687907853269984665640564039457584007913129639935"}, 247 {"-115792089237316195423570985008687907853269984665640564039457584007913129639935"}, 248 } 249 250 for _, tc := range tests { 251 x, err := FromDecimal(tc.x) 252 if err != nil { 253 t.Error(err) 254 continue 255 } 256 257 y := x.Clone() 258 259 if x.Cmp(y) != 0 { 260 t.Errorf("Clone(%s) = %v, want %v", tc.x, y, x) 261 } 262 } 263 }