github.com/mithrandie/csvq@v1.18.1/lib/value/conv_test.go (about) 1 package value 2 3 import ( 4 "math" 5 "testing" 6 "time" 7 8 "github.com/mithrandie/csvq/lib/option" 9 10 "github.com/mithrandie/ternary" 11 ) 12 13 func TestStrToTime(t *testing.T) { 14 formats := []string{"01/02/2006"} 15 location, _ := time.LoadLocation("UTC") 16 17 s := "01/02/2006" 18 if _, ok := StrToTime(s, formats, location); !ok { 19 t.Errorf("failed, want to success for %q", s) 20 } 21 22 s = "2006-01-02 15:04:05" 23 if _, ok := StrToTime(s, formats, location); !ok { 24 t.Errorf("failed, want to success for %q", s) 25 } 26 27 s = "2006-01-02" 28 if _, ok := StrToTime(s, formats, location); !ok { 29 t.Errorf("failed, want to success for %q", s) 30 } 31 32 s = "2006-01-02 15:04:05 -08:00" 33 if _, ok := StrToTime(s, formats, location); !ok { 34 t.Errorf("failed, want to success for %q", s) 35 } 36 37 s = "2006-01-02 15:04:05 -0800" 38 if _, ok := StrToTime(s, formats, location); !ok { 39 t.Errorf("failed, want to success for %q", s) 40 } 41 42 s = "2006-01-02 15:04:05 PST" 43 if _, ok := StrToTime(s, formats, location); !ok { 44 t.Errorf("failed, want to success for %q", s) 45 } 46 47 s = "2006/01/02 15:04:05" 48 if _, ok := StrToTime(s, formats, location); !ok { 49 t.Errorf("failed, want to success for %q", s) 50 } 51 52 s = "2006/01/02" 53 if _, ok := StrToTime(s, formats, location); !ok { 54 t.Errorf("failed, want to success for %q", s) 55 } 56 57 s = "2006/01/02 15:04:05 -08:00" 58 if _, ok := StrToTime(s, formats, location); !ok { 59 t.Errorf("failed, want to success for %q", s) 60 } 61 62 s = "2006/01/02 15:04:05 -0800" 63 if _, ok := StrToTime(s, formats, location); !ok { 64 t.Errorf("failed, want to success for %q", s) 65 } 66 67 s = "2006/01/02 15:04:05 -0800" 68 if _, ok := StrToTime(s, formats, location); !ok { 69 t.Errorf("failed, want to success for %q", s) 70 } 71 72 s = "2006/11/2 15:04:05 -0800" 73 if _, ok := StrToTime(s, formats, location); !ok { 74 t.Errorf("failed, want to success for %q", s) 75 } 76 77 s = "2006/01/02 15:04:05 PST" 78 if _, ok := StrToTime(s, formats, location); !ok { 79 t.Errorf("failed, want to success for %q", s) 80 } 81 82 s = "2006-1-2 15:04:05" 83 if _, ok := StrToTime(s, formats, location); !ok { 84 t.Errorf("failed, want to success for %q", s) 85 } 86 87 s = "2006-1-2" 88 if _, ok := StrToTime(s, formats, location); !ok { 89 t.Errorf("failed, want to success for %q", s) 90 } 91 92 s = "2006-1-2 15:04:05 -08:00" 93 if _, ok := StrToTime(s, formats, location); !ok { 94 t.Errorf("failed, want to success for %q", s) 95 } 96 97 s = "2006-1-2 15:04:05 -0800" 98 if _, ok := StrToTime(s, formats, location); !ok { 99 t.Errorf("failed, want to success for %q", s) 100 } 101 102 s = "2006-1-2 15:04:05 PST" 103 if _, ok := StrToTime(s, formats, location); !ok { 104 t.Errorf("failed, want to success for %q", s) 105 } 106 107 s = "2006/1/2 15:04:05" 108 if _, ok := StrToTime(s, formats, location); !ok { 109 t.Errorf("failed, want to success for %q", s) 110 } 111 112 s = "2006/1/2" 113 if _, ok := StrToTime(s, formats, location); !ok { 114 t.Errorf("failed, want to success for %q", s) 115 } 116 117 s = "2006/1/2 15:04:05 -08:00" 118 if _, ok := StrToTime(s, formats, location); !ok { 119 t.Errorf("failed, want to success for %q", s) 120 } 121 122 s = "2006/1/2 15:04:05 -0800" 123 if _, ok := StrToTime(s, formats, location); !ok { 124 t.Errorf("failed, want to success for %q", s) 125 } 126 127 s = "2006/1/2 15:04:05 PST" 128 if _, ok := StrToTime(s, formats, location); !ok { 129 t.Errorf("failed, want to success for %q", s) 130 } 131 132 s = "02 Jan 06 15:04 PDT" 133 if _, ok := StrToTime(s, formats, location); !ok { 134 t.Errorf("failed, want to success for %q", s) 135 } 136 137 s = "02 Jan 06 15:04 -0700" 138 if _, ok := StrToTime(s, formats, location); !ok { 139 t.Errorf("failed, want to success for %q", s) 140 } 141 142 s = "2006-01-02T15:04:05-08:00" 143 if _, ok := StrToTime(s, formats, location); !ok { 144 t.Errorf("failed, want to success for %q", s) 145 } 146 147 s = "2006-01-02T15:04:05+08:00" 148 if _, ok := StrToTime(s, formats, location); !ok { 149 t.Errorf("failed, want to success for %q", s) 150 } 151 152 s = "2006-01-02T15:04:05" 153 if _, ok := StrToTime(s, formats, location); !ok { 154 t.Errorf("failed, want to success for %q", s) 155 } 156 157 s = "e" 158 if _, ok := StrToTime(s, formats, location); ok { 159 t.Errorf("successeded, want to fail for %q", s) 160 } 161 162 s = "2006-01-02" 163 tm, _ := StrToTime(s, formats, location) 164 if tm.Location() != location { 165 t.Errorf("location should be %q", location.String()) 166 } 167 168 s = "2006-01-02T15:04:05+08:00" 169 tm, _ = StrToTime(s, formats, location) 170 z, i := tm.Zone() 171 if z != "" { 172 t.Errorf("zone name shoud be empty") 173 } 174 if i != 28800 { 175 t.Errorf("zone offset should be %d", 28800) 176 } 177 } 178 179 var convertDatetimeFormatTests = []struct { 180 Datetime string 181 Format string 182 Result string 183 }{ 184 { 185 Format: "datetime: %Y-%m-%d %H:%i:%s %% %g", 186 Result: "datetime: 2006-01-02 15:04:05 % g", 187 }, 188 { 189 Format: "%a", 190 Result: "Mon", 191 }, 192 { 193 Format: "%b", 194 Result: "Jan", 195 }, 196 { 197 Format: "%c", 198 Result: "1", 199 }, 200 { 201 Format: "%E", 202 Result: "_2", 203 }, 204 { 205 Format: "%e", 206 Result: "2", 207 }, 208 { 209 Format: "%F", 210 Result: ".999999", 211 }, 212 { 213 Format: "%f", 214 Result: ".000000", 215 }, 216 { 217 Format: "%h", 218 Result: "03", 219 }, 220 { 221 Format: "%l", 222 Result: "3", 223 }, 224 { 225 Format: "%M", 226 Result: "January", 227 }, 228 { 229 Format: "%N", 230 Result: ".999999999", 231 }, 232 { 233 Format: "%n", 234 Result: ".000000000", 235 }, 236 { 237 Format: "%p", 238 Result: "PM", 239 }, 240 { 241 Format: "%r", 242 Result: "03:04:05 PM", 243 }, 244 { 245 Format: "%T", 246 Result: "15:04:05", 247 }, 248 { 249 Format: "%W", 250 Result: "Monday", 251 }, 252 { 253 Format: "%y", 254 Result: "06", 255 }, 256 { 257 Format: "%Z", 258 Result: "Z07:00", 259 }, 260 { 261 Format: "%z", 262 Result: "MST", 263 }, 264 } 265 266 func TestConvertDatetimeFormat(t *testing.T) { 267 for _, v := range convertDatetimeFormatTests { 268 converted := ConvertDatetimeFormat(v.Format) 269 if converted != v.Result { 270 t.Errorf("result = %q, want %q for %q", converted, v.Result, v.Format) 271 } 272 } 273 } 274 275 func TestFloat64ToTime(t *testing.T) { 276 location, _ := time.LoadLocation("UTC") 277 278 f := float64(1136181845) 279 expect := time.Date(2006, 1, 2, 6, 4, 5, 0, time.UTC).In(location) 280 result := Float64ToTime(f, location) 281 if !result.Equal(expect) { 282 t.Errorf("result = %q, want %q for %f", result, expect, f) 283 } 284 285 f = 1136181845.123 286 expect = time.Date(2006, 1, 2, 6, 4, 5, 123000000, time.UTC).In(location) 287 result = Float64ToTime(f, location) 288 if !result.Equal(expect) { 289 t.Errorf("result = %q, want %q for %f", result, expect, f) 290 } 291 292 f = 1.123456789012 293 expect = time.Date(1970, 1, 1, 0, 0, 1, 123456789, time.UTC).In(location) 294 result = Float64ToTime(f, location) 295 if !result.Equal(expect) { 296 t.Errorf("result = %q, want %q for %f", result, expect, f) 297 } 298 } 299 300 func TestFloat64ToStr(t *testing.T) { 301 f := 0.000000123 302 expect := "0.000000123" 303 304 result := Float64ToStr(f, false) 305 if result != expect { 306 t.Errorf("result = %q, want %q for %f", result, expect, f) 307 } 308 309 expect = "1.23e-07" 310 result = Float64ToStr(f, true) 311 if result != expect { 312 t.Errorf("result = %q, want %q for %f", result, expect, f) 313 } 314 } 315 316 func TestToInteger(t *testing.T) { 317 var p Primary 318 var i Primary 319 320 p = NewInteger(1) 321 i = ToInteger(p) 322 if _, ok := i.(*Integer); !ok { 323 t.Errorf("primary type = %T, want Integer for %#v", i, p) 324 } 325 326 p = NewFloat(1) 327 i = ToInteger(p) 328 if _, ok := i.(*Integer); !ok { 329 t.Errorf("primary type = %T, want Integer for %#v", i, p) 330 } 331 332 p = NewFloat(1.6) 333 i = ToInteger(p) 334 if _, ok := i.(*Integer); !ok { 335 t.Errorf("primary type = %T, want Null for %#v", i, p) 336 } 337 338 p = NewFloat(math.NaN()) 339 i = ToInteger(p) 340 if _, ok := i.(*Null); !ok { 341 t.Errorf("primary type = %T, want Null for %#v", i, p) 342 } 343 344 p = NewFloat(math.Inf(1)) 345 i = ToInteger(p) 346 if _, ok := i.(*Null); !ok { 347 t.Errorf("primary type = %T, want Null for %#v", i, p) 348 } 349 350 p = NewFloat(math.Inf(-1)) 351 i = ToInteger(p) 352 if _, ok := i.(*Null); !ok { 353 t.Errorf("primary type = %T, want Null for %#v", i, p) 354 } 355 356 p = NewString(" 1") 357 i = ToInteger(p) 358 if _, ok := i.(*Integer); !ok { 359 t.Errorf("primary type = %T, want Integer for %#v", i, p) 360 } 361 362 p = NewString("-1") 363 i = ToInteger(p) 364 if _, ok := i.(*Integer); !ok { 365 t.Errorf("primary type = %T, want Integer for %#v", i, p) 366 } 367 368 p = NewString("1e+02") 369 i = ToInteger(p) 370 if _, ok := i.(*Integer); !ok { 371 t.Errorf("primary type = %T, want Integer for %#v", i, p) 372 } 373 374 p = NewString("1.5") 375 i = ToInteger(p) 376 if _, ok := i.(*Integer); !ok { 377 t.Errorf("primary type = %T, want Null for %#v", i, p) 378 } 379 380 p = NewString("error") 381 i = ToInteger(p) 382 if _, ok := i.(*Null); !ok { 383 t.Errorf("primary type = %T, want Null for %#v", i, p) 384 } 385 386 p = NewString("error") 387 i = ToInteger(p) 388 if _, ok := i.(*Null); !ok { 389 t.Errorf("primary type = %T, want Null for %#v", i, p) 390 } 391 392 p = NewString("2002-02-02") 393 i = ToInteger(p) 394 if _, ok := i.(*Null); !ok { 395 t.Errorf("primary type = %T, want Null for %#v", i, p) 396 } 397 398 p = NewString("2002/02/02") 399 i = ToInteger(p) 400 if _, ok := i.(*Null); !ok { 401 t.Errorf("primary type = %T, want Null for %#v", i, p) 402 } 403 404 p = NewString("03 Mar 12 12:03 PST") 405 i = ToInteger(p) 406 if _, ok := i.(*Null); !ok { 407 t.Errorf("primary type = %T, want Null for %#v", i, p) 408 } 409 410 p = NewString("") 411 i = ToInteger(p) 412 if _, ok := i.(*Null); !ok { 413 t.Errorf("primary type = %T, want Null for %#v", i, p) 414 } 415 } 416 417 func TestToIntegerStrictly(t *testing.T) { 418 var p Primary 419 var i Primary 420 421 p = NewInteger(1) 422 i = ToIntegerStrictly(p) 423 if _, ok := i.(*Integer); !ok { 424 t.Errorf("primary type = %T, want Integer for %#v", i, p) 425 } 426 427 p = NewFloat(1) 428 i = ToIntegerStrictly(p) 429 if _, ok := i.(*Null); !ok { 430 t.Errorf("primary type = %T, want Null for %#v", i, p) 431 } 432 433 p = NewFloat(1.6) 434 i = ToIntegerStrictly(p) 435 if _, ok := i.(*Null); !ok { 436 t.Errorf("primary type = %T, want Null for %#v", i, p) 437 } 438 439 p = NewString(" 1") 440 i = ToIntegerStrictly(p) 441 if _, ok := i.(*Integer); !ok { 442 t.Errorf("primary type = %T, want Integer for %#v", i, p) 443 } 444 445 p = NewString("-1") 446 i = ToIntegerStrictly(p) 447 if _, ok := i.(*Integer); !ok { 448 t.Errorf("primary type = %T, want Integer for %#v", i, p) 449 } 450 451 p = NewString("1e+02") 452 i = ToIntegerStrictly(p) 453 if _, ok := i.(*Null); !ok { 454 t.Errorf("primary type = %T, want Null for %#v", i, p) 455 } 456 457 p = NewString("2002-02-02") 458 i = ToIntegerStrictly(p) 459 if _, ok := i.(*Null); !ok { 460 t.Errorf("primary type = %T, want Null for %#v", i, p) 461 } 462 } 463 464 func TestToFloat(t *testing.T) { 465 var p Primary 466 var f Primary 467 468 p = NewInteger(1) 469 f = ToFloat(p) 470 if _, ok := f.(*Float); !ok { 471 t.Errorf("primary type = %T, want Float for %#v", f, p) 472 } 473 474 p = NewFloat(1.234) 475 f = ToFloat(p) 476 if _, ok := f.(*Float); !ok { 477 t.Errorf("primary type = %T, want Float for %#v", f, p) 478 } 479 480 p = NewString("1") 481 f = ToFloat(p) 482 if _, ok := f.(*Float); !ok { 483 t.Errorf("primary type = %T, want Float for %#v", f, p) 484 } 485 486 p = NewString("error") 487 f = ToFloat(p) 488 if _, ok := f.(*Null); !ok { 489 t.Errorf("primary type = %T, want Null for %#v", f, p) 490 } 491 } 492 493 func TestToDatetime(t *testing.T) { 494 var p Primary 495 var dt Primary 496 497 location, _ := time.LoadLocation("UTC") 498 499 formats := []string{"01022006"} 500 p = NewString("02012012") 501 dt = ToDatetime(p, formats, location) 502 if _, ok := dt.(*Datetime); !ok { 503 t.Errorf("primary type = %T, want Datetime for %#v", dt, p) 504 } else { 505 expect := time.Date(2012, 2, 1, 0, 0, 0, 0, location) 506 if !dt.(*Datetime).Raw().Equal(expect) { 507 t.Errorf("datetime = %s, want %s for %#v", dt, expect, p) 508 } 509 } 510 511 p = NewDatetimeFromString("2006-01-02 15:04:05", nil, location) 512 dt = ToDatetime(p, nil, location) 513 if _, ok := dt.(*Datetime); !ok { 514 t.Errorf("primary type = %T, want Datetime for %#v", dt, p) 515 } 516 517 p = NewString("2006-01-02 15:04:05") 518 dt = ToDatetime(p, nil, location) 519 if _, ok := dt.(*Datetime); !ok { 520 t.Errorf("primary type = %T, want Datetime for %#v", dt, p) 521 } 522 523 p = NewString("error") 524 dt = ToDatetime(p, nil, location) 525 if _, ok := dt.(*Null); !ok { 526 t.Errorf("primary type = %T, want Null for %#v", dt, p) 527 } 528 } 529 530 func TestToBoolean(t *testing.T) { 531 var p Primary 532 var b Primary 533 534 p = NewBoolean(true) 535 b = ToBoolean(p) 536 if _, ok := b.(*Boolean); !ok { 537 t.Errorf("primary type = %T, want Boolean for %#v", b, p) 538 } 539 540 p = NewTernary(ternary.TRUE) 541 b = ToBoolean(p) 542 if _, ok := b.(*Boolean); !ok { 543 t.Errorf("primary type = %T, want Boolean for %#v", b, p) 544 } 545 546 p = NewInteger(1) 547 b = ToBoolean(p) 548 if _, ok := b.(*Boolean); !ok { 549 t.Errorf("primary type = %T, want Boolean for %#v", b, p) 550 } 551 552 p = NewFloat(0) 553 b = ToBoolean(p) 554 if _, ok := b.(*Boolean); !ok { 555 t.Errorf("primary type = %T, want Boolean for %#v", b, p) 556 } 557 558 p = NewString("true") 559 b = ToBoolean(p) 560 if _, ok := b.(*Boolean); !ok { 561 t.Errorf("primary type = %T, want Boolean for %#v", b, p) 562 } 563 564 p = NewTernary(ternary.UNKNOWN) 565 b = ToBoolean(p) 566 if _, ok := b.(*Null); !ok { 567 t.Errorf("primary type = %T, want Null for %#v", b, p) 568 } 569 570 p = NewString("error") 571 b = ToBoolean(p) 572 if _, ok := b.(*Null); !ok { 573 t.Errorf("primary type = %T, want Null for %#v", b, p) 574 } 575 } 576 577 func TestToString(t *testing.T) { 578 var p Primary 579 var s Primary 580 581 location, _ := option.GetLocation("UTC") 582 583 p = NewString("str") 584 s = ToString(p) 585 if _, ok := s.(*String); !ok { 586 t.Errorf("primary type = %T, want String for %#v", s, p) 587 } 588 589 p = NewInteger(1) 590 s = ToString(p) 591 if _, ok := s.(*String); !ok { 592 t.Errorf("primary type = %T, want String for %#v", s, p) 593 } 594 595 p = NewFloat(1) 596 s = ToString(p) 597 if _, ok := s.(*String); !ok { 598 t.Errorf("primary type = %T, want String for %#v", s, p) 599 } 600 601 p = NewDatetimeFromString("2006-01-02 15:04:05", nil, location) 602 s = ToString(p) 603 if _, ok := s.(*Null); !ok { 604 t.Errorf("primary type = %T, want Null for %#v", s, p) 605 } 606 } 607 608 func BenchmarkStrToTime1(b *testing.B) { 609 formats := []string{"01/02/2006"} 610 location, _ := time.LoadLocation("UTC") 611 612 for i := 0; i < b.N; i++ { 613 s := "01/02/2006" 614 _, _ = StrToTime(s, formats, location) 615 } 616 } 617 618 func BenchmarkStrToTime2(b *testing.B) { 619 formats := []string{"01/02/2006"} 620 location, _ := time.LoadLocation("UTC") 621 622 for i := 0; i < b.N; i++ { 623 s := "2006-01-02T15:04:05-07:00" 624 _, _ = StrToTime(s, formats, location) 625 } 626 } 627 628 func BenchmarkStrToTime3(b *testing.B) { 629 formats := []string{"01/02/2006"} 630 location, _ := time.LoadLocation("UTC") 631 632 for i := 0; i < b.N; i++ { 633 s := "2006-01-02" 634 _, _ = StrToTime(s, formats, location) 635 } 636 } 637 638 func BenchmarkStrToTime4(b *testing.B) { 639 formats := []string{"01/02/2006"} 640 location, _ := time.LoadLocation("UTC") 641 642 for i := 0; i < b.N; i++ { 643 s := "2006-01-02 15:04:05" 644 _, _ = StrToTime(s, formats, location) 645 } 646 } 647 648 func BenchmarkStrToTime5(b *testing.B) { 649 formats := []string{"01/02/2006"} 650 location, _ := time.LoadLocation("UTC") 651 652 for i := 0; i < b.N; i++ { 653 s := "2006-01-02 15:04:05 -0700" 654 _, _ = StrToTime(s, formats, location) 655 } 656 } 657 658 func BenchmarkStrToTime6(b *testing.B) { 659 formats := []string{"01/02/2006"} 660 location, _ := option.GetLocation("UTC") 661 662 for i := 0; i < b.N; i++ { 663 s := "02 Jan 06 15:04 PDT" 664 _, _ = StrToTime(s, formats, location) 665 } 666 } 667 668 func BenchmarkStrToTime7(b *testing.B) { 669 formats := []string{"01/02/2006"} 670 location, _ := option.GetLocation("UTC") 671 672 for i := 0; i < b.N; i++ { 673 s := "abcdefghijklmnopq" 674 _, _ = StrToTime(s, formats, location) 675 } 676 } 677 678 func BenchmarkToInteger(b *testing.B) { 679 p := NewString("a") 680 for i := 0; i < b.N; i++ { 681 _ = ToInteger(p) 682 } 683 } 684 685 func BenchmarkToInteger2(b *testing.B) { 686 p := NewString("2012-02-02") 687 for i := 0; i < b.N; i++ { 688 _ = ToInteger(p) 689 } 690 } 691 692 func BenchmarkToInteger3(b *testing.B) { 693 p := NewString(" 12345") 694 for i := 0; i < b.N; i++ { 695 _ = ToInteger(p) 696 } 697 } 698 699 func BenchmarkToInteger4(b *testing.B) { 700 p := NewString(" 123.456") 701 for i := 0; i < b.N; i++ { 702 _ = ToInteger(p) 703 } 704 } 705 706 func BenchmarkToInteger5(b *testing.B) { 707 p := NewFloat(123.456) 708 for i := 0; i < b.N; i++ { 709 _ = ToInteger(p) 710 } 711 } 712 713 func BenchmarkToFloat(b *testing.B) { 714 for i := 0; i < b.N; i++ { 715 p := NewString("a") 716 ToFloat(p) 717 } 718 } 719 720 func BenchmarkToFloat2(b *testing.B) { 721 for i := 0; i < b.N; i++ { 722 p := NewString("2012-02-02") 723 ToFloat(p) 724 } 725 } 726 727 var convertDatetimeFormatBenchString = "%Y-%m-%d %H:%i:%s" 728 729 func BenchmarkConvertDatetimeFormat(b *testing.B) { 730 for i := 0; i < b.N; i++ { 731 _ = ConvertDatetimeFormat(convertDatetimeFormatBenchString) 732 } 733 }