Пример #1
0
        public void AssortedMix(bool preserveTypes, int expectedLength)
        {
            KeyValuePair <object, object>[] items = new KeyValuePair <object, object>[] {
                new KeyValuePair <object, object>(true, true),
                new KeyValuePair <object, object>(false, false),
                new KeyValuePair <object, object>(null, null),
                new KeyValuePair <object, object>(int.MinValue, uint.MaxValue),
                new KeyValuePair <object, object>((byte)1, 5.2d),
                new KeyValuePair <object, object>((byte)2, 900.1f),
                new KeyValuePair <object, object>("Hallo!", "Hallo!"),
            };
            if (preserveTypes)
            {
                MsgPackTests.DynamicallyCompactValue = false;
            }
            try {
                MsgPackItem item = MsgPackTests.RoundTripTest <MpMap, KeyValuePair <object, object>[]>(items, expectedLength, MsgPackTypeId.MpMap4);

                KeyValuePair <object, object>[] ret = item.GetTypedValue <KeyValuePair <object, object>[]>();

                Assert.AreEqual(items.Length, ret.Length, string.Concat("Expected ", items.Length, " items but got ", ret.Length, " items in the array."));
                for (int t = ret.Length - 1; t >= 0; t--)
                {
                    if (preserveTypes && t != 2)
                    {
                        Assert.IsTrue(items[t].GetType() == ret[t].GetType(), string.Concat("Expected type ", items[t].GetType(), " items but got ", ret[t].GetType(), "."));
                    }
                    Assert.AreEqual(items[t], ret[t], string.Concat("Expected ", items[t], " but got ", ret[t], " at index ", t));
                }
            } finally {
                MsgPackTests.DynamicallyCompactValue = true;
            }
        }
Пример #2
0
        public static MsgPackItem RoundTripTest <Typ, T>(T value, int expectedLength, MsgPackTypeId expectedMsgPackType, sbyte extensionType = 0) where Typ : MsgPackItem
        {
            bool preservingType = !DynamicallyCompactValue;

            MsgPackItem item;

            if (typeof(Typ) == typeof(MpExt))
            {
                item = new MpExt()
                {
                    Value         = value,
                    TypeSpecifier = extensionType
                };
            }
            else
            {
                item = MsgPackItem.Pack(value, new MsgPackSettings()
                {
                    DynamicallyCompact = DynamicallyCompactValue,
                    PreservePackages   = false,
                    ContinueProcessingOnBreakingError = false
                });
            }
            byte[] buffer       = item.ToBytes();
            Type   expectedType = typeof(Typ);
            Type   foundType    = item.GetType();

            MsgPackTypeId resultType    = preservingType && item is MpInt ? ((MpInt)item).PreservedType : item.TypeId;
            bool          typesAreEqual = (expectedMsgPackType & resultType) == expectedMsgPackType;

            Assert.IsTrue(typesAreEqual, string.Concat("Expected packed type of ", expectedMsgPackType, " but received the type ", resultType));
            Assert.True(foundType == expectedType, string.Concat("Expected type of ", expectedType.ToString(), " but received the type ", foundType.ToString()));
            if (expectedLength >= 0)
            {
                Assert.AreEqual(expectedLength, buffer.Length, string.Concat("Expected a packed length of ", expectedLength.ToString(), " bytes but got ", buffer.Length.ToString(), " bytes."));
            }

            MsgPackItem recreate = MsgPackItem.Unpack(new MemoryStream(buffer));

            resultType    = preservingType && item is MpInt ? ((MpInt)recreate).PreservedType : recreate.TypeId;
            typesAreEqual = (expectedMsgPackType & resultType) == expectedMsgPackType;
            Assert.IsTrue(typesAreEqual, string.Concat("Expected unpacked type of ", expectedMsgPackType, " but received the type ", resultType));

            T ret = recreate.GetTypedValue <T>();

            Assert.AreEqual(value, ret, "The returned value ", ret, " differs from the input value ", value);

            return(recreate);
        }
