-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring-to-integer-atoi_test.go
99 lines (96 loc) · 1.55 KB
/
string-to-integer-atoi_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
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
package leetcode
import (
"testing"
)
func Test_stripLeadSpace(t *testing.T) {
tables := []struct {
input string
expect int
}{
{
input: "1",
expect: 0,
}, {
input: " 1",
expect: 4,
}, {
input: " -1",
expect: 1,
}, {
input: " +1",
expect: 1,
}, {
input: "+",
expect: 0,
}, {
input: " ",
expect: -1,
},
}
for _, item := range tables {
actual := stripLeadSpace(item.input)
if item.expect != actual {
t.Errorf("expect: %d, actual %d.", item.expect, actual)
}
}
}
func Test_myAtoi(t *testing.T) {
tables := []struct {
input string
expect int
}{
{
input: "42",
expect: 42,
}, {
input: " -42",
expect: -42,
}, {
input: " ",
expect: 0,
}, {
input: " +1",
expect: 1,
}, {
input: "-",
expect: 0,
}, {
input: "+",
expect: 0,
}, {
input: "ab",
expect: 0,
}, {
input: "4193 with words",
expect: 4193,
}, {
input: "words and 987",
expect: 0,
}, {
input: "-91283472332",
expect: -1 << 31,
}, {
input: "91283472332",
expect: 1<<31 - 1,
}, {
input: "2147483648",
expect: 2147483647,
}, {
input: "2147483647",
expect: 2147483647,
}, {
input: "-2147483649",
expect: -2147483648,
}, {
input: "-2147483648",
expect: -2147483648,
},
}
// fmt.Printf("%d %d %d %d\n", MAX_INT, MIN_INT, MAX_LIMIT, MIN_LIMIT)
for _, item := range tables {
actual := myAtoi(item.input)
if item.expect != actual {
t.Errorf("%s expect: %d, actual %d.", item.input, item.expect, actual)
}
}
}