Skip to content

Commit

Permalink
Merge pull request #11 from davies/fix_endian
Browse files Browse the repository at this point in the history
Fix endianness in array of float/double
  • Loading branch information
irmen committed Sep 15, 2014
2 parents 501f8b3 + 1954ab9 commit 36a5530
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 51 deletions.
40 changes: 20 additions & 20 deletions dotnet/Pyrolite.Tests/Pickle/ArrayConstructorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,47 +159,47 @@ public void testFloats()
// f/d
ArrayConstructor ac=new ArrayConstructor();
Assert.AreEqual(16711938.0f,
((float[])ac.construct('f', 14, new byte[]{0x4b,0x7f,0x01,0x02}))[0] );
((float[])ac.construct('f', 15, new byte[]{0x4b,0x7f,0x01,0x02}))[0] );
Assert.AreEqual(float.PositiveInfinity,
((float[])ac.construct('f', 14, new byte[]{0x7f,0x80,0x00,0x00}))[0]);
((float[])ac.construct('f', 15, new byte[]{0x7f,0x80,0x00,0x00}))[0]);
Assert.AreEqual(float.NegativeInfinity,
((float[])ac.construct('f', 14, new byte[]{0xff,0x80,0x00,0x00}))[0]);
((float[])ac.construct('f', 15, new byte[]{0xff,0x80,0x00,0x00}))[0]);
Assert.AreEqual(-0.0f,
((float[])ac.construct('f', 14, new byte[]{0x80,0x00,0x00,0x00}))[0]);
((float[])ac.construct('f', 15, new byte[]{0x80,0x00,0x00,0x00}))[0]);

Assert.AreEqual(16711938.0f,
((float[])ac.construct('f', 15, new byte[]{0x02,0x01,0x7f,0x4b}))[0]);
((float[])ac.construct('f', 14, new byte[]{0x02,0x01,0x7f,0x4b}))[0]);
Assert.AreEqual(float.PositiveInfinity,
((float[])ac.construct('f', 15, new byte[]{0x00,0x00,0x80,0x7f}))[0]);
((float[])ac.construct('f', 14, new byte[]{0x00,0x00,0x80,0x7f}))[0]);
Assert.AreEqual(float.NegativeInfinity,
((float[])ac.construct('f', 15, new byte[]{0x00,0x00,0x80,0xff}))[0]);
((float[])ac.construct('f', 14, new byte[]{0x00,0x00,0x80,0xff}))[0]);
Assert.AreEqual(-0.0f,
((float[])ac.construct('f', 15, new byte[]{0x00,0x00,0x00,0x80}))[0]);
((float[])ac.construct('f', 14, new byte[]{0x00,0x00,0x00,0x80}))[0]);

Assert.AreEqual(9006104071832581.0d,
((double[])ac.construct('d', 16, new byte[]{0x43,0x3f,0xff,0x01,0x02,0x03,0x04,0x05}))[0]);
((double[])ac.construct('d', 17, new byte[]{0x43,0x3f,0xff,0x01,0x02,0x03,0x04,0x05}))[0]);
Assert.AreEqual(double.PositiveInfinity,
((double[])ac.construct('d', 16, new byte[]{0x7f,0xf0,0x00,0x00,0x00,0x00,0x00,0x00}))[0]);
((double[])ac.construct('d', 17, new byte[]{0x7f,0xf0,0x00,0x00,0x00,0x00,0x00,0x00}))[0]);
Assert.AreEqual(double.NegativeInfinity,
((double[])ac.construct('d', 16, new byte[]{0xff,0xf0,0x00,0x00,0x00,0x00,0x00,0x00}))[0]);
((double[])ac.construct('d', 17, new byte[]{0xff,0xf0,0x00,0x00,0x00,0x00,0x00,0x00}))[0]);
Assert.AreEqual(-0.0d,
((double[])ac.construct('d', 16, new byte[]{0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00}))[0]);
((double[])ac.construct('d', 17, new byte[]{0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00}))[0]);

