-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumbers_test.go
More file actions
217 lines (191 loc) · 5.23 KB
/
Copy pathnumbers_test.go
File metadata and controls
217 lines (191 loc) · 5.23 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package strings
import (
"fmt"
"golang.org/x/exp/constraints"
"math"
"strconv"
"testing"
)
func ExampleExtractNumbers() {
a := ExtractNumbers("a1b2 c3")
fmt.Println(a)
//Output: 123
}
func TestExtractNumbers(t *testing.T) {
tests := []struct {
name string
in string
wantOut string
}{
{"empty", "", ""},
{"123", "123", "123"},
{"abc", "abc", ""},
{"a1c", "a1c", "1"},
{"a1c", "a1c", "1"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotOut := ExtractNumbers(tt.in); gotOut != tt.wantOut {
t.Errorf("ExtractNumbers() = %v, want %v", gotOut, tt.wantOut)
}
})
}
}
// TestAtoI tests the AtoI function for various integer types.
func TestAtoI(t *testing.T) {
type testCase[T constraints.Integer] struct {
input string
expected T
}
// Test cases for int8
testCasesInt8 := []testCase[int8]{
{"0", 0},
{"127", 127},
{"-128", -128},
{"128", 0}, // Overflow
{"-129", 0}, // Underflow
{"abc", 0}, // Invalid input
{"12abc", 0}, // Invalid input
}
for _, tc := range testCasesInt8 {
result := AtoI[int8](tc.input)
if result != tc.expected {
t.Errorf("AtoI[int8](%q) = %d; want %d", tc.input, result, tc.expected)
}
}
// Test cases for uint8
testCasesUint8 := []testCase[uint8]{
{"0", 0},
{"255", 255},
{"256", 0}, // Overflow
{"-1", 0}, // Negative number for unsigned
{"abc", 0}, // Invalid input
{"12abc", 0}, // Invalid input
}
for _, tc := range testCasesUint8 {
result := AtoI[uint8](tc.input)
if result != tc.expected {
t.Errorf("AtoI[uint8](%q) = %d; want %d", tc.input, result, tc.expected)
}
}
// Test cases for int16
testCasesInt16 := []testCase[int16]{
{"0", 0},
{"32767", 32767},
{"-32768", -32768},
{"32768", 0}, // Overflow
{"-32769", 0}, // Underflow
{"abc", 0}, // Invalid input
}
for _, tc := range testCasesInt16 {
result := AtoI[int16](tc.input)
if result != tc.expected {
t.Errorf("AtoI[int16](%q) = %d; want %d", tc.input, result, tc.expected)
}
}
// Test cases for uint16
testCasesUint16 := []testCase[uint16]{
{"0", 0},
{"65535", 65535},
{"65536", 0}, // Overflow
{"-1", 0}, // Negative number for unsigned
{"abc", 0}, // Invalid input
}
for _, tc := range testCasesUint16 {
result := AtoI[uint16](tc.input)
if result != tc.expected {
t.Errorf("AtoI[uint16](%q) = %d; want %d", tc.input, result, tc.expected)
}
}
// Test cases for int32
testCasesInt32 := []testCase[int32]{
{"0", 0},
{"2147483647", 2147483647},
{"-2147483648", -2147483648},
{"2147483648", 0}, // Overflow
{"-2147483649", 0}, // Underflow
{"abc", 0}, // Invalid input
}
for _, tc := range testCasesInt32 {
result := AtoI[int32](tc.input)
if result != tc.expected {
t.Errorf("AtoI[int32](%q) = %d; want %d", tc.input, result, tc.expected)
}
}
// Test cases for uint32
testCasesUint32 := []testCase[uint32]{
{"0", 0},
{"4294967295", 4294967295},
{"4294967296", 0}, // Overflow
{"-1", 0}, // Negative number for unsigned
{"abc", 0}, // Invalid input
}
for _, tc := range testCasesUint32 {
result := AtoI[uint32](tc.input)
if result != tc.expected {
t.Errorf("AtoI[uint32](%q) = %d; want %d", tc.input, result, tc.expected)
}
}
// Test cases for int64
testCasesInt64 := []testCase[int64]{
{"0", 0},
{"9223372036854775807", 9223372036854775807},
{"-9223372036854775808", -9223372036854775808},
{"9223372036854775808", 0}, // Overflow
{"-9223372036854775809", 0}, // Underflow
{"abc", 0}, // Invalid input
}
for _, tc := range testCasesInt64 {
result := AtoI[int64](tc.input)
if result != tc.expected {
t.Errorf("AtoI[int64](%q) = %d; want %d", tc.input, result, tc.expected)
}
}
// Test cases for uint64
testCasesUint64 := []testCase[uint64]{
{"0", 0},
{"18446744073709551615", 18446744073709551615},
{"18446744073709551616", 0}, // Overflow
{"-1", 0}, // Negative number for unsigned
{"abc", 0}, // Invalid input
}
for _, tc := range testCasesUint64 {
result := AtoI[uint64](tc.input)
if result != tc.expected {
t.Errorf("AtoI[uint64](%q) = %d; want %d", tc.input, result, tc.expected)
}
}
// Test cases for int (architecture-dependent)
testCasesInt := []testCase[int]{
{"0", 0},
{strconv.FormatInt(int64(math.MaxInt), 10), int(math.MaxInt)},
{strconv.FormatInt(int64(math.MinInt), 10), int(math.MinInt)},
{strconv.FormatUint(uint64(math.MaxInt)+1, 10), 0}, // Overflow
{"abc", 0}, // Invalid input
}
for _, tc := range testCasesInt {
result := AtoI[int](tc.input)
if result != tc.expected {
t.Errorf("AtoI[int](%q) = %d; want %d", tc.input, result, tc.expected)
}
}
// Test cases for uint (architecture-dependent)
testCasesUint := []testCase[uint]{
{"0", 0},
{strconv.FormatUint(uint64(math.MaxUint), 10), uint(math.MaxUint)},
{strconv.FormatUint(uint64(math.MaxUint), 10) + "0", 0}, // Overflow
{"-1", 0}, // Negative number for unsigned
{"abc", 0}, // Invalid input
}
for _, tc := range testCasesUint {
result := AtoI[uint](tc.input)
if result != tc.expected {
t.Errorf("AtoI[uint](%q) = %d; want %d", tc.input, result, tc.expected)
}
}
}
func ExampleAtoI() {
i := AtoI[uint8]("23")
fmt.Println(i)
// Output: 23
}