Skip to content

Commit 6516ba3

Browse files
committedMay 24, 2016
Add implementations for long[] and double[] writes

File tree

4 files changed

+398
-139
lines changed

4 files changed

+398
-139
lines changed
 

‎protobuf/release-notes/VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Project: jackson-dataformat-protobuf
66

77
2.8.0 (not yet released)
88

9-
- Support `writeStartArray(int[])`
9+
- Support `writeArray()` for `int[]`, `long[]` and `double[]`
1010
- Add `ProtobufMapper.generateSchemaFor(type)` helper methods
1111

1212
2.7.4 (29-Apr-2016)

‎protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufGenerator.java

+155-19
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,69 @@ public final void writeEndObject() throws IOException
480480

481481
@Override
482482
public void writeArray(int[] array, int offset, int length) throws IOException
483+
{
484+
_verifyArrayWrite(array);
485+
_verifyOffsets(array.length, offset, length);
486+
487+
// one minor optimization: empty arrays do not produce anything
488+
if (length > 0) {
489+
// NOTE: as a short-cut, leave out construction of intermediate ARRAY
490+
final int end = offset+length;
491+
if (_currField.packed) {
492+
_writePackedArray(array, offset, end);
493+
} else {
494+
_writeNonPackedArray(array, offset, end);
495+
}
496+
// and then pieces of END_ARRAY
497+
_writeTag = true;
498+
}
499+
}
500+
501+
@Override
502+
public void writeArray(long[] array, int offset, int length) throws IOException
503+
{
504+
_verifyArrayWrite(array);
505+
_verifyOffsets(array.length, offset, length);
506+
507+
// one minor optimization: empty arrays do not produce anything
508+
if (length > 0) {
509+
// NOTE: as a short-cut, leave out construction of intermediate ARRAY
510+
final int end = offset+length;
511+
if (_currField.packed) {
512+
_writePackedArray(array, offset, end);
513+
} else {
514+
_writeNonPackedArray(array, offset, end);
515+
}
516+
// and then pieces of END_ARRAY
517+
_writeTag = true;
518+
}
519+
}
520+
521+
@Override
522+
public void writeArray(double[] array, int offset, int length) throws IOException
523+
{
524+
_verifyArrayWrite(array);
525+
_verifyOffsets(array.length, offset, length);
526+
527+
// one minor optimization: empty arrays do not produce anything
528+
if (length > 0) {
529+
// NOTE: as a short-cut, leave out construction of intermediate ARRAY
530+
final int end = offset+length;
531+
if (_currField.packed) {
532+
_writePackedArray(array, offset, end);
533+
} else {
534+
_writeNonPackedArray(array, offset, end);
535+
}
536+
// and then pieces of END_ARRAY
537+
_writeTag = true;
538+
}
539+
}
540+
541+
private void _verifyArrayWrite(Object array) throws IOException
483542
{
484543
if (array == null) {
485544
throw new IllegalArgumentException("null array");
486545
}
487-
_verifyOffsets(array.length, offset, length);
488546
if (!_inObject) {
489547
_reportError("Current context not an OBJECT, can not write arrays");
490548
}
@@ -494,22 +552,6 @@ public void writeArray(int[] array, int offset, int length) throws IOException
494552
if (!_currField.isArray()) {
495553
_reportError("Can not write START_ARRAY: field '"+_currField.name+"' not declared as 'repeated'");
496554
}
497-
498-
// one minor optimization: empty arrays do not produce anything
499-
if (length == 0) {
500-
return;
501-
}
502-
503-
// NOTE: as a short-cut, leave out construction of intermediate ARRAY
504-
505-
final int end = offset+length;
506-
if (_currField.packed) {
507-
_writePackedArray(array, offset, end);
508-
} else {
509-
_writeNonPackedArray(array, offset, end);
510-
}
511-
// and then pieces of END_ARRAY
512-
_writeTag = true;
513555
}
514556

515557
private void _writePackedArray(int[] array, int i, int end) throws IOException
@@ -541,6 +583,54 @@ private void _writePackedArray(int[] array, int i, int end) throws IOException
541583
_finishBuffering();
542584
}
543585

