Exemplo n.º 1
0
        public void testBytes_to_double()
        {
            try {
                PickleUtils.bytes_to_double(new byte[] {}, 0);
                Assert.Fail("expected PickleException");
            } catch (PickleException) {}
            try {
                PickleUtils.bytes_to_double(new byte[] { 0 }, 0);
                Assert.Fail("expected PickleException");
            } catch (PickleException) {}
            Assert.AreEqual(0.0d, PickleUtils.bytes_to_double(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, 0));
            Assert.AreEqual(1.0d, PickleUtils.bytes_to_double(new byte[] { 0x3f, 0xf0, 0, 0, 0, 0, 0, 0 }, 0));
            Assert.AreEqual(1.1d, PickleUtils.bytes_to_double(new byte[] { 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a }, 0));
            Assert.AreEqual(1234.5678d, PickleUtils.bytes_to_double(new byte[] { 0x40, 0x93, 0x4a, 0x45, 0x6d, 0x5c, 0xfa, 0xad }, 0));
            Assert.AreEqual(2.17e123d, PickleUtils.bytes_to_double(new byte[] { 0x59, 0x8a, 0x42, 0xd1, 0xce, 0xf5, 0x3f, 0x46 }, 0));
            Assert.AreEqual(1.23456789e300d, PickleUtils.bytes_to_double(new byte[] { 0x7e, 0x3d, 0x7e, 0xe8, 0xbc, 0xaf, 0x28, 0x3a }, 0));
            Assert.AreEqual(double.PositiveInfinity, PickleUtils.bytes_to_double(new byte[] { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 }, 0));
            Assert.AreEqual(double.NegativeInfinity, PickleUtils.bytes_to_double(new byte[] { 0xff, 0xf0, 0, 0, 0, 0, 0, 0 }, 0));
            try
            {
                PickleUtils.bytes_to_double(new byte[] { 200, 50, 25, 100 }, 0);
                Assert.Fail("expected PickleException");
            } catch (PickleException) {}

            // test offset
            Assert.AreEqual(1.23456789e300d, PickleUtils.bytes_to_double(new byte[] { 0, 0, 0, 0x7e, 0x3d, 0x7e, 0xe8, 0xbc, 0xaf, 0x28, 0x3a }, 3));
            Assert.AreEqual(1.23456789e300d, PickleUtils.bytes_to_double(new byte[] { 0x7e, 0x3d, 0x7e, 0xe8, 0xbc, 0xaf, 0x28, 0x3a, 0, 0, 0 }, 0));
        }
Exemplo n.º 2
0
 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)
         {
             result[i] = PickleUtils.bytes_to_double(data, i * 8);
         }
         else
         {
             // 17=big endian, flip the bytes
             bigendian[0] = data[7 + i * 8];
             bigendian[1] = data[6 + i * 8];
             bigendian[2] = data[5 + i * 8];
             bigendian[3] = data[4 + i * 8];
             bigendian[4] = data[3 + i * 8];
             bigendian[5] = data[2 + i * 8];
             bigendian[6] = data[1 + i * 8];
             bigendian[7] = data[0 + i * 8];
             result[i]    = PickleUtils.bytes_to_double(bigendian, 0);
         }
     }
     return(result);
 }