Esempio n. 1
0
 /// <summary>
 /// Encodes a decimal number and appends the bytes to the buffer.
 /// </summary>
 /// <param name="data">The decimal number.</param>
 /// <param name="buffer">The destination buffer.</param>
 public static void EncodeDecimal(decimal?data, ByteBuffer buffer)
 {
     DecimalEncoding.Encode(data, buffer);
 }
Esempio n. 2
0
 /// <summary>
 /// Decodes a decimal number from the buffer and advances the buffer's position.
 /// </summary>
 /// <param name="buffer">The buffer to read.</param>
 /// <returns>A decimal number.</returns>
 public static decimal?DecodeDecimal(ByteBuffer buffer)
 {
     return(DecimalEncoding.Decode(buffer, 0));
 }
Esempio n. 3
0
        public void AmqpCodecSingleValueTest()
        {
            byte[]     workBuffer = new byte[2048];
            ByteBuffer buffer;

            // boolean true
            AmqpCodec.EncodeBoolean(boolTrue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(boolTrueBin, 0, boolTrueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            bool?bv = AmqpCodec.DecodeBoolean(new ByteBuffer(new ArraySegment <byte>(boolTrueBin)));

            Assert.IsTrue(bv.Value, "Boolean value is not true.");

            // boolean false
            AmqpCodec.EncodeBoolean(boolFalse, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(boolFalseBin, 0, boolFalseBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            bv = AmqpCodec.DecodeBoolean(new ByteBuffer(new ArraySegment <byte>(boolFalseBin)));
            Assert.IsFalse(bv.Value, "Boolean value is not false.");

            // ubyte
            AmqpCodec.EncodeUByte(ubyteValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(ubyteValueBin, 0, ubyteValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            byte?bytev = AmqpCodec.DecodeUByte(new ByteBuffer(new ArraySegment <byte>(ubyteValueBin)));

            Assert.IsTrue(bytev == ubyteValue, "UByte value is not equal.");

            // ushort
            AmqpCodec.EncodeUShort(ushortValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(ushortValueBin, 0, ushortValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            ushort?ushortv = AmqpCodec.DecodeUShort(new ByteBuffer(new ArraySegment <byte>(ushortValueBin)));

            Assert.IsTrue(ushortv == ushortValue, "UShort value is not equal.");

            // uint0
            AmqpCodec.EncodeUInt(uint0Value, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(uint0ValueBin, 0, uint0ValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            uint?uint0v = AmqpCodec.DecodeUInt(new ByteBuffer(new ArraySegment <byte>(uint0ValueBin)));

            Assert.IsTrue(uint0v == uint0Value, "UInt0 value is not equal.");

            // uint small
            AmqpCodec.EncodeUInt(uintSmallValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(uintSmallValueBin, 0, uintSmallValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            uint?uintSmallV = AmqpCodec.DecodeUInt(new ByteBuffer(new ArraySegment <byte>(uintSmallValueBin)));

            Assert.IsTrue(uintSmallV == uintSmallValue, "UIntSmall value is not equal.");

            // uint
            AmqpCodec.EncodeUInt(uintValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(uintValueBin, 0, uintValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            uint?uintv = AmqpCodec.DecodeUInt(new ByteBuffer(new ArraySegment <byte>(uintValueBin)));

            Assert.IsTrue(uintv == uintValue, "UInt value is not equal.");

            // ulong0
            AmqpCodec.EncodeULong(ulong0Value, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(ulong0ValueBin, 0, ulong0ValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            ulong?ulong0v = AmqpCodec.DecodeULong(new ByteBuffer(new ArraySegment <byte>(ulong0ValueBin)));

            Assert.IsTrue(ulong0v == ulong0Value, "ULong0 value is not equal.");

            // ulong small
            AmqpCodec.EncodeULong(ulongSmallValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(ulongSmallValueBin, 0, ulongSmallValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            ulong?ulongSmallV = AmqpCodec.DecodeULong(new ByteBuffer(new ArraySegment <byte>(ulongSmallValueBin)));

            Assert.IsTrue(ulongSmallV == ulongSmallValue, "ULong value is not equal.");

            // ulong
            AmqpCodec.EncodeULong(ulongValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(ulongValueBin, 0, ulongValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            ulong?ulongv = AmqpCodec.DecodeULong(new ByteBuffer(new ArraySegment <byte>(ulongValueBin)));

            Assert.IsTrue(ulongv == ulongValue, "ULong value is not equal.");

            // byte
            AmqpCodec.EncodeByte(byteValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(byteValueBin, 0, byteValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            sbyte?sbytev = AmqpCodec.DecodeByte(new ByteBuffer(new ArraySegment <byte>(byteValueBin)));

            Assert.IsTrue(sbytev == byteValue, "Byte value is not equal.");

            // short
            AmqpCodec.EncodeShort(shortValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(shortValueBin, 0, shortValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            short?shortv = AmqpCodec.DecodeShort(new ByteBuffer(new ArraySegment <byte>(shortValueBin)));

            Assert.IsTrue(shortv == shortValue, "Short value is not equal.");

            // int small
            AmqpCodec.EncodeInt(intSmallValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(intSmallValueBin, 0, intSmallValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            int?intSmallV = AmqpCodec.DecodeInt(new ByteBuffer(new ArraySegment <byte>(intSmallValueBin)));

            Assert.IsTrue(intSmallV == intSmallValue, "Int small value is not equal.");

            // int
            AmqpCodec.EncodeInt(intValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(intValueBin, 0, intValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            int?intv = AmqpCodec.DecodeInt(new ByteBuffer(new ArraySegment <byte>(intValueBin)));

            Assert.IsTrue(intv == intValue, "Int value is not equal.");

            // long
            AmqpCodec.EncodeLong(longSmallValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(longSmallValueBin, 0, longSmallValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            long?longSmallV = AmqpCodec.DecodeLong(new ByteBuffer(new ArraySegment <byte>(longSmallValueBin)));

            Assert.IsTrue(longSmallV == longSmallValue, "Long small value is not equal.");

            // long
            AmqpCodec.EncodeLong(longValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(longValueBin, 0, longValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            long?longv = AmqpCodec.DecodeLong(new ByteBuffer(new ArraySegment <byte>(longValueBin)));

            Assert.IsTrue(longv == longValue, "Long value is not equal.");

            // float
            AmqpCodec.EncodeFloat(floatValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(floatValueBin, 0, floatValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            float?floatv = AmqpCodec.DecodeFloat(new ByteBuffer(new ArraySegment <byte>(floatValueBin)));

            Assert.IsTrue(floatv == floatValue, "Float value is not equal.");

            // double
            AmqpCodec.EncodeDouble(doubleValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(doubleValueBin, 0, doubleValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            double?doublev = AmqpCodec.DecodeDouble(new ByteBuffer(new ArraySegment <byte>(doubleValueBin)));

            Assert.IsTrue(doublev == doubleValue, "Double value is not equal.");

            //decimal
            decimal?dec32 = DecimalEncoding.Decode(new ByteBuffer(new ArraySegment <byte>(decimal32ValueBin)), FormatCode.Decimal32);

            Assert.IsTrue(dec32.Value == decimal32Value, "Decimal32 value is not equal");

            decimal?dec64 = DecimalEncoding.Decode(new ByteBuffer(new ArraySegment <byte>(decimal64ValueBin)), FormatCode.Decimal64);

            Assert.IsTrue(dec64.Value == decimal64Value, "Decimal64 value is not equal");

            decimal?dec128 = DecimalEncoding.Decode(new ByteBuffer(new ArraySegment <byte>(decimal128ValueBin)), FormatCode.Decimal128);

            Assert.IsTrue(dec128.Value == decimal128Value, "Decimal128 value is not equal");

            // char
            AmqpCodec.EncodeChar(charValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(charValueBin, 0, charValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            char?charv = AmqpCodec.DecodeChar(new ByteBuffer(new ArraySegment <byte>(charValueBin)));

            Assert.IsTrue(charv == charValue, "Char value is not equal.");

            // timestamp
            AmqpCodec.EncodeTimeStamp(dtValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(dtValueBin, 0, dtValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            DateTime?dtv = AmqpCodec.DecodeTimeStamp(new ByteBuffer(new ArraySegment <byte>(dtValueBin)));

            Assert.IsTrue(dtv == dtValue.ToUniversalTime(), "UByte value is not equal.");

            // uuid
            AmqpCodec.EncodeUuid(uuidValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(uuidValueBin, 0, uuidValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            Guid?uuidv = AmqpCodec.DecodeUuid(new ByteBuffer(new ArraySegment <byte>(uuidValueBin)));

            Assert.IsTrue(uuidv == uuidValue, "Uuid value is not equal.");

            // binary 8
            AmqpCodec.EncodeBinary(bin8Value, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(bin8ValueBin, 0, bin8ValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            ArraySegment <byte> bin8v = AmqpCodec.DecodeBinary(new ByteBuffer(new ArraySegment <byte>(bin8ValueBin)));

            EnsureEqual(bin8v.Array, bin8v.Offset, bin8v.Count, bin8Value.Array, bin8Value.Offset, bin8Value.Count);

            // binary 32
            AmqpCodec.EncodeBinary(bin32Value, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(bin32ValueBin, 0, bin32ValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            ArraySegment <byte> bin32v = AmqpCodec.DecodeBinary(new ByteBuffer(new ArraySegment <byte>(bin32ValueBin)));

            EnsureEqual(bin32v.Array, bin32v.Offset, bin32v.Count, bin32Value.Array, bin32Value.Offset, bin32Value.Count);

            // symbol 8
            AmqpCodec.EncodeSymbol(strValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(sym8ValueBin, 0, sym8ValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            AmqpSymbol symbol8v = AmqpCodec.DecodeSymbol(new ByteBuffer(new ArraySegment <byte>(sym8ValueBin)));

            Assert.IsTrue(symbol8v.Value == strValue, "Symbol8 string value is not equal.");

            // symbol 32
            AmqpSymbol symbol32v = AmqpCodec.DecodeSymbol(new ByteBuffer(new ArraySegment <byte>(sym32ValueBin)));

            Assert.IsTrue(symbol32v.Value == strValue, "Symbol32 string value is not equal.");

            // string 8 UTF8
            AmqpCodec.EncodeString(strValue, buffer = new ByteBuffer(workBuffer));
            EnsureEqual(str8Utf8ValueBin, 0, str8Utf8ValueBin.Length, buffer.Buffer, buffer.Offset, buffer.Length);
            string str8Utf8 = AmqpCodec.DecodeString(new ByteBuffer(new ArraySegment <byte>(str8Utf8ValueBin)));

            Assert.IsTrue(str8Utf8 == strValue, "UTF8 string8 string value is not equal.");

            // string 32 UTF8
            string str32Utf8 = AmqpCodec.DecodeString(new ByteBuffer(new ArraySegment <byte>(str32Utf8ValueBin)));

            Assert.IsTrue(str32Utf8 == strValue, "UTF8 string32 string value is not equal.");
        }