Skip to content

Commit 43e7fb5

Browse files
test(util): add unit tests for Atoi, ParseInt, ParseUint, and ParseFloat (#3377)
1 parent 6c6dddc commit 43e7fb5

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

internal/util/strconv_test.go

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package util
2+
3+
import (
4+
"math"
5+
"testing"
6+
)
7+
8+
func TestAtoi(t *testing.T) {
9+
tests := []struct {
10+
input []byte
11+
expected int
12+
wantErr bool
13+
}{
14+
{[]byte("123"), 123, false},
15+
{[]byte("-456"), -456, false},
16+
{[]byte("abc"), 0, true},
17+
}
18+
19+
for _, tt := range tests {
20+
result, err := Atoi(tt.input)
21+
if (err != nil) != tt.wantErr {
22+
t.Errorf("Atoi(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
23+
}
24+
if result != tt.expected && !tt.wantErr {
25+
t.Errorf("Atoi(%q) = %d, want %d", tt.input, result, tt.expected)
26+
}
27+
}
28+
}
29+
30+
func TestParseInt(t *testing.T) {
31+
tests := []struct {
32+
input []byte
33+
base int
34+
bitSize int
35+
expected int64
36+
wantErr bool
37+
}{
38+
{[]byte("123"), 10, 64, 123, false},
39+
{[]byte("-7F"), 16, 64, -127, false},
40+
{[]byte("zzz"), 36, 64, 46655, false},
41+
{[]byte("invalid"), 10, 64, 0, true},
42+
}
43+
44+
for _, tt := range tests {
45+
result, err := ParseInt(tt.input, tt.base, tt.bitSize)
46+
if (err != nil) != tt.wantErr {
47+
t.Errorf("ParseInt(%q, base=%d) error = %v, wantErr %v", tt.input, tt.base, err, tt.wantErr)
48+
}
49+
if result != tt.expected && !tt.wantErr {
50+
t.Errorf("ParseInt(%q, base=%d) = %d, want %d", tt.input, tt.base, result, tt.expected)
51+
}
52+
}
53+
}
54+
55+
func TestParseUint(t *testing.T) {
56+
tests := []struct {
57+
input []byte
58+
base int
59+
bitSize int
60+
expected uint64
61+
wantErr bool
62+
}{
63+
{[]byte("255"), 10, 8, 255, false},
64+
{[]byte("FF"), 16, 16, 255, false},
65+
{[]byte("-1"), 10, 8, 0, true}, // negative should error for unsigned
66+
}
67+
68+
for _, tt := range tests {
69+
result, err := ParseUint(tt.input, tt.base, tt.bitSize)
70+
if (err != nil) != tt.wantErr {
71+
t.Errorf("ParseUint(%q, base=%d) error = %v, wantErr %v", tt.input, tt.base, err, tt.wantErr)
72+
}
73+
if result != tt.expected && !tt.wantErr {
74+
t.Errorf("ParseUint(%q, base=%d) = %d, want %d", tt.input, tt.base, result, tt.expected)
75+
}
76+
}
77+
}
78+
79+
func TestParseFloat(t *testing.T) {
80+
tests := []struct {
81+
input []byte
82+
bitSize int
83+
expected float64
84+
wantErr bool
85+
}{
86+
{[]byte("3.14"), 64, 3.14, false},
87+
{[]byte("-2.71"), 64, -2.71, false},
88+
{[]byte("NaN"), 64, math.NaN(), false},
89+
{[]byte("invalid"), 64, 0, true},
90+
}
91+
92+
for _, tt := range tests {
93+
result, err := ParseFloat(tt.input, tt.bitSize)
94+
if (err != nil) != tt.wantErr {
95+
t.Errorf("ParseFloat(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
96+
}
97+
if !tt.wantErr && !(math.IsNaN(tt.expected) && math.IsNaN(result)) && result != tt.expected {
98+
t.Errorf("ParseFloat(%q) = %v, want %v", tt.input, result, tt.expected)
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)