Esempio n. 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);
            }
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
        private void Traverse(TreeNode node, MsgPackItem item)
        {
            _nodeCount++;
            if (_nodeCount > _displayLimit)
            {
                return;
            }
            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(MpRoot))
            {
                MpRoot        root     = (MpRoot)item;
                MsgPackItem[] children = (MsgPackItem[])root.Value;
                for (int t = 0; t < children.Length; t++)
                {
                    TreeNode child = GetTreeNodeFor(children[t]);
                    node.Nodes.Add(child);
                    Traverse(child, children[t]);
                    if (_nodeCount > _displayLimit)
                    {
                        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 (_nodeCount > _displayLimit)
                    {
                        return;
                    }
                }
            }
            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);
                    if (_nodeCount > _displayLimit)
                    {
                        return;
                    }
                    TreeNode childVal = GetTreeNodeFor(children[t].Value);
                    childVal.StateImageIndex = 9; // Value
                    child.Nodes.Add(childVal);
                    Traverse(childVal, children[t].Value);
                    if (_nodeCount > _displayLimit)
                    {
                        return;
                    }
                }
            }
            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);
                    if (_nodeCount > _displayLimit)
                    {
                        return;
                    }
                }
            }
        }