Skip to content

Commit

Permalink
Fix #85
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 3, 2017
1 parent 78bd410 commit 1a53cd5
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2382,8 +2382,8 @@ protected final int _decode32Bits() throws IOException {
return _slow32();
}
final byte[] b = _inputBuffer;
int v = (b[ptr] & 0xFF) + ((b[ptr+1]) << 8)
+ ((b[ptr+2] & 0xFF) << 16) + (b[ptr+3] << 24);
int v = (b[ptr] & 0xFF) + ((b[ptr+1] & 0xFF) << 8)
+ ((b[ptr+2] & 0xFF) << 16) + ((b[ptr+3] & 0xFF) << 24);
_inputPtr = ptr+4;
return v;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,9 @@ protected void assertToken(JsonToken expToken, JsonToken actToken)
}
}

protected void assertToken(JsonToken expToken, JsonParser jp)
protected void assertToken(JsonToken expToken, JsonParser p)
{
assertToken(expToken, jp.getCurrentToken());
assertToken(expToken, p.getCurrentToken());
}

protected void assertType(Object ob, Class<?> expType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ public class WritePrimitiveArrayTest extends ProtobufTestBase
+" repeated double values = 1 [packed=true];\n"
+"}\n"
;

final protected static String PROTOC_FLOAT_ARRAY_SPARSE = "message Floats {\n"
+" repeated float values = 1;\n"
+"}\n"
;

final protected static String PROTOC_FLOAT_ARRAY_PACKED = "message Floats {\n"
+" repeated float values = 1 [packed=true];\n"
+"}\n"
;

static class IntArray {
public int[] values;
Expand Down Expand Up @@ -75,8 +85,17 @@ public DoubleArray(double... v) {
values = v;
}
}

final ObjectMapper MAPPER = new ObjectMapper(new ProtobufFactory());

static class FloatArray {
public float[] values;

protected FloatArray() { }
public FloatArray(float... v) {
values = v;
}
}

final ObjectMapper MAPPER = new ProtobufMapper();

public WritePrimitiveArrayTest() throws Exception { }

Expand Down Expand Up @@ -205,9 +224,9 @@ public void testDoubleArraySparse() throws Exception
{
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_DOUBLE_ARRAY_SPARSE);
final ObjectWriter w = MAPPER.writer(schema);
DoubleArray input = new DoubleArray(0.25, -2.5, 1000.125);
DoubleArray input = new DoubleArray(0.25, -2.5, 1000.125, 1234567891234567890.5);
byte[] bytes = w.writeValueAsBytes(input);
assertEquals(27, bytes.length);
assertEquals(36, bytes.length);

DoubleArray result = MAPPER.readerFor(DoubleArray.class).with(schema)
.readValue(bytes);
Expand All @@ -218,9 +237,9 @@ public void testDoubleArrayPacked() throws Exception
{
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_DOUBLE_ARRAY_PACKED);
final ObjectWriter w = MAPPER.writer(schema);
DoubleArray input = new DoubleArray(-0.5, 89245.25, 0.625);
DoubleArray input = new DoubleArray(-0.5, 89245.25, 0.625, 1234567891234567890.5);
byte[] bytes = w.writeValueAsBytes(input);
assertEquals(26, bytes.length);
assertEquals(34, bytes.length);

DoubleArray result = MAPPER.readerFor(DoubleArray.class).with(schema)
.readValue(bytes);
Expand All @@ -237,4 +256,48 @@ private void _assertEquals(double[] exp, double[] act)
}
}
}

/*
/**********************************************************
/* Test methods, floating-point arrays
/**********************************************************
*/

public void testfloatArraySparse() throws Exception
{
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_FLOAT_ARRAY_SPARSE);
final ObjectWriter w = MAPPER.writer(schema);
FloatArray input = new FloatArray(0.25f, -2.5f, 55555555.5f);
byte[] bytes = w.writeValueAsBytes(input);
assertEquals(15, bytes.length);

FloatArray result = MAPPER.readerFor(FloatArray.class).with(schema)
.readValue(bytes);
_assertEquals(input.values, result.values);
}

public void testfloatArrayPacked() throws Exception
{
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_FLOAT_ARRAY_PACKED);
final ObjectWriter w = MAPPER.writer(schema);
FloatArray input = new FloatArray(-0.5f, 89245.25f, 55555555.5f);
byte[] bytes = w.writeValueAsBytes(input);
assertEquals(14, bytes.length);

FloatArray result = MAPPER.readerFor(FloatArray.class).with(schema)
.readValue(bytes);
_assertEquals(input.values, result.values);
}

private void _assertEquals(float[] exp, float[] act)
{
assertEquals(exp.length, act.length);
for (int i = 0; i < exp.length; ++i) {
// note: caller ensures it only uses values that reliably round-trip
if (exp[i] != act[i]) {
fail("Entry #"+i+" wrong: expected "+exp[i]+", got "+act[i]);
}
}
}

}
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Modules:
2.8.9 (not yet released)

#72: (proto) parser fails with /* comment */
#85: _decode32Bits() bug in ProtobufParser
(reported by marsqing@github)

2.8.8 (05-Apr-2017)

Expand Down

0 comments on commit 1a53cd5

Please sign in to comment.