Exemple #1
0
        public void RoundTripTest(bool dynamicallyCompact, int expectedLength, params object[] items)
        {
            MpRoot root = MsgPackItem.PackMultiple(dynamicallyCompact, items);

            byte[] bytes = root.ToBytes();

            Assert.AreEqual(expectedLength, bytes.Length, string.Concat("Expected ", expectedLength, " serialized bytes items but got ", bytes.Length, " bytes."));

            MpRoot result = MsgPackItem.UnpackMultiple(bytes, dynamicallyCompact);

            Assert.AreEqual(items.Length, result.Count, string.Concat("Expected ", items.Length, " items but got ", result.Count, " items after round trip."));

            for (int t = 0; t < result.Count; t++)
            {
                object expected = items[t];
                object actual   = result[t].Value;
                if (!dynamicallyCompact && !(expected is null))
                {
                    Type expectedType = expected.GetType();
                    Type foundType    = actual.GetType();
                    Assert.True(foundType == expectedType, string.Concat("Expected type of ", expectedType.ToString(), " but received the type ", foundType.ToString()));
                }

                Assert.AreEqual(expected, actual, "The returned value ", actual, " differs from the input value ", expected);
            }
        }
Exemple #2
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;
            }
        }
 public void SlidingTackle(string directory)
 {
     KeyValuePair <object, object>[] items = new KeyValuePair <object, object>[] {
         new KeyValuePair <object, object>(true, false),
         new KeyValuePair <object, object>(null, "Maps are quite flexible!"),
         new KeyValuePair <object, object>(true, "Double!"),
         new KeyValuePair <object, object>(new KeyValuePair <object, object> [0], new object[0]),
         new KeyValuePair <object, object>(new object[0], new KeyValuePair <object, object> [0])
     };
     File.WriteAllBytes(Path.Combine(directory, "SlidingTackle.MsgPack"), MsgPackItem.Pack(items).ToBytes());
 }
Exemple #4
0
        public void EnumDictionaryUse()
        {
            Dictionary <myEnum, object> objectProps = new Dictionary <myEnum, object>();

            objectProps.Add(myEnum.SomeString, "Hallo!");
            objectProps.Add(myEnum.SomeInt, 5);
            objectProps.Add(myEnum.SomeBool, true);
            objectProps.Add(myEnum.SomeNothing, null);
            objectProps.Add(myEnum.SomeFloat, 3.5f);

            MsgPackItem item = MsgPackTests.RoundTripTest <MpMap, Dictionary <myEnum, object> >(objectProps, 21, MsgPackTypeId.MpMap4);
        }
Exemple #5
0
        public void TypicalDictionaryUse()
        {
            Dictionary <string, object> objectProps = new Dictionary <string, object>();

            objectProps.Add("string", "Hallo!");
            objectProps.Add("int", 5);
            objectProps.Add("bool", true);
            objectProps.Add("nothing", null);
            objectProps.Add("float", 3.5f);

            MsgPackItem item = MsgPackTests.RoundTripTest <MpMap, Dictionary <string, object> >(objectProps, 46, MsgPackTypeId.MpMap4);
        }
        public void Example(string directory)
        {
            MyClass message = new MyClass()
            {
                Name     = "TestMessage",
                Quantity = 35,
                Anything = new List <object>(new object[] { "First", 2, false, null, 5.5d, "last", DateTime.Now })
            };

            byte[] bytes = MsgPackItem.Pack(message).ToBytes();

            File.WriteAllBytes(Path.Combine(directory, "Example.MsgPack"), bytes);
        }
        private int GetIconFor(MsgPackItem item)
        {
            if (ReferenceEquals(item, null))
            {
                return(0);
            }
            Type typ = item.GetType();

            if (typ == typeof(MpBool))
            {
                return(1);
            }
            if (typ == typeof(MpInt))
            {
                return(2);
            }
            if (typ == typeof(MpFloat))
            {
                return(3);
            }
            if (typ == typeof(MpBin))
            {
                return(4);
            }
            if (typ == typeof(MpString))
            {
                return(5);
            }
            if (typ == typeof(MpArray))
            {
                return(6);
            }
            if (typ == typeof(MpMap))
            {
                return(7);
            }
            if (typ == typeof(MpExt))
            {
                return(10);
            }
            if (typ == typeof(MpError))
            {
                return(11);
            }
            if (typ == typeof(MpRoot))
            {
                return(12);
            }
            return(-1);
        }
