Debian Codegraph @

src/util/util_test.go

raw
1 package util
2
3 import (
4 "math"
5 "strings"
6 "testing"
7 )
8
9 func TestConstrain(t *testing.T) {
10 if Constrain(-3, -1, 3) != -1 {
11 t.Error("Expected", -1)
12 }
13 if Constrain(2, -1, 3) != 2 {
14 t.Error("Expected", 2)
15 }
16
17 if Constrain(5, -1, 3) != 3 {
18 t.Error("Expected", 3)
19 }
20 }
21
22 func TestAsUint16(t *testing.T) {
23 if AsUint16(5) != 5 {
24 t.Error("Expected", 5)
25 }
26 if AsUint16(-10) != 0 {
27 t.Error("Expected", 0)
28 }
29 if AsUint16(math.MaxUint16) != math.MaxUint16 {
30 t.Error("Expected", math.MaxUint16)
31 }
32 if AsUint16(math.MinInt32) != 0 {
33 t.Error("Expected", 0)
34 }
35 if AsUint16(math.MinInt16) != 0 {
36 t.Error("Expected", 0)
37 }
38 if AsUint16(math.MaxUint16+1) != math.MaxUint16 {
39 t.Error("Expected", math.MaxUint16)
40 }
41 }
42
43 func TestOnce(t *testing.T) {
44 o := Once(false)
45 if o() {
46 t.Error("Expected: false")
47 }
48 if !o() {
49 t.Error("Expected: true")
50 }
51 if !o() {
52 t.Error("Expected: true")
53 }
54
55 o = Once(true)
56 if !o() {
57 t.Error("Expected: true")
58 }
59 if o() {
60 t.Error("Expected: false")
61 }
62 if o() {
63 t.Error("Expected: false")
64 }
65 }
66
67 func TestRunesWidth(t *testing.T) {
68 for _, args := range [][]int{
69 {100, 5, -1},
70 {3, 4, 3},
71 {0, 1, 0},
72 } {
73 width, overflowIdx := RunesWidth([]rune("hello"), 0, 0, args[0])
74 if width != args[1] {
75 t.Errorf("Expected width: %d, actual: %d", args[1], width)
76 }
77 if overflowIdx != args[2] {
78 t.Errorf("Expected overflow index: %d, actual: %d", args[2], overflowIdx)
79 }
80 }
81 for _, input := range []struct {
82 s string
83 w int
84 }{
85 {"▶", 1},
86 {"▶️", 2},
87 } {
88 width, _ := RunesWidth([]rune(input.s), 0, 0, 100)
89 if width != input.w {
90 t.Errorf("Expected width of %s: %d, actual: %d", input.s, input.w, width)
91 }
92 }
93 }
94
95 func TestTruncate(t *testing.T) {
96 truncated, width := Truncate("가나다라마", 7)
97 if string(truncated) != "가나다" {
98 t.Errorf("Expected: 가나다, actual: %s", string(truncated))
99 }
100 if width != 6 {
101 t.Errorf("Expected: 6, actual: %d", width)
102 }
103 }
104
105 func TestRepeatToFill(t *testing.T) {
106 if RepeatToFill("abcde", 10, 50) != strings.Repeat("abcde", 5) {
107 t.Error("Expected:", strings.Repeat("abcde", 5))
108 }
109 if RepeatToFill("abcde", 10, 42) != strings.Repeat("abcde", 4)+"abcde"[:2] {
110 t.Error("Expected:", strings.Repeat("abcde", 4)+"abcde"[:2])
111 }
112 }
113
114 func TestStringWidth(t *testing.T) {
115 w := StringWidth("─")
116 if w != 1 {
117 t.Errorf("Expected: %d, Actual: %d", 1, w)
118 }
119 }
120
121 func TestCompareVersions(t *testing.T) {
122 assert := func(a, b string, expected int) {
123 if result := CompareVersions(a, b); result != expected {
124 t.Errorf("Expected: %d, Actual: %d", expected, result)
125 }
126 }
127
128 assert("2", "1", 1)
129 assert("2", "2", 0)
130 assert("2", "10", -1)
131
132 assert("2.1", "2.2", -1)
133 assert("2.1", "2.1.1", -1)
134
135 assert("1.2.3", "1.2.2", 1)
136 assert("1.2.3", "1.2.3", 0)
137 assert("1.2.3", "1.2.3.0", 0)
138 assert("1.2.3", "1.2.4", -1)
139
140 // Different number of parts
141 assert("1.0.0", "1", 0)
142 assert("1.0.0", "1.0", 0)
143 assert("1.0.0", "1.0.0", 0)
144 assert("1.0", "1.0.0", 0)
145 assert("1", "1.0.0", 0)
146 assert("1.0.0", "1.0.0.1", -1)
147 assert("1.0.0.1.0", "1.0.0.1", 0)
148
149 assert("", "3.4.5", -1)
150 }