Assert.AreEqual(9006104071832581.0d,
((double[])ac.construct('d', 17, new byte[]{0x05,0x04,0x03,0x02,0x01,0xff,0x3f,0x43}))[0]);
((double[])ac.construct('d', 16, new byte[]{0x05,0x04,0x03,0x02,0x01,0xff,0x3f,0x43}))[0]);
Assert.AreEqual(double.PositiveInfinity,
((double[])ac.construct('d', 17, new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x7f}))[0]);
((double[])ac.construct('d', 16, new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x7f}))[0]);
Assert.AreEqual(double.NegativeInfinity,
((double[])ac.construct('d', 17, new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0xff}))[0]);
((double[])ac.construct('d', 16, new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0xff}))[0]);
Assert.AreEqual(-0.0d,
((double[])ac.construct('d', 17, new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80}))[0]);
((double[])ac.construct('d', 16, new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80}))[0]);


// check if multiple values in an array work
Assert.AreEqual(new float[] {1.1f, 2.2f}, (float[]) ac.construct('f', 14, new byte[]{0x3f,0x8c,0xcc,0xcd, 0x40,0x0c,0xcc,0xcd}));
Assert.AreEqual(new float[] {1.1f, 2.2f}, (float[]) ac.construct('f', 15, new byte[]{0xcd,0xcc,0x8c,0x3f, 0xcd,0xcc,0x0c,0x40}));
Assert.AreEqual(new double[]{1.1d, 2.2d}, (double[]) ac.construct('d', 16, new byte[]{0x3f,0xf1,0x99,0x99,0x99,0x99,0x99,0x9a, 0x40,0x01,0x99,0x99,0x99,0x99,0x99,0x9a}));
Assert.AreEqual(new double[]{1.1d, 2.2d}, (double[]) ac.construct('d', 17, new byte[]{0x9a,0x99,0x99,0x99,0x99,0x99,0xf1,0x3f, 0x9a,0x99,0x99,0x99,0x99,0x99,0x01,0x40}));
Assert.AreEqual(new float[] {1.1f, 2.2f}, (float[]) ac.construct('f', 15, new byte[]{0x3f,0x8c,0xcc,0xcd, 0x40,0x0c,0xcc,0xcd}));
Assert.AreEqual(new float[] {1.1f, 2.2f}, (float[]) ac.construct('f', 14, new byte[]{0xcd,0xcc,0x8c,0x3f, 0xcd,0xcc,0x0c,0x40}));
Assert.AreEqual(new double[]{1.1d, 2.2d}, (double[]) ac.construct('d', 17, new byte[]{0x3f,0xf1,0x99,0x99,0x99,0x99,0x99,0x9a, 0x40,0x01,0x99,0x99,0x99,0x99,0x99,0x9a}));
Assert.AreEqual(new double[]{1.1d, 2.2d}, (double[]) ac.construct('d', 16, new byte[]{0x9a,0x99,0x99,0x99,0x99,0x99,0xf1,0x3f, 0x9a,0x99,0x99,0x99,0x99,0x99,0x01,0x40}));
}
}

