github.com/lmars/docker@v1.6.0-rc2/pkg/units/size_test.go (about)

     1  package units
     2  
     3  import (
     4  	"reflect"
     5  	"runtime"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestBytesSize(t *testing.T) {
    11  	assertEquals(t, "1 KiB", BytesSize(1024))
    12  	assertEquals(t, "1 MiB", BytesSize(1024*1024))
    13  	assertEquals(t, "1 MiB", BytesSize(1048576))
    14  	assertEquals(t, "2 MiB", BytesSize(2*MiB))
    15  	assertEquals(t, "3.42 GiB", BytesSize(3.42*GiB))
    16  	assertEquals(t, "5.372 TiB", BytesSize(5.372*TiB))
    17  	assertEquals(t, "2.22 PiB", BytesSize(2.22*PiB))
    18  }
    19  
    20  func TestHumanSize(t *testing.T) {
    21  	assertEquals(t, "1 kB", HumanSize(1000))
    22  	assertEquals(t, "1.024 kB", HumanSize(1024))
    23  	assertEquals(t, "1 MB", HumanSize(1000000))
    24  	assertEquals(t, "1.049 MB", HumanSize(1048576))
    25  	assertEquals(t, "2 MB", HumanSize(2*MB))
    26  	assertEquals(t, "3.42 GB", HumanSize(float64(3.42*GB)))
    27  	assertEquals(t, "5.372 TB", HumanSize(float64(5.372*TB)))
    28  	assertEquals(t, "2.22 PB", HumanSize(float64(2.22*PB)))
    29  }
    30  
    31  func TestFromHumanSize(t *testing.T) {
    32  	assertSuccessEquals(t, 32, FromHumanSize, "32")
    33  	assertSuccessEquals(t, 32, FromHumanSize, "32b")
    34  	assertSuccessEquals(t, 32, FromHumanSize, "32B")
    35  	assertSuccessEquals(t, 32*KB, FromHumanSize, "32k")
    36  	assertSuccessEquals(t, 32*KB, FromHumanSize, "32K")
    37  	assertSuccessEquals(t, 32*KB, FromHumanSize, "32kb")
    38  	assertSuccessEquals(t, 32*KB, FromHumanSize, "32Kb")
    39  	assertSuccessEquals(t, 32*MB, FromHumanSize, "32Mb")
    40  	assertSuccessEquals(t, 32*GB, FromHumanSize, "32Gb")
    41  	assertSuccessEquals(t, 32*TB, FromHumanSize, "32Tb")
    42  	assertSuccessEquals(t, 32*PB, FromHumanSize, "32Pb")
    43  
    44  	assertError(t, FromHumanSize, "")
    45  	assertError(t, FromHumanSize, "hello")
    46  	assertError(t, FromHumanSize, "-32")
    47  	assertError(t, FromHumanSize, "32.3")
    48  	assertError(t, FromHumanSize, " 32 ")
    49  	assertError(t, FromHumanSize, "32.3Kb")
    50  	assertError(t, FromHumanSize, "32 mb")
    51  	assertError(t, FromHumanSize, "32m b")
    52  	assertError(t, FromHumanSize, "32bm")
    53  }
    54  
    55  func TestRAMInBytes(t *testing.T) {
    56  	assertSuccessEquals(t, 32, RAMInBytes, "32")
    57  	assertSuccessEquals(t, 32, RAMInBytes, "32b")
    58  	assertSuccessEquals(t, 32, RAMInBytes, "32B")
    59  	assertSuccessEquals(t, 32*KiB, RAMInBytes, "32k")
    60  	assertSuccessEquals(t, 32*KiB, RAMInBytes, "32K")
    61  	assertSuccessEquals(t, 32*KiB, RAMInBytes, "32kb")
    62  	assertSuccessEquals(t, 32*KiB, RAMInBytes, "32Kb")
    63  	assertSuccessEquals(t, 32*MiB, RAMInBytes, "32Mb")
    64  	assertSuccessEquals(t, 32*GiB, RAMInBytes, "32Gb")
    65  	assertSuccessEquals(t, 32*TiB, RAMInBytes, "32Tb")
    66  	assertSuccessEquals(t, 32*PiB, RAMInBytes, "32Pb")
    67  	assertSuccessEquals(t, 32*PiB, RAMInBytes, "32PB")
    68  	assertSuccessEquals(t, 32*PiB, RAMInBytes, "32P")
    69  
    70  	assertError(t, RAMInBytes, "")
    71  	assertError(t, RAMInBytes, "hello")
    72  	assertError(t, RAMInBytes, "-32")
    73  	assertError(t, RAMInBytes, "32.3")
    74  	assertError(t, RAMInBytes, " 32 ")
    75  	assertError(t, RAMInBytes, "32.3Kb")
    76  	assertError(t, RAMInBytes, "32 mb")
    77  	assertError(t, RAMInBytes, "32m b")
    78  	assertError(t, RAMInBytes, "32bm")
    79  }
    80  
    81  func assertEquals(t *testing.T, expected, actual interface{}) {
    82  	if expected != actual {
    83  		t.Errorf("Expected '%v' but got '%v'", expected, actual)
    84  	}
    85  }
    86  
    87  // func that maps to the parse function signatures as testing abstraction
    88  type parseFn func(string) (int64, error)
    89  
    90  // Define 'String()' for pretty-print
    91  func (fn parseFn) String() string {
    92  	fnName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
    93  	return fnName[strings.LastIndex(fnName, ".")+1:]
    94  }
    95  
    96  func assertSuccessEquals(t *testing.T, expected int64, fn parseFn, arg string) {
    97  	res, err := fn(arg)
    98  	if err != nil || res != expected {
    99  		t.Errorf("%s(\"%s\") -> expected '%d' but got '%d' with error '%v'", fn, arg, expected, res, err)
   100  	}
   101  }
   102  
   103  func assertError(t *testing.T, fn parseFn, arg string) {
   104  	res, err := fn(arg)
   105  	if err == nil && res != -1 {
   106  		t.Errorf("%s(\"%s\") -> expected error but got '%d'", fn, arg, res)
   107  	}
   108  }