3
3
import java .io .ByteArrayOutputStream ;
4
4
5
5
import com .fasterxml .jackson .core .*;
6
+ import com .fasterxml .jackson .core .JsonParser .NumberType ;
6
7
import com .fasterxml .jackson .dataformat .cbor .CBORFactory ;
8
+ import com .fasterxml .jackson .dataformat .cbor .CBORGenerator ;
9
+ import com .fasterxml .jackson .dataformat .cbor .CBORParser ;
7
10
import com .fasterxml .jackson .dataformat .cbor .CBORTestBase ;
8
11
9
12
/**
@@ -15,23 +18,54 @@ public class ArrayGenerationTest extends CBORTestBase
15
18
16
19
public void testIntArray () throws Exception
17
20
{
18
- _testIntArray (false );
19
- _testIntArray (true );
21
+ _testIntArray ();
20
22
}
21
23
22
24
public void testLongArray () throws Exception
23
25
{
24
- _testLongArray (false );
25
- _testLongArray (true );
26
+ _testLongArray ();
26
27
}
27
28
28
29
public void testDoubleArray () throws Exception
29
30
{
30
- _testDoubleArray (false );
31
- _testDoubleArray (true );
31
+ _testDoubleArray ();
32
32
}
33
33
34
- private void _testIntArray (boolean useBytes ) throws Exception {
34
+ public void testMinimalIntValues2 () throws Exception
35
+ {
36
+ // Array with 2 values that can't be passed as `int`s but DO fit
37
+ // CBOR 5-byte int (sign + 32-bits)
38
+ final long [] input = new long [] {
39
+ 0xffffffffL , // max value that fits
40
+ -0xffffffffL - 1 // min value that fits
41
+ };
42
+ ByteArrayOutputStream bytes = new ByteArrayOutputStream ();
43
+ CBORGenerator gen = FACTORY .createGenerator (bytes );
44
+ assertTrue (gen .isEnabled (CBORGenerator .Feature .WRITE_MINIMAL_INTS ));
45
+ gen .writeArray (input , 0 , 2 );
46
+ gen .close ();
47
+
48
+ // With default settings, should get:
49
+ byte [] encoded = bytes .toByteArray ();
50
+ assertEquals (11 , encoded .length );
51
+
52
+ // then verify contents
53
+
54
+ CBORParser p = FACTORY .createParser (encoded );
55
+ assertToken (JsonToken .START_ARRAY , p .nextToken ());
56
+ assertToken (JsonToken .VALUE_NUMBER_INT , p .nextToken ());
57
+ assertEquals (NumberType .LONG , p .getNumberType ());
58
+ assertEquals (input [0 ], p .getLongValue ());
59
+ assertToken (JsonToken .VALUE_NUMBER_INT , p .nextToken ());
60
+ assertEquals (NumberType .LONG , p .getNumberType ());
61
+ assertEquals (input [1 ], p .getLongValue ());
62
+ assertToken (JsonToken .END_ARRAY , p .nextToken ());
63
+ p .close ();
64
+
65
+ // but then also check without minimization
66
+ }
67
+
68
+ private void _testIntArray () throws Exception {
35
69
// first special cases of 0, 1 values
36
70
_testIntArray (0 , 0 , 0 );
37
71
_testIntArray (0 , 1 , 1 );
@@ -52,7 +86,7 @@ private void _testIntArray(boolean useBytes) throws Exception {
52
86
_testIntArray (7777 , 0 , 1 );
53
87
}
54
88
55
- private void _testLongArray (boolean useBytes ) throws Exception {
89
+ private void _testLongArray () throws Exception {
56
90
// first special cases of 0, 1 values
57
91
_testLongArray (0 , 0 , 0 );
58
92
_testLongArray (0 , 1 , 1 );
@@ -73,7 +107,7 @@ private void _testLongArray(boolean useBytes) throws Exception {
73
107
_testLongArray (6110 , 0 , 1 );
74
108
}
75
109
76
- private void _testDoubleArray (boolean useBytes ) throws Exception {
110
+ private void _testDoubleArray () throws Exception {
77
111
// first special cases of 0, 1 values
78
112
_testDoubleArray (0 , 0 , 0 );
79
113
_testDoubleArray (0 , 1 , 1 );
0 commit comments