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

import (
liming6's avatar
liming6 committed
4
	"fmt"
liming6's avatar
liming6 committed
5
	"slices"
liming6's avatar
liming6 committed
6
	"strings"
7
	"sync/atomic"
liming6's avatar
liming6 committed
8
	"testing"
9
10
	"time"

liming6's avatar
liming6 committed
11
12
	"github.com/charmbracelet/lipgloss"
	"github.com/charmbracelet/lipgloss/tree"
13
	"github.com/emirpasic/gods/v2/maps/treemap"
liming6's avatar
liming6 committed
14
	"github.com/shirou/gopsutil/v4/process"
liming6's avatar
liming6 committed
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
64
65
)

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
66
67

func TestHumanReadStr(t *testing.T) {
68
	ms := MemorySize{Num: 0, Unit: Byte}
69
	t.Log(ms.HumanReadStr(2))
liming6's avatar
liming6 committed
70
71
	ms.Num = 1025
	ms.Unit = PiB
72
73
74
	t.Log(ms.HumanReadStr(1))
}

75
76
77
78
79
80
81
82
83
84
85
86
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
87
}
liming6's avatar
liming6 committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

func TestTimesort(t *testing.T) {
	fmt.Println("--- 打印所有 256 个 8 点盲文(Unicode 字符)---")
	fmt.Println("  (如果您的字体支持,将显示从 U+2800 到 U+28FF 的符号)")
	fmt.Println(strings.Repeat("-", 60))

	// 8点盲文的 Unicode 范围是 U+2800 到 U+28FF
	const startCodePoint = 0x2800
	const endCodePoint = 0x28FF
	const charsPerRow = 16 // 每行打印 16 个字符,共 16 行

	// 循环遍历并打印所有 256 个字符
	for i := startCodePoint; i <= endCodePoint; i++ {
		// 打印 Unicode 符号本身
		fmt.Printf("%c", rune(i))

		// 每 16 个字符换一行
		if (i-startCodePoint+1)%charsPerRow == 0 {
			fmt.Print("\n")
		} else {
			fmt.Print("|") // 字符之间用空格分隔
		}
	}

	fmt.Println(strings.Repeat("-", 60))
	fmt.Println("所有 256 个组合打印完成 (从 U+2800 到 U+28FF)。")
}

func TestChartype(t *testing.T) {
	r := GetCharType(LeftFullMW)
	t.Log(r)
	t.Log(CharTypeOr(GetCharType(' '), GetCharType(' ')))
	t.Log(GetCharType('⢠'))
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
	s := treemap.NewWith[time.Time, int](func(x, y time.Time) int {
		if x.Before(y) {
			return -1
		}
		if x.After(y) {
			return 1
		}
		return 0
	})

	s.Put(time.Now(), 3)
	s.Put(time.Now().Add(time.Second), 2)
	s.Put(time.Now().Add(-2*time.Second), 9)
	i := s.Iterator()
	for {
		if !i.Next() {
			break
		}
		t.Logf("%v: %d", i.Key(), i.Value())
	}
liming6's avatar
liming6 committed
141
}
liming6's avatar
liming6 committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

func ff(t *tree.Tree, root *ProcessInfo, ctx []int32) []int32 {
	ctx = append(ctx, root.Pid)
	pids := make([]int32, 0, len(root.Child))
	for k := range root.Child {
		pids = append(pids, k)
	}
	slices.Sort(pids)
	for _, v := range pids {
		c := root.Child[v]
		tt := tree.New().Root(fmt.Sprintf("%d: %s", c.Pid, c.Cmd))
		t.Child(tt)
		if len(c.Child) > 0 {
			ctx = ff(tt, c, ctx)
		} else {
			ctx = append(ctx, c.Pid)
		}
	}
	return ctx
}

func TestTree(t *testing.T) {
	start := time.Now()
	_, root := GetPsTree([]int32{
		2925089, 2925087, 2947677, 2925085, 2927231, 2925091, 2927238, 2927228, 2927236, 2927226, 2924226, 2925088, 2947517, 2927224, 2924385, 2925086, 2924872, 2925092, 2925090, 2927219, 2927237,
	})
	d := time.Since(start)
	tree := tree.New()
	tree.Root(fmt.Sprintf("%d: %s", root.Pid, root.Cmd))
	t.Logf("%d ms\n", d.Milliseconds())
	so := make([]int32, 0, 128)
	so = ff(tree, root, so)
	d = time.Since(start)
	t.Logf("%d ms\n", d.Milliseconds())
	st := lipgloss.NewStyle().MaxWidth(100)
	t.Logf("\n%s", st.Render(tree.String()))
	t.Logf("%+v \n", so)
}

func TestAddChilds(t *testing.T) {
	style := lipgloss.NewStyle().Inline(true).MaxWidth(10).Width(10)
	t.Logf("|%s|", style.Render("01234567890123456789"))
	t.Logf("|%s|", style.Align(lipgloss.Center).Render("01234567"))
}

func TestChild(t *testing.T) {
	p, _ := process.NewProcess(2810755)
	start := time.Now()
	childs, err := p.Children()
	d := time.Since(start)
	if err != nil {
		t.Error(err)
	}
	t.Logf("%d ms \n", d.Milliseconds())
	t.Logf("childs num: %d", len(childs))
}
liming6's avatar
liming6 committed
198
199

func TestCHar(t *testing.T) {
200
	p, err := process.NewProcess(1)
liming6's avatar
liming6 committed
201
202
	if err != nil {
		t.Error(err)
liming6's avatar
liming6 committed
203
	}
204
	env, err := p.Environ()
liming6's avatar
liming6 committed
205
	if err != nil {
206
207
208
209
210
211
212
213
		t.Errorf("error get env of process: %v", err)
	}
	slices.Sort(env)
	for _, v := range env {
		if v == "" {
			continue
		}
		t.Log(v)
liming6's avatar
liming6 committed
214
	}
liming6's avatar
liming6 committed
215
}
216
217
218
219

func TestSplitN(t *testing.T) {
	t.Logf("%v",strings.SplitN("a=b=c","=",2))
}