1
1
package com .fasterxml .jackson .dataformat .cbor .gen ;
2
2
3
+ import com .fasterxml .jackson .core .JsonGenerationException ;
4
+ import com .fasterxml .jackson .databind .ObjectMapper ;
5
+ import com .fasterxml .jackson .dataformat .cbor .*;
6
+ import org .junit .jupiter .api .Test ;
7
+
3
8
import java .io .ByteArrayOutputStream ;
4
9
import java .math .BigDecimal ;
5
10
import java .math .BigInteger ;
6
11
import java .util .LinkedHashMap ;
7
12
import java .util .Map ;
8
13
9
- import org .junit .jupiter .api .Test ;
10
-
11
- import com .fasterxml .jackson .core .JsonGenerationException ;
12
- import com .fasterxml .jackson .databind .ObjectMapper ;
13
- import com .fasterxml .jackson .dataformat .cbor .*;
14
-
15
14
import static org .junit .jupiter .api .Assertions .*;
16
15
17
16
public class GeneratorSimpleTest extends CBORTestBase
@@ -181,6 +180,98 @@ public void testIntValues() throws Exception
181
180
(byte ) 0x7F , (byte ) 0xFF , (byte ) 0xFF , (byte ) 0xFF );
182
181
}
183
182
183
+ @ Test
184
+ public void testUnsignedIntValues () throws Exception
185
+ {
186
+ // uint32 max
187
+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
188
+ CBORGenerator gen = cborGenerator (out );
189
+ gen .writeNumberUnsigned (-1 );
190
+ gen .close ();
191
+ _verifyBytes (out .toByteArray (),
192
+ (byte ) (CBORConstants .PREFIX_TYPE_INT_POS + CBORConstants .SUFFIX_UINT32_ELEMENTS ),
193
+ (byte ) 0xFF ,
194
+ (byte ) 0xFF ,
195
+ (byte ) 0xFF ,
196
+ (byte ) 0xFF );
197
+
198
+ // Min int
199
+ out = new ByteArrayOutputStream ();
200
+ gen = cborGenerator (out );
201
+ gen .writeNumberUnsigned (Integer .MIN_VALUE );
202
+ gen .close ();
203
+ _verifyBytes (out .toByteArray (),
204
+ (byte ) (CBORConstants .PREFIX_TYPE_INT_POS + CBORConstants .SUFFIX_UINT32_ELEMENTS ),
205
+ (byte ) 0x80 ,
206
+ (byte ) 0x00 ,
207
+ (byte ) 0x00 ,
208
+ (byte ) 0x00 );
209
+
210
+ // Truncated to 2 bytes
211
+ out = new ByteArrayOutputStream ();
212
+ gen = cborGenerator (out );
213
+ gen .writeNumberUnsigned (1000 );
214
+ gen .close ();
215
+ _verifyBytes (out .toByteArray (),
216
+ (byte ) (CBORConstants .PREFIX_TYPE_INT_POS + CBORConstants .SUFFIX_UINT16_ELEMENTS ),
217
+ (byte ) 0x03 ,
218
+ (byte ) 0xE8 );
219
+
220
+ // Truncated to 1 byte
221
+ out = new ByteArrayOutputStream ();
222
+ gen = cborGenerator (out );
223
+ gen .writeNumberUnsigned (100 );
224
+ gen .close ();
225
+ _verifyBytes (out .toByteArray (),
226
+ (byte ) (CBORConstants .PREFIX_TYPE_INT_POS + CBORConstants .SUFFIX_UINT8_ELEMENTS ),
227
+ (byte ) 0x64 );
228
+
229
+ out = new ByteArrayOutputStream ();
230
+ gen = cborGenerator (out );
231
+ gen .writeNumberUnsigned (25 );
232
+ gen .close ();
233
+ _verifyBytes (out .toByteArray (),
234
+ (byte ) (CBORConstants .PREFIX_TYPE_INT_POS + CBORConstants .SUFFIX_UINT8_ELEMENTS ),
235
+ (byte ) 0x19 );
236
+
237
+ out = new ByteArrayOutputStream ();
238
+ gen = cborGenerator (out );
239
+ gen .writeNumberUnsigned (24 );
240
+ gen .close ();
241
+ _verifyBytes (out .toByteArray (),
242
+ (byte ) (CBORConstants .PREFIX_TYPE_INT_POS + CBORConstants .SUFFIX_UINT8_ELEMENTS ),
243
+ (byte ) 0x18 );
244
+
245
+ // Truncated to not take any extra bytes
246
+ out = new ByteArrayOutputStream ();
247
+ gen = cborGenerator (out );
248
+ gen .writeNumberUnsigned (23 );
249
+ gen .close ();
250
+ _verifyBytes (out .toByteArray (),
251
+ (byte ) 0x17 );
252
+
253
+ out = new ByteArrayOutputStream ();
254
+ gen = cborGenerator (out );
255
+ gen .writeNumberUnsigned (10 );
256
+ gen .close ();
257
+ _verifyBytes (out .toByteArray (),
258
+ (byte ) 0x0A );
259
+
260
+ out = new ByteArrayOutputStream ();
261
+ gen = cborGenerator (out );
262
+ gen .writeNumberUnsigned (1 );
263
+ gen .close ();
264
+ _verifyBytes (out .toByteArray (),
265
+ (byte ) 0x01 );
266
+
267
+ out = new ByteArrayOutputStream ();
268
+ gen = cborGenerator (out );
269
+ gen .writeNumberUnsigned (0 );
270
+ gen .close ();
271
+ _verifyBytes (out .toByteArray (),
272
+ (byte ) 0x00 );
273
+ }
274
+
184
275
@ Test
185
276
public void testLongValues () throws Exception
186
277
{
@@ -201,6 +292,119 @@ public void testLongValues() throws Exception
201
292
assertEquals (0 , b [3 ]);
202
293
}
203
294
295
+ @ Test
296
+ public void testUnsignedLongValues () throws Exception
297
+ {
298
+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
299
+ CBORGenerator gen = cborGenerator (out );
300
+
301
+ // uint64 max
302
+ gen .writeNumberUnsigned (-1L );
303
+ gen .close ();
304
+ _verifyBytes (out .toByteArray (),
305
+ (byte ) (CBORConstants .PREFIX_TYPE_INT_POS + CBORConstants .SUFFIX_UINT64_ELEMENTS ),
306
+ (byte ) 0xFF ,
307
+ (byte ) 0xFF ,
308
+ (byte ) 0xFF ,
309
+ (byte ) 0xFF ,
310
+ (byte ) 0xFF ,
311
+ (byte ) 0xFF ,
312
+ (byte ) 0xFF ,
313
+ (byte ) 0xFF );
314
+
315
+ // Min long
316
+ out = new ByteArrayOutputStream ();
317
+ gen = cborGenerator (out );
318
+ gen .writeNumberUnsigned (Long .MIN_VALUE );
319
+ gen .close ();
320
+ _verifyBytes (out .toByteArray (),
321
+ (byte ) (CBORConstants .PREFIX_TYPE_INT_POS + CBORConstants .SUFFIX_UINT64_ELEMENTS ),
322
+ (byte ) 0x80 ,
323
+ (byte ) 0x00 ,
324
+ (byte ) 0x00 ,
325
+ (byte ) 0x00 ,
326
+ (byte ) 0x00 ,
327
+ (byte ) 0x00 ,
328
+ (byte ) 0x00 ,
329
+ (byte ) 0x00 );
330
+
331
+ // Truncated to 4 bytes
332
+ out = new ByteArrayOutputStream ();
333
+ gen = cborGenerator (out );
334
+ gen .writeNumberUnsigned (1000000L );
335
+ gen .close ();
336
+ _verifyBytes (out .toByteArray (),
337
+ (byte ) (CBORConstants .PREFIX_TYPE_INT_POS + CBORConstants .SUFFIX_UINT32_ELEMENTS ),
338
+ (byte ) 0x00 ,
339
+ (byte ) 0x0F ,
340
+ (byte ) 0x42 ,
341
+ (byte ) 0x40 );
342
+
343
+ // Truncated to 2 bytes
344
+ out = new ByteArrayOutputStream ();
345
+ gen = cborGenerator (out );
346
+ gen .writeNumberUnsigned (1000L );
347
+ gen .close ();
348
+ _verifyBytes (out .toByteArray (),
349
+ (byte ) (CBORConstants .PREFIX_TYPE_INT_POS + CBORConstants .SUFFIX_UINT16_ELEMENTS ),
350
+ (byte ) 0x03 ,
351
+ (byte ) 0xE8 );
352
+
353
+ // Truncated to 1 byte
354
+ out = new ByteArrayOutputStream ();
355
+ gen = cborGenerator (out );
356
+ gen .writeNumberUnsigned (100L );
357
+ gen .close ();
358
+ _verifyBytes (out .toByteArray (),
359
+ (byte ) (CBORConstants .PREFIX_TYPE_INT_POS + CBORConstants .SUFFIX_UINT8_ELEMENTS ),
360
+ (byte ) 0x64 );
361
+
362
+ out = new ByteArrayOutputStream ();
363
+ gen = cborGenerator (out );
364
+ gen .writeNumberUnsigned (25L );
365
+ gen .close ();
366
+ _verifyBytes (out .toByteArray (),
367
+ (byte ) (CBORConstants .PREFIX_TYPE_INT_POS + CBORConstants .SUFFIX_UINT8_ELEMENTS ),
368
+ (byte ) 0x19 );
369
+
370
+ out = new ByteArrayOutputStream ();
371
+ gen = cborGenerator (out );
372
+ gen .writeNumberUnsigned (24L );
373
+ gen .close ();
374
+ _verifyBytes (out .toByteArray (),
375
+ (byte ) (CBORConstants .PREFIX_TYPE_INT_POS + CBORConstants .SUFFIX_UINT8_ELEMENTS ),
376
+ (byte ) 0x18 );
377
+
378
+ // Truncated to not take any extra bytes
379
+ out = new ByteArrayOutputStream ();
380
+ gen = cborGenerator (out );
381
+ gen .writeNumberUnsigned (23L );
382
+ gen .close ();
383
+ _verifyBytes (out .toByteArray (),
384
+ (byte ) 0x17 );
385
+
386
+ out = new ByteArrayOutputStream ();
387
+ gen = cborGenerator (out );
388
+ gen .writeNumberUnsigned (10L );
389
+ gen .close ();
390
+ _verifyBytes (out .toByteArray (),
391
+ (byte ) 0x0A );
392
+
393
+ out = new ByteArrayOutputStream ();
394
+ gen = cborGenerator (out );
395
+ gen .writeNumberUnsigned (1L );
396
+ gen .close ();
397
+ _verifyBytes (out .toByteArray (),
398
+ (byte ) 0x01 );
399
+
400
+ out = new ByteArrayOutputStream ();
401
+ gen = cborGenerator (out );
402
+ gen .writeNumberUnsigned (0L );
403
+ gen .close ();
404
+ _verifyBytes (out .toByteArray (),
405
+ (byte ) 0x00 );
406
+ }
407
+
204
408
@ Test
205
409
public void testFloatValues () throws Exception
206
410
{
0 commit comments