forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestInteger.java
More file actions
212 lines (192 loc) · 7.49 KB
/
TestInteger.java
File metadata and controls
212 lines (192 loc) · 7.49 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
package com.jsoniter;
import com.jsoniter.spi.JsonException;
import junit.framework.TestCase;
import org.junit.experimental.categories.Category;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
//import java.math.BigDecimal;
//import java.math.BigInteger;
public class TestInteger extends TestCase {
static {
// JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_STRICTLY);
}
private boolean isStreaming;
public void test_char() throws IOException {
Character c = JsonIterator.deserialize("50", Character.class);
assertEquals(50, (int) c);
}
public void test_positive_negative_int() throws IOException {
assertEquals(0, parseInt("0"));
assertEquals(4321, parseInt("4321"));
assertEquals(54321, parseInt("54321"));
assertEquals(654321, parseInt("654321"));
assertEquals(7654321, parseInt("7654321"));
assertEquals(87654321, parseInt("87654321"));
assertEquals(987654321, parseInt("987654321"));
assertEquals(2147483647, parseInt("2147483647"));
assertEquals(-4321, parseInt("-4321"));
assertEquals(-2147483648, parseInt("-2147483648"));
}
public void test_positive_negative_long() throws IOException {
assertEquals(0L, parseLong("0"));
assertEquals(4321L, parseLong("4321"));
assertEquals(54321L, parseLong("54321"));
assertEquals(654321L, parseLong("654321"));
assertEquals(7654321L, parseLong("7654321"));
assertEquals(87654321L, parseLong("87654321"));
assertEquals(987654321L, parseLong("987654321"));
assertEquals(9223372036854775807L, parseLong("9223372036854775807"));
assertEquals(-4321L, parseLong("-4321"));
assertEquals(-9223372036854775808L, parseLong("-9223372036854775808"));
}
public void test_max_min_int() throws IOException {
assertEquals(Integer.MAX_VALUE, parseInt(Integer.toString(Integer.MAX_VALUE)));
assertEquals(Integer.MAX_VALUE - 1, parseInt(Integer.toString(Integer.MAX_VALUE - 1)));
assertEquals(Integer.MIN_VALUE + 1, parseInt(Integer.toString(Integer.MIN_VALUE + 1)));
assertEquals(Integer.MIN_VALUE, parseInt(Integer.toString(Integer.MIN_VALUE)));
}
public void test_max_min_long() throws IOException {
assertEquals(Long.MAX_VALUE, parseLong(Long.toString(Long.MAX_VALUE)));
assertEquals(Long.MAX_VALUE - 1, parseLong(Long.toString(Long.MAX_VALUE - 1)));
assertEquals(Long.MIN_VALUE + 1, parseLong(Long.toString(Long.MIN_VALUE + 1)));
assertEquals(Long.MIN_VALUE, parseLong(Long.toString(Long.MIN_VALUE)));
}
public void test_large_number() throws IOException {
try {
JsonIterator.deserialize("2147483648", Integer.class);
fail();
} catch (JsonException e) {
}
for (int i = 300000000; i < 2000000000; i += 10000000) {
try {
JsonIterator.deserialize(i + "0", Integer.class);
fail();
} catch (JsonException e) {
}
try {
JsonIterator.deserialize(-i + "0", Integer.class);
fail();
} catch (JsonException e) {
}
}
try {
JsonIterator.deserialize("9223372036854775808", Long.class);
fail();
} catch (JsonException e) {
}
for (long i = 1000000000000000000L; i < 9000000000000000000L; i += 100000000000000000L) {
try {
JsonIterator.deserialize(i + "0", Long.class);
fail();
} catch (JsonException e) {
}
try {
JsonIterator.deserialize(-i + "0", Long.class);
fail();
} catch (JsonException e) {
}
}
}
public void test_byte() throws IOException {
Byte val = JsonIterator.deserialize("120", Byte.class);
assertEquals(Byte.valueOf((byte) 120), val);
byte[] vals = JsonIterator.deserialize("[120]", byte[].class);
assertEquals((byte) 120, vals[0]);
}
@Category(StreamingCategory.class)
public void test_streaming() throws IOException {
isStreaming = true;
test_positive_negative_int();
test_positive_negative_long();
test_max_min_int();
test_max_min_long();
test_large_number();
}
public void test_leading_zero() throws IOException {
assertEquals(Integer.valueOf(0), JsonIterator.deserialize("0", int.class));
assertEquals(Long.valueOf(0), JsonIterator.deserialize("0", long.class));
try {
JsonIterator.deserialize("01", int.class);
fail();
} catch (JsonException e) {
}
try {
JsonIterator.deserialize("02147483647", int.class);
fail();
} catch (JsonException e) {
}
try {
JsonIterator.deserialize("01", long.class);
fail();
} catch (JsonException e) {
}
try {
JsonIterator.deserialize("09223372036854775807", long.class);
fail();
} catch (JsonException e) {
}
/* FIXME if we should fail on parsing of leading zeroes for other numbers
try {
JsonIterator.deserialize("01", double.class);
fail();
} catch (JsonException e) {
}
try {
JsonIterator.deserialize("01", float.class);
fail();
} catch (JsonException e) {
}
try {
JsonIterator.deserialize("01", BigInteger.class);
fail();
} catch (JsonException e) {
}
try {
JsonIterator.deserialize("01", BigDecimal.class);
fail();
} catch (JsonException e) {
}
*/
}
public void test_max_int() throws IOException {
int[] ints = JsonIterator.deserialize("[2147483647,-2147483648]", int[].class);
assertEquals(Integer.MAX_VALUE, ints[0]);
assertEquals(Integer.MIN_VALUE, ints[1]);
}
private int parseInt(String input) throws IOException {
if (isStreaming) {
JsonIterator iter = JsonIterator.parse(new ByteArrayInputStream(input.getBytes()), 2);
return iter.readInt();
} else {
JsonIterator iter = JsonIterator.parse(input);
int v = iter.readInt();
assertEquals(input.length(), iter.head); // iterator head should point on next non-parsed byte
return v;
}
}
private long parseLong(String input) throws IOException {
if (isStreaming) {
JsonIterator iter = JsonIterator.parse(new ByteArrayInputStream(input.getBytes()), 2);
return iter.readLong();
} else {
JsonIterator iter = JsonIterator.parse(input);
long v = iter.readLong();
assertEquals(input.length(), iter.head); // iterator head should point on next non-parsed byte
return v;
}
}
public void testBigInteger() {
BigInteger number = JsonIterator.deserialize("100", BigInteger.class);
assertEquals(new BigInteger("100"), number);
}
public void testChooseInteger() {
Object number = JsonIterator.deserialize("100", Object.class);
assertEquals(100, number);
}
public void testChooseLong() {
Object number = JsonIterator.deserialize(Long.valueOf(Long.MAX_VALUE).toString(), Object.class);
assertEquals(Long.MAX_VALUE, number);
}
}