Пример #3
0
        // [TestCase(0x7FEFFFF9, 0x7FEFFFF9 + 6, MsgPackTypeId.MpArray32)] // Out of memory on my machine
        public void ArrayLengths(int length, int expectedBytes, MsgPackTypeId expedctedType)
        {
            object[] test            = new object[length];
            int      additionalBytes = FillArrayWithRandomNumbers(test);

            additionalBytes -= test.Length;
            MsgPackItem item = MsgPackTests.RoundTripTest <MpArray, object[]>(test, expectedBytes + additionalBytes, expedctedType);

            object[] ret = item.GetTypedValue <object[]>();

            Assert.AreEqual(length, ret.Length, string.Concat("Expected ", length, " items but got ", ret.Length, " items in the array."));
            for (int t = ret.Length - 1; t >= 0; t--)
            {
                Assert.AreEqual(test[t], ret[t], string.Concat("Expected ", test[t], " but got ", ret[t], " at index ", t));
            }
        }
Пример #4
0
        // [TestCase(0x7FEFFFF9, 0x7FEFFFF9 + 6, MsgPackTypeId.MpMap32)] // Out of memory on my machine
        public void MapLengths(int length, int expectedBytes, MsgPackTypeId expedctedType)
        {
            KeyValuePair <object, object>[] test = new KeyValuePair <object, object> [length];
            for (int t = test.Length - 1; t >= 0; t--)
            {
                test[t] = new KeyValuePair <object, object>(null, null);
            }
            int         additionalBytes = test.Length;
            MsgPackItem item            = MsgPackTests.RoundTripTest <MpMap, KeyValuePair <object, object>[]>(test, expectedBytes + additionalBytes, expedctedType);

            KeyValuePair <object, object>[] ret = item.GetTypedValue <KeyValuePair <object, object>[]>();

            Assert.AreEqual(length, ret.Length, string.Concat("Expected ", length, " items but got ", ret.Length, " items in the map."));
            for (int t = ret.Length - 1; t >= 0; t--)
            {
                Assert.AreEqual(test[t], ret[t], string.Concat("Expected ", test[t], " but got ", ret[t], " at index ", t));
            }
        }
Пример #5
0
        public static MsgPackItem RoundTripTest <Typ, T>(T value, int expectedLength, MsgPackTypeId expectedMsgPackType, sbyte extensionType = 0) where Typ : MsgPackItem
        {
            MsgPackItem item;

            if (typeof(Typ) == typeof(MpExt))
            {
                item = new MpExt()
                {
                    Value         = value,
                    TypeSpecifier = extensionType
                };
            }
            else
            {
                item = MsgPackItem.Pack(value);
            }
            byte[] buffer       = item.ToBytes();
            Type   expectedType = typeof(Typ);
            Type   foundType    = item.GetType();

            MsgPackTypeId resultType    = item.TypeId;
            bool          typesAreEqual = (expectedMsgPackType & resultType) == expectedMsgPackType;

            Assert.IsTrue(typesAreEqual, string.Concat("Expected packed type of ", expectedMsgPackType, " but received the type ", resultType));
            Assert.True(foundType == expectedType, string.Concat("Expected type of ", expectedType.ToString(), " but received the type ", foundType.ToString()));
            if (expectedLength >= 0)
            {
                Assert.AreEqual(expectedLength, buffer.Length, string.Concat("Expected a packed length of ", expectedLength.ToString(), " bytes but got ", buffer.Length.ToString(), " bytes."));
            }

            MsgPackItem recreate = MsgPackItem.Unpack(new MemoryStream(buffer));

            resultType    = recreate.TypeId;
            typesAreEqual = (expectedMsgPackType & resultType) == expectedMsgPackType;
            Assert.IsTrue(typesAreEqual, string.Concat("Expected unpacked type of ", expectedMsgPackType, " but received the type ", resultType));

            T ret = recreate.GetTypedValue <T>();

            Assert.AreEqual(value, ret, "The returned value ", ret, " differs from the input value ", value);

            return(recreate);
        }
