-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
ArrayGenerationTest.java
214 lines (190 loc) · 7.28 KB
/
ArrayGenerationTest.java
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
213
214
package com.fasterxml.jackson.dataformat.cbor.gen;
import java.io.ByteArrayOutputStream;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.JsonParser.NumberType;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import com.fasterxml.jackson.dataformat.cbor.CBORGenerator;
import com.fasterxml.jackson.dataformat.cbor.CBORParser;
import com.fasterxml.jackson.dataformat.cbor.CBORTestBase;
/**
* Basic testing for scalar-array write methods added in 2.8.
*/
public class ArrayGenerationTest extends CBORTestBase
{
private final CBORFactory FACTORY = new CBORFactory();
public void testIntArray() throws Exception
{
_testIntArray();
}
public void testLongArray() throws Exception
{
_testLongArray();
}
public void testDoubleArray() throws Exception
{
_testDoubleArray();
}
public void testMinimalIntValues2() throws Exception
{
// Array with 2 values that can't be passed as `int`s but DO fit
// CBOR 5-byte int (sign + 32-bits)
final long[] input = new long[] {
0xffffffffL, // max value that fits
-0xffffffffL - 1 // min value that fits
};
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
CBORGenerator gen = FACTORY.createGenerator(bytes);
assertTrue(gen.isEnabled(CBORGenerator.Feature.WRITE_MINIMAL_INTS));
gen.writeArray(input, 0, 2);
gen.close();
// With default settings, should get:
byte[] encoded = bytes.toByteArray();
assertEquals(11, encoded.length);
// then verify contents
CBORParser p = FACTORY.createParser(encoded);
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(NumberType.LONG, p.getNumberType());
assertEquals(input[0], p.getLongValue());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(NumberType.LONG, p.getNumberType());
assertEquals(input[1], p.getLongValue());
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
// but then also check without minimization
}
private void _testIntArray() throws Exception {
// first special cases of 0, 1 values
_testIntArray(0, 0, 0);
_testIntArray(0, 1, 1);
_testIntArray(1, 0, 0);
_testIntArray(1, 1, 1);
// and then some bigger data
_testIntArray(15, 0, 0);
_testIntArray(15, 2, 3);
_testIntArray(39, 0, 0);
_testIntArray(39, 4, 0);
_testIntArray(271, 0, 0);
_testIntArray(271, 0, 4);
_testIntArray(666, 0, 0);
_testIntArray(789, 0, 4);
_testIntArray(5009, 0, 0);
_testIntArray(7777, 0, 1);
}
private void _testLongArray() throws Exception {
// first special cases of 0, 1 values
_testLongArray(0, 0, 0);
_testLongArray(0, 1, 1);
_testLongArray(1, 0, 0);
_testLongArray(1, 1, 1);
// and then some bigger data
_testLongArray(15, 0, 0);
_testLongArray(15, 2, 3);
_testLongArray(39, 0, 0);
_testLongArray(39, 4, 0);
_testLongArray(271, 0, 0);
_testLongArray(271, 0, 4);
_testLongArray(911, 0, 0);
_testLongArray(1121, 0, 1);
_testLongArray(5009, 0, 0);
_testLongArray(6110, 0, 1);
}
private void _testDoubleArray() throws Exception {
// first special cases of 0, 1 values
_testDoubleArray(0, 0, 0);
_testDoubleArray(0, 1, 1);
_testDoubleArray(1, 0, 0);
_testDoubleArray(1, 1, 1);
// and then some bigger data
_testDoubleArray(15, 0, 0);
_testDoubleArray(15, 2, 3);
_testDoubleArray(39, 0, 0);
_testDoubleArray(39, 4, 0);
_testDoubleArray(271, 0, 0);
_testDoubleArray(271, 0, 4);
_testDoubleArray(744, 0, 0);
_testDoubleArray(999, 0, 4);
_testDoubleArray(5009, 0, 0);
_testDoubleArray(7256, 0, 1);
}
private void _testIntArray(int elements, int pre, int post) throws Exception
{
int[] values = new int[elements+pre+post];
for (int i = pre, end = pre+elements; i < end; ++i) {
values[i] = i-pre;
}
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
JsonGenerator gen = FACTORY.createGenerator(bytes);
gen.writeArray(values, pre, elements);
gen.close();
JsonParser p = FACTORY.createParser(bytes.toByteArray());
assertToken(JsonToken.START_ARRAY, p.nextToken());
for (int i = 0; i < elements; ++i) {
if ((i & 1) == 0) { // alternate
JsonToken t = p.nextToken();
if (t != JsonToken.VALUE_NUMBER_INT) {
fail("Expected number, got "+t+", element #"+i);
}
int act = p.getIntValue();
if (act != i) {
fail("Entry #"+i+", expected "+i+", got "+act);
}
} else {
assertEquals(i, p.nextIntValue(-1));
}
}
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
}
private void _testLongArray(int elements, int pre, int post) throws Exception
{
long[] values = new long[elements+pre+post];
for (int i = pre, end = pre+elements; i < end; ++i) {
values[i] = i-pre;
}
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
JsonGenerator gen = FACTORY.createGenerator(bytes);
gen.writeArray(values, pre, elements);
gen.close();
JsonParser p = FACTORY.createParser(bytes.toByteArray());
assertToken(JsonToken.START_ARRAY, p.nextToken());
for (int i = 0; i < elements; ++i) {
if ((i & 1) == 0) { // alternate
JsonToken t = p.nextToken();
if (t != JsonToken.VALUE_NUMBER_INT) {
fail("Expected number, got "+t+", element #"+i);
}
long act = p.getLongValue();
if (act != i) {
fail("Entry #"+i+", expected "+i+", got "+act);
}
} else {
assertEquals(i, p.nextLongValue(-1));
}
}
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
}
private void _testDoubleArray(int elements, int pre, int post) throws Exception
{
double[] values = new double[elements+pre+post];
for (int i = pre, end = pre+elements; i < end; ++i) {
values[i] = i-pre;
}
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
JsonGenerator gen = FACTORY.createGenerator(bytes);
gen.writeArray(values, pre, elements);
gen.close();
JsonParser p = FACTORY.createParser(bytes.toByteArray());
assertToken(JsonToken.START_ARRAY, p.nextToken());
for (int i = 0; i < elements; ++i) {
JsonToken t = p.nextToken();
if (t != JsonToken.VALUE_NUMBER_FLOAT) {
fail("Expected floating-point number, got "+t+", element #"+i);
}
assertEquals((double) i, p.getDoubleValue());
}
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
}
}