-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathenc_int_test.go
148 lines (134 loc) · 3.1 KB
/
enc_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
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
package jx
import (
"fmt"
"math"
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
func TestEncoder_Uint64(t *testing.T) {
test := func(i uint64) (string, func(t *testing.T)) {
return fmt.Sprintf("Test%d", i), func(t *testing.T) {
enc := GetEncoder()
enc.Reset()
enc.UInt64(i)
require.Equal(t, enc.String(), strconv.FormatUint(i, 10))
}
}
overflows := func(i uint64) bool {
result := i * 10
return result/10 != i
}
t.Run(test(0))
t.Run(test(1337))
t.Run(test(math.MaxUint64))
for i := uint64(1); !overflows(i); i *= 10 {
t.Run(test(i))
t.Run(test(i + 1))
}
}
func TestEncoder_Uint32(t *testing.T) {
test := func(i uint32) (string, func(t *testing.T)) {
return fmt.Sprintf("Test%d", i), func(t *testing.T) {
enc := GetEncoder()
enc.Reset()
enc.UInt32(i)
require.Equal(t, enc.String(), strconv.FormatUint(uint64(i), 10))
}
}
overflows := func(i uint32) bool {
result := i * 10
return result/10 != i
}
t.Run(test(0))
t.Run(test(1337))
t.Run(test(math.MaxUint32))
for i := uint32(1); !overflows(i); i *= 10 {
t.Run(test(i))
t.Run(test(i + 1))
}
}
func TestEncoder_Uint16(t *testing.T) {
test := func(i uint16) (string, func(t *testing.T)) {
return fmt.Sprintf("Test%d", i), func(t *testing.T) {
enc := GetEncoder()
enc.Reset()
enc.UInt16(i)
require.Equal(t, enc.String(), strconv.FormatUint(uint64(i), 10))
}
}
overflows := func(i uint16) bool {
result := i * 10
return result/10 != i
}
t.Run(test(0))
t.Run(test(1337))
t.Run(test(math.MaxUint16))
for i := uint16(1); !overflows(i); i *= 10 {
t.Run(test(i))
t.Run(test(i + 1))
}
}
func TestEncoder_Uint8(t *testing.T) {
test := func(i uint8) (string, func(t *testing.T)) {
return fmt.Sprintf("Test%d", i), func(t *testing.T) {
enc := GetEncoder()
enc.Reset()
enc.UInt8(i)
require.Equal(t, enc.String(), strconv.FormatUint(uint64(i), 10))
}
}
overflows := func(i uint8) bool {
result := i * 10
return result/10 != i
}
t.Run(test(0))
t.Run(test(237))
t.Run(test(math.MaxUint8))
for i := uint8(1); !overflows(i); i *= 10 {
t.Run(test(i))
t.Run(test(i + 1))
}
}
func TestEncoder_Int16(t *testing.T) {
test := func(i int16) (string, func(t *testing.T)) {
return fmt.Sprintf("Test%d", i), func(t *testing.T) {
enc := GetEncoder()
enc.Reset()
enc.Int16(i)
require.Equal(t, enc.String(), strconv.FormatInt(int64(i), 10))
}
}
overflows := func(i int16) bool {
result := i * 10
return result/10 != i
}
t.Run(test(0))
t.Run(test(-13))
t.Run(test(math.MaxInt16))
for i := int16(1); !overflows(i); i *= 10 {
t.Run(test(i))
t.Run(test(i + 1))
}
}
func TestEncoder_Int8(t *testing.T) {
test := func(i int8) (string, func(t *testing.T)) {
return fmt.Sprintf("Test%d", i), func(t *testing.T) {
enc := GetEncoder()
enc.Reset()
enc.Int8(i)
require.Equal(t, enc.String(), strconv.FormatInt(int64(i), 10))
}
}
overflows := func(i int8) bool {
result := i * 10
return result/10 != i
}
t.Run(test(0))
t.Run(test(-13))
t.Run(test(math.MaxInt8))
for i := int8(1); !overflows(i); i *= 10 {
t.Run(test(i))
t.Run(test(i + 1))
}
}