Пример #1
0
        private byte[] Msg2bytes(Message msg, bool?stringTypeAndField)
        {
            FlexBuffer buf = new FlexBuffer();

            URL u = new URL("none:");

            if (stringTypeAndField != null)
            {
                u.AddTerm(BinaryTaggedDataOutput.STRING_TYPE_AND_FIELD, stringTypeAndField.ToString());
            }

            BinaryTaggedDataOutput btdo = new BinaryTaggedDataOutput(vf, u.ToString());

            btdo.WriteMessage(msg, buf);
            buf.SetIndex(0);
            return(buf.GetAvailBytes());
        }
Пример #2
0
        private void assertValueToBytes(Object value, sbyte[] expectedBytes)
        {
            XType t = new XType(1, "a");
            Field f = new Field(2, "b");

            t.PutValidator(f, Validator_object.Get(0));

            Message msg = new Message(t, vf);

            msg.Add(f, value);

            BinaryTaggedDataOutput btdo = new BinaryTaggedDataOutput(vf, "none:");
            FlexBuffer             buf  = new FlexBuffer();

            btdo.WriteMessage(msg, buf);

            buf.SetIndex(0);
            byte[]  b  = buf.GetAvailBytes();
            sbyte[] b1 = new sbyte[b.Length];
            Buffer.BlockCopy(b, 0, b1, 0, b.Length);
            Dump(b);
            AssertArrayEquals(expectedBytes, b1);
        }
Пример #3
0
        public void Test_Check_Value()
        {
            BinaryTaggedDataOutput btdo = new BinaryTaggedDataOutput(vf, "none:");

            foreach (sbyte i in GetBytes(SByte.MinValue, 256))
            {
                if (i >= TypeCode.MIN_TINY_INT && i <= TypeCode.MAX_TINY_INT)
                {
                    Assert.AreEqual(i, btdo.CheckValue(i));
                }
                else
                {
                    Assert.AreEqual(TypeCode.BYTE, btdo.CheckValue(i));
                }
            }

            // short values
            foreach (short i in GetShorts(short.MinValue, 65536))
            {
                if (i >= TypeCode.MIN_TINY_INT && i <= TypeCode.MAX_TINY_INT)
                {
                    Assert.AreEqual((sbyte)i, btdo.CheckValue(i));
                }
                else if (i >= SByte.MinValue && i <= SByte.MaxValue)
                {
                    Assert.AreEqual(TypeCode.BYTE, btdo.CheckValue(i));
                }
                else
                {
                    Assert.AreEqual(TypeCode.SHORT, btdo.CheckValue(i));
                }
            }

            // int values
            foreach (int i in GetTestInts())
            {
                if (i >= TypeCode.MIN_TINY_INT && i <= TypeCode.MAX_TINY_INT)
                {
                    Assert.AreEqual((sbyte)i, btdo.CheckValue(i));
                }
                else if (i >= SByte.MinValue && i <= SByte.MaxValue)
                {
                    Assert.AreEqual(TypeCode.BYTE, btdo.CheckValue(i));
                }
                else if (i >= short.MinValue && i <= short.MaxValue)
                {
                    Assert.AreEqual(TypeCode.SHORT, btdo.CheckValue(i));
                }
                else
                {
                    Assert.AreEqual(TypeCode.INT, btdo.CheckValue(i));
                }
            }

            // long values
            foreach (long i in GetTestLongs())
            {
                if (i >= TypeCode.MIN_TINY_INT && i <= TypeCode.MAX_TINY_INT)
                {
                    Assert.AreEqual((sbyte)i, btdo.CheckValue(i));
                }
                else if (i >= SByte.MinValue && i <= SByte.MaxValue)
                {
                    Assert.AreEqual(TypeCode.BYTE, btdo.CheckValue(i));
                }
                else if (i >= short.MinValue && i <= short.MaxValue)
                {
                    Assert.AreEqual(TypeCode.SHORT, btdo.CheckValue(i));
                }
                else if (i >= int.MinValue && i <= int.MaxValue)
                {
                    Assert.AreEqual(TypeCode.INT, btdo.CheckValue(i));
                }
                else
                {
                    Assert.AreEqual(TypeCode.LONG, btdo.CheckValue(i));
                }
            }

            // null value
            Assert.AreEqual(TypeCode.NULL, btdo.CheckValue(null));

            // boolean values
            Assert.AreEqual(TypeCode.BOOLEAN_FALSE, btdo.CheckValue(false));
            Assert.AreEqual(TypeCode.BOOLEAN_TRUE, btdo.CheckValue(true));

            // float
            Assert.AreEqual(TypeCode.FLOAT, btdo.CheckValue(3.14159f));

            // double
            Assert.AreEqual(TypeCode.DOUBLE, btdo.CheckValue(3.14159));

            // byte array
            Assert.AreEqual(TypeCode.BYTES, btdo.CheckValue(new sbyte[0]));
            Assert.AreEqual(TypeCode.BYTES, btdo.CheckValue(new sbyte[1]));

            // string
            Assert.AreEqual(TypeCode.EMPTY_STRING, btdo.CheckValue(""));
            Assert.AreEqual(TypeCode.STRING, btdo.CheckValue("abc"));

            // struct
            Assert.AreEqual(TypeCode.CUSTOM, btdo.CheckValue(new StructValue(new XType("foo"), vf)));

            // custom
            Assert.AreEqual(TypeCode.CUSTOM, btdo.CheckValue(new DateTime()));

            // none
            Assert.AreEqual(TypeCode.NONE, btdo.CheckValue(BinaryTaggedData.NONE));
        }