forked from docker-archive/runc
/
units_test.go
63 lines (55 loc) · 1.77 KB
/
units_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package units
import (
"reflect"
"runtime"
"strings"
"testing"
)
func TestRAMInBytes(t *testing.T) {
assertEqual(t, RAMInBytes, "-1", -1)
assertEqual(t, RAMInBytes, "32", 32)
assertEqual(t, RAMInBytes, "32b", 32)
assertEqual(t, RAMInBytes, "32B", 32)
assertEqual(t, RAMInBytes, "32k", 32*kb)
assertEqual(t, RAMInBytes, "32K", 32*kb)
assertEqual(t, RAMInBytes, "32kb", 32*kb)
assertEqual(t, RAMInBytes, "32Kb", 32*kb)
assertEqual(t, RAMInBytes, "32Kib", 32*kb)
assertEqual(t, RAMInBytes, "32KIB", 32*kb)
assertEqual(t, RAMInBytes, "32Mb", 32*mb)
assertEqual(t, RAMInBytes, "32Gb", 32*gb)
assertEqual(t, RAMInBytes, "32Tb", 32*tb)
assertEqual(t, RAMInBytes, "32Pb", 32*pb)
assertEqual(t, RAMInBytes, "32PB", 32*pb)
assertEqual(t, RAMInBytes, "32P", 32*pb)
assertEqual(t, RAMInBytes, "32.3", 32)
tmp := 32.3 * mb
assertEqual(t, RAMInBytes, "32.3 mb", int64(tmp))
tmp = 0.3 * mb
assertEqual(t, RAMInBytes, "0.3MB", int64(tmp))
assertError(t, RAMInBytes, "")
assertError(t, RAMInBytes, "hello")
assertError(t, RAMInBytes, "-32")
assertError(t, RAMInBytes, " 32 ")
assertError(t, RAMInBytes, "32m b")
assertError(t, RAMInBytes, "32bm")
}
type parseFn func(string) (int64, error)
func (fn parseFn) String() string {
fnName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
return fnName[strings.LastIndex(fnName, ".")+1:]
}
func assertEqual(t *testing.T, fn parseFn, arg string, expected int64) {
t.Helper()
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) {
t.Helper()
res, err := fn(arg)
if err == nil && res != -1 {
t.Errorf("%s(\"%s\") -> expected error but got '%d'", fn, arg, res)
}
}