github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-bucket-remote-add_test.go (about)

     1  // Copyright (c) 2015-2022 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package cmd
    19  
    20  import (
    21  	"testing"
    22  )
    23  
    24  func TestGetBandwidthInBytes(t *testing.T) {
    25  	type args struct {
    26  		bandwidthStr string
    27  	}
    28  	f1 := 999.123457 * 1024 * 1024 / 8
    29  	f2 := 10.123456790 * 1024 * 1024 * 1024 / 8
    30  	f3 := 10000.123456790 * 1024 * 1024 * 1024 / 8
    31  	f4 := (0.001*1024*1024*1024 + 1) / 8 // round up
    32  	tests := []struct {
    33  		name string
    34  		args args
    35  		want uint64
    36  	}{
    37  		{
    38  			name: "1MegaByte",
    39  			args: args{
    40  				bandwidthStr: "1Mi",
    41  			},
    42  			want: 1024 * 1024 / 8,
    43  		},
    44  		{
    45  			name: "1MegaBit",
    46  			args: args{
    47  				bandwidthStr: "1M",
    48  			},
    49  			want: 1000000 / 8,
    50  		},
    51  		{
    52  			name: "1GigaBit",
    53  			args: args{
    54  				bandwidthStr: "1G",
    55  			},
    56  			want: 1000000000 / 8,
    57  		},
    58  		{
    59  			name: "1GigaByte",
    60  			args: args{
    61  				bandwidthStr: "1Gi",
    62  			},
    63  			want: 1024 * 1024 * 1024 / 8,
    64  		},
    65  		{
    66  			name: "FractionalMegaBits",
    67  			args: args{
    68  				bandwidthStr: "999.123456789123456789M",
    69  			},
    70  			want: 999123457 / 8,
    71  		},
    72  		{
    73  			name: "FractionalGigaBits",
    74  			args: args{
    75  				bandwidthStr: "10.123456789123456789123456G",
    76  			},
    77  			want: 10123456789 / 8,
    78  		},
    79  		{
    80  			name: "FractionalBigGigaBits",
    81  			args: args{
    82  				bandwidthStr: "10000.123456789123456789123456G",
    83  			},
    84  			want: 10000123456789 / 8,
    85  		},
    86  		{
    87  			name: "FractionalMegaBytes",
    88  			args: args{
    89  				bandwidthStr: "999.123456789123456789Mi",
    90  			},
    91  			want: uint64(f1),
    92  		},
    93  		{
    94  			name: "FractionalGigaBytes",
    95  			args: args{
    96  				bandwidthStr: "10.123456789123456789123456Gi",
    97  			},
    98  			want: uint64(f2),
    99  		},
   100  		{
   101  			name: "FractionalBigGigaBytes",
   102  			args: args{
   103  				bandwidthStr: "10000.123456789123456789123456Gi",
   104  			},
   105  			want: uint64(f3),
   106  		},
   107  		{
   108  			name: "SmallGiga",
   109  			args: args{
   110  				bandwidthStr: "0.001Gi",
   111  			},
   112  			want: uint64(f4),
   113  		},
   114  		{
   115  			name: "LargeK",
   116  			args: args{
   117  				bandwidthStr: "1024Ki",
   118  			},
   119  			want: 1024 * 1024 / 8,
   120  		},
   121  	}
   122  	t.Parallel()
   123  	for _, tt := range tests {
   124  		tt := tt
   125  		t.Run(tt.name, func(t *testing.T) {
   126  			if got, err := getBandwidthInBytes(tt.args.bandwidthStr); err != nil || got != tt.want {
   127  				t.Errorf("getBandwidthInBytes() = %v, want %v", got, tt.want)
   128  			}
   129  		})
   130  	}
   131  }