Expand Down
8 changes: 4 additions & 4 deletions dotnet/Pyrolite/Pickle/Objects/ArrayConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,10 @@ protected double[] constructDoubleArray(int machinecode, byte[] data) {
double[] result = new double[data.Length / 8];
byte[] bigendian=new byte[8];
for (int i = 0; i < data.Length / 8; ++i) {
if(machinecode==16) {
if(machinecode==17) {
result[i] = PickleUtils.bytes_to_double(data, i * 8);
} else {
// 17=big endian, flip the bytes
// 16=little endian, flip the bytes
bigendian[0]=data[7+i*8];
bigendian[1]=data[6+i*8];
bigendian[2]=data[5+i*8];
Expand All @@ -416,10 +416,10 @@ protected float[] constructFloatArray(int machinecode, byte[] data) {
float[] result = new float[data.Length / 4];
byte[] bigendian=new byte[4];
for (int i = 0; i < data.Length / 4; ++i) {
if (machinecode == 14) {
if (machinecode == 15) {
result[i] = PickleUtils.bytes_to_float(data, i * 4);
} else {
// 15=big endian, flip the bytes
// 14=little endian, flip the bytes
bigendian[0]=data[3+i*4];
bigendian[1]=data[2+i*4];
bigendian[2]=data[1+i*4];
Expand Down
8 changes: 4 additions & 4 deletions java/src/net/razorvine/pickle/objects/ArrayConstructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,10 @@ protected double[] constructDoubleArray(int machinecode, byte[] data) {
double[] result = new double[data.length / 8];
byte[] bigendian=new byte[8];
for (int i = 0; i < data.length / 8; ++i) {
if(machinecode==16) {
if(machinecode == 17) {
result[i] = PickleUtils.bytes_to_double(data, i * 8);
} else {
// 17=big endian, flip the bytes
// 16=little endian, flip the bytes
bigendian[0]=data[7+i*8];
bigendian[1]=data[6+i*8];
bigendian[2]=data[5+i*8];
Expand All @@ -352,10 +352,10 @@ protected float[] constructFloatArray(int machinecode, byte[] data) {
float[] result = new float[data.length / 4];
byte[] bigendian=new byte[4];
for (int i = 0; i < data.length / 4; ++i) {
if (machinecode == 14) {
if (machinecode == 15) {
result[i] = PickleUtils.bytes_to_float(data, i * 4);
} else {
// 15=big endian, flip the bytes
// 14=little endian, flip the bytes
bigendian[0]=data[3+i*4];
bigendian[1]=data[2+i*4];
bigendian[2]=data[1+i*4];
Expand Down
47 changes: 24 additions & 23 deletions java/test/net/razorvine/pickle/test/ArrayConstructorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,47 +160,48 @@ public void testFloats()
{
// f/d
ArrayConstructor ac=new ArrayConstructor();
// big endian
assertTrue(16711938.0f ==
((float[])ac.construct('f', 14, new byte[]{0x4b,0x7f,0x01,0x02}))[0] );
((float[])ac.construct('f', 15, new byte[]{0x4b,0x7f,0x01,0x02}))[0] );
assertTrue(Float.POSITIVE_INFINITY ==
((float[])ac.construct('f', 14, new byte[]{(byte)0x7f,(byte)0x80,0x00,0x00}))[0]);
((float[])ac.construct('f', 15, new byte[]{(byte)0x7f,(byte)0x80,0x00,0x00}))[0]);
assertTrue(Float.NEGATIVE_INFINITY ==
((float[])ac.construct('f', 14, new byte[]{(byte)0xff,(byte)0x80,0x00,0x00}))[0]);
((float[])ac.construct('f', 15, new byte[]{(byte)0xff,(byte)0x80,0x00,0x00}))[0]);
assertTrue(-0.0f ==
((float[])ac.construct('f', 14, new byte[]{(byte)0x80,0x00,0x00,0x00}))[0]);

((float[])ac.construct('f', 15, new byte[]{(byte)0x80,0x00,0x00,0x00}))[0]);
// little endian
assertTrue(16711938.0f ==
((float[])ac.construct('f', 15, new byte[]{0x02,0x01,0x7f,0x4b}))[0]);
((float[])ac.construct('f', 14, new byte[]{0x02,0x01,0x7f,0x4b}))[0]);
assertTrue(Float.POSITIVE_INFINITY ==
((float[])ac.construct('f', 15, new byte[]{0x00,0x00,(byte)0x80,(byte)0x7f}))[0]);
((float[])ac.construct('f', 14, new byte[]{0x00,0x00,(byte)0x80,(byte)0x7f}))[0]);
assertTrue(Float.NEGATIVE_INFINITY ==
((float[])ac.construct('f', 15, new byte[]{0x00,0x00,(byte)0x80,(byte)0xff}))[0]);
((float[])ac.construct('f', 14, new byte[]{0x00,0x00,(byte)0x80,(byte)0xff}))[0]);
assertTrue(-0.0f ==
((float[])ac.construct('f', 15, new byte[]{0x00,0x00,0x00,(byte)0x80}))[0]);

((float[])ac.construct('f', 14, new byte[]{0x00,0x00,0x00,(byte)0x80}))[0]);
// big endian
assertTrue(9006104071832581.0d ==
((double[])ac.construct('d', 16, new byte[]{0x43,0x3f,(byte)0xff,0x01,0x02,0x03,0x04,0x05}))[0]);
((double[])ac.construct('d', 17, new byte[]{0x43,0x3f,(byte)0xff,0x01,0x02,0x03,0x04,0x05}))[0]);
assertTrue(Double.POSITIVE_INFINITY ==
((double[])ac.construct('d', 16, new byte[]{(byte)0x7f,(byte)0xf0,0x00,0x00,0x00,0x00,0x00,0x00}))[0]);
((double[])ac.construct('d', 17, new byte[]{(byte)0x7f,(byte)0xf0,0x00,0x00,0x00,0x00,0x00,0x00}))[0]);
assertTrue(Double.NEGATIVE_INFINITY ==
((double[])ac.construct('d', 16, new byte[]{(byte)0xff,(byte)0xf0,0x00,0x00,0x00,0x00,0x00,0x00}))[0]);
((double[])ac.construct('d', 17, new byte[]{(byte)0xff,(byte)0xf0,0x00,0x00,0x00,0x00,0x00,0x00}))[0]);
assertTrue(-0.0d ==
((double[])ac.construct('d', 16, new byte[]{(byte)0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00}))[0]);

((double[])ac.construct('d', 17, new byte[]{(byte)0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00}))[0]);
// little endian
assertTrue(9006104071832581.0d ==
((double[])ac.construct('d', 17, new byte[]{0x05,0x04,0x03,0x02,0x01,(byte)0xff,0x3f,0x43}))[0]);
((double[])ac.construct('d', 16, new byte[]{0x05,0x04,0x03,0x02,0x01,(byte)0xff,0x3f,0x43}))[0]);
assertTrue(Double.POSITIVE_INFINITY ==
((double[])ac.construct('d', 17, new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,(byte)0xf0,(byte)0x7f}))[0]);
((double[])ac.construct('d', 16, new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,(byte)0xf0,(byte)0x7f}))[0]);
assertTrue(Double.NEGATIVE_INFINITY ==
((double[])ac.construct('d', 17, new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,(byte)0xf0,(byte)0xff}))[0]);
((double[])ac.construct('d', 16, new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,(byte)0xf0,(byte)0xff}))[0]);
assertTrue(-0.0d ==
((double[])ac.construct('d', 17, new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,(byte)0x80}))[0]);
((double[])ac.construct('d', 16, new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,(byte)0x80}))[0]);