586+
private void _writePackedArray(long[] array, int i, int end) throws IOException
587+
{
588+
_startBuffering(_currField.typedTag);
589+
final int type = _currField.wireType;
590+
591+
if (type == WireType.VINT) {
592+
final boolean zigzag = _currField.usesZigZag;
593+
for (; i < end; ++i) {
594+
long v = array[i];
595+
if (zigzag) {
596+
v = ProtobufUtil.zigzagEncode(v);
597+
}
598+
_writeVLongNoTag(v);
599+
}
600+
} else if (type == WireType.FIXED_32BIT) {
601+
for (; i < end; ++i) {
602+
_writeInt32NoTag((int) array[i]);
603+
}
604+
} else if (type == WireType.FIXED_64BIT) {
605+
for (; i < end; ++i) {
606+
_writeInt64NoTag(array[i]);
607+
}
608+
} else {
609+
_reportWrongWireType("int");
610+
}
611+
_finishBuffering();
612+
}
613+
614+
private void _writePackedArray(double[] array, int i, int end) throws IOException
615+
{
616+
_startBuffering(_currField.typedTag);
617+
final int type = _currField.wireType;
618+
619+
if (type == WireType.FIXED_64BIT) {
620+
for (; i < end; ++i) {
621+
_writeInt64NoTag(Double.doubleToLongBits( array[i]));
622+
}
623+
} else if (type == WireType.FIXED_32BIT) { // should we support such coercion?
624+
for (; i < end; ++i) {
625+
float f = (float) array[i];
626+
_writeInt32NoTag(Float.floatToRawIntBits(f));
627+
}
628+
} else {
629+
_reportWrongWireType("double");
630+
}
631+
_finishBuffering();
632+
}
633+
544634
private void _writeNonPackedArray(int[] array, int i, int end) throws IOException
545635
{
546636
final int type = _currField.wireType;
@@ -567,6 +657,52 @@ private void _writeNonPackedArray(int[] array, int i, int end) throws IOExceptio
567657
}
568658
}
569659

