github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/datacodec/conversions_test.go (about) 1 // Copyright 2021 DataStax 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package datacodec 16 17 import ( 18 "math" 19 "math/big" 20 "strconv" 21 "testing" 22 "time" 23 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func Test_int64ToInt(t *testing.T) { 28 tests := []struct { 29 name string 30 val int64 31 intSize int 32 wantVal int64 33 wantErr string 34 }{ 35 {"32 bits out of range pos", math.MaxInt32 + 1, 32, 0, "value out of range: 2147483648"}, 36 {"32 bits out of range neg", math.MinInt32 - 1, 32, 0, "value out of range: -2147483649"}, 37 {"32 bits max", math.MaxInt32, 32, math.MaxInt32, ""}, 38 {"32 bits min", math.MinInt32, 32, math.MinInt32, ""}, 39 } 40 if strconv.IntSize == 64 { 41 tests = append(tests, []struct { 42 name string 43 val int64 44 intSize int 45 wantVal int64 46 wantErr string 47 }{ 48 {"64 bits max", math.MaxInt64, 64, math.MaxInt64, ""}, 49 {"64 bits min", math.MinInt64, 64, math.MinInt64, ""}, 50 }...) 51 } 52 for _, tt := range tests { 53 t.Run(tt.name, func(t *testing.T) { 54 gotVal, gotErr := int64ToInt(tt.val, tt.intSize) 55 assert.EqualValues(t, tt.wantVal, gotVal) 56 assertErrorMessage(t, tt.wantErr, gotErr) 57 }) 58 } 59 } 60 61 func Test_int64ToInt32(t *testing.T) { 62 tests := []struct { 63 name string 64 val int64 65 wantVal int32 66 wantErr string 67 }{ 68 {"out of range pos", math.MaxInt32 + 1, 0, "value out of range: 2147483648"}, 69 {"out of range neg", math.MinInt32 - 1, 0, "value out of range: -2147483649"}, 70 {"max", math.MaxInt32, math.MaxInt32, ""}, 71 {"min", math.MinInt32, math.MinInt32, ""}, 72 } 73 for _, tt := range tests { 74 t.Run(tt.name, func(t *testing.T) { 75 gotVal, gotErr := int64ToInt32(tt.val) 76 assert.Equal(t, tt.wantVal, gotVal) 77 assertErrorMessage(t, tt.wantErr, gotErr) 78 }) 79 } 80 } 81 82 func Test_int64ToInt16(t *testing.T) { 83 tests := []struct { 84 name string 85 val int64 86 wantVal int16 87 wantErr string 88 }{ 89 {"out of range pos", math.MaxInt16 + 1, 0, "value out of range: 32768"}, 90 {"out of range neg", math.MinInt16 - 1, 0, "value out of range: -32769"}, 91 {"max", math.MaxInt16, math.MaxInt16, ""}, 92 {"min", math.MinInt16, math.MinInt16, ""}, 93 } 94 for _, tt := range tests { 95 t.Run(tt.name, func(t *testing.T) { 96 gotVal, gotErr := int64ToInt16(tt.val) 97 assert.Equal(t, tt.wantVal, gotVal) 98 assertErrorMessage(t, tt.wantErr, gotErr) 99 }) 100 } 101 } 102 103 func Test_int64ToInt8(t *testing.T) { 104 tests := []struct { 105 name string 106 val int64 107 wantVal int8 108 wantErr string 109 }{ 110 {"out of range pos", math.MaxInt8 + 1, 0, "value out of range: 128"}, 111 {"out of range neg", math.MinInt8 - 1, 0, "value out of range: -129"}, 112 {"max", math.MaxInt8, math.MaxInt8, ""}, 113 {"min", math.MinInt8, math.MinInt8, ""}, 114 } 115 for _, tt := range tests { 116 t.Run(tt.name, func(t *testing.T) { 117 gotVal, gotErr := int64ToInt8(tt.val) 118 assert.Equal(t, tt.wantVal, gotVal) 119 assertErrorMessage(t, tt.wantErr, gotErr) 120 }) 121 } 122 } 123 124 func Test_int64ToUint64(t *testing.T) { 125 tests := []struct { 126 name string 127 val int64 128 wantVal uint64 129 wantErr string 130 }{ 131 {"out of range neg", -1, 0, "value out of range: -1"}, 132 {"max", math.MaxInt64, math.MaxInt64, ""}, 133 {"min", 0, 0, ""}, 134 } 135 for _, tt := range tests { 136 t.Run(tt.name, func(t *testing.T) { 137 gotVal, gotErr := int64ToUint64(tt.val) 138 assert.Equal(t, tt.wantVal, gotVal) 139 assertErrorMessage(t, tt.wantErr, gotErr) 140 }) 141 } 142 } 143 144 func Test_int64ToUint32(t *testing.T) { 145 tests := []struct { 146 name string 147 val int64 148 wantVal uint32 149 wantErr string 150 }{ 151 {"out of range neg", -1, 0, "value out of range: -1"}, 152 {"out of range pos", math.MaxUint32 + 1, 0, "value out of range: 4294967296"}, 153 {"max", math.MaxUint32, math.MaxUint32, ""}, 154 {"min", 0, 0, ""}, 155 } 156 for _, tt := range tests { 157 t.Run(tt.name, func(t *testing.T) { 158 gotVal, gotErr := int64ToUint32(tt.val) 159 assert.Equal(t, tt.wantVal, gotVal) 160 assertErrorMessage(t, tt.wantErr, gotErr) 161 }) 162 } 163 } 164 165 func Test_int64ToUint(t *testing.T) { 166 tests := []struct { 167 name string 168 val int64 169 intSize int 170 wantVal uint64 171 wantErr string 172 }{ 173 {"32 bits out of range pos", math.MaxUint32 + 1, 32, 0, "value out of range: 4294967296"}, 174 {"32 bits out of range neg", -1, 32, 0, "value out of range: -1"}, 175 {"32 bits max", math.MaxUint32, 32, math.MaxUint32, ""}, 176 {"32 bits min", 0, 32, 0, ""}, 177 } 178 if strconv.IntSize == 64 { 179 tests = append(tests, []struct { 180 name string 181 val int64 182 intSize int 183 wantVal uint64 184 wantErr string 185 }{ 186 {"64 bits out of range neg", -1, 64, 0, "value out of range: -1"}, 187 {"64 bits max", math.MaxInt64, 64, math.MaxInt64, ""}, 188 {"64 bits min", 0, 64, 0, ""}, 189 }...) 190 } 191 192 for _, tt := range tests { 193 t.Run(tt.name, func(t *testing.T) { 194 gotVal, gotErr := int64ToUint(tt.val, tt.intSize) 195 assert.EqualValues(t, tt.wantVal, gotVal) 196 assertErrorMessage(t, tt.wantErr, gotErr) 197 }) 198 } 199 } 200 201 func Test_int64ToUint16(t *testing.T) { 202 tests := []struct { 203 name string 204 val int64 205 wantVal uint16 206 wantErr string 207 }{ 208 {"out of range neg", -1, 0, "value out of range: -1"}, 209 {"out of range pos", math.MaxUint16 + 1, 0, "value out of range: 65536"}, 210 {"max", math.MaxUint16, math.MaxUint16, ""}, 211 {"min", 0, 0, ""}, 212 } 213 for _, tt := range tests { 214 t.Run(tt.name, func(t *testing.T) { 215 gotVal, gotErr := int64ToUint16(tt.val) 216 assert.Equal(t, tt.wantVal, gotVal) 217 assertErrorMessage(t, tt.wantErr, gotErr) 218 }) 219 } 220 } 221 222 func Test_int64ToUint8(t *testing.T) { 223 tests := []struct { 224 name string 225 val int64 226 wantVal uint8 227 wantErr string 228 }{ 229 {"out of range neg", -1, 0, "value out of range: -1"}, 230 {"out of range pos", math.MaxUint8 + 1, 0, "value out of range: 256"}, 231 {"max", math.MaxUint8, math.MaxUint8, ""}, 232 {"min", 0, 0, ""}, 233 } 234 for _, tt := range tests { 235 t.Run(tt.name, func(t *testing.T) { 236 gotVal, gotErr := int64ToUint8(tt.val) 237 assert.Equal(t, tt.wantVal, gotVal) 238 assertErrorMessage(t, tt.wantErr, gotErr) 239 }) 240 } 241 } 242 243 func Test_intToInt32(t *testing.T) { 244 tests := []struct { 245 name string 246 val int 247 wantVal int32 248 wantErr string 249 }{ 250 {"out of range neg", math.MinInt32 - 1, 0, "value out of range: -2147483649"}, 251 {"out of range pos", math.MaxInt32 + 1, 0, "value out of range: 2147483648"}, 252 {"max", math.MaxInt32, math.MaxInt32, ""}, 253 {"min", math.MinInt32, math.MinInt32, ""}, 254 } 255 for _, tt := range tests { 256 t.Run(tt.name, func(t *testing.T) { 257 gotVal, gotErr := intToInt32(tt.val) 258 assert.Equal(t, tt.wantVal, gotVal) 259 assertErrorMessage(t, tt.wantErr, gotErr) 260 }) 261 } 262 } 263 264 func Test_intToInt16(t *testing.T) { 265 tests := []struct { 266 name string 267 val int 268 wantVal int16 269 wantErr string 270 }{ 271 {"out of range neg", math.MinInt16 - 1, 0, "value out of range: -32769"}, 272 {"out of range pos", math.MaxInt16 + 1, 0, "value out of range: 32768"}, 273 {"max", math.MaxInt16, math.MaxInt16, ""}, 274 {"min", math.MinInt16, math.MinInt16, ""}, 275 } 276 for _, tt := range tests { 277 t.Run(tt.name, func(t *testing.T) { 278 gotVal, gotErr := intToInt16(tt.val) 279 assert.Equal(t, tt.wantVal, gotVal) 280 assertErrorMessage(t, tt.wantErr, gotErr) 281 }) 282 } 283 } 284 285 func Test_intToInt8(t *testing.T) { 286 tests := []struct { 287 name string 288 val int 289 wantVal int8 290 wantErr string 291 }{ 292 {"out of range neg", math.MinInt8 - 1, 0, "value out of range: -1"}, 293 {"out of range pos", math.MaxInt8 + 1, 0, "value out of range: 128"}, 294 {"max", math.MaxInt8, math.MaxInt8, ""}, 295 {"min", math.MinInt8, math.MinInt8, ""}, 296 } 297 for _, tt := range tests { 298 t.Run(tt.name, func(t *testing.T) { 299 gotVal, gotErr := intToInt8(tt.val) 300 assert.Equal(t, tt.wantVal, gotVal) 301 assertErrorMessage(t, tt.wantErr, gotErr) 302 }) 303 } 304 } 305 306 func Test_int32ToInt16(t *testing.T) { 307 tests := []struct { 308 name string 309 val int32 310 wantVal int16 311 wantErr string 312 }{ 313 {"out of range pos", math.MaxInt16 + 1, 0, "value out of range: 32768"}, 314 {"out of range neg", math.MinInt16 - 1, 0, "value out of range: -32769"}, 315 {"max", math.MaxInt16, math.MaxInt16, ""}, 316 {"min", math.MinInt16, math.MinInt16, ""}, 317 } 318 for _, tt := range tests { 319 t.Run(tt.name, func(t *testing.T) { 320 gotVal, gotErr := int32ToInt16(tt.val) 321 assert.Equal(t, tt.wantVal, gotVal) 322 assertErrorMessage(t, tt.wantErr, gotErr) 323 }) 324 } 325 } 326 327 func Test_int32ToInt8(t *testing.T) { 328 tests := []struct { 329 name string 330 val int32 331 wantVal int8 332 wantErr string 333 }{ 334 {"out of range pos", math.MaxInt8 + 1, 0, "value out of range: 128"}, 335 {"out of range neg", math.MinInt8 - 1, 0, "value out of range: -129"}, 336 {"max", math.MaxInt8, math.MaxInt8, ""}, 337 {"min", math.MinInt8, math.MinInt8, ""}, 338 } 339 for _, tt := range tests { 340 t.Run(tt.name, func(t *testing.T) { 341 gotVal, gotErr := int32ToInt8(tt.val) 342 assert.Equal(t, tt.wantVal, gotVal) 343 assertErrorMessage(t, tt.wantErr, gotErr) 344 }) 345 } 346 } 347 348 func Test_int32ToUint64(t *testing.T) { 349 tests := []struct { 350 name string 351 val int32 352 wantVal uint64 353 wantErr string 354 }{ 355 {"out of range neg", -1, 0, "value out of range: -1"}, 356 {"max", math.MaxInt32, math.MaxInt32, ""}, 357 {"min", 0, 0, ""}, 358 } 359 for _, tt := range tests { 360 t.Run(tt.name, func(t *testing.T) { 361 gotVal, gotErr := int32ToUint64(tt.val) 362 assert.Equal(t, tt.wantVal, gotVal) 363 assertErrorMessage(t, tt.wantErr, gotErr) 364 }) 365 } 366 } 367 368 func Test_int32ToUint(t *testing.T) { 369 tests := []struct { 370 name string 371 val int32 372 wantVal uint 373 wantErr string 374 }{ 375 {"out of range neg", -1, 0, "value out of range: -1"}, 376 {"max", math.MaxInt32, math.MaxInt32, ""}, 377 {"min", 0, 0, ""}, 378 } 379 for _, tt := range tests { 380 t.Run(tt.name, func(t *testing.T) { 381 gotVal, gotErr := int32ToUint(tt.val) 382 assert.Equal(t, tt.wantVal, gotVal) 383 assertErrorMessage(t, tt.wantErr, gotErr) 384 }) 385 } 386 } 387 388 func Test_int32ToUint32(t *testing.T) { 389 tests := []struct { 390 name string 391 val int32 392 wantVal uint32 393 wantErr string 394 }{ 395 {"out of range neg", -1, 0, "value out of range: -1"}, 396 {"max", math.MaxInt32, math.MaxInt32, ""}, 397 {"min", 0, 0, ""}, 398 } 399 for _, tt := range tests { 400 t.Run(tt.name, func(t *testing.T) { 401 gotVal, gotErr := int32ToUint32(tt.val) 402 assert.Equal(t, tt.wantVal, gotVal) 403 assertErrorMessage(t, tt.wantErr, gotErr) 404 }) 405 } 406 } 407 408 func Test_int32ToUint16(t *testing.T) { 409 tests := []struct { 410 name string 411 val int32 412 wantVal uint16 413 wantErr string 414 }{ 415 {"out of range neg", -1, 0, "value out of range: -1"}, 416 {"max", math.MaxInt16, math.MaxInt16, ""}, 417 {"min", 0, 0, ""}, 418 } 419 for _, tt := range tests { 420 t.Run(tt.name, func(t *testing.T) { 421 gotVal, gotErr := int32ToUint16(tt.val) 422 assert.Equal(t, tt.wantVal, gotVal) 423 assertErrorMessage(t, tt.wantErr, gotErr) 424 }) 425 } 426 } 427 428 func Test_int32ToUint8(t *testing.T) { 429 tests := []struct { 430 name string 431 val int32 432 wantVal uint8 433 wantErr string 434 }{ 435 {"out of range neg", -1, 0, "value out of range: -1"}, 436 {"max", math.MaxInt8, math.MaxInt8, ""}, 437 {"min", 0, 0, ""}, 438 } 439 for _, tt := range tests { 440 t.Run(tt.name, func(t *testing.T) { 441 gotVal, gotErr := int32ToUint8(tt.val) 442 assert.Equal(t, tt.wantVal, gotVal) 443 assertErrorMessage(t, tt.wantErr, gotErr) 444 }) 445 } 446 } 447 448 func Test_int16ToInt8(t *testing.T) { 449 tests := []struct { 450 name string 451 val int16 452 wantVal int8 453 wantErr string 454 }{ 455 {"out of range pos", math.MaxInt8 + 1, 0, "value out of range: 128"}, 456 {"out of range neg", math.MinInt8 - 1, 0, "value out of range: -129"}, 457 {"max", math.MaxInt8, math.MaxInt8, ""}, 458 {"min", math.MinInt8, math.MinInt8, ""}, 459 } 460 for _, tt := range tests { 461 t.Run(tt.name, func(t *testing.T) { 462 gotVal, gotErr := int16ToInt8(tt.val) 463 assert.Equal(t, tt.wantVal, gotVal) 464 assertErrorMessage(t, tt.wantErr, gotErr) 465 }) 466 } 467 } 468 469 func Test_int16ToUint64(t *testing.T) { 470 tests := []struct { 471 name string 472 val int16 473 wantVal uint64 474 wantErr string 475 }{ 476 {"out of range neg", -1, 0, "value out of range: -1"}, 477 {"max", math.MaxInt16, math.MaxInt16, ""}, 478 {"min", 0, 0, ""}, 479 } 480 for _, tt := range tests { 481 t.Run(tt.name, func(t *testing.T) { 482 gotVal, gotErr := int16ToUint64(tt.val) 483 assert.Equal(t, tt.wantVal, gotVal) 484 assertErrorMessage(t, tt.wantErr, gotErr) 485 }) 486 } 487 } 488 489 func Test_int16ToUint(t *testing.T) { 490 tests := []struct { 491 name string 492 val int16 493 wantVal uint 494 wantErr string 495 }{ 496 {"out of range neg", -1, 0, "value out of range: -1"}, 497 {"max", math.MaxInt16, math.MaxInt16, ""}, 498 {"min", 0, 0, ""}, 499 } 500 for _, tt := range tests { 501 t.Run(tt.name, func(t *testing.T) { 502 gotVal, gotErr := int16ToUint(tt.val) 503 assert.Equal(t, tt.wantVal, gotVal) 504 assertErrorMessage(t, tt.wantErr, gotErr) 505 }) 506 } 507 } 508 509 func Test_int16ToUint32(t *testing.T) { 510 tests := []struct { 511 name string 512 val int16 513 wantVal uint32 514 wantErr string 515 }{ 516 {"out of range neg", -1, 0, "value out of range: -1"}, 517 {"max", math.MaxInt16, math.MaxInt16, ""}, 518 {"min", 0, 0, ""}, 519 } 520 for _, tt := range tests { 521 t.Run(tt.name, func(t *testing.T) { 522 gotVal, gotErr := int16ToUint32(tt.val) 523 assert.Equal(t, tt.wantVal, gotVal) 524 assertErrorMessage(t, tt.wantErr, gotErr) 525 }) 526 } 527 } 528 529 func Test_int16ToUint16(t *testing.T) { 530 tests := []struct { 531 name string 532 val int16 533 wantVal uint16 534 wantErr string 535 }{ 536 {"out of range neg", -1, 0, "value out of range: -1"}, 537 {"max", math.MaxInt16, math.MaxInt16, ""}, 538 {"min", 0, 0, ""}, 539 } 540 for _, tt := range tests { 541 t.Run(tt.name, func(t *testing.T) { 542 gotVal, gotErr := int16ToUint16(tt.val) 543 assert.Equal(t, tt.wantVal, gotVal) 544 assertErrorMessage(t, tt.wantErr, gotErr) 545 }) 546 } 547 } 548 549 func Test_int16ToUint8(t *testing.T) { 550 tests := []struct { 551 name string 552 val int16 553 wantVal uint8 554 wantErr string 555 }{ 556 {"out of range neg", -1, 0, "value out of range: -1"}, 557 {"max", math.MaxInt8, math.MaxInt8, ""}, 558 {"min", 0, 0, ""}, 559 } 560 for _, tt := range tests { 561 t.Run(tt.name, func(t *testing.T) { 562 gotVal, gotErr := int16ToUint8(tt.val) 563 assert.Equal(t, tt.wantVal, gotVal) 564 assertErrorMessage(t, tt.wantErr, gotErr) 565 }) 566 } 567 } 568 569 func Test_int8ToUint64(t *testing.T) { 570 tests := []struct { 571 name string 572 val int8 573 wantVal uint64 574 wantErr string 575 }{ 576 {"out of range neg", -1, 0, "value out of range: -1"}, 577 {"max", math.MaxInt8, math.MaxInt8, ""}, 578 {"min", 0, 0, ""}, 579 } 580 for _, tt := range tests { 581 t.Run(tt.name, func(t *testing.T) { 582 gotVal, gotErr := int8ToUint64(tt.val) 583 assert.Equal(t, tt.wantVal, gotVal) 584 assertErrorMessage(t, tt.wantErr, gotErr) 585 }) 586 } 587 } 588 589 func Test_int8ToUint(t *testing.T) { 590 tests := []struct { 591 name string 592 val int8 593 wantVal uint 594 wantErr string 595 }{ 596 {"out of range neg", -1, 0, "value out of range: -1"}, 597 {"max", math.MaxInt8, math.MaxInt8, ""}, 598 {"min", 0, 0, ""}, 599 } 600 for _, tt := range tests { 601 t.Run(tt.name, func(t *testing.T) { 602 gotVal, gotErr := int8ToUint(tt.val) 603 assert.Equal(t, tt.wantVal, gotVal) 604 assertErrorMessage(t, tt.wantErr, gotErr) 605 }) 606 } 607 } 608 609 func Test_int8ToUint32(t *testing.T) { 610 tests := []struct { 611 name string 612 val int8 613 wantVal uint32 614 wantErr string 615 }{ 616 {"out of range neg", -1, 0, "value out of range: -1"}, 617 {"max", math.MaxInt8, math.MaxInt8, ""}, 618 {"min", 0, 0, ""}, 619 } 620 for _, tt := range tests { 621 t.Run(tt.name, func(t *testing.T) { 622 gotVal, gotErr := int8ToUint32(tt.val) 623 assert.Equal(t, tt.wantVal, gotVal) 624 assertErrorMessage(t, tt.wantErr, gotErr) 625 }) 626 } 627 } 628 629 func Test_int8ToUint16(t *testing.T) { 630 tests := []struct { 631 name string 632 val int8 633 wantVal uint16 634 wantErr string 635 }{ 636 {"out of range neg", -1, 0, "value out of range: -1"}, 637 {"max", math.MaxInt8, math.MaxInt8, ""}, 638 {"min", 0, 0, ""}, 639 } 640 for _, tt := range tests { 641 t.Run(tt.name, func(t *testing.T) { 642 gotVal, gotErr := int8ToUint16(tt.val) 643 assert.Equal(t, tt.wantVal, gotVal) 644 assertErrorMessage(t, tt.wantErr, gotErr) 645 }) 646 } 647 } 648 649 func Test_int8ToUint8(t *testing.T) { 650 tests := []struct { 651 name string 652 val int8 653 wantVal uint8 654 wantErr string 655 }{ 656 {"out of range neg", -1, 0, "value out of range: -1"}, 657 {"max", math.MaxInt8, math.MaxInt8, ""}, 658 {"min", 0, 0, ""}, 659 } 660 for _, tt := range tests { 661 t.Run(tt.name, func(t *testing.T) { 662 gotVal, gotErr := int8ToUint8(tt.val) 663 assert.Equal(t, tt.wantVal, gotVal) 664 assertErrorMessage(t, tt.wantErr, gotErr) 665 }) 666 } 667 } 668 669 func Test_uint64ToInt64(t *testing.T) { 670 tests := []struct { 671 name string 672 val uint64 673 wantVal int64 674 wantErr string 675 }{ 676 {"out of range pos", math.MaxInt64 + 1, 0, "value out of range: 9223372036854775808"}, 677 {"max", math.MaxInt64, math.MaxInt64, ""}, 678 {"min", 0, 0, ""}, 679 } 680 for _, tt := range tests { 681 t.Run(tt.name, func(t *testing.T) { 682 gotVal, gotErr := uint64ToInt64(tt.val) 683 assert.Equal(t, tt.wantVal, gotVal) 684 assertErrorMessage(t, tt.wantErr, gotErr) 685 }) 686 } 687 } 688 689 func Test_uint64ToInt32(t *testing.T) { 690 tests := []struct { 691 name string 692 val uint64 693 wantVal int32 694 wantErr string 695 }{ 696 {"out of range pos", math.MaxInt32 + 1, 0, "value out of range: 2147483648"}, 697 {"max", math.MaxInt32, math.MaxInt32, ""}, 698 {"min", 0, 0, ""}, 699 } 700 for _, tt := range tests { 701 t.Run(tt.name, func(t *testing.T) { 702 gotVal, gotErr := uint64ToInt32(tt.val) 703 assert.Equal(t, tt.wantVal, gotVal) 704 assertErrorMessage(t, tt.wantErr, gotErr) 705 }) 706 } 707 } 708 709 func Test_uint64ToInt16(t *testing.T) { 710 tests := []struct { 711 name string 712 val uint64 713 wantVal int16 714 wantErr string 715 }{ 716 {"out of range pos", math.MaxInt16 + 1, 0, "value out of range: 32768"}, 717 {"max", math.MaxInt16, math.MaxInt16, ""}, 718 {"min", 0, 0, ""}, 719 } 720 for _, tt := range tests { 721 t.Run(tt.name, func(t *testing.T) { 722 gotVal, gotErr := uint64ToInt16(tt.val) 723 assert.Equal(t, tt.wantVal, gotVal) 724 assertErrorMessage(t, tt.wantErr, gotErr) 725 }) 726 } 727 } 728 729 func Test_uint64ToInt8(t *testing.T) { 730 tests := []struct { 731 name string 732 val uint64 733 wantVal int8 734 wantErr string 735 }{ 736 {"out of range pos", math.MaxInt8 + 1, 0, "value out of range: 128"}, 737 {"max", math.MaxInt8, math.MaxInt8, ""}, 738 {"min", 0, 0, ""}, 739 } 740 for _, tt := range tests { 741 t.Run(tt.name, func(t *testing.T) { 742 gotVal, gotErr := uint64ToInt8(tt.val) 743 assert.Equal(t, tt.wantVal, gotVal) 744 assertErrorMessage(t, tt.wantErr, gotErr) 745 }) 746 } 747 } 748 749 func Test_uintToInt64(t *testing.T) { 750 tests := []struct { 751 name string 752 val uint 753 wantVal int64 754 wantErr string 755 }{ 756 {"out of range pos", math.MaxInt64 + 1, 0, "value out of range: 9223372036854775808"}, 757 {"max", math.MaxInt64, math.MaxInt64, ""}, 758 {"min", 0, 0, ""}, 759 } 760 for _, tt := range tests { 761 t.Run(tt.name, func(t *testing.T) { 762 gotVal, gotErr := uintToInt64(tt.val) 763 assert.Equal(t, tt.wantVal, gotVal) 764 assertErrorMessage(t, tt.wantErr, gotErr) 765 }) 766 } 767 } 768 769 func Test_uintToInt32(t *testing.T) { 770 tests := []struct { 771 name string 772 val uint 773 wantVal int32 774 wantErr string 775 }{ 776 {"out of range pos", math.MaxInt32 + 1, 0, "value out of range: 2147483648"}, 777 {"max", math.MaxInt32, math.MaxInt32, ""}, 778 {"min", 0, 0, ""}, 779 } 780 for _, tt := range tests { 781 t.Run(tt.name, func(t *testing.T) { 782 gotVal, gotErr := uintToInt32(tt.val) 783 assert.Equal(t, tt.wantVal, gotVal) 784 assertErrorMessage(t, tt.wantErr, gotErr) 785 }) 786 } 787 } 788 789 func Test_uintToInt16(t *testing.T) { 790 tests := []struct { 791 name string 792 val uint 793 wantVal int16 794 wantErr string 795 }{ 796 {"out of range pos", math.MaxInt16 + 1, 0, "value out of range: 32768"}, 797 {"max", math.MaxInt16, math.MaxInt16, ""}, 798 {"min", 0, 0, ""}, 799 } 800 for _, tt := range tests { 801 t.Run(tt.name, func(t *testing.T) { 802 gotVal, gotErr := uintToInt16(tt.val) 803 assert.Equal(t, tt.wantVal, gotVal) 804 assertErrorMessage(t, tt.wantErr, gotErr) 805 }) 806 } 807 } 808 809 func Test_uintToInt8(t *testing.T) { 810 tests := []struct { 811 name string 812 val uint 813 wantVal int8 814 wantErr string 815 }{ 816 {"out of range pos", math.MaxInt8 + 1, 0, "value out of range: 128"}, 817 {"max", math.MaxInt8, math.MaxInt8, ""}, 818 {"min", 0, 0, ""}, 819 } 820 for _, tt := range tests { 821 t.Run(tt.name, func(t *testing.T) { 822 gotVal, gotErr := uintToInt8(tt.val) 823 assert.Equal(t, tt.wantVal, gotVal) 824 assertErrorMessage(t, tt.wantErr, gotErr) 825 }) 826 } 827 } 828 829 func Test_uint32ToInt32(t *testing.T) { 830 tests := []struct { 831 name string 832 val uint32 833 wantVal int32 834 wantErr string 835 }{ 836 {"out of range pos", math.MaxInt32 + 1, 0, "value out of range: 2147483648"}, 837 {"max", math.MaxInt32, math.MaxInt32, ""}, 838 {"min", 0, 0, ""}, 839 } 840 for _, tt := range tests { 841 t.Run(tt.name, func(t *testing.T) { 842 gotVal, gotErr := uint32ToInt32(tt.val) 843 assert.Equal(t, tt.wantVal, gotVal) 844 assertErrorMessage(t, tt.wantErr, gotErr) 845 }) 846 } 847 } 848 849 func Test_uint32ToInt16(t *testing.T) { 850 tests := []struct { 851 name string 852 val uint32 853 wantVal int16 854 wantErr string 855 }{ 856 {"out of range pos", math.MaxInt16 + 1, 0, "value out of range: 32768"}, 857 {"max", math.MaxInt16, math.MaxInt16, ""}, 858 {"min", 0, 0, ""}, 859 } 860 for _, tt := range tests { 861 t.Run(tt.name, func(t *testing.T) { 862 gotVal, gotErr := uint32ToInt16(tt.val) 863 assert.Equal(t, tt.wantVal, gotVal) 864 assertErrorMessage(t, tt.wantErr, gotErr) 865 }) 866 } 867 } 868 869 func Test_uint32ToInt8(t *testing.T) { 870 tests := []struct { 871 name string 872 val uint32 873 wantVal int8 874 wantErr string 875 }{ 876 {"out of range pos", math.MaxInt8 + 1, 0, "value out of range: 128"}, 877 {"max", math.MaxInt8, math.MaxInt8, ""}, 878 {"min", 0, 0, ""}, 879 } 880 for _, tt := range tests { 881 t.Run(tt.name, func(t *testing.T) { 882 gotVal, gotErr := uint32ToInt8(tt.val) 883 assert.Equal(t, tt.wantVal, gotVal) 884 assertErrorMessage(t, tt.wantErr, gotErr) 885 }) 886 } 887 } 888 889 func Test_uint16ToInt16(t *testing.T) { 890 tests := []struct { 891 name string 892 val uint16 893 wantVal int16 894 wantErr string 895 }{ 896 {"out of range pos", math.MaxInt16 + 1, 0, "value out of range: 32768"}, 897 {"max", math.MaxInt16, math.MaxInt16, ""}, 898 {"min", 0, 0, ""}, 899 } 900 for _, tt := range tests { 901 t.Run(tt.name, func(t *testing.T) { 902 gotVal, gotErr := uint16ToInt16(tt.val) 903 assert.Equal(t, tt.wantVal, gotVal) 904 assertErrorMessage(t, tt.wantErr, gotErr) 905 }) 906 } 907 } 908 909 func Test_uint16ToInt8(t *testing.T) { 910 tests := []struct { 911 name string 912 val uint16 913 wantVal int8 914 wantErr string 915 }{ 916 {"out of range pos", math.MaxInt8 + 1, 0, "value out of range: 128"}, 917 {"max", math.MaxInt8, math.MaxInt8, ""}, 918 {"min", 0, 0, ""}, 919 } 920 for _, tt := range tests { 921 t.Run(tt.name, func(t *testing.T) { 922 gotVal, gotErr := uint16ToInt8(tt.val) 923 assert.Equal(t, tt.wantVal, gotVal) 924 assertErrorMessage(t, tt.wantErr, gotErr) 925 }) 926 } 927 } 928 929 func Test_uint8ToInt8(t *testing.T) { 930 tests := []struct { 931 name string 932 val uint8 933 wantVal int8 934 wantErr string 935 }{ 936 {"out of range pos", math.MaxInt8 + 1, 0, "value out of range: 128"}, 937 {"max", math.MaxInt8, math.MaxInt8, ""}, 938 {"min", 0, 0, ""}, 939 } 940 for _, tt := range tests { 941 t.Run(tt.name, func(t *testing.T) { 942 gotVal, gotErr := uint8ToInt8(tt.val) 943 assert.Equal(t, tt.wantVal, gotVal) 944 assertErrorMessage(t, tt.wantErr, gotErr) 945 }) 946 } 947 } 948 949 func Test_stringToInt64(t *testing.T) { 950 tests := []struct { 951 name string 952 val string 953 wantVal int64 954 wantErr string 955 }{ 956 {"invalid", "invalid", 0, "invalid syntax"}, 957 {"out of range pos", "9223372036854775808", 0, "value out of range"}, 958 {"max", "9223372036854775807", math.MaxInt64, ""}, 959 {"min", "-9223372036854775808", math.MinInt64, ""}, 960 } 961 for _, tt := range tests { 962 t.Run(tt.name, func(t *testing.T) { 963 gotVal, gotErr := stringToInt64(tt.val) 964 assert.Equal(t, tt.wantVal, gotVal) 965 assertErrorMessage(t, tt.wantErr, gotErr) 966 }) 967 } 968 } 969 970 func Test_stringToInt32(t *testing.T) { 971 tests := []struct { 972 name string 973 val string 974 wantVal int32 975 wantErr string 976 }{ 977 {"invalid", "invalid", 0, "invalid syntax"}, 978 {"out of range pos", "2147483648", 0, "value out of range"}, 979 {"max", "2147483647", math.MaxInt32, ""}, 980 {"min", "-2147483648", math.MinInt32, ""}, 981 } 982 for _, tt := range tests { 983 t.Run(tt.name, func(t *testing.T) { 984 gotVal, gotErr := stringToInt32(tt.val) 985 assert.Equal(t, tt.wantVal, gotVal) 986 assertErrorMessage(t, tt.wantErr, gotErr) 987 }) 988 } 989 } 990 991 func Test_stringToInt16(t *testing.T) { 992 tests := []struct { 993 name string 994 val string 995 wantVal int16 996 wantErr string 997 }{ 998 {"invalid", "invalid", 0, "invalid syntax"}, 999 {"out of range pos", "32768", 0, "value out of range"}, 1000 {"max", "32767", math.MaxInt16, ""}, 1001 {"min", "-32768", math.MinInt16, ""}, 1002 } 1003 for _, tt := range tests { 1004 t.Run(tt.name, func(t *testing.T) { 1005 gotVal, gotErr := stringToInt16(tt.val) 1006 assert.Equal(t, tt.wantVal, gotVal) 1007 assertErrorMessage(t, tt.wantErr, gotErr) 1008 }) 1009 } 1010 } 1011 1012 func Test_stringToInt8(t *testing.T) { 1013 tests := []struct { 1014 name string 1015 val string 1016 wantVal int8 1017 wantErr string 1018 }{ 1019 {"invalid", "invalid", 0, "invalid syntax"}, 1020 {"out of range pos", "128", 0, "value out of range"}, 1021 {"max", "127", math.MaxInt8, ""}, 1022 {"min", "-128", math.MinInt8, ""}, 1023 } 1024 for _, tt := range tests { 1025 t.Run(tt.name, func(t *testing.T) { 1026 gotVal, gotErr := stringToInt8(tt.val) 1027 assert.Equal(t, tt.wantVal, gotVal) 1028 assertErrorMessage(t, tt.wantErr, gotErr) 1029 }) 1030 } 1031 } 1032 1033 func Test_stringToBigInt(t *testing.T) { 1034 tests := []struct { 1035 name string 1036 val string 1037 wantVal *big.Int 1038 wantErr string 1039 }{ 1040 {"invalid", "invalid", nil, "cannot parse 'invalid'"}, 1041 {"pos", "18446744073709551615", new(big.Int).SetUint64(math.MaxUint64), ""}, 1042 {"neg", "-9223372036854775808", new(big.Int).SetInt64(math.MinInt64), ""}, 1043 } 1044 for _, tt := range tests { 1045 t.Run(tt.name, func(t *testing.T) { 1046 gotVal, gotErr := stringToBigInt(tt.val) 1047 assert.Equal(t, tt.wantVal, gotVal) 1048 assertErrorMessage(t, tt.wantErr, gotErr) 1049 }) 1050 } 1051 } 1052 1053 func Test_stringToEpochMillis(t *testing.T) { 1054 tests := []struct { 1055 name string 1056 val string 1057 layout string 1058 location *time.Location 1059 wantVal int64 1060 wantErr string 1061 }{ 1062 {"invalid", "invalid", TimestampLayoutDefault, time.UTC, 0, "cannot parse \"invalid\" as \"2006\""}, 1063 {"layout with TZ", "2021-10-12 00:00:00.999 +01:00", "2006-01-02 15:04:05 -07:00", paris, 1633993200999, ""}, 1064 {"layout without TZ", "2021-10-12 01:00:00.999", "2006-01-02 15:04:05", paris, 1633993200999, ""}, 1065 } 1066 for _, tt := range tests { 1067 t.Run(tt.name, func(t *testing.T) { 1068 gotVal, gotErr := stringToEpochMillis(tt.val, tt.layout, tt.location) 1069 assert.Equal(t, tt.wantVal, gotVal) 1070 assertErrorMessage(t, tt.wantErr, gotErr) 1071 }) 1072 } 1073 } 1074 1075 func Test_stringToNanosOfDay(t *testing.T) { 1076 tests := []struct { 1077 name string 1078 val string 1079 layout string 1080 wantVal int64 1081 wantErr string 1082 }{ 1083 {"invalid", "invalid", TimeLayoutDefault, 0, "cannot parse \"invalid\" as \"15\""}, 1084 {"ok", "12:34:56.123456789", "15:04:05.999999999", 45296123456789, ""}, 1085 } 1086 for _, tt := range tests { 1087 t.Run(tt.name, func(t *testing.T) { 1088 gotVal, gotErr := stringToNanosOfDay(tt.val, tt.layout) 1089 assert.Equal(t, tt.wantVal, gotVal) 1090 assertErrorMessage(t, tt.wantErr, gotErr) 1091 }) 1092 } 1093 } 1094 1095 func Test_stringToEpochDays(t *testing.T) { 1096 tests := []struct { 1097 name string 1098 val string 1099 layout string 1100 wantVal int32 1101 wantErr string 1102 }{ 1103 {"invalid", "invalid", DateLayoutDefault, 0, "cannot parse \"invalid\" as \"2006\""}, 1104 {"ok", "2021-10-12", DateLayoutDefault, 18912, ""}, 1105 } 1106 for _, tt := range tests { 1107 t.Run(tt.name, func(t *testing.T) { 1108 gotVal, gotErr := stringToEpochDays(tt.val, tt.layout) 1109 assert.Equal(t, tt.wantVal, gotVal) 1110 assertErrorMessage(t, tt.wantErr, gotErr) 1111 }) 1112 } 1113 } 1114 1115 func Test_bigIntToInt64(t *testing.T) { 1116 tests := []struct { 1117 name string 1118 val *big.Int 1119 wantVal int64 1120 wantErr string 1121 }{ 1122 {"not int64", new(big.Int).SetUint64(math.MaxInt64 + 1), 0, "value out of range: 9223372036854775808"}, 1123 {"ok", new(big.Int).SetUint64(math.MaxInt64), math.MaxInt64, ""}, 1124 } 1125 for _, tt := range tests { 1126 t.Run(tt.name, func(t *testing.T) { 1127 gotVal, gotErr := bigIntToInt64(tt.val) 1128 assert.Equal(t, tt.wantVal, gotVal) 1129 assertErrorMessage(t, tt.wantErr, gotErr) 1130 }) 1131 } 1132 } 1133 1134 func Test_bigIntToInt32(t *testing.T) { 1135 tests := []struct { 1136 name string 1137 val *big.Int 1138 wantVal int32 1139 wantErr string 1140 }{ 1141 {"out of range", new(big.Int).SetUint64(math.MaxInt32 + 1), 0, "value out of range: 2147483648"}, 1142 {"ok", new(big.Int).SetUint64(math.MaxInt32), math.MaxInt32, ""}, 1143 } 1144 for _, tt := range tests { 1145 t.Run(tt.name, func(t *testing.T) { 1146 gotVal, gotErr := bigIntToInt32(tt.val) 1147 assert.Equal(t, tt.wantVal, gotVal) 1148 assertErrorMessage(t, tt.wantErr, gotErr) 1149 }) 1150 } 1151 } 1152 1153 func Test_bigIntToInt(t *testing.T) { 1154 tests := []struct { 1155 name string 1156 val *big.Int 1157 intSize int 1158 wantVal int64 1159 wantErr string 1160 }{ 1161 {"32 bits out of range pos", big.NewInt(math.MaxInt32 + 1), 32, 0, "value out of range: 2147483648"}, 1162 {"32 bits out of range neg", big.NewInt(math.MinInt32 - 1), 32, 0, "value out of range: -2147483649"}, 1163 {"32 bits max", big.NewInt(math.MaxInt32), 32, math.MaxInt32, ""}, 1164 {"32 bits min", big.NewInt(math.MinInt32), 32, math.MinInt32, ""}, 1165 } 1166 if strconv.IntSize == 64 { 1167 tests = append(tests, []struct { 1168 name string 1169 val *big.Int 1170 intSize int 1171 wantVal int64 1172 wantErr string 1173 }{ 1174 {"64 bits max", big.NewInt(math.MaxInt64), 64, math.MaxInt64, ""}, 1175 {"64 bits min", big.NewInt(math.MinInt64), 64, math.MinInt64, ""}, 1176 {"64 bits out of range pos", new(big.Int).Add(big.NewInt(math.MaxInt64), big.NewInt(1)), 64, 0, "value out of range: 9223372036854775808"}, 1177 {"64 bits out of range neg", new(big.Int).Add(big.NewInt(math.MinInt64), big.NewInt(-1)), 64, 0, "value out of range: -9223372036854775809"}, 1178 }...) 1179 } 1180 for _, tt := range tests { 1181 t.Run(tt.name, func(t *testing.T) { 1182 gotVal, gotErr := bigIntToInt(tt.val, tt.intSize) 1183 assert.EqualValues(t, tt.wantVal, gotVal) 1184 assertErrorMessage(t, tt.wantErr, gotErr) 1185 }) 1186 } 1187 } 1188 1189 func Test_bigIntToInt16(t *testing.T) { 1190 tests := []struct { 1191 name string 1192 val *big.Int 1193 wantVal int16 1194 wantErr string 1195 }{ 1196 {"out of range", new(big.Int).SetUint64(math.MaxInt16 + 1), 0, "value out of range: 32768"}, 1197 {"ok", new(big.Int).SetUint64(math.MaxInt16), math.MaxInt16, ""}, 1198 } 1199 for _, tt := range tests { 1200 t.Run(tt.name, func(t *testing.T) { 1201 gotVal, gotErr := bigIntToInt16(tt.val) 1202 assert.Equal(t, tt.wantVal, gotVal) 1203 assertErrorMessage(t, tt.wantErr, gotErr) 1204 }) 1205 } 1206 } 1207 1208 func Test_bigIntToInt8(t *testing.T) { 1209 tests := []struct { 1210 name string 1211 val *big.Int 1212 wantVal int8 1213 wantErr string 1214 }{ 1215 {"out of range", new(big.Int).SetUint64(math.MaxInt8 + 1), 0, "value out of range: 128"}, 1216 {"ok", new(big.Int).SetUint64(math.MaxInt8), math.MaxInt8, ""}, 1217 } 1218 for _, tt := range tests { 1219 t.Run(tt.name, func(t *testing.T) { 1220 gotVal, gotErr := bigIntToInt8(tt.val) 1221 assert.Equal(t, tt.wantVal, gotVal) 1222 assertErrorMessage(t, tt.wantErr, gotErr) 1223 }) 1224 } 1225 } 1226 1227 func Test_bigIntToUint64(t *testing.T) { 1228 tests := []struct { 1229 name string 1230 val *big.Int 1231 wantVal uint64 1232 wantErr string 1233 }{ 1234 {"out of range", new(big.Int).Add(new(big.Int).SetUint64(math.MaxUint64), big.NewInt(1)), 0, "value out of range: 18446744073709551616"}, 1235 {"ok", new(big.Int).SetUint64(math.MaxUint64), math.MaxUint64, ""}, 1236 } 1237 for _, tt := range tests { 1238 t.Run(tt.name, func(t *testing.T) { 1239 gotVal, gotErr := bigIntToUint64(tt.val) 1240 assert.Equal(t, tt.wantVal, gotVal) 1241 assertErrorMessage(t, tt.wantErr, gotErr) 1242 }) 1243 } 1244 } 1245 1246 func Test_bigIntToUint32(t *testing.T) { 1247 tests := []struct { 1248 name string 1249 val *big.Int 1250 wantVal uint32 1251 wantErr string 1252 }{ 1253 {"out of range", new(big.Int).SetUint64(math.MaxUint32 + 1), 0, "value out of range: 4294967296"}, 1254 {"ok", new(big.Int).SetUint64(math.MaxUint32), math.MaxUint32, ""}, 1255 } 1256 for _, tt := range tests { 1257 t.Run(tt.name, func(t *testing.T) { 1258 gotVal, gotErr := bigIntToUint32(tt.val) 1259 assert.Equal(t, tt.wantVal, gotVal) 1260 assertErrorMessage(t, tt.wantErr, gotErr) 1261 }) 1262 } 1263 } 1264 1265 func Test_bigIntToUint(t *testing.T) { 1266 tests := []struct { 1267 name string 1268 val *big.Int 1269 intSize int 1270 wantVal uint64 1271 wantErr string 1272 }{ 1273 {"32 bits out of range pos", big.NewInt(math.MaxUint32 + 1), 32, 0, "value out of range: 4294967296"}, 1274 {"32 bits out of range neg", big.NewInt(-1), 32, 0, "value out of range: -1"}, 1275 {"32 bits max", big.NewInt(math.MaxUint32), 32, math.MaxUint32, ""}, 1276 {"32 bits min", big.NewInt(0), 32, 0, ""}, 1277 } 1278 if strconv.IntSize == 64 { 1279 tests = append(tests, []struct { 1280 name string 1281 val *big.Int 1282 intSize int 1283 wantVal uint64 1284 wantErr string 1285 }{ 1286 {"64 bits max", new(big.Int).SetUint64(math.MaxUint64), 64, math.MaxUint64, ""}, 1287 {"64 bits min", big.NewInt(0), 64, 0, ""}, 1288 {"64 bits out of range pos", new(big.Int).Add(new(big.Int).SetUint64(math.MaxUint64), big.NewInt(1)), 64, 0, "value out of range: 18446744073709551616"}, 1289 {"64 bits out of range neg", big.NewInt(-1), 64, 0, "value out of range: -1"}, 1290 }...) 1291 } 1292 for _, tt := range tests { 1293 t.Run(tt.name, func(t *testing.T) { 1294 gotVal, gotErr := bigIntToUint(tt.val, tt.intSize) 1295 assert.EqualValues(t, tt.wantVal, gotVal) 1296 assertErrorMessage(t, tt.wantErr, gotErr) 1297 }) 1298 } 1299 } 1300 1301 func Test_bigIntToUint16(t *testing.T) { 1302 tests := []struct { 1303 name string 1304 val *big.Int 1305 wantVal uint16 1306 wantErr string 1307 }{ 1308 {"out of range", new(big.Int).SetUint64(math.MaxUint16 + 1), 0, "value out of range: 65536"}, 1309 {"ok", new(big.Int).SetUint64(math.MaxUint16), math.MaxUint16, ""}, 1310 } 1311 for _, tt := range tests { 1312 t.Run(tt.name, func(t *testing.T) { 1313 gotVal, gotErr := bigIntToUint16(tt.val) 1314 assert.Equal(t, tt.wantVal, gotVal) 1315 assertErrorMessage(t, tt.wantErr, gotErr) 1316 }) 1317 } 1318 } 1319 1320 func Test_bigIntToUint8(t *testing.T) { 1321 tests := []struct { 1322 name string 1323 val *big.Int 1324 wantVal uint8 1325 wantErr string 1326 }{ 1327 {"out of range", new(big.Int).SetUint64(math.MaxUint8 + 1), 0, "value out of range: 256"}, 1328 {"ok", new(big.Int).SetUint64(math.MaxUint8), math.MaxUint8, ""}, 1329 } 1330 for _, tt := range tests { 1331 t.Run(tt.name, func(t *testing.T) { 1332 gotVal, gotErr := bigIntToUint8(tt.val) 1333 assert.Equal(t, tt.wantVal, gotVal) 1334 assertErrorMessage(t, tt.wantErr, gotErr) 1335 }) 1336 } 1337 } 1338 1339 func Test_bigFloatToFloat64(t *testing.T) { 1340 tests := []struct { 1341 name string 1342 val *big.Float 1343 wantVal float64 1344 wantErr string 1345 }{ 1346 {"exact", big.NewFloat(123.4), 123.4, ""}, 1347 {"rounded", new(big.Float).Add(big.NewFloat(math.MaxFloat64), big.NewFloat(math.MaxFloat64)), 0, "value out of range: 3.5953862697246314e+308"}, 1348 } 1349 for _, tt := range tests { 1350 t.Run(tt.name, func(t *testing.T) { 1351 gotVal, gotErr := bigFloatToFloat64(tt.val) 1352 assert.Equal(t, tt.wantVal, gotVal) 1353 assertErrorMessage(t, tt.wantErr, gotErr) 1354 }) 1355 } 1356 } 1357 1358 func Test_float64ToBigFloat(t *testing.T) { 1359 tests := []struct { 1360 name string 1361 val float64 1362 destVal *big.Float 1363 wantVal *big.Float 1364 wantErr string 1365 }{ 1366 {"exact", 123.4, new(big.Float), big.NewFloat(123.4), ""}, 1367 {"Nan", math.NaN(), new(big.Float), new(big.Float), "value out of range: NaN"}, 1368 } 1369 for _, tt := range tests { 1370 t.Run(tt.name, func(t *testing.T) { 1371 gotErr := float64ToBigFloat(tt.val, tt.destVal) 1372 assert.Equal(t, tt.wantVal, tt.destVal) 1373 assertErrorMessage(t, tt.wantErr, gotErr) 1374 }) 1375 } 1376 } 1377 1378 func Test_float64ToFloat32(t *testing.T) { 1379 tests := []struct { 1380 name string 1381 val float64 1382 wantVal float32 1383 wantErr string 1384 }{ 1385 {"exact", 123, float32(123), ""}, 1386 {"max", math.MaxFloat64, 0, "value out of range: 1.7976931348623157e+308"}, 1387 } 1388 for _, tt := range tests { 1389 t.Run(tt.name, func(t *testing.T) { 1390 gotVal, gotErr := float64ToFloat32(tt.val) 1391 assert.Equal(t, tt.wantVal, gotVal) 1392 assertErrorMessage(t, tt.wantErr, gotErr) 1393 }) 1394 } 1395 }