pkg/units: Updated tests with unit constants

Also, now that I was at it, gave them a small refactor.

Docker-DCO-1.1-Signed-off-by: Francisco Carriedo <fcarriedo@gmail.com> (github: fcarriedo)
This commit is contained in:
Francisco Carriedo 2014-07-26 19:48:02 -07:00
parent 804ea3b58e
commit 2261fc98e1
2 changed files with 75 additions and 76 deletions

View file

@ -44,9 +44,3 @@ func TestHumanDuration(t *testing.T) {
assertEquals(t, "2.010959 years", HumanDuration(24*month+2*week)) assertEquals(t, "2.010959 years", HumanDuration(24*month+2*week))
assertEquals(t, "3.164384 years", HumanDuration(3*year+2*month)) assertEquals(t, "3.164384 years", HumanDuration(3*year+2*month))
} }
func assertEquals(t *testing.T, expected, actual interface{}) {
if expected != actual {
t.Errorf("Expected '%s' but got '%s'", expected, actual)
}
}

View file

@ -1,6 +1,9 @@
package units package units
import ( import (
"reflect"
"runtime"
"strings"
"testing" "testing"
) )
@ -9,85 +12,87 @@ func TestHumanSize(t *testing.T) {
assertEquals(t, "1.024 kB", HumanSize(1024)) assertEquals(t, "1.024 kB", HumanSize(1024))
assertEquals(t, "1 MB", HumanSize(1000000)) assertEquals(t, "1 MB", HumanSize(1000000))
assertEquals(t, "1.049 MB", HumanSize(1048576)) assertEquals(t, "1.049 MB", HumanSize(1048576))
assertEquals(t, "2 MB", HumanSize(2*1000*1000)) assertEquals(t, "2 MB", HumanSize(2*MB))
assertEquals(t, "3.42 GB", HumanSize(3.42*1000*1000*1000)) assertEquals(t, "3.42 GB", HumanSize(3.42*GB))
assertEquals(t, "5.372 TB", HumanSize(5.372*1000*1000*1000*1000)) assertEquals(t, "5.372 TB", HumanSize(5.372*TB))
assertEquals(t, "2.22 PB", HumanSize(2.22*1000*1000*1000*1000*1000)) assertEquals(t, "2.22 PB", HumanSize(2.22*PB))
assertEquals(t, "2.22 EB", HumanSize(2.22*1000*1000*1000*1000*1000*1000))
assertEquals(t, "7.707 EB", HumanSize(7.707*1000*1000*1000*1000*1000*1000))
} }
func TestFromHumanSize(t *testing.T) { func TestFromHumanSize(t *testing.T) {
assertFromHumanSize(t, "32", false, 32) assertSuccessEquals(t, 32, FromHumanSize, "32")
assertFromHumanSize(t, "32b", false, 32) assertSuccessEquals(t, 32, FromHumanSize, "32b")
assertFromHumanSize(t, "32B", false, 32) assertSuccessEquals(t, 32, FromHumanSize, "32B")
assertFromHumanSize(t, "32k", false, 32*1000) assertSuccessEquals(t, 32*KB, FromHumanSize, "32k")
assertFromHumanSize(t, "32K", false, 32*1000) assertSuccessEquals(t, 32*KB, FromHumanSize, "32K")
assertFromHumanSize(t, "32kb", false, 32*1000) assertSuccessEquals(t, 32*KB, FromHumanSize, "32kb")
assertFromHumanSize(t, "32Kb", false, 32*1000) assertSuccessEquals(t, 32*KB, FromHumanSize, "32Kb")
assertFromHumanSize(t, "32Mb", false, 32*1000*1000) assertSuccessEquals(t, 32*MB, FromHumanSize, "32Mb")
assertFromHumanSize(t, "32Gb", false, 32*1000*1000*1000) assertSuccessEquals(t, 32*GB, FromHumanSize, "32Gb")
assertFromHumanSize(t, "32Tb", false, 32*1000*1000*1000*1000) assertSuccessEquals(t, 32*TB, FromHumanSize, "32Tb")
assertFromHumanSize(t, "8Pb", false, 8*1000*1000*1000*1000*1000) assertSuccessEquals(t, 32*PB, FromHumanSize, "32Pb")
assertFromHumanSize(t, "", true, -1) assertError(t, FromHumanSize, "")
assertFromHumanSize(t, "hello", true, -1) assertError(t, FromHumanSize, "hello")
assertFromHumanSize(t, "-32", true, -1) assertError(t, FromHumanSize, "-32")
assertFromHumanSize(t, " 32 ", true, -1) assertError(t, FromHumanSize, "32.3")
assertFromHumanSize(t, "32 mb", true, -1) assertError(t, FromHumanSize, " 32 ")
assertFromHumanSize(t, "32m b", true, -1) assertError(t, FromHumanSize, "32.3Kb")
assertFromHumanSize(t, "32bm", true, -1) assertError(t, FromHumanSize, "32 mb")
} assertError(t, FromHumanSize, "32m b")
assertError(t, FromHumanSize, "32bm")
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) { func TestRAMInBytes(t *testing.T) {
assertRAMInBytes(t, "32", false, 32) assertSuccessEquals(t, 32, RAMInBytes, "32")
assertRAMInBytes(t, "32b", false, 32) assertSuccessEquals(t, 32, RAMInBytes, "32b")
assertRAMInBytes(t, "32B", false, 32) assertSuccessEquals(t, 32, RAMInBytes, "32B")
assertRAMInBytes(t, "32k", false, 32*1024) assertSuccessEquals(t, 32*KiB, RAMInBytes, "32k")
assertRAMInBytes(t, "32K", false, 32*1024) assertSuccessEquals(t, 32*KiB, RAMInBytes, "32K")
assertRAMInBytes(t, "32kb", false, 32*1024) assertSuccessEquals(t, 32*KiB, RAMInBytes, "32kb")
assertRAMInBytes(t, "32Kb", false, 32*1024) assertSuccessEquals(t, 32*KiB, RAMInBytes, "32Kb")
assertRAMInBytes(t, "32Mb", false, 32*1024*1024) assertSuccessEquals(t, 32*MiB, RAMInBytes, "32Mb")
assertRAMInBytes(t, "32MB", false, 32*1024*1024) assertSuccessEquals(t, 32*GiB, RAMInBytes, "32Gb")
assertRAMInBytes(t, "32Gb", false, 32*1024*1024*1024) assertSuccessEquals(t, 32*TiB, RAMInBytes, "32Tb")
assertRAMInBytes(t, "32G", false, 32*1024*1024*1024) assertSuccessEquals(t, 32*PiB, RAMInBytes, "32Pb")
assertRAMInBytes(t, "32GB", false, 32*1024*1024*1024) assertSuccessEquals(t, 32*PiB, RAMInBytes, "32PB")
assertRAMInBytes(t, "32Tb", false, 32*1024*1024*1024*1024) assertSuccessEquals(t, 32*PiB, RAMInBytes, "32P")
assertRAMInBytes(t, "8Pb", false, 8*1024*1024*1024*1024*1024)
assertRAMInBytes(t, "8PB", false, 8*1024*1024*1024*1024*1024)
assertRAMInBytes(t, "8P", false, 8*1024*1024*1024*1024*1024)
assertRAMInBytes(t, "", true, -1) assertError(t, RAMInBytes, "")
assertRAMInBytes(t, "hello", true, -1) assertError(t, RAMInBytes, "hello")
assertRAMInBytes(t, "-32", true, -1) assertError(t, RAMInBytes, "-32")
assertRAMInBytes(t, " 32 ", true, -1) assertError(t, RAMInBytes, "32.3")
assertRAMInBytes(t, "32 mb", true, -1) assertError(t, RAMInBytes, " 32 ")
assertRAMInBytes(t, "32m b", true, -1) assertError(t, RAMInBytes, "32.3Kb")
assertRAMInBytes(t, "32bm", true, -1) assertError(t, RAMInBytes, "32 mb")
assertError(t, RAMInBytes, "32m b")
assertError(t, RAMInBytes, "32bm")
} }
func assertRAMInBytes(t *testing.T, size string, expectError bool, expectedBytes int64) { func assertEquals(t *testing.T, expected, actual interface{}) {
actualBytes, err := RAMInBytes(size) if expected != actual {
if (err != nil) && !expectError { t.Errorf("Expected '%v' but got '%v'", expected, actual)
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) // func that maps to the parse function signatures as testing abstraction
} type parseFn func(string) (int64, error)
if actualBytes != expectedBytes {
t.Errorf("Expected '%s' to parse as %d bytes, got %d", size, expectedBytes, actualBytes) // Define 'String()' for pretty-print
func (fn parseFn) String() string {
fnName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
return fnName[strings.LastIndex(fnName, ".")+1:]
}
func assertSuccessEquals(t *testing.T, expected int64, fn parseFn, arg string) {
res, err := fn(arg)
if err != nil || res != expected {
t.Errorf("%s(\"%s\") -> expected '%d' but got '%d' with error '%v'", fn, arg, expected, res, err)
}
}
func assertError(t *testing.T, fn parseFn, arg string) {
res, err := fn(arg)
if err == nil && res != -1 {
t.Errorf("%s(\"%s\") -> expected error but got '%d'", fn, arg, res)
} }
} }