github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/lnutil/stdlib_test.go (about) 1 package lnutil 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 // I32tB 9 // 10 // Test three patterns, zero, max and min for int32 11 // These are obvious but test in case 12 func TestI32tB(t *testing.T) { 13 // if i is nil, it occurs an error 14 // when execution because a type of i is not int32 15 16 // test for a normal situation 17 // input: zero(int32) 18 // output: 4 bytes array, []byte{0x00, 0x00, 0x00, 0x00} 19 // 20 // no problem if input equals to output 21 var zeroI32 int32 = 0 22 zeroB32 := []byte{0x00, 0x00, 0x00, 0x00} 23 if !bytes.Equal(I32tB(zeroI32), zeroB32) { 24 t.Fatalf("it needs that input equals to output") 25 } 26 27 // test for a normal situation 28 // input: max(int32) 29 // output: 4 bytes array, []byte{0x7f, 0xff, 0xff, 0xff} 30 // 31 // no problem if input equals to output 32 var plusMaxI32 int32 = 2147483647 33 plusMaxB32 := []byte{0x7f, 0xff, 0xff, 0xff} 34 if !bytes.Equal(I32tB(plusMaxI32), plusMaxB32) { 35 t.Fatalf("it needs that input equals to output") 36 } 37 38 // test for a normal situation 39 // input: min(int32) 40 // output: 4 bytes array, []byte{0x80, 0x00, 0x00, 0x00} 41 // 42 // no problem if input equals to output 43 var minusMaxI32 int32 = -2147483648 44 minusMaxB32 := []byte{0x80, 0x00, 0x00, 0x00} 45 if !bytes.Equal(I32tB(minusMaxI32), minusMaxB32) { 46 t.Fatalf("it needs that input equals to output") 47 } 48 } 49 50 // U32tB 51 // 52 // Test three patterns, zero and max for uint32 53 // These are obvious but test in case 54 func TestU32tB(t *testing.T) { 55 // if i is nil, it occurs an error 56 // when execution because a type of i is not uint32 57 58 // test for a normal situation 59 // input: zero(uint32) 60 // output: 4 bytes array, []byte{0x00, 0x00, 0x00, 0x00} 61 // 62 // no problem if input equals to output 63 var zeroU32 uint32 = 0 64 zeroB32 := []byte{0x00, 0x00, 0x00, 0x00} 65 if !bytes.Equal(U32tB(zeroU32), zeroB32) { 66 t.Fatalf("it needs that input equals to output") 67 } 68 69 // test for a normal situation 70 // input: max(uint32) 71 // output: 4 bytes array, []byte{0xff, 0xff, 0xff, 0xff} 72 // 73 // no problem if input equals to output 74 var maxU32 uint32 = 4294967295 75 maxB32 := []byte{0xff, 0xff, 0xff, 0xff} 76 if !bytes.Equal(U32tB(maxU32), maxB32) { 77 t.Fatalf("it needs that input equals to output") 78 } 79 } 80 81 // BtU32 82 // 83 // BtU32 has an input of a byte array for arbitrary length (the length is an int type) 84 // If the length of the input is not 4, it returns fixed value. 85 // This function tests the length 86 func TestLenBInBtU32(t *testing.T) { 87 // if b is nil, it occurs an error 88 // when execution because a type of b is not []byte 89 90 // test for a normal situation 91 // input: 4 bytes array, []byte{0x00, 0x00, 0x00, 0x00} 92 // output: 0xffffffff 93 // 94 // no problem if input unequals to output 95 zeroB32 := []byte{0x00, 0x00, 0x00, 0x00} 96 if BtU32(zeroB32) == 0xffffffff { 97 t.Fatalf("it needs that input unequals to output") 98 } 99 100 // test for an anomaly situation 101 // input: 5 bytes array, []byte{0x00, 0x00, 0x00, 0x00, 0x00} 102 // output: 0xffffffff 103 // 104 // no problem if input equals to output 105 zeroB40 := []byte{0x00, 0x00, 0x00, 0x00, 106 0x00} 107 if BtU32(zeroB40) != 0xffffffff { 108 t.Fatalf("it needs that input equals to output") 109 } 110 111 // test for an anomaly situation 112 // input: 3 bytes array, []byte{0x00, 0x00, 0x00} 113 // output: 0xffffffff 114 // 115 // no problem if input equals to output 116 zeroB24 := []byte{0x00, 0x00, 0x00} 117 if BtU32(zeroB24) != 0xffffffff { 118 t.Fatalf("it needs that input equals to output") 119 } 120 121 // TODO: test a large size bytes arary 122 //zeroB18446744073709551615 := []byte{0x00 * 18446744073709551615} 123 //if BtU32(zeroB18446744073709551615) != 0xffffffff { 124 // t.Fatalf("it needs that input equals to output") 125 //} 126 127 // test for an anomaly situation 128 // input: empty bytes array 129 // output: 0xffffffff 130 // 131 // no problem if input equals to output 132 emptyB0 := []byte{} 133 if BtU32(emptyB0) != 0xffffffff { 134 t.Fatalf("it needs that input equals to output") 135 } 136 } 137 138 // BtU32 139 // 140 // Test three patterns, zero and max for uint32 141 // These are obvious but test in case 142 func TestBtU32(t *testing.T) { 143 // if b is nil, it occurs an error 144 // when execution because a type of b is not []byte 145 146 // test for a normal situation 147 // input: 4 bytes array, []byte{0x00, 0x00, 0x00, 0x00} 148 // output: zero(uint32) 149 // 150 // no problem if input equals to output 151 var zeroU32 uint32 = 0 152 zeroB32 := []byte{0x00, 0x00, 0x00, 0x00} 153 if BtU32(zeroB32) != zeroU32 { 154 t.Fatalf("it needs that input equals to output") 155 } 156 157 // test for a normal situation 158 // input: 4 bytes array, []byte{0xff, 0xff, 0xff, 0xff} 159 // output: max(uint32) 160 // 161 // no problem if input equals to output 162 var maxU32 uint32 = 4294967295 163 maxB32 := []byte{0xff, 0xff, 0xff, 0xff} 164 if BtU32(maxB32) != maxU32 { 165 t.Fatalf("it needs that input equals to output") 166 } 167 } 168 169 // BtI32 170 // 171 // BtI32 has an input of a byte array for arbitrary length (the length is an int type) 172 // If the length of the input is not 4, it returns fixed value. 173 // This function tests the length 174 func TestLenBInBtI32(t *testing.T) { 175 // if b is nil, it occurs an error 176 // when execution because a type of b is not []byte 177 178 // test for a normal situation 179 // input: 4 bytes array, []byte{0x00, 0x00, 0x00, 0x00} 180 // output: 0x7fffffff 181 // 182 // no problem if input unequals to output 183 zeroB32 := []byte{0x00, 0x00, 0x00, 0x00} 184 if BtI32(zeroB32) == 0x7fffffff { 185 t.Fatalf("it needs that input unequals to output") 186 } 187 188 // test for a normal situation 189 // input: 5 bytes array, []byte{0x00, 0x00, 0x00, 0x00, 0x00} 190 // output: 0x7fffffff 191 // 192 // no problem if input equals to output 193 zeroB40 := []byte{0x00, 0x00, 0x00, 0x00, 194 0x00} 195 if BtI32(zeroB40) != 0x7fffffff { 196 t.Fatalf("it needs that input equals to output") 197 } 198 199 // test for a normal situation 200 // input: 3 bytes array, []byte{0x00, 0x00, 0x00} 201 // output: 0x7fffffff 202 // 203 // no problem if input equals to output 204 zeroB24 := []byte{0x00, 0x00, 0x00} 205 if BtI32(zeroB24) != 0x7fffffff { 206 t.Fatalf("it needs that input equals to output") 207 } 208 209 // TODO: test a large size bytes arary 210 //zeroB18446744073709551615 := []byte{0x00 * 18446744073709551615} 211 //if BtI32(zeroB18446744073709551615) != 0x7fffffff { 212 // t.Fatalf("it needs that input equals to output") 213 //} 214 215 // test for an anomaly situation 216 // input: empty bytes array 217 // output: 0x7fffffff 218 // 219 // no problem if input equals to output 220 emptyB0 := []byte{} 221 if BtI32(emptyB0) != 0x7fffffff { 222 t.Fatalf("it needs that input equals to output") 223 } 224 } 225 226 // BtI32 227 // 228 // Test three patterns, zero, max and min for int32 229 // These are obvious but test in case 230 func TestBtI32(t *testing.T) { 231 // if b is nil, it occurs an error 232 // when execution because a type of b is not []byte 233 234 // test for a normal situation 235 // input: 4 bytes array, []byte{0x00, 0x00, 0x00, 0x00} 236 // output: zero(int32) 237 // 238 // no problem if input equals to output 239 var zeroI32 int32 = 0 240 zeroB32 := []byte{0x00, 0x00, 0x00, 0x00} 241 if BtI32(zeroB32) != zeroI32 { 242 t.Fatalf("it needs that input equals to output") 243 } 244 245 // test for a normal situation 246 // input: 4 bytes array, []byte{0x7f, 0xff, 0xff, 0xff} 247 // output: max(int32) 248 // 249 // no problem if input equals to output 250 var plusMaxI32 int32 = 2147483647 251 plusMaxB32 := []byte{0x7f, 0xff, 0xff, 0xff} 252 if BtI32(plusMaxB32) != plusMaxI32 { 253 t.Fatalf("it needs that input equals to output") 254 } 255 256 // test for a normal situation 257 // input: 4 bytes array, []byte{0x80, 0x00, 0x00, 0x00} 258 // output: min(int32) 259 // 260 // no problem if input equals to output 261 var minusMaxI32 int32 = -2147483648 262 minusMaxB32 := []byte{0x80, 0x00, 0x00, 0x00} 263 if BtI32(minusMaxB32) != minusMaxI32 { 264 t.Fatalf("it needs that input equals to output") 265 } 266 } 267 268 // I64tB 269 // 270 // Test three patterns, zero, max and min for int64 271 // These are obvious but test in case 272 func TestI64tB(t *testing.T) { 273 // if i is nil, it occurs an error 274 // when execution because a type of i is not int64 275 276 // test for a normal situation 277 // input: zero(int64) 278 // output: 8 bytes array, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} 279 // 280 // no problem if input equals to output 281 var zeroI64 int64 = 0 282 zeroB64 := []byte{0x00, 0x00, 0x00, 0x00, 283 0x00, 0x00, 0x00, 0x00} 284 if !bytes.Equal(I64tB(zeroI64), zeroB64) { 285 t.Fatalf("it needs that input equals to output") 286 } 287 288 // test for a normal situation 289 // input: max(int64) 290 // output: 8 bytes array, []byte{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} 291 // 292 // no problem if input equals to output 293 var plusMaxI64 int64 = 9223372036854775807 294 plusMaxB64 := []byte{0x7f, 0xff, 0xff, 0xff, 295 0xff, 0xff, 0xff, 0xff} 296 if !bytes.Equal(I64tB(plusMaxI64), plusMaxB64) { 297 t.Fatalf("it needs that input equals to output") 298 } 299 300 // test for a normal situation 301 // input: min(int64) 302 // output: 8 bytes array, []byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} 303 // 304 // no problem if input equals to output 305 var minusMaxI64 int64 = -9223372036854775808 306 minusMaxB64 := []byte{0x80, 0x00, 0x00, 0x00, 307 0x00, 0x00, 0x00, 0x00} 308 if !bytes.Equal(I64tB(minusMaxI64), minusMaxB64) { 309 t.Fatalf("it needs that input equals to output") 310 } 311 } 312 313 // U64tB 314 // 315 // Test three patterns, zero, max and min for uint64 316 // These are obvious but test in case 317 func TestU64tB(t *testing.T) { 318 // if i is nil, it occurs an error 319 // when execution because a type of i is not uint64 320 321 // test for a normal situation 322 // input: zero(uint64) 323 // output: 8 bytes array, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} 324 // 325 // no problem if input equals to output 326 var zeroU64 uint64 = 0 327 zeroB64 := []byte{0x00, 0x00, 0x00, 0x00, 328 0x00, 0x00, 0x00, 0x00} 329 if !bytes.Equal(U64tB(zeroU64), zeroB64) { 330 t.Fatalf("it needs that input equals to output") 331 } 332 333 // test for a normal situation 334 // input: max(uint64) 335 // output: 8 bytes array, []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} 336 // 337 // no problem if input equals to output 338 var plusMaxU64 uint64 = 18446744073709551615 339 plusMaxB64 := []byte{0xff, 0xff, 0xff, 0xff, 340 0xff, 0xff, 0xff, 0xff} 341 if !bytes.Equal(U64tB(plusMaxU64), plusMaxB64) { 342 t.Fatalf("it needs that input equals to output") 343 } 344 } 345 346 // BtI64 347 // 348 // BtI64 has an input of a byte array for arbitrary length (the length is an int type) 349 // If the length of the input is not 8, it returns fixed value. 350 // This function tests the length 351 func TestLenBInBtI64(t *testing.T) { 352 // if b is nil, it occurs an error 353 // when execution because a type of b is not []byte 354 355 // test for a normal situation 356 // input: 8 bytes array, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} 357 // output: 0x7fffffffffffffff 358 // 359 // no problem if input unequals to output 360 zeroB64 := []byte{0x00, 0x00, 0x00, 0x00, 361 0x00, 0x00, 0x00, 0x00} 362 if BtI64(zeroB64) == 0x7fffffffffffffff { 363 t.Fatalf("it needs that input unequals to output") 364 } 365 366 // test for an anomaly situation 367 // input: 9 bytes array, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} 368 // output: 0x7fffffffffffffff 369 // 370 // no problem if input unequals to output 371 zeroB72 := []byte{0x00, 0x00, 0x00, 0x00, 372 0x00, 0x00, 0x00, 0x00, 373 0x00} 374 if BtI64(zeroB72) != 0x7fffffffffffffff { 375 t.Fatalf("it needs that input equals to output") 376 } 377 378 // test for an anomaly situation 379 // input: 7 bytes array, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} 380 // output: 0x7fffffffffffffff 381 // 382 // no problem if input unequals to output 383 zeroB56 := []byte{0x00, 0x00, 0x00, 0x00, 384 0x00, 0x00, 0x00} 385 if BtI64(zeroB56) != 0x7fffffffffffffff { 386 t.Fatalf("it needs that input equals to output") 387 } 388 389 // TODO: test a large size bytes array 390 //zeroB18446744073709551615 := []byte{0x00 * 18446744073709551615} 391 //if BtI64(zeroB18446744073709551615) != 0x7fffffffffffffff { 392 // t.Fatalf("it needs that input equals to output") 393 //} 394 395 // test for an anomaly situation 396 // input: empty bytes array 397 // output: 0x7fffffffffffffff 398 // 399 // no problem if input equals to output 400 emptyB0 := []byte{} 401 if BtI64(emptyB0) != 0x7fffffffffffffff { 402 t.Fatalf("it needs that input equals to output") 403 } 404 } 405 406 // BtI64 407 // 408 // Test three patterns, zero and max for int64 409 // These are obvious but test in case 410 func TestBtI64(t *testing.T) { 411 // if b is nil, it occurs an error 412 // when execution because a type of b is not []byte 413 414 // test for a normal situation 415 // input: 8 bytes array, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} 416 // output: zero(int64) 417 // 418 // no problem if input equals to output 419 var zeroI64 int64 = 0 420 zeroB64 := []byte{0x00, 0x00, 0x00, 0x00, 421 0x00, 0x00, 0x00, 0x00} 422 if BtI64(zeroB64) != zeroI64 { 423 t.Fatalf("it needs that input equals to output") 424 } 425 426 // test for a normal situation 427 // input: 8 bytes array, []byte{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} 428 // output: max(int64) 429 // 430 // no problem if input equals to output 431 var plusMaxI64 int64 = 9223372036854775807 432 plusMaxB64 := []byte{0x7f, 0xff, 0xff, 0xff, 433 0xff, 0xff, 0xff, 0xff} 434 if BtI64(plusMaxB64) != plusMaxI64 { 435 t.Fatalf("it needs that input equals to output") 436 } 437 438 // test for a normal situation 439 // input: 8 bytes array, []byte{0x80, 0x00, 0x00, 0x00} 440 // output: min(int64) 441 // 442 // no problem if input equals to output 443 var minusMaxI64 int64 = -9223372036854775808 444 minusMaxB64 := []byte{0x80, 0x00, 0x00, 0x00, 445 0x00, 0x00, 0x00, 0x00} 446 if BtI64(minusMaxB64) != minusMaxI64 { 447 t.Fatalf("it needs that input equals to output") 448 } 449 } 450 451 // BtU64 452 // 453 // BtU64 has an input of a byte array for arbitrary length (the length is an int type) 454 // If the length of the input is not 8, it returns fixed value. 455 // This function tests the length 456 func TestLenBInBtU64(t *testing.T) { 457 // if b is nil, it occurs an error 458 // when execution because a type of b is not []byte 459 460 // test for a normal situation 461 // input: 8 bytes array, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} 462 // output: 0xffffffffffffffff 463 // 464 // no problem if input unequals to output 465 zeroB64 := []byte{0x00, 0x00, 0x00, 0x00, 466 0x00, 0x00, 0x00, 0x00} 467 if BtU64(zeroB64) == 0xffffffffffffffff { 468 t.Fatalf("it needs that input unequals to output") 469 } 470 471 // test for an anomaly situation 472 // input: 9 bytes array, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} 473 // output: 0xffffffffffffffff 474 // 475 // no problem if input equals to output 476 zeroB72 := []byte{0x00, 0x00, 0x00, 0x00, 477 0x00, 0x00, 0x00, 0x00, 478 0x00} 479 if BtU64(zeroB72) != 0xffffffffffffffff { 480 t.Fatalf("it needs that input equals to output") 481 } 482 483 // test for an anomaly situation 484 // input: 7 bytes array, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00} 485 // output: 0xffffffffffffffff 486 // 487 // no problem if input equals to output 488 zeroB56 := []byte{0x00, 0x00, 0x00, 0x00, 489 0x00, 0x00, 0x00} 490 if BtU64(zeroB56) != 0xffffffffffffffff { 491 t.Fatalf("it needs that input equals to output") 492 } 493 494 // TODO: test a large size bytes arary 495 //zeroB18446744073709551615 := []byte{0x00 * 18446744073709551615} 496 //if BtU64(zeroB18446744073709551615) != 0xffffffffffffffff { 497 // t.Fatalf("it needs that input equals to output") 498 //} 499 500 // test for an anomaly situation 501 // input: empty bytes array 502 // output: 0xffffffffffffffff 503 // 504 // no problem if input equals to output 505 emptyB0 := []byte{} 506 if BtU64(emptyB0) != 0xffffffffffffffff { 507 t.Fatalf("it needs that input equals to output") 508 } 509 } 510 511 // BtU64 512 // 513 // Test three patterns, zero and max for uint64 514 // These are obvious but test in case 515 func TestBtU64(t *testing.T) { 516 // if b is nil, it occurs an error 517 // when execution because a type of b is not []byte 518 519 // test for a normal situation 520 // input: 8 bytes array, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} 521 // output: zero(uint32) 522 // 523 // no problem if input equals to output 524 var zeroU64 uint64 = 0 525 zeroB64 := []byte{0x00, 0x00, 0x00, 0x00, 526 0x00, 0x00, 0x00, 0x00} 527 if BtU64(zeroB64) != zeroU64 { 528 t.Fatalf("it needs that input equals to output") 529 } 530 531 // test for a normal situation 532 // input: 8 bytes array, []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} 533 // output: max(uint32) 534 // 535 // no problem if input equals to output 536 var maxU64 uint64 = 18446744073709551615 537 maxB64 := []byte{0xff, 0xff, 0xff, 0xff, 538 0xff, 0xff, 0xff, 0xff} 539 if BtU64(maxB64) != maxU64 { 540 t.Fatalf("it needs that input equals to output") 541 } 542 }