-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint_test.go
70 lines (63 loc) · 1.34 KB
/
int_test.go
1
2
3
4
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package g
import (
"testing"
)
func TestStringToInt(t *testing.T) {
tests := []struct {
input string
expected int
def int
err bool
}{
{"123", 123, 0, false},
{"abc", 10, 10, true},
{"", 10, 10, true},
{"", 0, 0, true},
}
for _, test := range tests {
result, err := StringToInt(test.input, test.def)
if (err != nil) != test.err {
t.Errorf("StringToInt(%q) error = %v, wantErr %v", test.input, err, test.err)
continue
}
if result != test.expected {
t.Errorf("StringToInt(%q) = %d, want %d", test.input, result, test.expected)
}
}
}
func TestIntToString(t *testing.T) {
tests := []struct {
input any // Using 'any' to handle multiple types
expected string
}{
{123, "123"},
{int64(123), "123"},
{int32(123), "123"},
{uint(123), "123"},
{uint64(123), "123"},
{uint32(123), "123"},
}
for _, test := range tests {
var result string
switch v := test.input.(type) {
case int:
result = IntToString(v)
case int64:
result = IntToString(v)
case int32:
result = IntToString(v)
case uint:
result = IntToString(v)
case uint64:
result = IntToString(v)
case uint32:
result = IntToString(v)
default:
t.Errorf("Unsupported type %T", v)
continue
}
if result != test.expected {
t.Errorf("IntToString(%v) = %q, want %q", test.input, result, test.expected)
}
}
}