Пример #6
0
        public void AssortedMix(int expectedLength)
        {
            KeyValuePair <object, object>[] items = new KeyValuePair <object, object>[] {
                new KeyValuePair <object, object>(true, true),
                new KeyValuePair <object, object>(false, false),
                new KeyValuePair <object, object>(null, null),
                new KeyValuePair <object, object>(int.MinValue, uint.MaxValue),
                new KeyValuePair <object, object>((byte)1, 5.2d),
                new KeyValuePair <object, object>((byte)2, 900.1f),
                new KeyValuePair <object, object>("Hallo!", "Hallo!"),
            };
            MsgPackItem item = MsgPackTests.RoundTripTest <MpMap, KeyValuePair <object, object>[]>(items, expectedLength, MsgPackTypeId.MpMap4);

            KeyValuePair <object, object>[] ret = item.GetTypedValue <KeyValuePair <object, object>[]>();

            Assert.AreEqual(items.Length, ret.Length, string.Concat("Expected ", items.Length, " items but got ", ret.Length, " items in the array."));
            for (int t = ret.Length - 1; t >= 0; t--)
            {
                Assert.AreEqual(items[t], ret[t], string.Concat("Expected ", items[t], " but got ", ret[t], " at index ", t));
            }
        }
Пример #7
0
        public void AssortedMix(int expectedLength)
        {
            object[] items = new object[] {
                true,
                false,
                null,
                128,
                -30,
                5.2d,
                900.1f,
                "Hallo!",
                new byte[] { 1, 2, 3 },
                new object[] { true, null, "yes" }
            };
            MsgPackItem item = MsgPackTests.RoundTripTest <MpArray, object[]>(items, expectedLength, MsgPackTypeId.MpArray4);

            object[] ret = item.GetTypedValue <object[]>();

            Assert.AreEqual(items.Length, ret.Length, string.Concat("Expected ", items.Length, " items but got ", ret.Length, " items in the array."));
            for (int t = ret.Length - 1; t >= 0; t--)
            {
                Assert.AreEqual(items[t], ret[t], string.Concat("Expected ", items[t], " but got ", ret[t], " at index ", t));
            }
        }
Пример #8
0
        public void AssortedMix(bool preserveTypes, int expectedLength)
        {
            object[] items = new object[] {
                true,
                false,
                null,
                128,
                -30,
                5.2d,
                900.1f,
                "Hallo!",
                new byte[] { 1, 2, 3 },
                new object[] { true, null, "yes" }
            };
            if (preserveTypes)
            {
                MsgPackTests.DynamicallyCompactValue = false;
            }
            try {
                MsgPackItem item = MsgPackTests.RoundTripTest <MpArray, object[]>(items, expectedLength, MsgPackTypeId.MpArray4);

                object[] ret = item.GetTypedValue <object[]>();

                Assert.AreEqual(items.Length, ret.Length, string.Concat("Expected ", items.Length, " items but got ", ret.Length, " items in the array."));
                for (int t = ret.Length - 1; t >= 0; t--)
                {
                    if (preserveTypes && t != 2)
                    {
                        Assert.IsTrue(items[t].GetType() == ret[t].GetType(), string.Concat("Expected type ", items[t].GetType(), " items but got ", ret[t].GetType(), "."));
                    }
                    Assert.AreEqual(items[t], ret[t], string.Concat("Expected ", items[t], " but got ", ret[t], " at index ", t));
                }
            } finally {
                MsgPackTests.DynamicallyCompactValue = true;
            }
        }
