@@ -469,3 +469,333 @@ func TestConvertStringToBool(t *testing.T) {
469
469
})
470
470
}
471
471
}
472
+
473
+ func TestConvertStringToInt64 (t * testing.T ) {
474
+ tests := []struct {
475
+ desc string
476
+ inputStr string
477
+ expInt64 int64
478
+ expectError bool
479
+ }{
480
+ {
481
+ desc : "valid number string" ,
482
+ inputStr : "10000" ,
483
+ expInt64 : 10000 ,
484
+ expectError : false ,
485
+ },
486
+ {
487
+ desc : "test higher number" ,
488
+ inputStr : "15000" ,
489
+ expInt64 : 15000 ,
490
+ expectError : false ,
491
+ },
492
+ {
493
+ desc : "round M to number" ,
494
+ inputStr : "1M" ,
495
+ expInt64 : 1000000 ,
496
+ expectError : false ,
497
+ },
498
+ {
499
+ desc : "round m to number" ,
500
+ inputStr : "1m" ,
501
+ expInt64 : 1 ,
502
+ expectError : false ,
503
+ },
504
+ {
505
+ desc : "round k to number" ,
506
+ inputStr : "1k" ,
507
+ expInt64 : 1000 ,
508
+ expectError : false ,
509
+ },
510
+ {
511
+ desc : "invalid empty string" ,
512
+ inputStr : "" ,
513
+ expInt64 : 0 ,
514
+ expectError : true ,
515
+ },
516
+ {
517
+ desc : "invalid string" ,
518
+ inputStr : "ew%65" ,
519
+ expInt64 : 0 ,
520
+ expectError : true ,
521
+ },
522
+ {
523
+ desc : "invalid KiB string" ,
524
+ inputStr : "10KiB" ,
525
+ expInt64 : 10000 ,
526
+ expectError : true ,
527
+ },
528
+ {
529
+ desc : "invalid GB string" ,
530
+ inputStr : "10GB" ,
531
+ expInt64 : 0 ,
532
+ expectError : true ,
533
+ },
534
+ {
535
+ desc : "round Ki to number" ,
536
+ inputStr : "1Ki" ,
537
+ expInt64 : 1024 ,
538
+ expectError : false ,
539
+ },
540
+ {
541
+ desc : "round k to number" ,
542
+ inputStr : "10k" ,
543
+ expInt64 : 10000 ,
544
+ expectError : false ,
545
+ },
546
+ {
547
+ desc : "round Mi to number" ,
548
+ inputStr : "10Mi" ,
549
+ expInt64 : 10485760 ,
550
+ expectError : false ,
551
+ },
552
+ {
553
+ desc : "round M to number" ,
554
+ inputStr : "10M" ,
555
+ expInt64 : 10000000 ,
556
+ expectError : false ,
557
+ },
558
+ {
559
+ desc : "round G to number" ,
560
+ inputStr : "10G" ,
561
+ expInt64 : 10000000000 ,
562
+ expectError : false ,
563
+ },
564
+ {
565
+ desc : "round Gi to number" ,
566
+ inputStr : "100Gi" ,
567
+ expInt64 : 107374182400 ,
568
+ expectError : false ,
569
+ },
570
+ {
571
+ desc : "round decimal to number" ,
572
+ inputStr : "1.2Gi" ,
573
+ expInt64 : 1288490189 ,
574
+ expectError : false ,
575
+ },
576
+ {
577
+ desc : "round big value to number" ,
578
+ inputStr : "8191Pi" ,
579
+ expInt64 : 9222246136947933184 ,
580
+ expectError : false ,
581
+ },
582
+ }
583
+ for _ , tc := range tests {
584
+ t .Run (tc .desc , func (t * testing.T ) {
585
+ actualInt64 , err := ConvertStringToInt64 (tc .inputStr )
586
+ if err != nil && ! tc .expectError {
587
+ t .Errorf ("Got error %v converting string to int64 %s; expect no error" , err , tc .inputStr )
588
+ }
589
+ if err == nil && tc .expectError {
590
+ t .Errorf ("Got no error converting string to int64 %s; expect an error" , tc .inputStr )
591
+ }
592
+ if err == nil && actualInt64 != tc .expInt64 {
593
+ t .Errorf ("Got %d for converting string to int64; expect %d" , actualInt64 , tc .expInt64 )
594
+ }
595
+ })
596
+ }
597
+ }
598
+
599
+ func TestConvertMiStringToInt64 (t * testing.T ) {
600
+ tests := []struct {
601
+ desc string
602
+ inputStr string
603
+ expInt64 int64
604
+ expectError bool
605
+ }{
606
+ {
607
+ desc : "valid number string" ,
608
+ inputStr : "10000" ,
609
+ expInt64 : 1 ,
610
+ expectError : false ,
611
+ },
612
+ {
613
+ desc : "round Ki to MiB" ,
614
+ inputStr : "1000Ki" ,
615
+ expInt64 : 1 ,
616
+ expectError : false ,
617
+ },
618
+ {
619
+ desc : "round k to MiB" ,
620
+ inputStr : "1000k" ,
621
+ expInt64 : 1 ,
622
+ expectError : false ,
623
+ },
624
+ {
625
+ desc : "round Mi to MiB" ,
626
+ inputStr : "1000Mi" ,
627
+ expInt64 : 1000 ,
628
+ expectError : false ,
629
+ },
630
+ {
631
+ desc : "round M to MiB" ,
632
+ inputStr : "1000M" ,
633
+ expInt64 : 954 ,
634
+ expectError : false ,
635
+ },
636
+ {
637
+ desc : "round G to MiB" ,
638
+ inputStr : "1000G" ,
639
+ expInt64 : 953675 ,
640
+ expectError : false ,
641
+ },
642
+ {
643
+ desc : "round Gi to MiB" ,
644
+ inputStr : "10000Gi" ,
645
+ expInt64 : 10240000 ,
646
+ expectError : false ,
647
+ },
648
+ {
649
+ desc : "round decimal to MiB" ,
650
+ inputStr : "1.2Gi" ,
651
+ expInt64 : 1229 ,
652
+ expectError : false ,
653
+ },
654
+ {
655
+ desc : "round big value to MiB" ,
656
+ inputStr : "8191Pi" ,
657
+ expInt64 : 8795019280384 ,
658
+ expectError : false ,
659
+ },
660
+ {
661
+ desc : "invalid empty string" ,
662
+ inputStr : "" ,
663
+ expInt64 : 0 ,
664
+ expectError : true ,
665
+ },
666
+ {
667
+ desc : "invalid KiB string" ,
668
+ inputStr : "10KiB" ,
669
+ expInt64 : 10000 ,
670
+ expectError : true ,
671
+ },
672
+ {
673
+ desc : "invalid GB string" ,
674
+ inputStr : "10GB" ,
675
+ expInt64 : 0 ,
676
+ expectError : true ,
677
+ },
678
+ {
679
+ desc : "invalid string" ,
680
+ inputStr : "ew%65" ,
681
+ expInt64 : 0 ,
682
+ expectError : true ,
683
+ },
684
+ }
685
+ for _ , tc := range tests {
686
+ t .Run (tc .desc , func (t * testing.T ) {
687
+ actualInt64 , err := ConvertMiStringToInt64 (tc .inputStr )
688
+ if err != nil && ! tc .expectError {
689
+ t .Errorf ("Got error %v converting string to int64 %s; expect no error" , err , tc .inputStr )
690
+ }
691
+ if err == nil && tc .expectError {
692
+ t .Errorf ("Got no error converting string to int64 %s; expect an error" , tc .inputStr )
693
+ }
694
+ if err == nil && actualInt64 != tc .expInt64 {
695
+ t .Errorf ("Got %d for converting string to int64; expect %d" , actualInt64 , tc .expInt64 )
696
+ }
697
+ })
698
+ }
699
+ }
700
+
701
+ func TestConvertGiStringToInt64 (t * testing.T ) {
702
+ tests := []struct {
703
+ desc string
704
+ inputStr string
705
+ expInt64 int64
706
+ expectError bool
707
+ }{
708
+ {
709
+ desc : "valid number string" ,
710
+ inputStr : "10000" ,
711
+ expInt64 : 1 ,
712
+ expectError : false ,
713
+ },
714
+ {
715
+ desc : "round Ki to GiB" ,
716
+ inputStr : "1000000Ki" ,
717
+ expInt64 : 1 ,
718
+ expectError : false ,
719
+ },
720
+ {
721
+ desc : "round k to GiB" ,
722
+ inputStr : "1000000k" ,
723
+ expInt64 : 1 ,
724
+ expectError : false ,
725
+ },
726
+ {
727
+ desc : "round Mi to GiB" ,
728
+ inputStr : "1000Mi" ,
729
+ expInt64 : 1 ,
730
+ expectError : false ,
731
+ },
732
+ {
733
+ desc : "round M to GiB" ,
734
+ inputStr : "1000M" ,
735
+ expInt64 : 1 ,
736
+ expectError : false ,
737
+ },
738
+ {
739
+ desc : "round G to GiB" ,
740
+ inputStr : "1000G" ,
741
+ expInt64 : 932 ,
742
+ expectError : false ,
743
+ },
744
+ {
745
+ desc : "round Gi to GiB - most common case" ,
746
+ inputStr : "1234Gi" ,
747
+ expInt64 : 1234 ,
748
+ expectError : false ,
749
+ },
750
+ {
751
+ desc : "round decimal to GiB" ,
752
+ inputStr : "1.2Gi" ,
753
+ expInt64 : 2 ,
754
+ expectError : false ,
755
+ },
756
+ {
757
+ desc : "round big value to GiB" ,
758
+ inputStr : "8191Pi" ,
759
+ expInt64 : 8588886016 ,
760
+ expectError : false ,
761
+ },
762
+ {
763
+ desc : "invalid empty string" ,
764
+ inputStr : "" ,
765
+ expInt64 : 0 ,
766
+ expectError : true ,
767
+ },
768
+ {
769
+ desc : "invalid KiB string" ,
770
+ inputStr : "10KiB" ,
771
+ expInt64 : 10000 ,
772
+ expectError : true ,
773
+ },
774
+ {
775
+ desc : "invalid GB string" ,
776
+ inputStr : "10GB" ,
777
+ expInt64 : 0 ,
778
+ expectError : true ,
779
+ },
780
+ {
781
+ desc : "invalid string" ,
782
+ inputStr : "ew%65" ,
783
+ expInt64 : 0 ,
784
+ expectError : true ,
785
+ },
786
+ }
787
+ for _ , tc := range tests {
788
+ t .Run (tc .desc , func (t * testing.T ) {
789
+ actualInt64 , err := ConvertGiStringToInt64 (tc .inputStr )
790
+ if err != nil && ! tc .expectError {
791
+ t .Errorf ("Got error %v converting string to int64 %s; expect no error" , err , tc .inputStr )
792
+ }
793
+ if err == nil && tc .expectError {
794
+ t .Errorf ("Got no error converting string to int64 %s; expect an error" , tc .inputStr )
795
+ }
796
+ if err == nil && actualInt64 != tc .expInt64 {
797
+ t .Errorf ("Got %d for converting string to int64; expect %d" , actualInt64 , tc .expInt64 )
798
+ }
799
+ })
800
+ }
801
+ }
0 commit comments