Exemple #1
0
        public void TestFloat()
        {
            var stream = new MsgPackStream();

            stream.WriteDouble(178.5);
            Assert.AreEqual(stream.Position, 8);

            stream.WriteSingle(-10.3f);
            Assert.AreEqual(stream.Position, 12);

            stream.WriteSingle(float.NaN);
            stream.WriteSingle(float.NegativeInfinity);
            stream.WriteSingle(float.PositiveInfinity);
            stream.WriteDouble(double.NaN);
            stream.WriteDouble(double.NegativeInfinity);
            stream.WriteDouble(double.PositiveInfinity);

            stream.Position = 0;
            Assert.AreEqual(178.5, stream.ReadDouble());
            Assert.AreEqual(-10.3f, stream.ReadSingle());
            Assert.IsTrue(float.IsNaN(stream.ReadSingle()));
            Assert.IsTrue(float.IsNegativeInfinity(stream.ReadSingle()));
            Assert.IsTrue(float.IsPositiveInfinity(stream.ReadSingle()));
            Assert.IsTrue(double.IsNaN(stream.ReadDouble()));
            Assert.IsTrue(double.IsNegativeInfinity(stream.ReadDouble()));
            Assert.IsTrue(double.IsPositiveInfinity(stream.ReadDouble()));
        }
Exemple #2
0
        public static float ReadSingle(MsgPackStream stream)
        {
            var code = stream.ReadUInt8();

            switch (FormatMask.Match(code))
            {
            case FormatMask.PositiveFixInt: return(code);

            case FormatMask.NegativeFixInt: return(unchecked ((sbyte)code));
            }

            switch (code)
            {
            case FormatCode.Float32: return(stream.ReadSingle());

            case FormatCode.Int8: return(stream.ReadInt8());

            case FormatCode.UInt8: return(stream.ReadUInt8());

            case FormatCode.Int16: return(stream.ReadInt16());

            case FormatCode.UInt16: return(stream.ReadUInt16());

            case FormatCode.Int32: return(stream.ReadInt32());

            case FormatCode.UInt32: return(stream.ReadUInt32());

            case FormatCode.Int64: return(stream.ReadInt64());

            case FormatCode.UInt64: return(stream.ReadUInt64());

            default: throw new MsgPackCodeException(code);
            }
        }