utils_test.go 2.08 KB
Newer Older
liming6's avatar
liming6 committed
1
2
3
package utils

import (
4
	"sync/atomic"
liming6's avatar
liming6 committed
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
	"testing"
)

func TestRegexp(t *testing.T) {
	t.Log(ReUnit.MatchString("b"))
	t.Log(ReUnit.MatchString("kb"))
	t.Log(ReUnit.MatchString("kib"))

	t.Logf("%+v, %d", ReUnit.FindStringSubmatch("b"), len(ReUnit.FindStringSubmatch("b")))
	t.Logf("%+v, %d", ReUnit.FindStringSubmatch("kb"), len(ReUnit.FindStringSubmatch("kb")))
	t.Logf("%+v, %d", ReUnit.FindStringSubmatch("kib"), len(ReUnit.FindStringSubmatch("kib")))

	if ReStorageSize.MatchString("123MiB") {
		t.Logf("%+v", ReStorageSize.FindStringSubmatch("123MiB"))
	} else {
		t.Errorf("Error match 123MiB")
	}

	if ReStorageSize.MatchString("123MB") {
		t.Logf("%+v", ReStorageSize.FindStringSubmatch("123MB"))
	} else {
		t.Errorf("Error match 123MB")
	}
}

func TestParseUnit(t *testing.T) {
	testData := []string{"MiB", "MB", "B", "b"}
	result := []StorageCapacityUnit{MiB, MB, Byte, Byte}
	for index, unit := range testData {
		u, err := ParseUnit(unit)
		if err != nil {
			t.Errorf("Error match %d: %s", index, err)
		}
		if u != result[index] {
			t.Errorf("Error match %d: expected %d, got %d", index, u, result[index])
		}
	}
}

func TestParseMemorySize(t *testing.T) {
	testData := []string{"1MiB", "2MB", "3B", "4b", "5 PiB"}
	result := []MemorySize{{Num: 1, Unit: MiB}, {Num: 2, Unit: MB}, {Num: 3, Unit: Byte}, {Num: 4, Unit: Byte}, {Num: 5, Unit: PiB}}
	for index, unit := range testData {
		u, err := ParseMemorySize(unit)
		if err != nil {
			t.Errorf("Error match %d: %s", index, err)
		}
		if u.Num != result[index].Num || u.Unit != result[index].Unit {
			t.Errorf("Error match %d: expected %d, got %d", index, u, result[index])
		}
	}
}
liming6's avatar
liming6 committed
57
58

func TestHumanReadStr(t *testing.T) {
59
	ms := MemorySize{Num: 0, Unit: Byte}
60
	t.Log(ms.HumanReadStr(2))
liming6's avatar
liming6 committed
61
62
	ms.Num = 1025
	ms.Unit = PiB
63
64
65
	t.Log(ms.HumanReadStr(1))
}

66
67
68
69
70
71
72
73
74
75
76
77
func TestPCIBusId(t *testing.T) {
	t.Log(PCIBus(0x1, 1))
	t.Log(PCIBus(0x12, 1))
	t.Log(PCIBus(0x123, 1))
	t.Log(PCIBus(0x1234, 1))
	t.Log(PCIBus(0x12345, 1))
	t.Log(PCIBus(0x123456, 1))
	t.Log(PCIBus(0x1234567, 1))
	t.Log(PCIBus(0x12345678, 1))
	t.Log(PCIBus(0x123456789, 1))
	i := atomic.Int32{}
	t.Log(i.Load())
liming6's avatar
liming6 committed
78
}