// check if multiple values in an array work
assertArrayEquals(new float[] {1.1f, 2.2f}, (float[]) ac.construct('f', 14, new byte[]{0x3f,(byte)0x8c,(byte)0xcc,(byte)0xcd, 0x40,0x0c,(byte)0xcc,(byte)0xcd}) ,0);
assertArrayEquals(new float[] {1.1f, 2.2f}, (float[]) ac.construct('f', 15, new byte[]{(byte)0xcd,(byte)0xcc,(byte)0x8c,0x3f, (byte)0xcd,(byte)0xcc,0x0c,0x40}) ,0);
assertArrayEquals(new double[]{1.1d, 2.2d}, (double[]) ac.construct('d', 16, new byte[]{(byte)0x3f,(byte)0xf1,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x9a, (byte)0x40,(byte)0x01,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x9a}) ,0);
assertArrayEquals(new double[]{1.1d, 2.2d}, (double[]) ac.construct('d', 17, new byte[]{(byte)0x9a,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0xf1,(byte)0x3f, (byte)0x9a,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x01,(byte)0x40}) ,0);
assertArrayEquals(new float[] {1.1f, 2.2f}, (float[]) ac.construct('f', 15, new byte[]{0x3f,(byte)0x8c,(byte)0xcc,(byte)0xcd, 0x40,0x0c,(byte)0xcc,(byte)0xcd}) ,0);
assertArrayEquals(new float[] {1.1f, 2.2f}, (float[]) ac.construct('f', 14, new byte[]{(byte)0xcd,(byte)0xcc,(byte)0x8c,0x3f, (byte)0xcd,(byte)0xcc,0x0c,0x40}) ,0);
assertArrayEquals(new double[]{1.1d, 2.2d}, (double[]) ac.construct('d', 17, new byte[]{(byte)0x3f,(byte)0xf1,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x9a, (byte)0x40,(byte)0x01,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x9a}) ,0);
assertArrayEquals(new double[]{1.1d, 2.2d}, (double[]) ac.construct('d', 16, new byte[]{(byte)0x9a,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0xf1,(byte)0x3f, (byte)0x9a,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x99,(byte)0x01,(byte)0x40}) ,0);
}
}

0 comments on commit 36a5530

Please sign in to comment.