github.com/cs3org/reva/v2@v2.27.7/pkg/bytesize/bytesize_test.go (about)

     1  // Copyright 2018-2022 CERN
     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  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package bytesize_test
    20  
    21  import (
    22  	"fmt"
    23  	"testing"
    24  
    25  	"github.com/cs3org/reva/v2/pkg/bytesize"
    26  	"github.com/test-go/testify/require"
    27  )
    28  
    29  func TestParseSpecial(t *testing.T) {
    30  	testCases := []struct {
    31  		Alias          string
    32  		Input          string
    33  		ExpectedOutput uint64
    34  		ExpectError    bool
    35  	}{
    36  		{
    37  			Alias:          "it assumes bytes",
    38  			Input:          "100",
    39  			ExpectedOutput: 100,
    40  		},
    41  		{
    42  			Alias:          "it accepts a space between value and unit",
    43  			Input:          "1 MB",
    44  			ExpectedOutput: 1000000,
    45  		},
    46  		{
    47  			Alias:          "it accepts also more spaces between value and unit",
    48  			Input:          "1                                            MB",
    49  			ExpectedOutput: 1000000,
    50  		},
    51  		{
    52  			Alias:          "it ignores trailing and leading spaces",
    53  			Input:          " 1MB ",
    54  			ExpectedOutput: 1000000,
    55  		},
    56  		{
    57  			Alias:          "it errors on unknown units",
    58  			Input:          "1SB",
    59  			ExpectedOutput: 0,
    60  			ExpectError:    true,
    61  		},
    62  		{
    63  			Alias:          "it multiplies correctly",
    64  			Input:          "16MB",
    65  			ExpectedOutput: 16000000,
    66  		},
    67  		{
    68  			Alias:          "it errors when no value is given",
    69  			Input:          "GB",
    70  			ExpectedOutput: 0,
    71  			ExpectError:    true,
    72  		},
    73  		{
    74  			Alias:          "it errors when bad input is given",
    75  			Input:          ",as!@@delta",
    76  			ExpectedOutput: 0,
    77  			ExpectError:    true,
    78  		},
    79  		{
    80  			Alias:          "it errors when using floats",
    81  			Input:          "1.024GB",
    82  			ExpectedOutput: 0,
    83  			ExpectError:    true,
    84  		},
    85  	}
    86  
    87  	for _, tc := range testCases {
    88  		actual, err := bytesize.Parse(tc.Input)
    89  		if tc.ExpectError {
    90  			require.Error(t, err)
    91  		} else {
    92  			require.NoError(t, err)
    93  		}
    94  
    95  		require.Equal(t, tc.ExpectError, err != nil, tc.Alias)
    96  		require.Equal(t, int(tc.ExpectedOutput), int(actual), tc.Alias)
    97  	}
    98  }
    99  
   100  func TestParseHappy(t *testing.T) {
   101  	testCases := []struct {
   102  		Input    string
   103  		Expected uint64
   104  	}{
   105  		{Input: "1", Expected: 1},
   106  		{Input: "1KB", Expected: 1000},
   107  		{Input: "1MB", Expected: 1000000},
   108  		{Input: "1GB", Expected: 1000000000},
   109  		{Input: "1TB", Expected: 1000000000000},
   110  		{Input: "1PB", Expected: 1000000000000000},
   111  		{Input: "1EB", Expected: 1000000000000000000},
   112  		{Input: "1MiB", Expected: 1048576},
   113  		{Input: "1GiB", Expected: 1073741824},
   114  		{Input: "1TiB", Expected: 1099511627776},
   115  		{Input: "1PiB", Expected: 1125899906842624},
   116  		{Input: "1EiB", Expected: 1152921504606846976},
   117  	}
   118  
   119  	for _, tc := range testCases {
   120  		actual, err := bytesize.Parse(tc.Input)
   121  		require.NoError(t, err)
   122  		require.Equal(t, int(tc.Expected), int(actual), fmt.Sprintf("case %s", tc.Input))
   123  	}
   124  }