660+
private void _writeNonPackedArray(long[] array, int i, int end) throws IOException
661+
{
662+
final int type = _currField.wireType;
663+
664+
if (type == WireType.VINT) {
665+
final boolean zigzag = _currField.usesZigZag;
666+
for (; i < end; ++i) {
667+
long v = array[i];
668+
if (zigzag) {
669+
v = ProtobufUtil.zigzagEncode(v);
670+
}
671+
_writeVLong(v);
672+
}
673+
} else if (type == WireType.FIXED_32BIT) {
674+
for (; i < end; ++i) {
675+
_writeInt32((int) array[i]);
676+
}
677+
} else if (type == WireType.FIXED_64BIT) {
678+
for (; i < end; ++i) {
679+
_writeInt64(array[i]);
680+
}
681+
} else {
682+
_reportWrongWireType("int");
683+
}
684+
}
685+
686+
private void _writeNonPackedArray(double[] array, int i, int end) throws IOException
687+
{
688+
_startBuffering(_currField.typedTag);
689+
final int type = _currField.wireType;
690+
691+
if (type == WireType.FIXED_64BIT) {
692+
for (; i < end; ++i) {
693+
_writeInt64(Double.doubleToLongBits( array[i]));
694+
}
695+
} else if (type == WireType.FIXED_32BIT) { // should we support such coercion?
696+
for (; i < end; ++i) {
697+
float f = (float) array[i];
698+
_writeInt32(Float.floatToRawIntBits(f));
699+
}
700+
} else {
701+
_reportWrongWireType("double");
702+
}
703+
_finishBuffering();
704+
}
705+
570706
/*
571707
/**********************************************************
572708
/* Output method implementations, textual
@@ -1036,7 +1172,7 @@ public void writeNumber(BigInteger v) throws IOException
10361172
// !!! TODO: better scheme to detect overflow or something
10371173
writeNumber(v.longValue());
10381174
}
1039-
1175+
10401176
@Override
10411177
public void writeNumber(double d) throws IOException
10421178
{
@@ -1529,7 +1665,7 @@ private final void _writeInt64NoTag(long v64) throws IOException
15291665

15301666
_currPtr = ptr;
15311667
}
1532-
1668+
15331669
/*
15341670
/**********************************************************
15351671
/* Helper methods, buffering

‎protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/WriteArrayTest.java

+2-119
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,12 @@
99

1010
public class WriteArrayTest extends ProtobufTestBase
1111
{
12-
final protected static String PROTOC_INT_ARRAY_SPARSE = "message Ints {\n"
13-
+" repeated sint32 values = 1;\n"
14-
+"}\n"
15-
;
16-
17-
final protected static String PROTOC_INT_ARRAY_PACKED = "message Ints {\n"
18-
+" repeated sint32 values = 1 [packed=true];\n"
19-
+"}\n"
20-
;
21-
22-
final protected static String PROTOC_INT32_ARRAY_SPARSE = "message Ints {\n"
23-
+" repeated fixed32 values = 1;\n"
24-
+"}\n"
25-
;
26-
27-
final protected static String PROTOC_INT32_ARRAY_PACKED = "message Ints {\n"
28-
+" repeated fixed32 values = 1 [packed=true];\n"
29-
+"}\n"
30-
;
31-
32-
final protected static String PROTOC_INT64_ARRAY_SPARSE = "message Ints {\n"
33-
+" repeated fixed64 values = 1;\n"
34-
+"}\n"
35-
;
36-
37-
final protected static String PROTOC_INT64_ARRAY_PACKED = "message Ints {\n"
38-
+" repeated fixed64 values = 1 [packed=true];\n"
39-
+"}\n"
40-
;
41-
42-
final protected static String PROTOC_STRING_ARRAY_SPARSE = "message Ints {\n"
12+
final protected static String PROTOC_STRING_ARRAY_SPARSE = "message Strings {\n"
4313
+" repeated string values = 1;\n"
4414
+"}\n"
4515
;
4616

47-
final protected static String PROTOC_STRING_ARRAY_PACKED = "message Ints {\n"
17+
final protected static String PROTOC_STRING_ARRAY_PACKED = "message Strings {\n"
4818
+" repeated string values = 1 [packed=true];\n"
4919
+"}\n"
5020
;
@@ -61,14 +31,6 @@ public class WriteArrayTest extends ProtobufTestBase
6131
+PROTOC_POINT;
6232
;
6333

64-
static class IntArray {
65-
public int[] values;
66-
67-
public IntArray(int... v) {
68-
values = v;
69-
}
70-
}
71-
7234
static class StringArray {
7335
public String[] values;
7436

@@ -95,85 +57,6 @@ public WriteArrayTest() throws Exception {
9557
PACKED_STRING_SCHEMA = ProtobufSchemaLoader.std.parse(PROTOC_STRING_ARRAY_PACKED);
9658
}
9759

98-
/*
99-
/**********************************************************
100-
/* Test methods, int arrays
101-
/**********************************************************
102-
*/
103-
104-
public void testVIntArraySparse() throws Exception
105-
{
106-
/*
107-
final protected static String PROTOC_INT_ARRAY = "message Ints {\n"
108-
+" repeated int32 values = 1; }\n";
109-
*/
110-
final ObjectWriter w = MAPPER.writer(ProtobufSchemaLoader.std.parse(PROTOC_INT_ARRAY_SPARSE));
111-
byte[] bytes = w.writeValueAsBytes(new IntArray(3, -1, 2));
112-
// 3 x 2 bytes per value (typed tag, value) -> 6
113-
assertEquals(6, bytes.length);
114-
assertEquals(0x8, bytes[0]); // zig-zagged vint (0) value, field 1
115-
assertEquals(0x6, bytes[1]); // zig-zagged value for 3
116-
assertEquals(0x8, bytes[2]);
117-
assertEquals(0x1, bytes[3]); // zig-zagged value for -1
118-
assertEquals(0x8, bytes[4]);
119-
assertEquals(0x4, bytes[5]); // zig-zagged value for 2
120-
}
121-
122-
public void testVIntArrayPacked() throws Exception
123-
{
124-
final ObjectWriter w = MAPPER.writer(ProtobufSchemaLoader.std.parse(PROTOC_INT_ARRAY_PACKED));
125-
byte[] bytes = w.writeValueAsBytes(new IntArray(3, -1, 2));
126-
// 1 byte for typed tag, 1 byte for length, 3 x 1 byte per value -> 5
127-
assertEquals(5, bytes.length);
128-
assertEquals(0x8, bytes[0]); // zig-zagged vint (0) value, field 1
129-
assertEquals(0x3, bytes[1]); // length for array, 3 bytes
130-
assertEquals(0x6, bytes[2]); // zig-zagged value for 3
131-
assertEquals(0x1, bytes[3]); // zig-zagged value for -1
132-
assertEquals(0x4, bytes[4]); // zig-zagged value for 2
133-
}
134-
135-
public void testInt32ArraySparse() throws Exception
136-
{
137-
final ObjectWriter w = MAPPER.writer(ProtobufSchemaLoader.std.parse
138-
(PROTOC_INT32_ARRAY_SPARSE));
139-
byte[] bytes = w.writeValueAsBytes(new IntArray(3, -1, 2));
140-
// 3 x 5 bytes per value (typed tag, value) -> 18
141-
assertEquals(15, bytes.length);
142-
}
143-
144-
public void testInt32ArrayPacked() throws Exception
145-
{
146-
final ObjectWriter w = MAPPER.writer(ProtobufSchemaLoader.std.parse
147-
(PROTOC_INT32_ARRAY_PACKED));
148-
byte[] bytes = w.writeValueAsBytes(new IntArray(3, -1, 2));
149-
// 1 byte for typed tag, 1 byte for length, 3 x 4 byte per value -> 14
150-
assertEquals(14, bytes.length);
151-
}
152-
153-
/*
154-
/**********************************************************
155-
/* Test methods, long arrays
156-
/**********************************************************
157-
*/
158-
159-
public void testIntAsLongArraySparse() throws Exception
160-
{
161-
final ObjectWriter w = MAPPER.writer(ProtobufSchemaLoader.std.parse
162-
(PROTOC_INT64_ARRAY_SPARSE));
163-
byte[] bytes = w.writeValueAsBytes(new IntArray(3, -1, 2));
164-
// 3 x 9 bytes per value (typed tag, value) -> 30
165-
assertEquals(27, bytes.length);
166-
}
167-
168-
public void testIntAsLongArrayPacked() throws Exception
169-
{
170-
final ObjectWriter w = MAPPER.writer(ProtobufSchemaLoader.std.parse
171-
(PROTOC_INT64_ARRAY_PACKED));
172-
byte[] bytes = w.writeValueAsBytes(new IntArray(3, -1, 2));
173-
// 1 byte for typed tag, 1 byte for length, 3 x 8 byte per value -> 26
174-
assertEquals(26, bytes.length);
175-
}
176-
17760
/*
17861
/**********************************************************
17962
/* Test methods, String arrays
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
package com.fasterxml.jackson.dataformat.protobuf;
2+
3+
import org.junit.Assert;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.ObjectWriter;
7+
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema;
8+
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchemaLoader;
9+
10+
public class WritePrimitiveArrayTest extends ProtobufTestBase
11+
{
12+
final protected static String PROTOC_INT_ARRAY_SPARSE = "message Ints {\n"
13+
+" repeated sint32 values = 1;\n"
14+
+"}\n"
15+
;
16+
17+
final protected static String PROTOC_INT_ARRAY_PACKED = "message Ints {\n"
18+
+" repeated sint32 values = 1 [packed=true];\n"
19+
+"}\n"
20+
;
21+
22+
final protected static String PROTOC_INT32_ARRAY_SPARSE = "message Ints {\n"
23+
+" repeated fixed32 values = 1;\n"
24+
+"}\n"
25+
;
26+
27+
final protected static String PROTOC_INT32_ARRAY_PACKED = "message Ints {\n"
28+
+" repeated fixed32 values = 1 [packed=true];\n"
29+
+"}\n"
30+
;
31+
32+
final protected static String PROTOC_INT64_ARRAY_SPARSE = "message Longs {\n"
33+
+" repeated fixed64 values = 1;\n"
34+
+"}\n"
35+
;
36+
37+
final protected static String PROTOC_INT64_ARRAY_PACKED = "message Longs {\n"
38+
+" repeated fixed64 values = 1 [packed=true];\n"
39+
+"}\n"
40+
;
41+
42+
final protected static String PROTOC_DOUBLE_ARRAY_SPARSE = "message Doubles {\n"
43+
+" repeated double values = 1;\n"
44+
+"}\n"
45+
;
46+
47+
final protected static String PROTOC_DOUBLE_ARRAY_PACKED = "message Doubles {\n"
48+
+" repeated double values = 1 [packed=true];\n"
49+
+"}\n"
50+
;
51+
52+
static class IntArray {
53+
public int[] values;
54+
55+
protected IntArray() { }
56+
public IntArray(int... v) {
57+
values = v;
58+
}
59+
}
60+
61+
static class LongArray {
62+
public long[] values;
63+
64+
protected LongArray() { }
65+
public LongArray(long... v) {
66+
values = v;
67+
}
68+
}
69+
70+
static class DoubleArray {
71+
public double[] values;
72+
73+
protected DoubleArray() { }
74+
public DoubleArray(double... v) {
75+
values = v;
76+
}
77+
}
78+
79+
final ObjectMapper MAPPER = new ObjectMapper(new ProtobufFactory());
80+
81+
public WritePrimitiveArrayTest() throws Exception { }
82+
83+
/*
84+
/**********************************************************
85+
/* Test methods, int arrays
86+
/**********************************************************
87+
*/
88+
89+
public void testVIntArraySparse() throws Exception
90+
{
91+
final ObjectWriter w = MAPPER.writer(ProtobufSchemaLoader.std.parse(PROTOC_INT_ARRAY_SPARSE));
92+
byte[] bytes = w.writeValueAsBytes(new IntArray(3, -1, 2));
93+
// 3 x 2 bytes per value (typed tag, value) -> 6
94+
assertEquals(6, bytes.length);
95+
assertEquals(0x8, bytes[0]); // zig-zagged vint (0) value, field 1
96+
assertEquals(0x6, bytes[1]); // zig-zagged value for 3
97+
assertEquals(0x8, bytes[2]);
98+
assertEquals(0x1, bytes[3]); // zig-zagged value for -1
99+
assertEquals(0x8, bytes[4]);
100+
assertEquals(0x4, bytes[5]); // zig-zagged value for 2
101+
}
102+
103+
public void testVIntArrayPacked() throws Exception
104+
{
105+
final ObjectWriter w = MAPPER.writer(ProtobufSchemaLoader.std.parse(PROTOC_INT_ARRAY_PACKED));
106+
byte[] bytes = w.writeValueAsBytes(new IntArray(3, -1, 2));
107+
// 1 byte for typed tag, 1 byte for length, 3 x 1 byte per value -> 5
108+
assertEquals(5, bytes.length);
109+
assertEquals(0x8, bytes[0]); // zig-zagged vint (0) value, field 1
110+
assertEquals(0x3, bytes[1]); // length for array, 3 bytes
111+
assertEquals(0x6, bytes[2]); // zig-zagged value for 3
112+
assertEquals(0x1, bytes[3]); // zig-zagged value for -1
113+
assertEquals(0x4, bytes[4]); // zig-zagged value for 2
114+
}
115+
116+
public void testInt32ArraySparse() throws Exception
117+
{
118+
final ObjectWriter w = MAPPER.writer(ProtobufSchemaLoader.std.parse
119+
(PROTOC_INT32_ARRAY_SPARSE));
120+
byte[] bytes = w.writeValueAsBytes(new IntArray(3, -1, 2));
121+
// 3 x 5 bytes per value (typed tag, value) -> 18
122+
assertEquals(15, bytes.length);
123+
}
124+
125+
public void testInt32ArrayPacked() throws Exception
126+
{
127+
final ObjectWriter w = MAPPER.writer(ProtobufSchemaLoader.std.parse
128+
(PROTOC_INT32_ARRAY_PACKED));
129+
byte[] bytes = w.writeValueAsBytes(new IntArray(3, -1, 2));
130+
// 1 byte for typed tag, 1 byte for length, 3 x 4 byte per value -> 14
131+
assertEquals(14, bytes.length);
132+
}
133+
134+
/*
135+
/**********************************************************
136+
/* Test methods, long arrays
137+
/**********************************************************
138+
*/
139+
140+
// // // First as ints:
141+
142+
public void testIntAsLongArraySparse() throws Exception
143+
{
144+
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_INT64_ARRAY_SPARSE);
145+
final ObjectWriter w = MAPPER.writer(schema);
146+
IntArray input = new IntArray(3, -1, -2);
147+
byte[] bytes = w.writeValueAsBytes(input);
148+
// 3 x 9 bytes per value (typed tag, value) -> 30
149+
assertEquals(27, bytes.length);
150+
151+
IntArray result = MAPPER.readerFor(IntArray.class).with(schema)
152+
.readValue(bytes);
153+
Assert.assertArrayEquals(input.values, result.values);
154+
}
155+
156+
public void testIntAsLongArrayPacked() throws Exception
157+
{
158+
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_INT64_ARRAY_PACKED);
159+
final ObjectWriter w = MAPPER.writer(schema);
160+
IntArray input = new IntArray(3, -1, -2);
161+
byte[] bytes = w.writeValueAsBytes(input);
162+
// 1 byte for typed tag, 1 byte for length, 3 x 8 byte per value -> 26
163+
assertEquals(26, bytes.length);
164+
165+
IntArray result = MAPPER.readerFor(IntArray.class).with(schema)
166+
.readValue(bytes);
167+
Assert.assertArrayEquals(input.values, result.values);
168+
}
169+
170+
// // // But then as regular longs
171+
172+
public void testLongArraySparse() throws Exception
173+
{
174+
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_INT64_ARRAY_SPARSE);
175+
final ObjectWriter w = MAPPER.writer(schema);
176+
LongArray input = new LongArray(Integer.MAX_VALUE, -1, Long.MIN_VALUE);
177+
byte[] bytes = w.writeValueAsBytes(input);
178+
assertEquals(27, bytes.length);
179+
180+
LongArray result = MAPPER.readerFor(LongArray.class).with(schema)
181+
.readValue(bytes);
182+
Assert.assertArrayEquals(input.values, result.values);
183+
}
184+
185+
public void testLongArrayPacked() throws Exception
186+
{
187+
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_INT64_ARRAY_PACKED);
188+
final ObjectWriter w = MAPPER.writer(schema);
189+
LongArray input = new LongArray(Integer.MIN_VALUE, -1, Long.MAX_VALUE);
190+
byte[] bytes = w.writeValueAsBytes(input);
191+
assertEquals(26, bytes.length);
192+
193+
LongArray result = MAPPER.readerFor(LongArray.class).with(schema)
194+
.readValue(bytes);
195+
Assert.assertArrayEquals(input.values, result.values);
196+
}
197+
198+
/*
199+
/**********************************************************
200+
/* Test methods, floating-point arrays
201+
/**********************************************************
202+
*/
203+
204+
public void testDoubleArraySparse() throws Exception
205+
{
206+
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_DOUBLE_ARRAY_SPARSE);
207+
final ObjectWriter w = MAPPER.writer(schema);
208+
DoubleArray input = new DoubleArray(0.25, -2.5, 1000.125);
209+
byte[] bytes = w.writeValueAsBytes(input);
210+
assertEquals(27, bytes.length);
211+
212+
DoubleArray result = MAPPER.readerFor(DoubleArray.class).with(schema)
213+
.readValue(bytes);
214+
_assertEquals(input.values, result.values);
215+
}
216+
217+
public void testDoubleArrayPacked() throws Exception
218+
{
219+
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_DOUBLE_ARRAY_PACKED);
220+
final ObjectWriter w = MAPPER.writer(schema);
221+
DoubleArray input = new DoubleArray(-0.5, 89245.25, 0.625);
222+
byte[] bytes = w.writeValueAsBytes(input);
223+
assertEquals(26, bytes.length);
224+
225+
DoubleArray result = MAPPER.readerFor(DoubleArray.class).with(schema)
226+
.readValue(bytes);
227+
_assertEquals(input.values, result.values);
228+
}
229+
230+
private void _assertEquals(double[] exp, double[] act)
231+
{
232+
assertEquals(exp.length, act.length);
233+
for (int i = 0; i < exp.length; ++i) {
234+
// note: caller ensures it only uses values that reliably round-trip
235+
if (exp[i] != act[i]) {
236+
fail("Entry #"+i+" wrong: expected "+exp[i]+", got "+act[i]);
237+
}
238+
}
239+
}
240+
}

0 commit comments

Comments
 (0)
Please sign in to comment.