Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void testFloatUnusedBits() throws Exception
assertArrayEquals(new byte[] {
0x08, 0x00, 0x00, 0x00, 0x00
}, Arrays.copyOfRange(encoded, 1, encoded.length));
}
}

// [dataformats-binary#300]
@Test
Expand Down Expand Up @@ -202,4 +202,46 @@ public void testNumbersAsString() throws Exception
gen.close();
assertEquals(10, out.toByteArray().length);
}

// [dataformats-binary#608]
@Test
public void testFloat32FromSpecEncoding() throws Exception
{
final float f32 = 29.9510f;
assertEquals(0x41ef9ba6, Float.floatToIntBits(f32));

ByteArrayOutputStream out = new ByteArrayOutputStream();
try (SmileGenerator gen = smileGenerator(out, false)) {
gen.writeNumber(f32);
}
byte[] encoded = out.toByteArray();
assertEquals(6, encoded.length);
assertEquals(0x28, encoded[0]); // type byte, float

// From 0x80 0x00 0x00 0x00 (spread over 5 x 7bits)
assertArrayEquals(new byte[] {
0x04, 0x0f, 0x3e, 0x37, 0x26
}, Arrays.copyOfRange(encoded, 1, encoded.length));
}

// [dataformats-binary#608]
@Test
public void testDouble64FromSpecEncoding() throws Exception
{
final double d64 = -29.9510;
assertEquals(0xc03df374bc6a7efaL, Double.doubleToLongBits(d64));

ByteArrayOutputStream out = new ByteArrayOutputStream();
try (SmileGenerator gen = smileGenerator(out, false)) {
gen.writeNumber(d64);
}
byte[] encoded = out.toByteArray();
assertEquals(11, encoded.length);
assertEquals(0x29, encoded[0]); // type byte, float

// From 0x80 0x00 0x00 0x00 (spread over 5 x 7bits)
assertArrayEquals(new byte[] {
0x01, 0x40, 0x1e, 0x7c, 0x6e, 0x4b, 0x63, 0x29, 0x7d, 0x7a
}, Arrays.copyOfRange(encoded, 1, encoded.length));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public void testArrayWithInts() throws IOException
}

@Test
public void testFloats() throws IOException
public void testFloats() throws Exception
{
ByteArrayOutputStream bo = new ByteArrayOutputStream();
SmileGenerator g = smileGenerator(bo, false);
Expand All @@ -240,7 +240,7 @@ public void testFloats() throws IOException
}

@Test
public void testDoubles() throws IOException
public void testDoubles() throws Exception
{
ByteArrayOutputStream bo = new ByteArrayOutputStream();
SmileGenerator g = smileGenerator(bo, false);
Expand All @@ -265,6 +265,38 @@ public void testDoubles() throws IOException
p.close();
}

// [dataformats-binary#608]
@Test
public void testFloat32FromSpecEncoding() throws Exception {
final float f32 = 29.9510f;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (SmileGenerator gen = smileGenerator(out, false)) {
gen.writeNumber(f32);
}
try (SmileParser p = _smileParser(out.toByteArray())) {
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(JsonParser.NumberType.FLOAT, p.getNumberType());
assertEquals(f32, p.getFloatValue());
assertNull(p.nextToken());
}
}

// [dataformats-binary#608]
@Test
public void testDouble64FromSpecEncoding() throws Exception {
final double d64 = -29.9510;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (SmileGenerator gen = smileGenerator(out, false)) {
gen.writeNumber(d64);
}
try (SmileParser p = _smileParser(out.toByteArray())) {
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(JsonParser.NumberType.DOUBLE, p.getNumberType());
assertEquals(d64, p.getDoubleValue());
assertNull(p.nextToken());
}
}

@Test
public void testArrayWithDoubles() throws IOException
{
Expand Down