Пример #9
0
        public static MsgPackItem RoundTripTest <Typ, T>(T value, int expectedLength, MsgPackTypeId expectedMsgPackType, sbyte extensionType = 0) where Typ : MsgPackItem
        {
            MsgPackItem item;

            if (typeof(Typ) == typeof(MpExt))
            {
                item = new MpExt()
                {
                    Value         = value,
                    TypeSpecifier = extensionType
                };
            }
            else
            {
                item = MsgPackItem.Pack(value);
            }
            byte[] buffer       = item.ToBytes();
            Type   expectedType = typeof(Typ);
            Type   foundType    = item.GetType();

            MsgPackTypeId resultType    = item.TypeId;
            bool          typesAreEqual = (expectedMsgPackType & resultType) == expectedMsgPackType;

            Assert.IsTrue(typesAreEqual, string.Concat("Expected packed type of ", expectedMsgPackType, " but received the type ", resultType));
            Assert.True(foundType == expectedType, string.Concat("Expected type of ", expectedType.ToString(), " but received the type ", foundType.ToString()));
            if (expectedLength >= 0)
            {
                Assert.AreEqual(expectedLength, buffer.Length, string.Concat("Expected a packed length of ", expectedLength.ToString(), " bytes but got ", buffer.Length.ToString(), " bytes."));
            }

            MsgPackItem recreate = MsgPackItem.Unpack(new MemoryStream(buffer));



            resultType    = recreate.TypeId;
            typesAreEqual = (expectedMsgPackType & resultType) == expectedMsgPackType;
            Assert.IsTrue(typesAreEqual, string.Concat("Expected unpacked type of ", expectedMsgPackType, " but received the type ", resultType));

            T ret = recreate.GetTypedValue <T>();

            // Correct Local / UTC differences before comparing final result
            if (ret is DateTime)
            {
                DateTime idt = (DateTime)(object)value;
                DateTime odt = (DateTime)(object)ret;
                if (idt.Kind != odt.Kind)
                {
                    if (idt.Kind == DateTimeKind.Utc)
                    {
                        ret = (T)(object)odt.ToUniversalTime();
                    }
                    else
                    {
                        ret = (T)(object)odt.ToLocalTime();
                    }
                }
            }

            Assert.AreEqual(value, ret, "The returned value ", ret, " differs from the input value ", value);

            return(recreate);
        }
Пример #10
0
        public static MsgPackItem RoundTripTest <Typ, T>(T value, int expectedLength, MsgPackTypeId expectedMsgPackType, sbyte extensionType = 0) where Typ : MsgPackItem
        {
            bool preservingType = !DynamicallyCompactValue;

            MsgPackItem item;

            if (typeof(Typ) == typeof(MpExt))
            {
                item = new MpExt()
                {
                    Value         = value,
                    TypeSpecifier = extensionType
                };
            }
            else
            {
                item = MsgPackItem.Pack(value, new MsgPackSettings()
                {
                    DynamicallyCompact = DynamicallyCompactValue,
                    PreservePackages   = false,
                    ContinueProcessingOnBreakingError = false
                });
            }
            byte[] buffer       = item.ToBytes();
            Type   expectedType = typeof(Typ);
            Type   foundType    = item.GetType();

            MsgPackTypeId resultType    = preservingType && item is MpInt ? ((MpInt)item).PreservedType : item.TypeId;
            bool          typesAreEqual = (expectedMsgPackType & resultType) == expectedMsgPackType;

            Assert.IsTrue(typesAreEqual, string.Concat("Expected packed type of ", expectedMsgPackType, " but received the type ", resultType));
            Assert.True(foundType == expectedType, string.Concat("Expected type of ", expectedType.ToString(), " but received the type ", foundType.ToString()));
            if (expectedLength >= 0)
            {
                Assert.AreEqual(expectedLength, buffer.Length, string.Concat("Expected a packed length of ", expectedLength.ToString(), " bytes but got ", buffer.Length.ToString(), " bytes."));
            }

            MsgPackItem recreate = MsgPackItem.Unpack(new MemoryStream(buffer));

            MpError err = recreate as MpError;

            if (!(err is null))
            {
                throw new Exception(err.ToString());
            }

            resultType    = preservingType && item is MpInt ? ((MpInt)recreate).PreservedType : recreate.TypeId;
            typesAreEqual = (expectedMsgPackType & resultType) == expectedMsgPackType;
            Assert.IsTrue(typesAreEqual, string.Concat("Expected unpacked type of ", expectedMsgPackType, " but received the type ", resultType));

            T ret = recreate.GetTypedValue <T>();

            // Correct Local / UTC differences before comparing final result
            if (ret is DateTime)
            {
                DateTime idt = (DateTime)(object)value;
                DateTime odt = (DateTime)(object)ret;
                if (idt.Kind != odt.Kind)
                {
                    if (idt.Kind == DateTimeKind.Utc)
                    {
                        ret = (T)(object)odt.ToUniversalTime();
                    }
                    else
                    {
                        ret = (T)(object)odt.ToLocalTime();
                    }
                }
            }

            Assert.AreEqual(value, ret, "The returned value ", ret, " differs from the input value ", value);

            return(recreate);
        }