pkg/units: Add FromHumanSize
This does the "reverse" of HumanSize, i.e. maps a string to an int64 using SI prefixes for the extension. Docker-DCO-1.1-Signed-off-by: Alexander Larsson <alexl@redhat.com> (github: alexlarsson)
This commit is contained in:
parent
cd85dac87e
commit
c49c2a7df5
2 changed files with 71 additions and 0 deletions
|
@ -20,6 +20,41 @@ func TestHumanSize(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestFromHumanSize(t *testing.T) {
|
||||
assertFromHumanSize(t, "32", false, 32)
|
||||
assertFromHumanSize(t, "32b", false, 32)
|
||||
assertFromHumanSize(t, "32B", false, 32)
|
||||
assertFromHumanSize(t, "32k", false, 32*1000)
|
||||
assertFromHumanSize(t, "32K", false, 32*1000)
|
||||
assertFromHumanSize(t, "32kb", false, 32*1000)
|
||||
assertFromHumanSize(t, "32Kb", false, 32*1000)
|
||||
assertFromHumanSize(t, "32Mb", false, 32*1000*1000)
|
||||
assertFromHumanSize(t, "32Gb", false, 32*1000*1000*1000)
|
||||
assertFromHumanSize(t, "32Tb", false, 32*1000*1000*1000*1000)
|
||||
assertFromHumanSize(t, "8Pb", false, 8*1000*1000*1000*1000*1000)
|
||||
|
||||
assertFromHumanSize(t, "", true, -1)
|
||||
assertFromHumanSize(t, "hello", true, -1)
|
||||
assertFromHumanSize(t, "-32", true, -1)
|
||||
assertFromHumanSize(t, " 32 ", true, -1)
|
||||
assertFromHumanSize(t, "32 mb", true, -1)
|
||||
assertFromHumanSize(t, "32m b", true, -1)
|
||||
assertFromHumanSize(t, "32bm", true, -1)
|
||||
}
|
||||
|
||||
func assertFromHumanSize(t *testing.T, size string, expectError bool, expectedBytes int64) {
|
||||
actualBytes, err := FromHumanSize(size)
|
||||
if (err != nil) && !expectError {
|
||||
t.Errorf("Unexpected error parsing '%s': %s", size, err)
|
||||
}
|
||||
if (err == nil) && expectError {
|
||||
t.Errorf("Expected to get an error parsing '%s', but got none (bytes=%d)", size, actualBytes)
|
||||
}
|
||||
if actualBytes != expectedBytes {
|
||||
t.Errorf("Expected '%s' to parse as %d bytes, got %d", size, expectedBytes, actualBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRAMInBytes(t *testing.T) {
|
||||
assertRAMInBytes(t, "32", false, 32)
|
||||
assertRAMInBytes(t, "32b", false, 32)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue