diff options
author | Wolfy-J <[email protected]> | 2018-06-10 15:25:20 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-06-10 15:25:20 +0300 |
commit | ee2e495a76c1cb0666f6f210ed0e59f94314b9e5 (patch) | |
tree | 5484792ff964cca69bb282d6ddabf323f3d57571 /cmd | |
parent | 3eb8396c501bc8b1048273e6ca88c91d3426b744 (diff) |
tests
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/rr/utils/size.go | 28 | ||||
-rw-r--r-- | cmd/rr/utils/utils_test.go | 12 |
2 files changed, 40 insertions, 0 deletions
diff --git a/cmd/rr/utils/size.go b/cmd/rr/utils/size.go new file mode 100644 index 00000000..176cc9e1 --- /dev/null +++ b/cmd/rr/utils/size.go @@ -0,0 +1,28 @@ +package utils + +import ( + "strconv" + "strings" +) + +func ParseSize(size string) int64 { + if len(size) == 0 { + return 0 + } + + s, err := strconv.Atoi(size[:len(size)-1]) + if err != nil { + return 0 + } + + switch strings.ToLower(size[len(size)-1:]) { + case "k", "kb": + return int64(s * 1024) + case "m", "mb": + return int64(s * 1024 * 1024) + case "g", "gb": + return int64(s * 1024 * 1024 * 1024) + } + + return 0 +} diff --git a/cmd/rr/utils/utils_test.go b/cmd/rr/utils/utils_test.go new file mode 100644 index 00000000..f67b2a10 --- /dev/null +++ b/cmd/rr/utils/utils_test.go @@ -0,0 +1,12 @@ +package utils + +import ( + "testing" + "github.com/magiconair/properties/assert" +) + +func TestUtils(t *testing.T) { + assert.Equal(t, int64(1024), ParseSize("1K")) + assert.Equal(t, int64(1024*1024), ParseSize("1M")) + assert.Equal(t, int64(2*1024*1024*1024), ParseSize("2G")) +} |