Skip to content

Commit 217b87a

Browse files
committed
Add util tests
1 parent 510d99e commit 217b87a

1 file changed

Lines changed: 287 additions & 0 deletions

File tree

util/util_test.go

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
package util
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestParseCode(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
input string
12+
expected []uint32
13+
wantErr bool
14+
}{
15+
// Hex prefix tests (0x/0X)
16+
{
17+
name: "hex with lowercase 0x prefix",
18+
input: "0x1234",
19+
expected: []uint32{0x1234},
20+
wantErr: false,
21+
},
22+
{
23+
name: "hex with uppercase 0X prefix",
24+
input: "0X1234",
25+
expected: []uint32{0x1234},
26+
wantErr: false,
27+
},
28+
{
29+
name: "hex with 0x prefix - zero value",
30+
input: "0x0",
31+
expected: []uint32{0},
32+
wantErr: false,
33+
},
34+
{
35+
name: "hex with 0x prefix - max uint32",
36+
input: "0xFFFFFFFF",
37+
expected: []uint32{0xFFFFFFFF},
38+
wantErr: false,
39+
},
40+
{
41+
name: "hex with 0x prefix - lowercase letters",
42+
input: "0xabcdef",
43+
expected: []uint32{0xabcdef},
44+
wantErr: false,
45+
},
46+
{
47+
name: "hex with 0x prefix - uppercase letters",
48+
input: "0xABCDEF",
49+
expected: []uint32{0xABCDEF},
50+
wantErr: false,
51+
},
52+
{
53+
name: "hex with 0x prefix - invalid hex character",
54+
input: "0x123G",
55+
expected: nil,
56+
wantErr: true,
57+
},
58+
{
59+
name: "hex with 0x prefix - overflow uint32",
60+
input: "0x100000000",
61+
expected: nil,
62+
wantErr: true,
63+
},
64+
{
65+
name: "hex with 0x prefix - empty after prefix",
66+
input: "0x",
67+
expected: nil,
68+
wantErr: true,
69+
},
70+
71+
// Negative number tests
72+
{
73+
name: "negative decimal number",
74+
input: "-1",
75+
expected: []uint32{0xFFFFFFFF}, // -1 as uint32
76+
wantErr: false,
77+
},
78+
{
79+
name: "negative decimal - large negative",
80+
input: "-2147483648",
81+
expected: []uint32{0x80000000}, // -2147483648 as uint32
82+
wantErr: false,
83+
},
84+
{
85+
name: "negative decimal - small negative",
86+
input: "-123",
87+
expected: []uint32{4294967173}, // -123 as uint32 (0xFFFFFF85)
88+
wantErr: false,
89+
},
90+
{
91+
name: "negative decimal - overflow int32",
92+
input: "-2147483649",
93+
expected: nil,
94+
wantErr: true,
95+
},
96+
{
97+
name: "negative decimal - invalid format",
98+
input: "-abc",
99+
expected: nil,
100+
wantErr: true,
101+
},
102+
{
103+
name: "negative decimal - just minus sign",
104+
input: "-",
105+
expected: nil,
106+
wantErr: true,
107+
},
108+
109+
// Ambiguous codes (no prefix) - should try both hex and decimal
110+
{
111+
name: "ambiguous code - valid as both hex and decimal",
112+
input: "123",
113+
expected: []uint32{0x123, 123}, // Both hex and decimal interpretations
114+
wantErr: false,
115+
},
116+
{
117+
name: "ambiguous code - valid only as decimal",
118+
input: "999",
119+
expected: []uint32{0x999, 999}, // 999 is valid hex (0x999=2457) and decimal
120+
wantErr: false,
121+
},
122+
{
123+
name: "ambiguous code - valid only as hex",
124+
input: "ABC",
125+
expected: []uint32{0xABC}, // Only hex
126+
wantErr: false,
127+
},
128+
{
129+
name: "ambiguous code - zero",
130+
input: "0",
131+
expected: []uint32{0}, // Same value for both hex and decimal, should be compacted
132+
wantErr: false,
133+
},
134+
{
135+
name: "ambiguous code - invalid for both",
136+
input: "XYZ",
137+
expected: nil,
138+
wantErr: true,
139+
},
140+
141+
{
142+
name: "ambiguous code - max uint32 as decimal",
143+
input: "4294967295",
144+
expected: []uint32{4294967295},
145+
wantErr: false,
146+
},
147+
{
148+
name: "ambiguous code - overflow uint32 as decimal",
149+
input: "4294967296",
150+
expected: nil,
151+
wantErr: true,
152+
},
153+
154+
// Edge cases
155+
{
156+
name: "single character hex",
157+
input: "F",
158+
expected: []uint32{0xF}, // Only valid as hex
159+
wantErr: false,
160+
},
161+
{
162+
name: "single character decimal",
163+
input: "9",
164+
expected: []uint32{9}, // Same value for hex and decimal, compacted to one
165+
wantErr: false,
166+
},
167+
{
168+
name: "leading zeros in hex prefix",
169+
input: "0x0001",
170+
expected: []uint32{1},
171+
wantErr: false,
172+
},
173+
{
174+
name: "leading zeros in ambiguous",
175+
input: "0001",
176+
expected: []uint32{1}, // Both hex and decimal parse to 1, compacted
177+
wantErr: false,
178+
},
179+
{
180+
name: "empty string",
181+
input: "",
182+
expected: nil,
183+
wantErr: true,
184+
},
185+
}
186+
187+
for _, tt := range tests {
188+
t.Run(tt.name, func(t *testing.T) {
189+
result, err := ParseCode(tt.input)
190+
191+
if tt.wantErr {
192+
if err == nil {
193+
t.Errorf("ParseCode(%q) expected error, but got none", tt.input)
194+
}
195+
return
196+
}
197+
198+
if err != nil {
199+
t.Errorf("ParseCode(%q) unexpected error: %v", tt.input, err)
200+
return
201+
}
202+
203+
if !reflect.DeepEqual(result, tt.expected) {
204+
t.Errorf("ParseCode(%q) = %v, expected %v", tt.input, result, tt.expected)
205+
}
206+
})
207+
}
208+
}
209+
210+
func TestBoolToInt(t *testing.T) {
211+
tests := []struct {
212+
name string
213+
input bool
214+
expected int
215+
}{
216+
{
217+
name: "true converts to 1",
218+
input: true,
219+
expected: 1,
220+
},
221+
{
222+
name: "false converts to 0",
223+
input: false,
224+
expected: 0,
225+
},
226+
}
227+
228+
for _, tt := range tests {
229+
t.Run(tt.name, func(t *testing.T) {
230+
result := BoolToInt(tt.input)
231+
if result != tt.expected {
232+
t.Errorf("BoolToInt(%v) = %d, expected %d", tt.input, result, tt.expected)
233+
}
234+
})
235+
}
236+
}
237+
238+
// Benchmark tests for performance analysis
239+
func BenchmarkParseCode_HexPrefix(b *testing.B) {
240+
for i := 0; i < b.N; i++ {
241+
_, _ = ParseCode("0x1234ABCD")
242+
}
243+
}
244+
245+
func BenchmarkParseCode_Negative(b *testing.B) {
246+
for i := 0; i < b.N; i++ {
247+
_, _ = ParseCode("-12345")
248+
}
249+
}
250+
251+
func BenchmarkParseCode_Ambiguous(b *testing.B) {
252+
for i := 0; i < b.N; i++ {
253+
_, _ = ParseCode("12345")
254+
}
255+
}
256+
257+
func BenchmarkBoolToInt_True(b *testing.B) {
258+
for i := 0; i < b.N; i++ {
259+
_ = BoolToInt(true)
260+
}
261+
}
262+
263+
func BenchmarkBoolToInt_False(b *testing.B) {
264+
for i := 0; i < b.N; i++ {
265+
_ = BoolToInt(false)
266+
}
267+
}
268+
269+
// Example tests for documentation
270+
func ExampleParseCode() {
271+
// Hex with prefix
272+
codes, _ := ParseCode("0x1234")
273+
println(codes[0]) // 4660
274+
275+
// Negative decimal
276+
codes, _ = ParseCode("-1")
277+
println(codes[0]) // 4294967295 (0xFFFFFFFF)
278+
279+
// Ambiguous code (valid as both hex and decimal)
280+
codes, _ = ParseCode("123")
281+
println(len(codes)) // 2 (both interpretations)
282+
}
283+
284+
func ExampleBoolToInt() {
285+
println(BoolToInt(true)) // 1
286+
println(BoolToInt(false)) // 0
287+
}

0 commit comments

Comments
 (0)