Exemple #8
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);
        }
Exemple #9
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));
            }
        }
Exemple #10
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));
            }
        }
Exemple #11
0
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (ReferenceEquals(e.Node, null))
            {
                propertyGrid1.SelectedObject = null;
                ColorSelectedNodeInHexView(null);
                ClearValidationSelection();
            }
            else
            {
                propertyGrid1.SelectedObject = e.Node.Tag;
                propertyGrid1.ExpandAllGridItems();

                MsgPackItem item = e.Node.Tag as MsgPackItem;
                if (ReferenceEquals(item, null))
                {
                    statusOffset.Text = "0 (0x00)";
                    ColorSelectedNodeInHexView(null);
                }
                else
                {
                    statusOffset.Text = string.Concat(item.StoredOffset, " (0x", item.StoredOffset.ToString("X"), ")");
                    EditorMetaData meta = item.Tag as EditorMetaData;
                    ColorSelectedNodeInHexView(meta);
                }

                if (!lvSelecting)
                {
                    lvSelecting = true;
                    try {
                        for (int t = listView1.Items.Count - 1; t >= 0; t--)
                        {
                            bool select = listView1.Items[t].Tag == e.Node;
                            listView1.Items[t].Selected = select;
                        }
                    } finally {
                        lvSelecting          = false;
                        errorDetails.Visible = false;
                        splitter4.Visible    = false;
                    }
                }
            }
        }
Exemple #12
0
        private TreeNode GetTreeNodeFor(MsgPackItem item)
        {
            int    imgIdx = GetIconFor(item);
            string text   = ReferenceEquals(item, null) ? "NULL" : item.ToString();
            int    pos    = text.IndexOfAny(new char[] { '\r', '\n' });

            if (pos > 0)
            {
                text = text.Substring(0, pos - 1);
            }
            TreeNode node = new TreeNode(text, imgIdx, imgIdx);

            if (ReferenceEquals(item, null) || item.IsBestGuess)
            {
                node.ForeColor = Color.DarkGray;
            }
            node.Tag = item;
            return(node);
        }
Exemple #13
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);
        }
Exemple #14
0
        public void RoundTripTest(int expectedLength, params object[] items)
        {
            MpRoot root = MsgPackItem.PackMultiple(items);

            byte[] bytes = root.ToBytes();

            Assert.AreEqual(expectedLength, bytes.Length, string.Concat("Expected ", expectedLength, " serialized bytes items but got ", bytes.Length, " bytes."));

            MpRoot result = MsgPackItem.UnpackMultiple(bytes);

            Assert.AreEqual(items.Length, result.Count, string.Concat("Expected ", items.Length, " items but got ", result.Count, " items after round trip."));

            for (int t = 0; t < result.Count; t++)
            {
                object expected = items[t];
                object actual   = result[t].Value;

                Assert.AreEqual(expected, actual, "The returned value ", actual, " differs from the input value ", expected);
            }
        }
Exemple #15
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));
            }
        }
Exemple #16
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));
            }
        }
