21
21
public class ParserNumbersTest extends CBORTestBase
22
22
{
23
23
private final CBORFactory CBOR_F = cborFactory ();
24
-
24
+
25
25
public void testIntValues () throws Exception
26
26
{
27
27
// first, single-byte
@@ -101,6 +101,17 @@ public void testInt32Overflow() throws Exception
101
101
assertEquals (exp , p .getLongValue ());
102
102
assertEquals (NumberType .LONG , p .getNumberType ());
103
103
p .close ();
104
+
105
+ // and, combined, a negative number where the mantissa overflows a signed int32
106
+ input = new byte [] {
107
+ (byte ) CBORConstants .PREFIX_TYPE_INT_NEG + 26 , // int32, that is, 4 more bytes
108
+ -1 , -1 , -1 , -1
109
+ };
110
+ p = cborParser (input );
111
+ assertToken (JsonToken .VALUE_NUMBER_INT , p .nextToken ());
112
+ assertEquals (-1L - 0xFFFFFFFFL , p .getLongValue ());
113
+ assertEquals (NumberType .LONG , p .getNumberType ());
114
+ p .close ();
104
115
}
105
116
106
117
public void testLongValues () throws Exception
@@ -170,6 +181,21 @@ public void testInt64Overflow() throws Exception
170
181
assertEquals (exp , p .getBigIntegerValue ());
171
182
assertEquals (NumberType .BIG_INTEGER , p .getNumberType ());
172
183
p .close ();
184
+
185
+ // and, combined, a negative number where the mantissa overflows a signed int32
186
+ input = new byte [] {
187
+ (byte ) CBORConstants .PREFIX_TYPE_INT_NEG + 27 , // int32, that is, 4 more bytes
188
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1
189
+ };
190
+ p = cborParser (input );
191
+ assertToken (JsonToken .VALUE_NUMBER_INT , p .nextToken ());
192
+ exp = BigInteger .valueOf (Long .MAX_VALUE ).shiftLeft (1 )
193
+ .add (BigInteger .ONE )
194
+ .negate ()
195
+ .subtract (BigInteger .ONE );
196
+ assertEquals (exp , p .getBigIntegerValue ());
197
+ assertEquals (NumberType .BIG_INTEGER , p .getNumberType ());
198
+ p .close ();
173
199
}
174
200
175
201
public void testDoubleValues () throws Exception
@@ -198,15 +224,15 @@ private void _verifyDouble(CBORFactory f, double value, boolean isNaN) throws Ex
198
224
assertEquals ((float ) value , p .getFloatValue ());
199
225
200
226
assertNull (p .nextToken ());
201
-
227
+
202
228
// also skip
203
229
p = cborParser (f , out .toByteArray ());
204
230
assertEquals (JsonToken .VALUE_NUMBER_FLOAT , p .nextToken ());
205
231
assertNull (p .nextToken ());
206
-
232
+
207
233
p .close ();
208
234
}
209
-
235
+
210
236
public void testFloatValues () throws Exception
211
237
{
212
238
// first, single-byte
@@ -251,7 +277,7 @@ private void _verifyFloat(CBORFactory f, double value, boolean isNaN) throws Exc
251
277
p = cborParser (f , out .toByteArray ());
252
278
assertEquals (JsonToken .VALUE_NUMBER_FLOAT , p .nextToken ());
253
279
assertNull (p .nextToken ());
254
-
280
+
255
281
p .close ();
256
282
}
257
283
@@ -263,7 +289,7 @@ private void _verifyHalfFloat(JsonFactory f, int i16, double value) throws IOExc
263
289
};
264
290
265
291
boolean expNaN = Double .isNaN (value ) || Double .isInfinite (value );
266
-
292
+
267
293
JsonParser p = f .createParser (data );
268
294
assertEquals (JsonToken .VALUE_NUMBER_FLOAT , p .nextToken ());
269
295
assertEquals (expNaN , p .isNaN ());
0 commit comments