@@ -632,83 +632,113 @@ func TestParseMachineType(t *testing.T) {
632
632
}
633
633
}
634
634
635
- func TestConvertGiBStringToInt64 (t * testing.T ) {
635
+ func TestConvertStringToInt64 (t * testing.T ) {
636
636
tests := []struct {
637
637
desc string
638
638
inputStr string
639
639
expInt64 int64
640
640
expectError bool
641
641
}{
642
642
{
643
- "valid number string" ,
644
- "10000" ,
645
- 1 ,
646
- false ,
643
+ desc : "valid number string" ,
644
+ inputStr : "10000" ,
645
+ expInt64 : 10000 ,
646
+ expectError : false ,
647
647
},
648
648
{
649
- "round Ki to GiB " ,
650
- "1000Ki " ,
651
- 1 ,
652
- false ,
649
+ desc : "round M to number " ,
650
+ inputStr : "1M " ,
651
+ expInt64 : 1000000 ,
652
+ expectError : false ,
653
653
},
654
654
{
655
- "round k to GiB " ,
656
- "1000k " ,
657
- 1 ,
658
- false ,
655
+ desc : "round m to number " ,
656
+ inputStr : "1m " ,
657
+ expInt64 : 1 ,
658
+ expectError : false ,
659
659
},
660
660
{
661
- "round Mi to GiB " ,
662
- "1000Mi " ,
663
- 1 ,
664
- false ,
661
+ desc : "round k to number " ,
662
+ inputStr : "1k " ,
663
+ expInt64 : 1000 ,
664
+ expectError : false ,
665
665
},
666
666
{
667
- "round M to GiB " ,
668
- "1000M " ,
669
- 1 ,
670
- false ,
667
+ desc : "invalid empty string " ,
668
+ inputStr : " " ,
669
+ expInt64 : 0 ,
670
+ expectError : true ,
671
671
},
672
672
{
673
- "round G to GiB " ,
674
- "1000G " ,
675
- 932 ,
676
- false ,
673
+ desc : "invalid string " ,
674
+ inputStr : "ew%65 " ,
675
+ expInt64 : 0 ,
676
+ expectError : true ,
677
677
},
678
678
{
679
- "round Gi to GiB " ,
680
- "10000Gi " ,
681
- 10000 ,
682
- false ,
679
+ desc : "invalid KiB string " ,
680
+ inputStr : "10KiB " ,
681
+ expInt64 : 10000 ,
682
+ expectError : true ,
683
683
},
684
684
{
685
- "round decimal to GiB " ,
686
- "1.2Gi " ,
687
- 2 ,
688
- false ,
685
+ desc : "invalid GB string " ,
686
+ inputStr : "10GB " ,
687
+ expInt64 : 0 ,
688
+ expectError : true ,
689
689
},
690
690
{
691
- "round big value to GiB " ,
692
- "8191Pi " ,
693
- 8588886016 ,
694
- false ,
691
+ desc : "round Ki to number " ,
692
+ inputStr : "1Ki " ,
693
+ expInt64 : 1024 ,
694
+ expectError : false ,
695
695
},
696
696
{
697
- "invalid empty string " ,
698
- " " ,
699
- 10000 ,
700
- true ,
697
+ desc : "round k to number " ,
698
+ inputStr : "10k " ,
699
+ expInt64 : 10000 ,
700
+ expectError : false ,
701
701
},
702
702
{
703
- "invalid string" ,
704
- "ew%65" ,
705
- 10000 ,
706
- true ,
703
+ desc : "round Mi to number" ,
704
+ inputStr : "10Mi" ,
705
+ expInt64 : 10485760 ,
706
+ expectError : false ,
707
+ },
708
+ {
709
+ desc : "round M to number" ,
710
+ inputStr : "10M" ,
711
+ expInt64 : 10000000 ,
712
+ expectError : false ,
713
+ },
714
+ {
715
+ desc : "round G to number" ,
716
+ inputStr : "10G" ,
717
+ expInt64 : 10000000000 ,
718
+ expectError : false ,
719
+ },
720
+ {
721
+ desc : "round Gi to number" ,
722
+ inputStr : "100Gi" ,
723
+ expInt64 : 107374182400 ,
724
+ expectError : false ,
725
+ },
726
+ {
727
+ desc : "round decimal to number" ,
728
+ inputStr : "1.2Gi" ,
729
+ expInt64 : 1288490189 ,
730
+ expectError : false ,
731
+ },
732
+ {
733
+ desc : "round big value to number" ,
734
+ inputStr : "8191Pi" ,
735
+ expInt64 : 9222246136947933184 ,
736
+ expectError : false ,
707
737
},
708
738
}
709
739
for _ , tc := range tests {
710
740
t .Run (tc .desc , func (t * testing.T ) {
711
- actualInt64 , err := ConvertGiBStringToInt64 (tc .inputStr )
741
+ actualInt64 , err := ConvertStringToInt64 (tc .inputStr )
712
742
if err != nil && ! tc .expectError {
713
743
t .Errorf ("Got error %v converting string to int64 %s; expect no error" , err , tc .inputStr )
714
744
}
@@ -722,83 +752,95 @@ func TestConvertGiBStringToInt64(t *testing.T) {
722
752
}
723
753
}
724
754
725
- func TestConvertMiBStringToInt64 (t * testing.T ) {
755
+ func TestConvertMiStringToInt64 (t * testing.T ) {
726
756
tests := []struct {
727
757
desc string
728
758
inputStr string
729
759
expInt64 int64
730
760
expectError bool
731
761
}{
732
762
{
733
- "valid number string" ,
734
- "10000" ,
735
- 1 ,
736
- false ,
763
+ desc : "valid number string" ,
764
+ inputStr : "10000" ,
765
+ expInt64 : 1 ,
766
+ expectError : false ,
737
767
},
738
768
{
739
- "round Ki to MiB" ,
740
- "1000Ki" ,
741
- 1 ,
742
- false ,
769
+ desc : "round Ki to MiB" ,
770
+ inputStr : "1000Ki" ,
771
+ expInt64 : 1 ,
772
+ expectError : false ,
743
773
},
744
774
{
745
- "round k to MiB" ,
746
- "1000k" ,
747
- 1 ,
748
- false ,
775
+ desc : "round k to MiB" ,
776
+ inputStr : "1000k" ,
777
+ expInt64 : 1 ,
778
+ expectError : false ,
749
779
},
750
780
{
751
- "round Mi to MiB" ,
752
- "1000Mi" ,
753
- 1000 ,
754
- false ,
781
+ desc : "round Mi to MiB" ,
782
+ inputStr : "1000Mi" ,
783
+ expInt64 : 1000 ,
784
+ expectError : false ,
755
785
},
756
786
{
757
- "round M to MiB" ,
758
- "1000M" ,
759
- 954 ,
760
- false ,
787
+ desc : "round M to MiB" ,
788
+ inputStr : "1000M" ,
789
+ expInt64 : 954 ,
790
+ expectError : false ,
761
791
},
762
792
{
763
- "round G to MiB" ,
764
- "1000G" ,
765
- 953675 ,
766
- false ,
793
+ desc : "round G to MiB" ,
794
+ inputStr : "1000G" ,
795
+ expInt64 : 953675 ,
796
+ expectError : false ,
767
797
},
768
798
{
769
- "round Gi to MiB" ,
770
- "10000Gi" ,
771
- 10240000 ,
772
- false ,
799
+ desc : "round Gi to MiB" ,
800
+ inputStr : "10000Gi" ,
801
+ expInt64 : 10240000 ,
802
+ expectError : false ,
773
803
},
774
804
{
775
- "round decimal to MiB" ,
776
- "1.2Gi" ,
777
- 1229 ,
778
- false ,
805
+ desc : "round decimal to MiB" ,
806
+ inputStr : "1.2Gi" ,
807
+ expInt64 : 1229 ,
808
+ expectError : false ,
779
809
},
780
810
{
781
- "round big value to MiB" ,
782
- "8191Pi" ,
783
- 8795019280384 ,
784
- false ,
811
+ desc : "round big value to MiB" ,
812
+ inputStr : "8191Pi" ,
813
+ expInt64 : 8795019280384 ,
814
+ expectError : false ,
785
815
},
786
816
{
787
- "invalid empty string" ,
788
- "" ,
789
- 10000 ,
790
- true ,
817
+ desc : "invalid empty string" ,
818
+ inputStr : "" ,
819
+ expInt64 : 0 ,
820
+ expectError : true ,
791
821
},
792
822
{
793
- "invalid string" ,
794
- "ew%65" ,
795
- 10000 ,
796
- true ,
823
+ desc : "invalid KiB string" ,
824
+ inputStr : "10KiB" ,
825
+ expInt64 : 10000 ,
826
+ expectError : true ,
827
+ },
828
+ {
829
+ desc : "invalid GB string" ,
830
+ inputStr : "10GB" ,
831
+ expInt64 : 0 ,
832
+ expectError : true ,
833
+ },
834
+ {
835
+ desc : "invalid string" ,
836
+ inputStr : "ew%65" ,
837
+ expInt64 : 0 ,
838
+ expectError : true ,
797
839
},
798
840
}
799
841
for _ , tc := range tests {
800
842
t .Run (tc .desc , func (t * testing.T ) {
801
- actualInt64 , err := ConvertMiBStringToInt64 (tc .inputStr )
843
+ actualInt64 , err := ConvertMiStringToInt64 (tc .inputStr )
802
844
if err != nil && ! tc .expectError {
803
845
t .Errorf ("Got error %v converting string to int64 %s; expect no error" , err , tc .inputStr )
804
846
}
0 commit comments