Exemple #17
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;
            }
        }
        public void SomeBadChoices(string directory)
        {
            object[] items = new object[] {
                "Wrongfully signed types",
                (sbyte)50,
                (int)128,
                (short)129,
                (int)129,
                (long)129,
                (int)1234,
                short.MaxValue,
                int.MaxValue,
                long.MaxValue,
                "Uncompressed integers",

                (ulong)1,
                (ulong)127,
                (ulong)128,
                (ulong)255,
                (ulong)256,
                (ulong)ushort.MaxValue,
                (ulong)ushort.MaxValue + 1,
                (ulong)uint.MaxValue,

                (uint)1,
                (uint)127,
                (uint)128,
                (uint)255,
                (uint)256,
                (uint)ushort.MaxValue,

                (ushort)1,
                (ushort)127,
                (ushort)128,
                (ushort)255,

                (byte)1,
                (byte)127,

                (long)-1,
                (long)-31,
                (long)-32,
                (long)-128,
                (long)-129,
                (long)short.MinValue,
                ((long)short.MinValue) - 1,
                (long)int.MinValue,

                (int)-1,
                (int)-31,
                (int)-32,
                (int)-128,
                (int)-129,
                (int)short.MinValue,

                (short)-1,
                (short)-31,
                (short)-32,
                (short)-128,
            };

            File.WriteAllBytes(Path.Combine(directory, "SomeBadChoices.MsgPack"), MsgPackItem.Pack(items, false).ToBytes());
        }
Exemple #19
0
        private void Traverse(TreeNode node, MsgPackItem item)
        {
            if (ReferenceEquals(item, null))
            {
                return;
            }
            Type typ = item.GetType();

            if (typ == typeof(MpBool))
            {
                return;
            }
            if (typ == typeof(MpInt))
            {
                return;
            }
            if (typ == typeof(MpFloat))
            {
                return;
            }
            if (typ == typeof(MpBin))
            {
                return;
            }
            if (typ == typeof(MpString))
            {
                return;
            }
            if (typ == typeof(MpArray))
            {
                MpArray       arr      = (MpArray)item;
                MsgPackItem[] children = arr.PackedValues;
                for (int t = 0; t < children.Length; t++)
                {
                    TreeNode child = GetTreeNodeFor(children[t]);
                    node.Nodes.Add(child);
                    Traverse(child, children[t]);
                }
            }
            if (typ == typeof(MpMap))
            {
                MpMap map = (MpMap)item;
                KeyValuePair <MsgPackItem, MsgPackItem>[] children = map.PackedValues;
                for (int t = 0; t < children.Length; t++)
                {
                    TreeNode child = GetTreeNodeFor(children[t].Key);
                    child.StateImageIndex = 8; // Key
                    node.Nodes.Add(child);
                    Traverse(child, children[t].Key);
                    TreeNode childVal = GetTreeNodeFor(children[t].Value);
                    childVal.StateImageIndex = 9; // Value
                    child.Nodes.Add(childVal);
                    Traverse(childVal, children[t].Value);
                }
            }
            if (typ == typeof(MpError))
            {
                MpError err = (MpError)item;
                if (!ReferenceEquals(err.PartialItem, null))
                {
                    if (!(err.PartialItem is MpError))
                    {
                        node.StateImageIndex = GetIconFor(err.PartialItem);
                    }
                    TreeNode child = GetTreeNodeFor(err.PartialItem);
                    node.Nodes.Add(child);
                    Traverse(child, err.PartialItem);
                }
            }
        }
