-
Notifications
You must be signed in to change notification settings - Fork 97
/
intstrconv_test.go
74 lines (68 loc) · 1.81 KB
/
intstrconv_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
// Copyright (c) 2015, Peter Mrekaj. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.txt file.
package strings
import "testing"
func TestStringToInt(t *testing.T) {
for _, test := range []struct {
in string
want int64
err error
}{
{"", 0, ErrSyntax},
{"0", 0, nil},
{"-0", 0, nil},
{"1", 1, nil},
{"-1", -1, nil},
{"+1", 1, nil},
{"0123456789", 123456789, nil},
{"-0123456789", -123456789, nil},
{"123456789", 123456789, nil},
{"-123456789", -123456789, nil},
{"9223372036854775807", 1<<63 - 1, nil},
{"-9223372036854775807", -(1<<63 - 1), nil},
{"9223372036854775808", 0, ErrRange},
{"-9223372036854775808", -1 << 63, nil},
{"9223372036854775809", 0, ErrRange},
{"-9223372036854775809", 0, ErrRange},
{"9223372036854775810", 0, ErrRange},
{"-9223372036854775810", 0, ErrRange},
{"a", 0, ErrSyntax},
{"-a", 0, ErrSyntax},
{"123a", 0, ErrSyntax},
{"-123a", 0, ErrSyntax},
} {
if got, err := StringToInt(test.in); got != test.want || err != test.err {
t.Errorf("StringToInt(%q) = %d, %v; want %d, %v", test.in, got, err, test.want, test.err)
}
}
}
func BenchmarkStringToInt(b *testing.B) {
for i := 0; i < b.N; i++ {
StringToInt("9223372036854775807")
}
}
func TestIntToString(t *testing.T) {
for _, test := range []struct {
in int64
want string
}{
{0, "0"},
{1, "1"},
{-1, "-1"},
{123456789, "123456789"},
{-123456789, "-123456789"},
{1<<63 - 1, "9223372036854775807"},
{-(1<<63 - 1), "-9223372036854775807"},
{-1 << 63, "-9223372036854775808"},
} {
if got := IntToString(test.in); got != test.want {
t.Errorf("IntToString(%d) = %q; want %q", test.in, got, test.want)
}
}
}
func BenchmarkIntToString(b *testing.B) {
for i := 0; i < b.N; i++ {
IntToString(1<<63 - 1)
}
}