@@ -31,7 +31,69 @@ public void testDoubleArray() throws Exception
31
31
_testDoubleArray ();
32
32
}
33
33
34
- public void testMinimalIntValues2 () throws Exception
34
+ public void testMinimalIntValuesForInt () throws Exception
35
+ {
36
+ // Array with 3 values, with different sizing
37
+ _testMinimalIntValuesForInt (1 , -1 , 3 , 11 ); // single-byte
38
+ _testMinimalIntValuesForInt (200 , -200 , 5 , 11 ); // two-byte (marker, 0xFF)
39
+ _testMinimalIntValuesForInt (0xC831 , -50000 , 7 , 11 ); // three-byte (marker, 0xFFFF)
40
+ _testMinimalIntValuesForInt (0x35690001 , -(0x7FFFFFF0 ), 11 , 11 ); // full
41
+ }
42
+
43
+ private void _testMinimalIntValuesForInt (int v1 , int v2 ,
44
+ int minLen , int fullLen ) throws Exception
45
+ {
46
+ final int [] input = new int [] { v1 , v2 };
47
+ ByteArrayOutputStream bytes = new ByteArrayOutputStream ();
48
+ CBORGenerator gen = FACTORY .createGenerator (bytes );
49
+ assertTrue (gen .isEnabled (CBORGenerator .Feature .WRITE_MINIMAL_INTS ));
50
+ gen .writeArray (input , 0 , 2 );
51
+ gen .close ();
52
+
53
+ // With default settings, should get:
54
+ byte [] encoded = bytes .toByteArray ();
55
+ assertEquals (minLen , encoded .length );
56
+
57
+ // then verify contents
58
+
59
+ CBORParser p = FACTORY .createParser (encoded );
60
+ assertToken (JsonToken .START_ARRAY , p .nextToken ());
61
+ assertToken (JsonToken .VALUE_NUMBER_INT , p .nextToken ());
62
+ assertEquals (NumberType .INT , p .getNumberType ());
63
+ assertEquals (input [0 ], p .getIntValue ());
64
+ assertToken (JsonToken .VALUE_NUMBER_INT , p .nextToken ());
65
+ assertEquals (NumberType .INT , p .getNumberType ());
66
+ assertEquals (input [1 ], p .getIntValue ());
67
+ assertToken (JsonToken .END_ARRAY , p .nextToken ());
68
+ p .close ();
69
+
70
+ // but then also check without minimization
71
+ bytes = new ByteArrayOutputStream ();
72
+ gen = FACTORY .createGenerator (bytes );
73
+ gen .disable (CBORGenerator .Feature .WRITE_MINIMAL_INTS );
74
+
75
+ gen .writeArray (input , 0 , 2 );
76
+ gen .close ();
77
+
78
+ // With default settings, should get:
79
+ encoded = bytes .toByteArray ();
80
+ assertEquals (fullLen , encoded .length );
81
+
82
+ // then verify contents
83
+
84
+ p = FACTORY .createParser (encoded );
85
+ assertToken (JsonToken .START_ARRAY , p .nextToken ());
86
+ assertToken (JsonToken .VALUE_NUMBER_INT , p .nextToken ());
87
+ assertEquals (NumberType .INT , p .getNumberType ());
88
+ assertEquals (input [0 ], p .getIntValue ());
89
+ assertToken (JsonToken .VALUE_NUMBER_INT , p .nextToken ());
90
+ assertEquals (NumberType .INT , p .getNumberType ());
91
+ assertEquals (input [1 ], p .getIntValue ());
92
+ assertToken (JsonToken .END_ARRAY , p .nextToken ());
93
+ p .close ();
94
+ }
95
+
96
+ public void testMinimalIntValuesForLong () throws Exception
35
97
{
36
98
// Array with 2 values that can't be passed as `int`s but DO fit
37
99
// CBOR 5-byte int (sign + 32-bits)
@@ -63,6 +125,29 @@ public void testMinimalIntValues2() throws Exception
63
125
p .close ();
64
126
65
127
// but then also check without minimization
128
+ bytes = new ByteArrayOutputStream ();
129
+ gen = FACTORY .createGenerator (bytes );
130
+ gen .disable (CBORGenerator .Feature .WRITE_MINIMAL_INTS );
131
+
132
+ gen .writeArray (input , 0 , 2 );
133
+ gen .close ();
134
+
135
+ // With default settings, should get:
136
+ encoded = bytes .toByteArray ();
137
+ assertEquals (19 , encoded .length );
138
+
139
+ // then verify contents
140
+
141
+ p = FACTORY .createParser (encoded );
142
+ assertToken (JsonToken .START_ARRAY , p .nextToken ());
143
+ assertToken (JsonToken .VALUE_NUMBER_INT , p .nextToken ());
144
+ assertEquals (NumberType .LONG , p .getNumberType ());
145
+ assertEquals (input [0 ], p .getLongValue ());
146
+ assertToken (JsonToken .VALUE_NUMBER_INT , p .nextToken ());
147
+ assertEquals (NumberType .LONG , p .getNumberType ());
148
+ assertEquals (input [1 ], p .getLongValue ());
149
+ assertToken (JsonToken .END_ARRAY , p .nextToken ());
150
+ p .close ();
66
151
}
67
152
68
153
private void _testIntArray () throws Exception {
0 commit comments