Skip to content

Commit

Permalink
Add Bytes and IBytes functions in the unit package (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstmdev committed Jan 18, 2024
1 parent 517995c commit aabd286
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 29 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.0.2
v0.0.3
29 changes: 24 additions & 5 deletions unit/unit.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
package unit

import "github.com/dustin/go-humanize"
import (
"strings"

"github.com/dustin/go-humanize"
)

// ParseBytes parse the string to bytes
func ParseBytes(s string) (int, error) {
b, err := humanize.ParseBytes(s)
func ParseBytes(s string) (bytes uint64, iec bool, err error) {
bytes, err = humanize.ParseBytes(s)
if err != nil {
return 0, err
return bytes, iec, err
}
return int(b), nil
iec = isIEC(s)
return bytes, iec, nil
}

// Bytes produces a human readable representation of an SI size
func Bytes(b uint64) string {
return humanize.Bytes(b)
}

// IBytes produces a human readable representation of an IEC size
func IBytes(b uint64) string {
return humanize.IBytes(b)
}

func isIEC(s string) bool {
return strings.ContainsRune(strings.ToLower(s), 'i')
}
124 changes: 101 additions & 23 deletions unit/unit_test.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,72 @@
package unit

import "testing"
import (
"testing"
)

func TestParseBytes(t *testing.T) {
testCases := []struct {
s string
v int
s string
v uint64
iec bool
}{
{"1B", 1},
{"1MB", 1000000},
{"1GB", 1000000000},
{"1TB", 1000000000000},
{"1b", 1},
{"1mb", 1000000},
{"1gb", 1000000000},
{"1tb", 1000000000000},
{"1MIB", 1048576},
{"1GIB", 1073741824},
{"1TIB", 1099511627776},
{"1mib", 1048576},
{"1gib", 1073741824},
{"1tib", 1099511627776},
{"1", 1},
{"1000000", 1000000},
{"1,048,576", 1048576},
{"1048576", 1048576},
{"1B", 1, false},
{"1KB", 1000, false},
{"1MB", 1000000, false},
{"1GB", 1000000000, false},
{"1TB", 1000000000000, false},
{"1PB", 1000000000000000, false},
{"1EB", 1000000000000000000, false},
{"1b", 1, false},
{"1kb", 1000, false},
{"1mb", 1000000, false},
{"1gb", 1000000000, false},
{"1tb", 1000000000000, false},
{"1pb", 1000000000000000, false},
{"1eb", 1000000000000000000, false},
{"1K", 1000, false},
{"1M", 1000000, false},
{"1G", 1000000000, false},
{"1T", 1000000000000, false},
{"1P", 1000000000000000, false},
{"1E", 1000000000000000000, false},
{"1KIB", 1024, true},
{"1MIB", 1048576, true},
{"1GIB", 1073741824, true},
{"1TIB", 1099511627776, true},
{"1PIB", 1125899906842624, true},
{"1EIB", 1152921504606846976, true},
{"1kib", 1024, true},
{"1mib", 1048576, true},
{"1gib", 1073741824, true},
{"1tib", 1099511627776, true},
{"1pib", 1125899906842624, true},
{"1eib", 1152921504606846976, true},
{"1Ki", 1024, true},
{"1Mi", 1048576, true},
{"1Gi", 1073741824, true},
{"1Ti", 1099511627776, true},
{"1Pi", 1125899906842624, true},
{"1Ei", 1152921504606846976, true},
{"1", 1, false},
{"1000000", 1000000, false},
{"1,048,576", 1048576, false},
{"1048576", 1048576, false},
}
for _, tc := range testCases {
t.Run(tc.s, func(t *testing.T) {
bytes, err := ParseBytes(tc.s)
bytes, iec, err := ParseBytes(tc.s)
if err != nil {
t.Errorf("parse bytes error => %v", err)
return
}
if bytes != tc.v {
t.Errorf("expect %d, but get %d", tc.v, bytes)
return
}
if iec != tc.iec {
t.Errorf("IEC expect %v, but get %v", tc.iec, iec)
return
}
})
}
Expand All @@ -50,10 +82,56 @@ func TestParseBytesReturnError(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.s, func(t *testing.T) {
_, err := ParseBytes(tc.s)
_, _, err := ParseBytes(tc.s)
if err == nil {
t.Errorf("expect get an error, but get nil")
}
})
}
}

func TestBytes(t *testing.T) {
testCases := []struct {
s string
v uint64
}{
{"10 B", 10},
{"10 kB", 10000},
{"10 MB", 10000000},
{"10 GB", 10000000000},
{"10 TB", 10000000000000},
{"10 PB", 10000000000000000},
{"10 EB", 10000000000000000000},
}
for _, tc := range testCases {
t.Run(tc.s, func(t *testing.T) {
s := Bytes(tc.v)
if s != tc.s {
t.Errorf("expect %s, but get %s", tc.s, s)
}
})
}
}

func TestIBytes(t *testing.T) {
testCases := []struct {
s string
v uint64
}{
{"10 B", 10},
{"10 KiB", 10240},
{"10 MiB", 10485760},
{"10 GiB", 10737418240},
{"10 TiB", 10995116277760},
{"10 PiB", 11258999068426240},
{"10 EiB", 11529215046068469760},
}
for _, tc := range testCases {
t.Run(tc.s, func(t *testing.T) {
s := IBytes(tc.v)
if s != tc.s {
t.Errorf("expect %s, but get %s", tc.s, s)
}
})
}
}

0 comments on commit aabd286

Please sign in to comment.