Exemple #20
0
        private int AddParts(string[] hex, TreeNode node, int byteOffset, StringBuilder sb, ref EditorMetaData previousMeta)
        {
            MsgPackItem item = (MsgPackItem)node.Tag;

            if (ReferenceEquals(item, null))
            {
                return(byteOffset);
            }
            int additionalBytes = 0;

            while (item.StoredOffset > byteOffset)
            {
                sb.Append(hex[byteOffset]).Append(' ');
                byteOffset++;
                additionalBytes++;
            }
            if (additionalBytes > 0)
            {
                previousMeta.Length += additionalBytes;
                TreeNode parent = previousMeta.Node.Parent;
                while (!ReferenceEquals(parent, null))
                {
                    ((EditorMetaData)((MsgPackItem)parent.Tag).Tag).Length += additionalBytes;
                    parent = parent.Parent;
                }
            }

            EditorMetaData meta = new EditorMetaData()
            {
                CharOffset = byteOffset * 3,
                Node       = node,
                Item       = item
            };

            lineairList.Add(meta);
            item.Tag     = meta;
            previousMeta = meta;

            if (!ReferenceEquals(item, null) && !(item is MpError && !ReferenceEquals(((MpError)item).PartialItem, null)) && byteOffset < hex.Length)
            {
                sb.Append("\\cf1 "); // red
                sb.Append(hex[byteOffset]).Append(' ');
                byteOffset++;
            }

            if (item is MsgPackVarLen)
            {
                int lengthBytes = 0;
                switch (item.TypeId)
                {
                case MsgPackTypeId.MpBin8: lengthBytes = 1; break;

                case MsgPackTypeId.MpBin16: lengthBytes = 2; break;

                case MsgPackTypeId.MpBin32: lengthBytes = 4; break;

                case MsgPackTypeId.MpStr8: lengthBytes = 1; break;

                case MsgPackTypeId.MpStr16: lengthBytes = 2; break;

                case MsgPackTypeId.MpStr32: lengthBytes = 4; break;

                case MsgPackTypeId.MpMap16: lengthBytes = 2; break;

                case MsgPackTypeId.MpMap32: lengthBytes = 4; break;

                case MsgPackTypeId.MpArray16: lengthBytes = 2; break;

                case MsgPackTypeId.MpArray32: lengthBytes = 4; break;

                case MsgPackTypeId.MpExt8: lengthBytes = 1; break;

                case MsgPackTypeId.MpExt16: lengthBytes = 2; break;

                case MsgPackTypeId.MpExt32: lengthBytes = 4; break;
                }
                if (lengthBytes > 0)
                {
                    sb.Append("\\cf2 "); // blue
                    for (int t = lengthBytes - 1; t >= 0; t--)
                    {
                        sb.Append(hex[byteOffset]).Append(' ');
                        byteOffset++;
                    }
                }
            }
            sb.Append("\\cf0 "); // black

            for (int t = 0; t < node.Nodes.Count; t++)
            {
                byteOffset = AddParts(hex, node.Nodes[t], byteOffset, sb, ref previousMeta);
            }

            ValidateItem(meta);

            meta.Length = (byteOffset - (int)item.StoredOffset);
            return(byteOffset);
        }
Exemple #21
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);
        }
        public void AllSmallTypes(string directory)
        {
            Dictionary <string, int> simpleMap = new Dictionary <string, int>();

            simpleMap.Add("Black", 0);
            simpleMap.Add("Brown", 1);
            simpleMap.Add("Red", 2);
            simpleMap.Add("Orange", 3);
            simpleMap.Add("Yellow", 4);
            simpleMap.Add("Green", 5);
            simpleMap.Add("Blue", 6);
            simpleMap.Add("Violet", 7);
            simpleMap.Add("Gray", 8);
            simpleMap.Add("White", 9);

            object[] items = new object[] {
                "Null type:",
                null,
                "Bool types:",
                true,
                false,
                "Float types:",
                float.Epsilon,
                float.MaxValue,
                float.MinValue,
                float.NaN,
                float.NegativeInfinity,
                float.PositiveInfinity,
                double.Epsilon,
                double.MaxValue,
                double.MinValue,
                double.NaN,
                double.NegativeInfinity,
                double.PositiveInfinity,
                "Integer types:",
                0,
                1,
                65,
                127,
                -1,
                -15,
                -31,
                128,
                200,
                255,
                -32,
                -100,
                -128,
                256,
                1000,
                ushort.MaxValue,
                -129,
                -1000,
                short.MinValue,
                ushort.MaxValue + 1,
                int.MaxValue,
                uint.MaxValue,
                short.MinValue - 1,
                short.MinValue - 1000,
                int.MinValue,
                ((ulong)uint.MaxValue) + 1,
                long.MaxValue,
                ulong.MaxValue,
                ((long)int.MinValue) - 1,
                ((long)int.MinValue) - 1000,
                long.MinValue,
                "Map:",
                simpleMap,
                "Binary bytes blob:",
                new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 },
                "Extension (type 5)",
                new MpExt()
                {
                    Value         = new byte[] { 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 },
                    TypeSpecifier = 5
                }
            };

            File.WriteAllBytes(Path.Combine(directory, "AllSmallTypes.MsgPack"), MsgPackItem.Pack(items, true).ToBytes());
        }
Exemple #23
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);
        }