@@ -48,6 +48,16 @@ public class WritePrimitiveArrayTest extends ProtobufTestBase
48
48
+" repeated double values = 1 [packed=true];\n "
49
49
+"}\n "
50
50
;
51
+
52
+ final protected static String PROTOC_FLOAT_ARRAY_SPARSE = "message Floats {\n "
53
+ +" repeated float values = 1;\n "
54
+ +"}\n "
55
+ ;
56
+
57
+ final protected static String PROTOC_FLOAT_ARRAY_PACKED = "message Floats {\n "
58
+ +" repeated float values = 1 [packed=true];\n "
59
+ +"}\n "
60
+ ;
51
61
52
62
static class IntArray {
53
63
public int [] values ;
@@ -75,8 +85,17 @@ public DoubleArray(double... v) {
75
85
values = v ;
76
86
}
77
87
}
78
-
79
- final ObjectMapper MAPPER = new ObjectMapper (new ProtobufFactory ());
88
+
89
+ static class FloatArray {
90
+ public float [] values ;
91
+
92
+ protected FloatArray () { }
93
+ public FloatArray (float ... v ) {
94
+ values = v ;
95
+ }
96
+ }
97
+
98
+ final ObjectMapper MAPPER = new ProtobufMapper ();
80
99
81
100
public WritePrimitiveArrayTest () throws Exception { }
82
101
@@ -205,9 +224,9 @@ public void testDoubleArraySparse() throws Exception
205
224
{
206
225
ProtobufSchema schema = ProtobufSchemaLoader .std .parse (PROTOC_DOUBLE_ARRAY_SPARSE );
207
226
final ObjectWriter w = MAPPER .writer (schema );
208
- DoubleArray input = new DoubleArray (0.25 , -2.5 , 1000.125 );
227
+ DoubleArray input = new DoubleArray (0.25 , -2.5 , 1000.125 , 1234567891234567890.5 );
209
228
byte [] bytes = w .writeValueAsBytes (input );
210
- assertEquals (27 , bytes .length );
229
+ assertEquals (36 , bytes .length );
211
230
212
231
DoubleArray result = MAPPER .readerFor (DoubleArray .class ).with (schema )
213
232
.readValue (bytes );
@@ -218,9 +237,9 @@ public void testDoubleArrayPacked() throws Exception
218
237
{
219
238
ProtobufSchema schema = ProtobufSchemaLoader .std .parse (PROTOC_DOUBLE_ARRAY_PACKED );
220
239
final ObjectWriter w = MAPPER .writer (schema );
221
- DoubleArray input = new DoubleArray (-0.5 , 89245.25 , 0.625 );
240
+ DoubleArray input = new DoubleArray (-0.5 , 89245.25 , 0.625 , 1234567891234567890.5 );
222
241
byte [] bytes = w .writeValueAsBytes (input );
223
- assertEquals (26 , bytes .length );
242
+ assertEquals (34 , bytes .length );
224
243
225
244
DoubleArray result = MAPPER .readerFor (DoubleArray .class ).with (schema )
226
245
.readValue (bytes );
@@ -237,4 +256,48 @@ private void _assertEquals(double[] exp, double[] act)
237
256
}
238
257
}
239
258
}
259
+
260
+ /*
261
+ /**********************************************************
262
+ /* Test methods, floating-point arrays
263
+ /**********************************************************
264
+ */
265
+
266
+ public void testfloatArraySparse () throws Exception
267
+ {
268
+ ProtobufSchema schema = ProtobufSchemaLoader .std .parse (PROTOC_FLOAT_ARRAY_SPARSE );
269
+ final ObjectWriter w = MAPPER .writer (schema );
270
+ FloatArray input = new FloatArray (0.25f , -2.5f , 55555555.5f );
271
+ byte [] bytes = w .writeValueAsBytes (input );
272
+ assertEquals (15 , bytes .length );
273
+
274
+ FloatArray result = MAPPER .readerFor (FloatArray .class ).with (schema )
275
+ .readValue (bytes );
276
+ _assertEquals (input .values , result .values );
277
+ }
278
+
279
+ public void testfloatArrayPacked () throws Exception
280
+ {
281
+ ProtobufSchema schema = ProtobufSchemaLoader .std .parse (PROTOC_FLOAT_ARRAY_PACKED );
282
+ final ObjectWriter w = MAPPER .writer (schema );
283
+ FloatArray input = new FloatArray (-0.5f , 89245.25f , 55555555.5f );
284
+ byte [] bytes = w .writeValueAsBytes (input );
285
+ assertEquals (14 , bytes .length );
286
+
287
+ FloatArray result = MAPPER .readerFor (FloatArray .class ).with (schema )
288
+ .readValue (bytes );
289
+ _assertEquals (input .values , result .values );
290
+ }
291
+
292
+ private void _assertEquals (float [] exp , float [] act )
293
+ {
294
+ assertEquals (exp .length , act .length );
295
+ for (int i = 0 ; i < exp .length ; ++i ) {
296
+ // note: caller ensures it only uses values that reliably round-trip
297
+ if (exp [i ] != act [i ]) {
298
+ fail ("Entry #" +i +" wrong: expected " +exp [i ]+", got " +act [i ]);
299
+ }
300
+ }
301
+ }
302
+
240
303
}
0 commit comments